|
| 1 | +#!/usr/bin/env node |
| 2 | +/* |
| 3 | + * @format |
| 4 | + */ |
| 5 | + |
| 6 | +const child_process = require('child_process'); |
| 7 | +const commander = require('commander'); |
| 8 | +const fs = require('fs'); |
| 9 | +const path = require('path'); |
| 10 | +const rimraf = require('rimraf'); |
| 11 | +const semver = require('semver'); |
| 12 | + |
| 13 | +if (!semver.satisfies(process.versions.node, '>= 10.12.0')) { |
| 14 | + console.log('Please execute this script with node version >= 10.12.0'); |
| 15 | + process.exit(1); |
| 16 | +} |
| 17 | + |
| 18 | +commander |
| 19 | + .requiredOption('-T, --tag <tag>', 'NPM published tag') |
| 20 | + .arguments('<dist_tar_file>') |
| 21 | + .option('--dry-run', 'Dry run mode for npm publish') |
| 22 | + .parse(process.argv); |
| 23 | + |
| 24 | +const distTarFile = verifyFile(commander.args[0], '<dist_tar_file>'); |
| 25 | +const rootDir = path.dirname(__dirname); |
| 26 | +const workDir = path.join(rootDir, 'build', 'publish'); |
| 27 | +const distDir = path.join(rootDir, 'dist'); |
| 28 | +if (!fs.existsSync(workDir)) { |
| 29 | + fs.mkdirSync(workDir, {recursive: true}); |
| 30 | +} |
| 31 | + |
| 32 | +// Publish standard package |
| 33 | +console.log('\n\n========== Publish standard package =========='); |
| 34 | +createPatchedContext(rootDir, '', () => { |
| 35 | + if (fs.existsSync(distDir)) { |
| 36 | + rimraf.sync(distDir); |
| 37 | + } |
| 38 | + child_process.execFileSync('tar', ['-xf', distTarFile, '-C', workDir]); |
| 39 | + fs.renameSync(path.join(workDir, 'dist'), distDir); |
| 40 | + const publishArgs = ['publish', '--tag', commander.tag]; |
| 41 | + if (commander.dryRun) { |
| 42 | + publishArgs.push('--dry-run'); |
| 43 | + } |
| 44 | + child_process.execFileSync('npm', publishArgs); |
| 45 | +}); |
| 46 | + |
| 47 | +// Publish unstripped package |
| 48 | +// 1. Add suffix in version, e.g. 245459.0.0-unstripped |
| 49 | +// 2. Add suffix in tag, e.g. latest-unstripped |
| 50 | +// 3. Get unstripped distribution from dist.unstripped/ in CI dist.tgz |
| 51 | +console.log('\n\n========== Publish unstripped package =========='); |
| 52 | +createPatchedContext(rootDir, 'unstripped', () => { |
| 53 | + if (fs.existsSync(distDir)) { |
| 54 | + rimraf.sync(distDir); |
| 55 | + } |
| 56 | + child_process.execFileSync('tar', ['-xf', distTarFile, '-C', workDir]); |
| 57 | + fs.renameSync(path.join(workDir, 'dist.unstripped'), distDir); |
| 58 | + const publishArgs = ['publish', '--tag', `${commander.tag}-unstripped`]; |
| 59 | + if (commander.dryRun) { |
| 60 | + publishArgs.push('--dry-run'); |
| 61 | + } |
| 62 | + child_process.execFileSync('npm', publishArgs); |
| 63 | +}); |
| 64 | + |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | +// Helper functions |
| 67 | +// --------------------------------------------------------------------------- |
| 68 | +function verifyFile(filePath, argName) { |
| 69 | + if (filePath == null) { |
| 70 | + console.error(`Error: ${argName} is required`); |
| 71 | + process.exit(1); |
| 72 | + } |
| 73 | + |
| 74 | + let stat; |
| 75 | + try { |
| 76 | + stat = fs.lstatSync(filePath); |
| 77 | + } catch (error) { |
| 78 | + console.error(error.toString()); |
| 79 | + process.exit(1); |
| 80 | + } |
| 81 | + |
| 82 | + if (!stat.isFile()) { |
| 83 | + console.error(`Error: ${argName} is not a regular file`); |
| 84 | + process.exit(1); |
| 85 | + } |
| 86 | + |
| 87 | + return filePath; |
| 88 | +} |
| 89 | + |
| 90 | +function createPatchedContext(rootDir, versionSuffix, wrappedRunner) { |
| 91 | + const configPath = path.join(rootDir, 'package.json'); |
| 92 | + const origConfig = fs.readFileSync(configPath); |
| 93 | + |
| 94 | + function enter() { |
| 95 | + const patchedConfig = JSON.parse(origConfig); |
| 96 | + if (versionSuffix) { |
| 97 | + patchedConfig.version += '-' + versionSuffix; |
| 98 | + } |
| 99 | + fs.writeFileSync(configPath, JSON.stringify(patchedConfig, null, 2)); |
| 100 | + } |
| 101 | + |
| 102 | + function exit() { |
| 103 | + fs.writeFileSync(configPath, origConfig); |
| 104 | + } |
| 105 | + |
| 106 | + enter(); |
| 107 | + const ret = wrappedRunner.apply(this, arguments); |
| 108 | + exit(); |
| 109 | + return ret; |
| 110 | +} |
0 commit comments