Skip to content

Commit

Permalink
Merge pull request #998 from ecomfe/fix-npm-script
Browse files Browse the repository at this point in the history
chore: fix deprecated npm script & add check for version and unexpected files before publishing to npm
  • Loading branch information
plainheart authored Mar 14, 2023
2 parents 40abb1d + 197f81a commit 9377dd2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
88 changes: 88 additions & 0 deletions build/prepublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const fs = require('fs-extra');
const chalk = require('chalk');
const ignore = require('ignore');
const { execSync } = require('node:child_process');

console.log();
console.log(chalk.yellowBright(`⚠️ You should have run ${chalk.bold('`npm run release`')} before running this script!`));
console.log();

// check versions in key dist files

console.log(chalk.yellow('🔎 Checking versions in dist files...'));

const fileVersions = [
'package.json',
'package-lock.json',
'dist/zrender.js',
'dist/zrender.min.js'
].map(filePath => ({
file: filePath,
version: require('../' + filePath).version
}));

['lib/zrender.js', 'src/zrender.ts'].forEach(filePath => {
const version = fs.readFileSync(filePath, 'utf-8').match(/export (?:var|const) version = '(\S+)'/)[1];
fileVersions.push({
file: filePath,
version: version
});
});

const versions = fileVersions.map(({ file, version }) => {
console.log(` ∟ The version in [${chalk.blueBright(file)}] is ${chalk.cyanBright.bold(version)}`);
return version;
});

if (new Set(versions).size !== 1) {
console.log();
console.error(chalk.red('❌ Version does not match! Please check and rerun the release script via:'));
console.log();
console.error(chalk.yellow(' npm run release'));
console.log();
process.exit(-1);
}

console.log();
console.log(chalk.green('✔️ Versions are all the same.'));
console.log();

console.log(chalk.yellow('🔎 Checking unexpected files that probably shouldn\'t be published...\n'));

// check if there are unexpected files that not in .npmignore
const npmignore = fs.readFileSync('.npmignore', 'utf-8');
const npmignorePatterns = npmignore
.split(/\r?\n/)
.filter(item => item && !item.startsWith('#'));

const untrackedFiles = execSync('git ls-files --others --exclude-standard', { encoding: 'utf-8' })
.trim()
.split('\n')
.map(escapeOctal);

if (untrackedFiles.length) {
const maybeUnexpectedFiles = ignore().add(npmignorePatterns).filter(untrackedFiles);
if (maybeUnexpectedFiles.length) {
console.error(chalk.red(`❌ Found ${maybeUnexpectedFiles.length} file(s) that are neither tracked by git nor ignored by .npmignore! Please double-check before publishing them to npm.`));
maybeUnexpectedFiles.forEach(filePath => {
console.log(' ∟ ' + filePath);
});
console.log();
process.exit(-1);
}
}

console.log(chalk.green('✔️ No unexpected files found.'));
console.log();

function escapeOctal(str) {
const matches = str.match(/(\\\d{3}){3}/g);
if (matches) {
matches.forEach(match => {
let encoded = '';
match.split('\\').forEach(code => !code || (encoded += '%' + parseInt(code, 8).toString(16)));
str = str.replace(match, decodeURI(encoded));
});
}
return str;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"url": "https://github.com/ecomfe/zrender.git"
},
"scripts": {
"prepublish": "npm run release",
"prepare": "npm run build:lib",
"build": "npm run build:bundle && npm run build:lib",
"release": "node build/build.js --minify && npm run build:lib",
"prepublishOnly": "node build/prepublish.js",
"prepare:nightly": "node build/prepareNightly.js",
"prepare:nightly-next": "node build/prepareNightly.js --next",
"build:bundle": "node build/build.js",
Expand Down

0 comments on commit 9377dd2

Please sign in to comment.