2024-10-21 10:16:09 +00:00
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
import * as paths from './paths.js';
|
|
|
|
import { logger } from './logging.js';
|
|
|
|
|
2024-10-21 14:01:08 +00:00
|
|
|
export interface ITsPublishJson {
|
|
|
|
name: string;
|
|
|
|
dependencies: string[];
|
|
|
|
registries: string[];
|
|
|
|
}
|
|
|
|
|
2024-10-21 10:16:09 +00:00
|
|
|
export interface IPublishModuleOptions {
|
|
|
|
monoRepoDir: string;
|
|
|
|
packageSubFolder: string;
|
|
|
|
packageSubFolderFullPath?: string;
|
2024-10-21 14:01:08 +00:00
|
|
|
tsPublishJson?: ITsPublishJson;
|
2024-10-21 10:16:09 +00:00
|
|
|
publishModDirFullPath?: string;
|
|
|
|
name?: string;
|
|
|
|
version?: string;
|
2024-10-21 10:57:54 +00:00
|
|
|
dependencies?: { [key: string]: string };
|
2024-10-21 10:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PublishModule {
|
|
|
|
public options: IPublishModuleOptions;
|
|
|
|
constructor(options: IPublishModuleOptions) {
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async init() {
|
|
|
|
this.options.packageSubFolderFullPath = plugins.path.join(
|
|
|
|
this.options.monoRepoDir,
|
2024-11-04 23:34:56 +00:00
|
|
|
this.options.packageSubFolder,
|
2024-10-21 10:16:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// check requirements
|
|
|
|
if (!this.options.packageSubFolder.startsWith('ts')) {
|
|
|
|
throw new Error('subFolder must start with "ts"');
|
|
|
|
}
|
2024-10-21 14:01:08 +00:00
|
|
|
this.options.tsPublishJson = plugins.smartfile.fs.toObjectSync(
|
2024-11-04 23:34:56 +00:00
|
|
|
plugins.path.join(this.options.packageSubFolderFullPath, 'tspublish.json'),
|
2024-10-21 10:16:09 +00:00
|
|
|
);
|
2024-10-21 11:21:47 +00:00
|
|
|
const monoRepoPackageJson = JSON.parse(
|
2024-11-04 23:34:56 +00:00
|
|
|
plugins.smartfile.fs.toStringSync(
|
|
|
|
plugins.path.join(this.options.monoRepoDir, 'package.json'),
|
|
|
|
),
|
2024-10-21 12:51:12 +00:00
|
|
|
);
|
2024-10-21 10:16:09 +00:00
|
|
|
this.options.dependencies = {
|
|
|
|
...this.options.dependencies,
|
2024-10-21 11:21:47 +00:00
|
|
|
...(() => {
|
|
|
|
const resultDependencies = {};
|
2024-10-21 14:01:08 +00:00
|
|
|
for (const dependency of this.options.tsPublishJson.dependencies) {
|
2024-11-05 01:20:11 +00:00
|
|
|
if (monoRepoPackageJson.dependencies[dependency]) {
|
|
|
|
resultDependencies[dependency] = monoRepoPackageJson.dependencies[dependency];
|
|
|
|
} else {
|
|
|
|
resultDependencies[dependency] = monoRepoPackageJson.version;
|
|
|
|
}
|
2024-10-21 11:21:47 +00:00
|
|
|
}
|
|
|
|
return resultDependencies;
|
2024-10-21 12:51:12 +00:00
|
|
|
})(),
|
2024-10-21 10:16:09 +00:00
|
|
|
};
|
2024-10-21 14:01:08 +00:00
|
|
|
this.options.name = this.options.name || this.options.tsPublishJson.name;
|
2024-10-21 11:21:47 +00:00
|
|
|
this.options.version = monoRepoPackageJson.version;
|
2024-10-21 10:16:09 +00:00
|
|
|
|
|
|
|
// now that we have a name and version, lets check if there is already a package under the same name and version.
|
|
|
|
const smartnpmInstance = new plugins.smartnpm.NpmRegistry({}); // TODO: pass in options
|
2024-10-28 14:57:31 +00:00
|
|
|
let packageInfo: plugins.smartnpm.NpmPackage;
|
|
|
|
try {
|
|
|
|
packageInfo = await smartnpmInstance.getPackageInfo(this.options.name);
|
|
|
|
} catch (error) {
|
|
|
|
logger.log('warn', `package does not yet seem to exist. Proceeding in 10 seconds...`);
|
|
|
|
await plugins.smartdelay.delayFor(10000);
|
|
|
|
}
|
2024-10-21 10:16:09 +00:00
|
|
|
if (packageInfo) {
|
2024-10-21 10:57:54 +00:00
|
|
|
const availableVersions = packageInfo.allVersions.map((versionArg) => versionArg.version);
|
2024-10-21 10:16:09 +00:00
|
|
|
logger.log('info', `available versions are: ${availableVersions.toString()}`);
|
|
|
|
if (availableVersions.includes(this.options.version)) {
|
2024-11-04 23:34:56 +00:00
|
|
|
logger.log(
|
|
|
|
'error',
|
|
|
|
`package ${this.options.name} already exists with version ${this.options.version}`,
|
|
|
|
);
|
2024-10-23 13:59:26 +00:00
|
|
|
process.exit(1);
|
2024-10-21 10:57:54 +00:00
|
|
|
}
|
2024-10-21 10:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getLatestVersionOfPackage(name: string) {
|
|
|
|
const smartnpmInstance = new plugins.smartnpm.NpmRegistry({}); // TODO: pass in options
|
|
|
|
const packageInfo = await smartnpmInstance.getPackageInfo(name);
|
|
|
|
if (!packageInfo) {
|
|
|
|
throw new Error(`package ${name} not found`);
|
|
|
|
}
|
|
|
|
return packageInfo.allVersions[0].version;
|
|
|
|
}
|
|
|
|
|
2024-10-28 00:21:06 +00:00
|
|
|
public async createTsconfigJson() {
|
|
|
|
const originalTsConfig = plugins.smartfile.fs.toObjectSync(
|
2024-11-04 23:34:56 +00:00
|
|
|
plugins.path.join(paths.cwd, 'tsconfig.json'),
|
2024-10-28 00:21:06 +00:00
|
|
|
);
|
2024-10-28 00:24:52 +00:00
|
|
|
if (originalTsConfig?.compilerOptions?.paths) {
|
2024-10-28 00:36:24 +00:00
|
|
|
for (const path of Object.keys(originalTsConfig.compilerOptions.paths)) {
|
2024-11-04 23:34:56 +00:00
|
|
|
originalTsConfig.compilerOptions.paths[path][0] =
|
|
|
|
`.${originalTsConfig.compilerOptions.paths[path][0]}`;
|
2024-10-28 00:24:52 +00:00
|
|
|
}
|
2024-10-28 00:21:06 +00:00
|
|
|
}
|
|
|
|
const tsconfigJson = {
|
|
|
|
compilerOptions: {
|
|
|
|
experimentalDecorators: true,
|
|
|
|
useDefineForClassFields: false,
|
|
|
|
target: 'ES2022',
|
|
|
|
module: 'NodeNext',
|
|
|
|
moduleResolution: 'NodeNext',
|
|
|
|
esModuleInterop: true,
|
|
|
|
verbatimModuleSyntax: true,
|
2024-10-28 00:24:52 +00:00
|
|
|
paths: originalTsConfig?.compilerOptions?.paths,
|
2024-10-28 00:21:06 +00:00
|
|
|
},
|
2024-11-04 23:34:56 +00:00
|
|
|
exclude: ['dist_*/**/*.d.ts'],
|
2024-10-28 00:21:06 +00:00
|
|
|
};
|
|
|
|
return JSON.stringify(tsconfigJson, null, 2);
|
|
|
|
}
|
|
|
|
|
2024-10-21 10:16:09 +00:00
|
|
|
public async createPackageJson() {
|
|
|
|
const packageJson = {
|
|
|
|
name: this.options.name,
|
|
|
|
version: this.options.version,
|
2024-10-21 12:37:23 +00:00
|
|
|
type: 'module',
|
2024-10-21 10:16:09 +00:00
|
|
|
description: '',
|
2024-10-21 10:57:54 +00:00
|
|
|
exports: {
|
2024-10-21 10:16:09 +00:00
|
|
|
'.': {
|
2024-10-23 13:48:32 +00:00
|
|
|
import: `./dist_${this.options.packageSubFolder}/index.js`,
|
2024-10-21 10:16:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
scripts: {
|
|
|
|
build: 'tsbuild tsfolders --allowimplicitany',
|
|
|
|
},
|
|
|
|
dependencies: this.options.dependencies,
|
|
|
|
devDependencies: {
|
|
|
|
'@git.zone/tsbuild': await this.getLatestVersionOfPackage('@git.zone/tsbuild'),
|
|
|
|
},
|
2024-10-21 12:51:12 +00:00
|
|
|
files: [
|
|
|
|
'ts/**/*',
|
2024-10-23 13:49:22 +00:00
|
|
|
'ts_*/**/*',
|
2024-10-21 12:51:12 +00:00
|
|
|
'dist/**/*',
|
|
|
|
'dist_*/**/*',
|
|
|
|
'dist_ts/**/*',
|
|
|
|
'dist_ts_web/**/*',
|
|
|
|
'assets/**/*',
|
|
|
|
'cli.js',
|
|
|
|
'npmextra.json',
|
|
|
|
'readme.md',
|
|
|
|
],
|
2024-10-21 10:57:54 +00:00
|
|
|
};
|
|
|
|
return JSON.stringify(packageJson, null, 2);
|
2024-10-21 10:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async createPublishModuleDir() {
|
|
|
|
this.options.publishModDirFullPath = plugins.path.join(
|
|
|
|
this.options.monoRepoDir,
|
2024-11-04 23:34:56 +00:00
|
|
|
`dist_publish_${this.options.packageSubFolder}`,
|
2024-10-21 10:16:09 +00:00
|
|
|
);
|
2024-10-28 00:30:39 +00:00
|
|
|
await plugins.smartfile.fs.ensureEmptyDir(this.options.publishModDirFullPath);
|
2024-10-21 12:51:12 +00:00
|
|
|
|
2024-10-21 10:57:54 +00:00
|
|
|
// package.json
|
|
|
|
const packageJson = await plugins.smartfile.SmartFile.fromString(
|
|
|
|
plugins.path.join(this.options.publishModDirFullPath, 'package.json'),
|
|
|
|
await this.createPackageJson(),
|
2024-11-04 23:34:56 +00:00
|
|
|
'utf8',
|
2024-10-21 10:57:54 +00:00
|
|
|
);
|
2024-10-21 12:28:42 +00:00
|
|
|
await packageJson.write();
|
2024-10-21 10:57:54 +00:00
|
|
|
|
2024-10-28 00:21:06 +00:00
|
|
|
// tsconfig.json
|
|
|
|
const originalTsConfigJson = await plugins.smartfile.SmartFile.fromString(
|
|
|
|
plugins.path.join(this.options.publishModDirFullPath, 'tsconfig.json'),
|
|
|
|
await this.createTsconfigJson(),
|
2024-11-04 23:34:56 +00:00
|
|
|
'utf8',
|
2024-10-28 00:21:06 +00:00
|
|
|
);
|
|
|
|
await originalTsConfigJson.write();
|
|
|
|
|
2024-10-21 10:57:54 +00:00
|
|
|
// ts folder
|
2024-10-21 12:51:12 +00:00
|
|
|
await plugins.smartfile.fs.copy(
|
|
|
|
this.options.packageSubFolderFullPath,
|
2024-11-04 23:34:56 +00:00
|
|
|
plugins.path.join(this.options.publishModDirFullPath, this.options.packageSubFolder),
|
2024-10-21 12:51:12 +00:00
|
|
|
);
|
2024-10-28 20:53:18 +00:00
|
|
|
|
|
|
|
// readme
|
|
|
|
await plugins.smartfile.fs.copy(
|
|
|
|
plugins.path.join(this.options.packageSubFolderFullPath, 'readme.md'),
|
2024-11-04 23:34:56 +00:00
|
|
|
plugins.path.join(this.options.publishModDirFullPath, 'readme.md'),
|
2024-10-28 20:53:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// license
|
|
|
|
await plugins.smartfile.fs.copy(
|
|
|
|
plugins.path.join(this.options.monoRepoDir, 'license'),
|
2024-11-04 23:34:56 +00:00
|
|
|
plugins.path.join(this.options.publishModDirFullPath, 'license'),
|
2024-10-28 20:53:18 +00:00
|
|
|
);
|
2024-10-21 10:16:09 +00:00
|
|
|
}
|
2024-10-21 11:21:47 +00:00
|
|
|
|
|
|
|
public async build() {
|
|
|
|
const smartshellInstance = new plugins.smartshell.Smartshell({
|
|
|
|
executor: 'bash',
|
2024-10-21 12:51:12 +00:00
|
|
|
});
|
2024-10-21 11:21:47 +00:00
|
|
|
await smartshellInstance.exec(`cd ${this.options.publishModDirFullPath} && pnpm run build`);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async publish() {
|
|
|
|
const smartshellInstance = new plugins.smartshell.Smartshell({
|
|
|
|
executor: 'bash',
|
2024-10-21 12:51:12 +00:00
|
|
|
});
|
2024-10-21 14:01:08 +00:00
|
|
|
for (const registry of this.options.tsPublishJson.registries) {
|
|
|
|
const registryArray = registry.split(':');
|
|
|
|
const registryUrl = registryArray[0];
|
|
|
|
const registryAccessLevel = registryArray[1];
|
|
|
|
await smartshellInstance.exec(
|
|
|
|
`cd ${this.options.publishModDirFullPath} && pnpm publish ${
|
|
|
|
registryAccessLevel === 'public' ? '--access public' : ''
|
2024-11-04 23:34:56 +00:00
|
|
|
} --no-git-checks --registry https://${registryUrl}`,
|
2024-10-21 14:01:08 +00:00
|
|
|
);
|
|
|
|
}
|
2024-10-21 11:21:47 +00:00
|
|
|
}
|
2024-10-21 10:16:09 +00:00
|
|
|
}
|