2022-10-16 23:21:07 +00:00
|
|
|
const pFs = import('fs')
|
2024-08-29 01:37:40 +00:00
|
|
|
const pCps = import('child_process')
|
2022-10-16 23:21:07 +00:00
|
|
|
|
2025-01-19 21:45:16 +00:00
|
|
|
|
|
|
|
async function readFirst2000Characters(filePath: string): Promise<string> {
|
|
|
|
|
|
|
|
const fs = (await pFs).default
|
|
|
|
try {
|
|
|
|
const fileStream = fs.createReadStream(filePath, { encoding: 'utf8' });
|
|
|
|
let data = '';
|
|
|
|
|
|
|
|
for await (const chunk of fileStream) {
|
|
|
|
data += chunk;
|
|
|
|
if (data.length >= 2000) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.substring(0, 2000);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Error reading file: ${err}`);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function limitedSplit(str: string, delimiter: string, limit: number): string[] {
|
|
|
|
if (limit <= 0) {
|
|
|
|
throw new Error("Limit must be greater than 0");
|
|
|
|
}
|
|
|
|
const result: string[] = [];
|
2025-01-23 08:03:48 +00:00
|
|
|
let remaining: string = str;
|
|
|
|
for (let i = 0; i < limit; i++) {
|
|
|
|
const index = remaining.indexOf(delimiter);
|
|
|
|
if (index === -1) {
|
2025-01-19 21:45:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2025-01-23 08:03:48 +00:00
|
|
|
result.push(remaining.substring(0, index));
|
|
|
|
remaining = remaining.substring(index + delimiter.length);
|
|
|
|
}
|
2025-01-19 21:45:16 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2025-01-23 08:03:48 +00:00
|
|
|
export const getLastChangeLog = async () => {
|
2025-01-19 21:45:16 +00:00
|
|
|
const mainChainLogPath = 'CHANGELOG.md';
|
|
|
|
const fs = (await pFs).default
|
|
|
|
if (!fs.existsSync(mainChainLogPath)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
const mainChainLog = await readFirst2000Characters(mainChainLogPath)
|
2025-01-23 08:03:48 +00:00
|
|
|
const manifestVersions = limitedSplit(mainChainLog, '##', 2)[1]
|
|
|
|
const changesText = '##' + manifestVersions
|
2025-01-19 21:45:16 +00:00
|
|
|
return changesText
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function ghRelease (isRebuild: boolean) {
|
2022-10-16 23:21:07 +00:00
|
|
|
const fs = (await pFs).default
|
|
|
|
|
2024-08-29 01:37:40 +00:00
|
|
|
if (!fs.existsSync('releases')) {
|
2022-10-16 23:21:07 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2024-08-29 01:37:40 +00:00
|
|
|
if (!isRebuild) {
|
|
|
|
const changeLogPath = `releases/${pkg.version}.changelog.md`;
|
2025-01-23 08:03:48 +00:00
|
|
|
const releaseCreationDate = new Date().toISOString();
|
2022-10-16 22:25:20 +00:00
|
|
|
|
2024-08-29 01:37:40 +00:00
|
|
|
fs.writeFileSync(
|
|
|
|
changeLogPath,
|
2025-01-23 08:03:48 +00:00
|
|
|
`# Latest changes - (${releaseCreationDate})\n\n
|
2025-01-19 21:45:16 +00:00
|
|
|
${await getLastChangeLog()}`,
|
2024-08-29 01:37:40 +00:00
|
|
|
);
|
|
|
|
const cps = (await pCps)
|
|
|
|
console.log(
|
|
|
|
await new Promise((resolve) => {
|
2025-01-19 21:45:16 +00:00
|
|
|
const p = cps.spawn('gh', ['release', 'create', `v${pkg.version}`, `./${outputPath}`, '-F', `./${changeLogPath}`, '--target', 'main'], {
|
2024-08-29 01:37:40 +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);
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2022-10-16 22:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
2024-08-29 01:37:40 +00:00
|
|
|
|
2025-01-19 21:45:16 +00:00
|
|
|
const isRebuild = process.argv[2] === 'rebuild';
|
2024-08-29 01:37:40 +00:00
|
|
|
|
2025-01-19 21:45:16 +00:00
|
|
|
await ghRelease(isRebuild);
|
|
|
|
console.log('Release created');
|
2022-10-16 22:25:20 +00:00
|
|
|
})();
|