prepublish.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. function escapeOctal(str) {
  65. const matches = str.match(/(\\\d{3}){3}/g);
  66. if (matches) {
  67. matches.forEach(match => {
  68. let encoded = '';
  69. match.split('\\').forEach(code => !code || (encoded += '%' + parseInt(code, 8).toString(16)));
  70. str = str.replace(match, decodeURI(encoded));
  71. });
  72. }
  73. return str;
  74. }