108 lines
2.5 KiB
TypeScript
108 lines
2.5 KiB
TypeScript
import { execSync } from 'node:child_process';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import arg from 'arg';
|
|
import semver from 'semver';
|
|
|
|
const sdkPackages = ['sdk', 'react-native', 'web', 'nextjs'];
|
|
|
|
const workspacePath = (relativePath: string) =>
|
|
path.resolve(__dirname, '../../', relativePath);
|
|
|
|
function savePackageJson(absPath: string, data: Record<string, any>) {
|
|
fs.writeFileSync(absPath, JSON.stringify(data, null, 2), 'utf-8');
|
|
}
|
|
|
|
function exit(message: string, error?: unknown) {
|
|
console.log(`❌ ${message}`);
|
|
if (error instanceof Error) {
|
|
console.log(`Error: ${error.message}`);
|
|
console.log(error);
|
|
} else if (typeof error === 'string') {
|
|
console.log(`Error: ${error}`);
|
|
}
|
|
process.exit(1);
|
|
}
|
|
|
|
function main() {
|
|
const args = arg({
|
|
// Types
|
|
'--version': String,
|
|
'--test': Boolean,
|
|
// Aliases
|
|
'-v': '--version',
|
|
});
|
|
|
|
const version = args['--version'];
|
|
const test = args['--test'];
|
|
|
|
if (version && !semver.valid(version)) {
|
|
return console.error('Version is not valid');
|
|
}
|
|
|
|
const properties = {
|
|
private: false,
|
|
version,
|
|
type: 'module',
|
|
main: './dist/index.js',
|
|
module: './dist/index.mjs',
|
|
types: './dist/index.d.ts',
|
|
files: ['dist'],
|
|
exports: {
|
|
import: './dist/index.js',
|
|
require: './dist/index.cjs',
|
|
},
|
|
};
|
|
|
|
try {
|
|
for (const name of sdkPackages) {
|
|
const pkgJson = require(
|
|
workspacePath(`./packages/sdks/${name}/package.json`)
|
|
);
|
|
savePackageJson(workspacePath(`./packages/sdks/${name}/package.json`), {
|
|
...pkgJson,
|
|
...properties,
|
|
dependencies: Object.entries(pkgJson.dependencies).reduce(
|
|
(acc, [depName, depVersion]) => ({
|
|
...acc,
|
|
[depName]: depName.startsWith('@openpanel') ? version : depVersion,
|
|
}),
|
|
{}
|
|
),
|
|
});
|
|
}
|
|
} catch (error) {
|
|
exit('Update JSON files', error);
|
|
}
|
|
|
|
console.log('✅ Update JSON files');
|
|
|
|
try {
|
|
for (const name of sdkPackages) {
|
|
execSync('pnpm build', {
|
|
cwd: workspacePath(`./packages/sdks/${name}`),
|
|
});
|
|
}
|
|
} catch (error) {
|
|
exit('Failed build packages', error);
|
|
}
|
|
|
|
console.log('✅ Built packages');
|
|
|
|
if (!test) {
|
|
try {
|
|
for (const name of sdkPackages) {
|
|
execSync('npm publish --access=public', {
|
|
cwd: workspacePath(`./packages/sdks/${name}`),
|
|
});
|
|
}
|
|
} catch (error) {
|
|
exit('Failed publish packages', error);
|
|
}
|
|
}
|
|
|
|
console.log('✅ All done!');
|
|
}
|
|
|
|
main();
|