2022-10-16 23:21:07 +00:00
|
|
|
const pFs = import('fs')
|
|
|
|
const pCps = import ('child_process')
|
|
|
|
|
|
|
|
async function ghRelease(changes: string[]) {
|
|
|
|
const fs = (await pFs).default
|
|
|
|
|
|
|
|
if (!fs.existsSync('releases')){
|
|
|
|
fs.mkdirSync('releases');
|
|
|
|
}
|
2022-10-16 22:25:20 +00:00
|
|
|
|
|
|
|
const pkg = JSON.parse(fs.readFileSync('package.json').toString());
|
|
|
|
|
2022-10-16 23:21:07 +00:00
|
|
|
const archiver = (await import('archiver')).default
|
2022-10-16 22:25:20 +00:00
|
|
|
const archive = archiver('zip', { zlib: { level: 9 } });
|
|
|
|
const dirPipes = ['dist'];
|
|
|
|
|
|
|
|
const filePipes = ['LICENSE', 'README.md', 'PRIVACY_POLICY.md'];
|
|
|
|
const outputPath = `releases/${pkg.version}.zip`;
|
|
|
|
const outputZip = fs.createWriteStream(outputPath);
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
let arch = archive;
|
|
|
|
dirPipes.forEach((dir) => {
|
|
|
|
arch = arch.directory(dir, false);
|
|
|
|
});
|
|
|
|
filePipes.forEach((file) => {
|
|
|
|
arch = arch.file(file, { name: file });
|
|
|
|
});
|
2022-10-16 23:21:07 +00:00
|
|
|
arch.on('error', (err: unknown) => reject(err)).pipe(outputZip);
|
2022-10-16 22:25:20 +00:00
|
|
|
|
|
|
|
outputZip.on('close', () => resolve(true));
|
|
|
|
arch.finalize();
|
|
|
|
});
|
|
|
|
|
|
|
|
const changeLogPath = `releases/${pkg.version}.changelog.md`;
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
changeLogPath,
|
|
|
|
`# ${pkg.version} \n
|
2022-10-16 23:21:07 +00:00
|
|
|
${changes.reduce((acc: string, change: string) => {
|
2022-10-16 22:25:20 +00:00
|
|
|
return acc + `- ${change}\n`;
|
|
|
|
}, '')}`,
|
|
|
|
);
|
2022-10-16 23:21:07 +00:00
|
|
|
const cps = (await pCps)
|
2022-10-16 22:25:20 +00:00
|
|
|
console.log(
|
|
|
|
await new Promise((resolve) => {
|
2022-10-16 23:21:07 +00:00
|
|
|
const p = cps.spawn('gh', ['release', 'create', `v${pkg.version}`, `./${outputPath}`, '-F', `./${changeLogPath}`], {
|
2022-10-16 22:25:20 +00:00
|
|
|
shell: true,
|
|
|
|
});
|
|
|
|
let result = '';
|
|
|
|
p.stdout.on('data', (data) => (result += data.toString()));
|
|
|
|
p.stderr.on('data', (data) => (result += data.toString()));
|
|
|
|
p.on('close', () => {
|
|
|
|
resolve(result);
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
if (!process.argv[2]) {
|
|
|
|
console.log('No changes provided');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const changes = process.argv[2].split(',');
|
|
|
|
await ghRelease(changes);
|
|
|
|
console.log('Release created', changes);
|
|
|
|
})();
|