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 ( / e x p o r t (?: v a r | c o n s t ) v e r s i o n = ' ( \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
+ }
0 commit comments