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[] = [];
|
|
|
|
let current = 0;
|
|
|
|
let found;
|
|
|
|
|
|
|
|
do {
|
|
|
|
found = str.indexOf(delimiter, current);
|
|
|
|
if (found === -1 || result.length === limit - 1) {
|
|
|
|
result.push(str.substring(current));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result.push(str.substring(current, found));
|
|
|
|
current = found + delimiter.length;
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const getLastChangeLog = async () => {
|
|
|
|
const mainChainLogPath = 'CHANGELOG.md';
|
|
|
|
const fs = (await pFs).default
|
|
|
|
if (!fs.existsSync(mainChainLogPath)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
const mainChainLog = await readFirst2000Characters(mainChainLogPath)
|
|
|
|
const manifestVersions = limitedSplit(mainChainLog, '## ', 2)[1]
|
|
|
|
const changesText = '## ' + manifestVersions
|
|
|
|
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`;
|
2022-10-16 22:25:20 +00:00
|
|
|
|
2024-08-29 01:37:40 +00:00
|
|
|
fs.writeFileSync(
|
|
|
|
changeLogPath,
|
|
|
|
`# ${pkg.version} \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
|
|
|
})();
|