2023-03-24 19:41:17 +08:00
|
|
|
import fs from 'node:fs';
|
|
|
|
import fg from 'fast-glob';
|
|
|
|
import JSZip from 'jszip';
|
|
|
|
import consola from 'consola';
|
|
|
|
import chalk from 'chalk';
|
|
|
|
import { distPath, exec } from './utils';
|
|
|
|
|
|
|
|
main();
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
exec('pnpm build');
|
|
|
|
|
|
|
|
consola.info('Generate zip\n');
|
|
|
|
for (const filePath of await fg(`${distPath}/**.js`)) {
|
|
|
|
await generateZip(filePath, filePath.replace(/\.js$/, '.zip'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateZip(inputPath: string, outputPath: string) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const inputName = getFileName(inputPath);
|
|
|
|
const outputName = getFileName(outputPath);
|
|
|
|
|
|
|
|
console.log(chalk.cyanBright.bold(`${inputPath} → dist/${outputName}...`));
|
|
|
|
|
|
|
|
const start = Date.now();
|
|
|
|
JSZip()
|
2023-03-24 20:15:51 +08:00
|
|
|
.file(inputName, fs.createReadStream(inputPath), {
|
|
|
|
compressionOptions: {
|
|
|
|
level: 9,
|
|
|
|
},
|
|
|
|
})
|
2023-03-24 19:41:17 +08:00
|
|
|
.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
|
|
|
|
.pipe(fs.createWriteStream(outputPath))
|
|
|
|
.on('finish', (result) => {
|
|
|
|
console.log(
|
|
|
|
`${chalk.green('created')} ${chalk.greenBright.bold(
|
|
|
|
`dist/${outputName} in ${Date.now() - start}ms\n`,
|
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
exec(`rimraf ${inputPath}`);
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.on('error', reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFileName(filePath: string) {
|
|
|
|
const result = filePath.match(/\/([^/]*)$/);
|
|
|
|
if (!result) {
|
|
|
|
throw new Error(`无效的文件路径 ${filePath}`);
|
|
|
|
}
|
|
|
|
return result[1];
|
|
|
|
}
|