Skip to content

Commit 9377dd2

Browse files
authored
Merge pull request #998 from ecomfe/fix-npm-script
chore: fix deprecated npm script & add check for version and unexpected files before publishing to npm
2 parents 40abb1d + 197f81a commit 9377dd2

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

build/prepublish.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const fs = require('fs-extra');
2+
const chalk = require('chalk');
3+
const ignore = require('ignore');
4+
const { execSync } = require('node:child_process');
5+
6+
console.log();
7+
console.log(chalk.yellowBright(`⚠️ You should have run ${chalk.bold('`npm run release`')} before running this script!`));
8+
console.log();
9+
10+
// check versions in key dist files
11+
12+
console.log(chalk.yellow('🔎 Checking versions in dist files...'));
13+
14+
const fileVersions = [
15+
'package.json',
16+
'package-lock.json',
17+
'dist/zrender.js',
18+
'dist/zrender.min.js'
19+
].map(filePath => ({
20+
file: filePath,
21+
version: require('../' + filePath).version
22+
}));
23+
24+
['lib/zrender.js', 'src/zrender.ts'].forEach(filePath => {
25+
const version = fs.readFileSync(filePath, 'utf-8').match(/export (?:var|const) version = '(\S+)'/)[1];
26+
fileVersions.push({
27+
file: filePath,
28+
version: version
29+
});
30+
});
31+
32+
const versions = fileVersions.map(({ file, version }) => {
33+
console.log(` ∟ The version in [${chalk.blueBright(file)}] is ${chalk.cyanBright.bold(version)}`);
34+
return version;
35+
});
36+
37+
if (new Set(versions).size !== 1) {
38+
console.log();
39+
console.error(chalk.red('❌ Version does not match! Please check and rerun the release script via:'));
40+
console.log();
41+
console.error(chalk.yellow(' npm run release'));
42+
console.log();
43+
process.exit(-1);
44+
}
45+
46+
console.log();
47+
console.log(chalk.green('✔️ Versions are all the same.'));
48+
console.log();
49+
50+
console.log(chalk.yellow('🔎 Checking unexpected files that probably shouldn\'t be published...\n'));
51+
52+
// check if there are unexpected files that not in .npmignore
53+
const npmignore = fs.readFileSync('.npmignore', 'utf-8');
54+
const npmignorePatterns = npmignore
55+
.split(/\r?\n/)
56+
.filter(item => item && !item.startsWith('#'));
57+
58+
const untrackedFiles = execSync('git ls-files --others --exclude-standard', { encoding: 'utf-8' })
59+
.trim()
60+
.split('\n')
61+
.map(escapeOctal);
62+
63+
if (untrackedFiles.length) {
64+
const maybeUnexpectedFiles = ignore().add(npmignorePatterns).filter(untrackedFiles);
65+
if (maybeUnexpectedFiles.length) {
66+
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.`));
67+
maybeUnexpectedFiles.forEach(filePath => {
68+
console.log(' ∟ ' + filePath);
69+
});
70+
console.log();
71+
process.exit(-1);
72+
}
73+
}
74+
75+
console.log(chalk.green('✔️ No unexpected files found.'));
76+
console.log();
77+
78+
function escapeOctal(str) {
79+
const matches = str.match(/(\\\d{3}){3}/g);
80+
if (matches) {
81+
matches.forEach(match => {
82+
let encoded = '';
83+
match.split('\\').forEach(code => !code || (encoded += '%' + parseInt(code, 8).toString(16)));
84+
str = str.replace(match, decodeURI(encoded));
85+
});
86+
}
87+
return str;
88+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
"url": "https://github.com/ecomfe/zrender.git"
1515
},
1616
"scripts": {
17-
"prepublish": "npm run release",
17+
"prepare": "npm run build:lib",
1818
"build": "npm run build:bundle && npm run build:lib",
1919
"release": "node build/build.js --minify && npm run build:lib",
20+
"prepublishOnly": "node build/prepublish.js",
2021
"prepare:nightly": "node build/prepareNightly.js",
2122
"prepare:nightly-next": "node build/prepareNightly.js --next",
2223
"build:bundle": "node build/build.js",

0 commit comments

Comments
 (0)