From afe5d9d9bf9da683930b97f1adb6e6263e11dd1d Mon Sep 17 00:00:00 2001 From: "zhuo.quan" Date: Wed, 14 Aug 2024 14:05:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=93=E5=8C=85=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.mjs | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 build.mjs diff --git a/build.mjs b/build.mjs new file mode 100644 index 0000000..c748321 --- /dev/null +++ b/build.mjs @@ -0,0 +1,98 @@ +import * as cp from 'child_process'; +import * as fs from 'fs'; +import * as path from 'path'; +import * as url from 'url'; + +async function runCommand(command, args) { + return new Promise((resolve, reject) => { + const child = cp.spawn(command, args, { + stdio: 'inherit', + shell: true, + }); + + console.log(`Running command: ${command} ${args.join(' ')}`); + + child.on('exit', (code) => { + if (code === 0) { + resolve(); + } else { + reject(new Error(`Command failed with code ${code}`)); + } + }); + + child.on('error', (err) => { + console.error(`Failed to start child process: ${err}`); + reject(err); + }); + }); +} + + +class PackageStep { + constructor(folder) { + this.folder = folder; + this.packageJson = path.join(folder, 'package.json'); + this.packageJsonData = JSON.parse(fs.readFileSync(this.packageJson)); + } + + modifyPackageJson() { } + + async vscePackage() { } + + async postSetup() { + console.log('restore workspace...'); + await runCommand('git', ['restore', '.']); + await runCommand('git', ['clean', '-fdx', '-e', '*.vsix', '-e', 'node_modules']); + } + + async run() { + console.log('modify package.json...'); + this.modifyPackageJson(); + fs.writeFileSync(this.packageJson, JSON.stringify(this.packageJsonData, null, 2)); + await this.vscePackage(); + await this.postSetup(); + } +} + +class KylinIdePackage extends PackageStep { + constructor(folder) { + super(folder); + } + + async vscePackage() { + await runCommand('vsce', [ + 'package', + '-o', + `${this.packageJsonData.name}-Kylin-IDE-${this.packageJsonData.version}.vsix` + ]); + } +} + +class VSCodePackage extends PackageStep { + constructor(folder) { + super(folder); + } + + modifyPackageJson() { + this.packageJsonData.name = 'kylin-debug'; + } + + async vscePackage() { + await runCommand('vsce', [ + 'package', + '-o', + `${this.packageJsonData.name}-VSCode-${this.packageJsonData.version}.vsix` + ]); + } +} + +console.log('clean workspace...'); +await runCommand('git', ['clean', '-fdx']); +await runCommand('npm', ['i',]); +const workspace = url.fileURLToPath(new URL('.', import.meta.url)); +console.log('run Kylin-IDE package...'); +let packageTool = new KylinIdePackage(workspace); +await packageTool.run(); +console.log('run VSCode package...'); +packageTool = new VSCodePackage(workspace); +await packageTool.run();