prepublish.js 3.8 KB

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