Skip to content

Commit

Permalink
Replace original publish.py by node implemented publish.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudo committed Dec 17, 2019
1 parent 38c65a4 commit afb6f53
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 86 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
"config": {
"webkitGTK": "2.26.1",
"chromiumICUCommit": "64e5d7d43a1ff205e3787ab6150bbc1a1837332b"
},
"devDependencies": {
"commander": "^4.0.1",
"rimraf": "^3.0.0",
"semver": "^6.3.0"
}
}
110 changes: 110 additions & 0 deletions scripts/publish.js
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;
}
86 changes: 0 additions & 86 deletions scripts/publish.py

This file was deleted.

0 comments on commit afb6f53

Please sign in to comment.