From 23f89eabf3eb37d1230db35aa4f0d8606012e655 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 2 Jan 2025 03:31:15 +0100 Subject: [PATCH] feat(core): support cli tools inside mono repos. --- changelog.md | 7 ++++ package.json | 2 +- ts/00_commitinfo_data.ts | 2 +- ts/classes.giteaassets.ts | 4 +- ts/classes.publishmodule.ts | 76 +++++++++++++++++++++++++------------ ts/classes.tspublish.ts | 17 ++++++--- 6 files changed, 74 insertions(+), 34 deletions(-) diff --git a/changelog.md b/changelog.md index 56cc41c..2175ea1 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2025-01-02 - 1.9.0 - feat(core) +Refactor gitea asset handling and module initialization + +- Introduced GiteaAssets class to handle gitea asset fetching. +- Updated TsPublish and PublishModule classes to use GiteaAssets. +- Fixed queryParams in getFiles method of GiteaAssets. + ## 2025-01-01 - 1.8.0 - feat(core) Added GiteaAssets class for managing files in Gitea repositories diff --git a/package.json b/package.json index 3f23f7f..26bd4a8 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "buildDocs": "(tsdoc)" }, "bin": { - "tspublish": "cli.js" + "tspublish": "./cli.js" }, "devDependencies": { "@git.zone/tsbuild": "^2.1.85", diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index be7aa8b..9820c27 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@git.zone/tspublish', - version: '1.8.0', + version: '1.9.0', description: 'A tool to publish multiple, concise, and small packages from monorepos, specifically for TypeScript projects within a git environment.' } diff --git a/ts/classes.giteaassets.ts b/ts/classes.giteaassets.ts index 8bbf70c..214724b 100644 --- a/ts/classes.giteaassets.ts +++ b/ts/classes.giteaassets.ts @@ -49,7 +49,7 @@ export class GiteaAssets { { headers: this.headers, method: 'GET', - params: branch ? { ref: branch } : {}, + queryParams: branch ? { ref: branch } : {}, } ) if (!Array.isArray(response.body) && typeof response.body === 'object') { @@ -64,7 +64,7 @@ export class GiteaAssets { { headers: this.headers, method: 'GET', - params: branch ? { ref: branch } : {}, + queryParams: branch ? { ref: branch } : {}, } ); entry.encoding = response2.body.encoding; diff --git a/ts/classes.publishmodule.ts b/ts/classes.publishmodule.ts index cd7bf29..ae1a4b0 100644 --- a/ts/classes.publishmodule.ts +++ b/ts/classes.publishmodule.ts @@ -2,11 +2,8 @@ import * as plugins from './plugins.js'; import * as paths from './paths.js'; import { logger } from './logging.js'; -export interface ITsPublishJson { - name: string; - dependencies: string[]; - registries: string[]; -} +import { type ITsPublishJson } from './interfaces/index.js'; +import type { TsPublish } from './classes.tspublish.js'; export interface IPublishModuleOptions { monoRepoDir: string; @@ -20,15 +17,17 @@ export interface IPublishModuleOptions { } export class PublishModule { + tsPublishRef: TsPublish; public options: IPublishModuleOptions; - constructor(options: IPublishModuleOptions) { + constructor(tsPublishRef: TsPublish, options: IPublishModuleOptions) { + this.tsPublishRef = tsPublishRef; this.options = options; } public async init() { this.options.packageSubFolderFullPath = plugins.path.join( this.options.monoRepoDir, - this.options.packageSubFolder, + this.options.packageSubFolder ); // check requirements @@ -36,13 +35,14 @@ export class PublishModule { throw new Error('subFolder must start with "ts"'); } this.options.tsPublishJson = plugins.smartfile.fs.toObjectSync( - plugins.path.join(this.options.packageSubFolderFullPath, 'tspublish.json'), + plugins.path.join(this.options.packageSubFolderFullPath, 'tspublish.json') ); + + // the package.json of the parent mono repo const monoRepoPackageJson = JSON.parse( - plugins.smartfile.fs.toStringSync( - plugins.path.join(this.options.monoRepoDir, 'package.json'), - ), + plugins.smartfile.fs.toStringSync(plugins.path.join(this.options.monoRepoDir, 'package.json')) ); + this.options.dependencies = { ...this.options.dependencies, ...(() => { @@ -75,7 +75,7 @@ export class PublishModule { if (availableVersions.includes(this.options.version)) { logger.log( 'error', - `package ${this.options.name} already exists with version ${this.options.version}`, + `package ${this.options.name} already exists with version ${this.options.version}` ); process.exit(1); } @@ -93,12 +93,13 @@ export class PublishModule { public async createTsconfigJson() { const originalTsConfig = plugins.smartfile.fs.toObjectSync( - plugins.path.join(paths.cwd, 'tsconfig.json'), + plugins.path.join(paths.cwd, 'tsconfig.json') ); if (originalTsConfig?.compilerOptions?.paths) { for (const path of Object.keys(originalTsConfig.compilerOptions.paths)) { - originalTsConfig.compilerOptions.paths[path][0] = - `.${originalTsConfig.compilerOptions.paths[path][0]}`; + originalTsConfig.compilerOptions.paths[ + path + ][0] = `.${originalTsConfig.compilerOptions.paths[path][0]}`; } } const tsconfigJson = { @@ -147,6 +148,15 @@ export class PublishModule { 'npmextra.json', 'readme.md', ], + ...this.options.tsPublishJson.bin ? { + bin: (() => { + const binObject: {[key: string]: string} = {}; + for (const bin of this.options.tsPublishJson.bin) { + binObject[bin] = `./cli.js`; + } + return binObject; + })() + } : {}, }; return JSON.stringify(packageJson, null, 2); } @@ -154,7 +164,7 @@ export class PublishModule { public async createPublishModuleDir() { this.options.publishModDirFullPath = plugins.path.join( this.options.monoRepoDir, - `dist_publish_${this.options.packageSubFolder}`, + `dist_publish_${this.options.packageSubFolder}` ); await plugins.smartfile.fs.ensureEmptyDir(this.options.publishModDirFullPath); @@ -162,35 +172,38 @@ export class PublishModule { const packageJson = await plugins.smartfile.SmartFile.fromString( plugins.path.join(this.options.publishModDirFullPath, 'package.json'), await this.createPackageJson(), - 'utf8', + 'utf8' ); await packageJson.write(); // tsconfig.json - const originalTsConfigJson = await plugins.smartfile.SmartFile.fromString( + const tsconfigJson = await plugins.smartfile.SmartFile.fromString( plugins.path.join(this.options.publishModDirFullPath, 'tsconfig.json'), await this.createTsconfigJson(), - 'utf8', + 'utf8' ); - await originalTsConfigJson.write(); + await tsconfigJson.write(); - // ts folder + // ts subfolder, the folder that contains the source code and is being transpiled await plugins.smartfile.fs.copy( this.options.packageSubFolderFullPath, - plugins.path.join(this.options.publishModDirFullPath, this.options.packageSubFolder), + plugins.path.join(this.options.publishModDirFullPath, this.options.packageSubFolder) ); // readme await plugins.smartfile.fs.copy( plugins.path.join(this.options.packageSubFolderFullPath, 'readme.md'), - plugins.path.join(this.options.publishModDirFullPath, 'readme.md'), + plugins.path.join(this.options.publishModDirFullPath, 'readme.md') ); // license await plugins.smartfile.fs.copy( plugins.path.join(this.options.monoRepoDir, 'license'), - plugins.path.join(this.options.publishModDirFullPath, 'license'), + plugins.path.join(this.options.publishModDirFullPath, 'license') ); + + // cli stuff + this.createBinCliSetup(); } public async build() { @@ -200,6 +213,19 @@ export class PublishModule { await smartshellInstance.exec(`cd ${this.options.publishModDirFullPath} && pnpm run build`); } + public async createBinCliSetup() { + const binSetupApplies: boolean = + this.options.tsPublishJson.bin && + Array.isArray(this.options.tsPublishJson.bin) && + this.options.tsPublishJson.bin.length > 0; + const files = await this.tsPublishRef.giteaAssetsInstance.getFiles( + 'git.zone', + 'cli', + 'assets/templates/cli/cli.js' + ); + await plugins.smartfile.memory.toFs(atob(files[0].base64Content), plugins.path.join(this.options.publishModDirFullPath, 'cli.js')); + } + public async publish() { const smartshellInstance = new plugins.smartshell.Smartshell({ executor: 'bash', @@ -211,7 +237,7 @@ export class PublishModule { await smartshellInstance.exec( `cd ${this.options.publishModDirFullPath} && pnpm publish ${ registryAccessLevel === 'public' ? '--access public' : '' - } --no-git-checks --registry https://${registryUrl}`, + } --no-git-checks --registry https://${registryUrl}` ); } } diff --git a/ts/classes.tspublish.ts b/ts/classes.tspublish.ts index 97cd3fd..e107d56 100644 --- a/ts/classes.tspublish.ts +++ b/ts/classes.tspublish.ts @@ -3,9 +3,16 @@ import * as plugins from './plugins.js'; import * as interfaces from './interfaces/index.js'; import { PublishModule } from './classes.publishmodule.js'; +import { GiteaAssets } from './classes.giteaassets.js'; export class TsPublish { - constructor() {} + public giteaAssetsInstance: GiteaAssets; + + constructor() { + this.giteaAssetsInstance = new GiteaAssets({ + giteaBaseUrl: 'https://code.foss.global', + }); + } public async publish(monorepoDirArg: string) { const publishModules = await this.getModuleSubDirs(monorepoDirArg); @@ -13,7 +20,7 @@ export class TsPublish { for (const publishModule of Object.keys(publishModules)) { logger.log( 'info', - `Publishing module: ${publishModule} -> ${publishModules[publishModule].name}`, + `Publishing module: ${publishModule} -> ${publishModules[publishModule].name}` ); } for (const publishModule of Object.keys(publishModules)) { @@ -22,7 +29,7 @@ export class TsPublish { logger.log('warn', `no name found in tspublish.json for ${publishModule}. Skipping...`); continue; } - const publishModuleInstance = new PublishModule({ + const publishModuleInstance = new PublishModule(this, { monoRepoDir: monorepoDirArg, packageSubFolder: publishModule, }); @@ -48,12 +55,12 @@ export class TsPublish { logger.log('info', `found publish module: ${subDir}`); publishModules[subDir] = JSON.parse( - plugins.smartfile.fs.toStringSync(plugins.path.join(subDir, 'tspublish.json')), + plugins.smartfile.fs.toStringSync(plugins.path.join(subDir, 'tspublish.json')) ); } logger.log('ok', `found ${Object.keys(publishModules).length} publish modules`); logger.log('info', `Ordering publish modules...`); - + return publishModules; } }