From fd6aed2fc736d2789e3f7a46cf38e7fd20acbd2f Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Fri, 29 Nov 2019 16:34:16 +0800 Subject: [PATCH] Replace original publish.py by node implemented publish.js --- package.json | 5 +++ scripts/publish.js | 110 +++++++++++++++++++++++++++++++++++++++++++++ scripts/publish.py | 90 ------------------------------------- 3 files changed, 115 insertions(+), 90 deletions(-) create mode 100755 scripts/publish.js delete mode 100644 scripts/publish.py diff --git a/package.json b/package.json index 4d7c97a2e..107385551 100644 --- a/package.json +++ b/package.json @@ -28,5 +28,10 @@ "config": { "webkitGTK": "2.24.2", "chromiumICUCommit": "64e5d7d43a1ff205e3787ab6150bbc1a1837332b" + }, + "devDependencies": { + "commander": "^4.0.1", + "rimraf": "^3.0.0", + "semver": "^6.3.0" } } diff --git a/scripts/publish.js b/scripts/publish.js new file mode 100755 index 000000000..dd85a64dc --- /dev/null +++ b/scripts/publish.js @@ -0,0 +1,110 @@ +#!/usr/bin/env node +/* + * @format + */ + +const child_process = require('child_process'); +const commander = require('commander'); +const fs = require('fs'); +const path = require('path'); +const rimraf = require('rimraf'); +const semver = require('semver'); + +if (!semver.satisfies(process.versions.node, '>= 10.12.0')) { + console.log('Please execute this script with node version >= 10.12.0'); + process.exit(1); +} + +commander + .requiredOption('-T, --tag ', 'NPM published tag') + .arguments('') + .option('--dry-run', 'Dry run mode for npm publish') + .parse(process.argv); + +const distTarFile = verifyFile(commander.args[0], ''); +const rootDir = path.dirname(__dirname); +const workDir = path.join(rootDir, 'build', 'publish'); +const distDir = path.join(rootDir, 'dist'); +if (!fs.existsSync(workDir)) { + fs.mkdirSync(workDir, {recursive: true}); +} + +// Publish standard package +console.log('\n\n========== Publish standard package =========='); +createPatchedContext(rootDir, '', () => { + if (fs.existsSync(distDir)) { + rimraf.sync(distDir); + } + child_process.execFileSync('tar', ['-xf', distTarFile, '-C', workDir]); + fs.renameSync(path.join(workDir, 'dist'), distDir); + const publishArgs = ['publish', '--tag', commander.tag]; + if (commander.dryRun) { + publishArgs.push('--dry-run'); + } + child_process.execFileSync('npm', publishArgs); +}); + +// Publish unstripped package +// 1. Add suffix in version, e.g. 245459.0.0-unstripped +// 2. Add suffix in tag, e.g. latest-unstripped +// 3. Get unstripped distribution from dist.unstripped/ in CI dist.tgz +console.log('\n\n========== Publish unstripped package =========='); +createPatchedContext(rootDir, 'unstripped', () => { + if (fs.existsSync(distDir)) { + rimraf.sync(distDir); + } + child_process.execFileSync('tar', ['-xf', distTarFile, '-C', workDir]); + fs.renameSync(path.join(workDir, 'dist.unstripped'), distDir); + const publishArgs = ['publish', '--tag', `${commander.tag}-unstripped`]; + if (commander.dryRun) { + publishArgs.push('--dry-run'); + } + child_process.execFileSync('npm', publishArgs); +}); + +// --------------------------------------------------------------------------- +// Helper functions +// --------------------------------------------------------------------------- +function verifyFile(filePath, argName) { + if (filePath == null) { + console.error(`Error: ${argName} is required`); + process.exit(1); + } + + let stat; + try { + stat = fs.lstatSync(filePath); + } catch (error) { + console.error(error.toString()); + process.exit(1); + } + + if (!stat.isFile()) { + console.error(`Error: ${argName} is not a regular file`); + process.exit(1); + } + + return filePath; +} + +function createPatchedContext(rootDir, versionSuffix, wrappedRunner) { + const configPath = path.join(rootDir, 'package.json'); + const origConfig = fs.readFileSync(configPath); + + function enter() { + const patchedConfig = JSON.parse(origConfig); + if (versionSuffix) { + patchedConfig.version += '-' + versionSuffix; + } + fs.writeFileSync(configPath, JSON.stringify(patchedConfig, null, 2)); + } + + function exit() { + fs.writeFileSync(configPath, origConfig); + } + + enter(); + const ret = wrappedRunner.apply(this, arguments); + exit(); + return ret; +} diff --git a/scripts/publish.py b/scripts/publish.py deleted file mode 100644 index 09f029498..000000000 --- a/scripts/publish.py +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env python -import argparse -import json -import os -import shutil -import subprocess -import sys - -ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) - - -class PackageConfigPatcher: - def __init__(self, suffix=''): - self._suffix = suffix - self._config_path = os.path.join(ROOT_DIR, 'package.json') - self._orig_config = '' - - def __enter__(self): - with open(self._config_path, 'r') as f: - self._orig_config = f.read() - patched_config = self._create_patched_config(self._orig_config) - with open(self._config_path, 'w') as f: - f.write(patched_config) - - def __exit__(self, exc_type, exc_value, traceback): - with open(self._config_path, 'w') as f: - f.write(self._orig_config) - - def _create_patched_config(self, config): - patched_config = json.loads(config) - if self._suffix: - patched_config[ - 'version'] = patched_config['version'] + '-' + self._suffix - return json.dumps(patched_config, indent=2) - - -def parse_args(): - arg_parser = argparse.ArgumentParser() - - arg_parser.add_argument( - '--dry-run', action='store_true', help='Dry run mode for npm publish') - arg_parser.add_argument( - '--tag', '-T', type=str, required=True, help='NPM published tag') - arg_parser.add_argument( - 'dist_tar_file', action='store', help='dist.tgz created from CI') - - args = arg_parser.parse_args() - if not args.dist_tar_file: - arg_parser.print_help() - sys.exit(1) - return args - - -def main(): - args = parse_args() - - workdir = os.path.join(ROOT_DIR, 'build', 'publish') - if not os.path.exists(workdir): - os.makedirs(workdir) - distdir = os.path.join(ROOT_DIR, 'dist') - - print('\n\n========== Publish standard package ==========') - with PackageConfigPatcher(''): - if os.path.exists(distdir): - shutil.rmtree(distdir) - subprocess.check_call( - ['tar', '-xf', args.dist_tar_file, '-C', workdir]) - shutil.move(os.path.join(workdir, 'dist'), distdir) - cmds = ['npm', 'publish', '--tag', args.tag] - if args.dry_run: - cmds.append('--dry-run') - subprocess.check_call(cmds) - - print('\n\n========== Publish unstripped package ==========') - with PackageConfigPatcher('unstripped'): - if os.path.exists(distdir): - shutil.rmtree(distdir) - subprocess.check_call( - ['tar', '-xf', args.dist_tar_file, '-C', workdir]) - shutil.move(os.path.join(workdir, 'dist.unstripped'), distdir) - cmds = ['npm', 'publish', '--tag', args.tag + '-unstripped'] - if args.dry_run: - cmds.append('--dry-run') - subprocess.check_call(cmds) - - shutil.rmtree(workdir) - - -if __name__ == '__main__': - main()