-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace original publish.py by node implemented publish.js
- Loading branch information
Showing
3 changed files
with
115 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <tag>', 'NPM published tag') | ||
.arguments('<dist_tar_file>') | ||
.option('--dry-run', 'Dry run mode for npm publish') | ||
.parse(process.argv); | ||
|
||
const distTarFile = verifyFile(commander.args[0], '<dist_tar_file>'); | ||
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; | ||
} |
This file was deleted.
Oops, something went wrong.