@@ -38,15 +38,29 @@ const camelCase = require('camel-case');
38
38
const cssom = require ( 'cssom' ) ;
39
39
const recast = require ( 'recast' ) ;
40
40
41
- const pkg = require ( path . join ( process . env . PWD , process . argv [ process . argv . length - 1 ] ) ) ;
41
+ const CLI_PACKAGE_JSON_RELATIVE_PATH = process . argv [ 2 ] ;
42
+ if ( ! CLI_PACKAGE_JSON_RELATIVE_PATH ) {
43
+ console . error ( `Usage: node ${ path . basename ( process . argv [ 1 ] ) } packages/mdc-foo/package.json` ) ;
44
+ process . exit ( 1 ) ;
45
+ }
46
+
47
+ if ( ! new RegExp ( 'packages/[^/]+/package.json$' ) . test ( CLI_PACKAGE_JSON_RELATIVE_PATH ) ) {
48
+ console . error ( `Invalid argument: "${ CLI_PACKAGE_JSON_RELATIVE_PATH } " is not a valid path to a package.json file.` ) ;
49
+ console . error ( 'Expected format: packages/mdc-foo/package.json' ) ;
50
+ process . exit ( 1 ) ;
51
+ }
52
+
53
+ const CLI_PACKAGE_JSON = require ( path . resolve ( CLI_PACKAGE_JSON_RELATIVE_PATH ) ) ;
54
+ const REPO_PACKAGE_JSON = require ( path . resolve ( 'package.json' ) ) ;
55
+
56
+ const WEBPACK_CONFIG_RELATIVE_PATH = 'webpack.config.js' ;
57
+ const WEBPACK_CONFIG = require ( path . resolve ( WEBPACK_CONFIG_RELATIVE_PATH ) ) ;
58
+
59
+ const MASTER_TS_RELATIVE_PATH = 'packages/material-components-web/index.ts' ;
60
+ const MASTER_CSS_RELATIVE_PATH = 'packages/material-components-web/material-components-web.scss' ;
61
+ const MASTER_PACKAGE_JSON_RELATIVE_PATH = 'packages/material-components-web/package.json' ;
62
+ const MASTER_PACKAGE_JSON = require ( path . resolve ( MASTER_PACKAGE_JSON_RELATIVE_PATH ) ) ;
42
63
43
- const REPO_PKG = require ( path . join ( process . env . PWD , 'package.json' ) ) ;
44
- const WEBPACK_CONFIG_PATH = 'webpack.config.js' ;
45
- const WEBPACK_CONFIG = require ( path . join ( process . env . PWD , WEBPACK_CONFIG_PATH ) ) ;
46
- const MASTER_PKG_PATH = 'packages/material-components-web/package.json' ;
47
- const MASTER_CSS_PATH = 'packages/material-components-web/material-components-web.scss' ;
48
- const MASTER_JS_PATH = 'packages/material-components-web/index.js' ;
49
- const MASTER_PKG = require ( path . join ( process . env . PWD , MASTER_PKG_PATH ) ) ;
50
64
// These few MDC packages work as foundation or utility packages, and are not
51
65
// directly included in webpack or the material-component-web module. But they
52
66
// are necessary since other MDC packages depend on them.
@@ -81,10 +95,10 @@ main();
81
95
82
96
function main ( ) {
83
97
checkPublicConfigForNewComponent ( ) ;
84
- if ( pkg . name !== MASTER_PKG . name ) {
98
+ if ( CLI_PACKAGE_JSON . name !== MASTER_PACKAGE_JSON . name ) {
85
99
checkNameIsPresentInAllowedScope ( ) ;
86
- if ( pkg . private ) {
87
- console . log ( 'Skipping private component' , pkg . name ) ;
100
+ if ( CLI_PACKAGE_JSON . private ) {
101
+ console . log ( 'Skipping private component' , CLI_PACKAGE_JSON . name ) ;
88
102
} else {
89
103
checkDependencyAddedInWebpackConfig ( ) ;
90
104
checkDependencyAddedInMDCPackage ( ) ;
@@ -93,22 +107,22 @@ function main() {
93
107
}
94
108
95
109
function checkPublicConfigForNewComponent ( ) {
96
- if ( pkg . version === '0.0.0' ) {
97
- assert . notEqual ( typeof pkg . publishConfig , 'undefined' ,
98
- 'Please add publishConfig to' + pkg . name + '\'s package.json. Consult our ' +
110
+ if ( CLI_PACKAGE_JSON . version === '0.0.0' ) {
111
+ assert . notEqual ( typeof CLI_PACKAGE_JSON . publishConfig , 'undefined' ,
112
+ 'Please add publishConfig to' + CLI_PACKAGE_JSON . name + '\'s package.json. Consult our ' +
99
113
'docs/authoring-components.md to ensure your component\'s package.json ' +
100
114
'is well-formed.' ) ;
101
- assert . equal ( pkg . publishConfig . access , 'public' ,
102
- 'Please set publishConfig.access to "public" in ' + pkg . name + '\'s package.json. ' +
115
+ assert . equal ( CLI_PACKAGE_JSON . publishConfig . access , 'public' ,
116
+ 'Please set publishConfig.access to "public" in ' + CLI_PACKAGE_JSON . name + '\'s package.json. ' +
103
117
'Consult our docs/authoring-components.md to ensure your component\'s package.json ' +
104
118
'is well-formed.' ) ;
105
119
}
106
120
}
107
121
108
122
function checkNameIsPresentInAllowedScope ( ) {
109
123
const name = getPkgName ( ) ;
110
- assert . notEqual ( REPO_PKG . config [ 'validate-commit-msg' ] [ 'scope' ] [ 'allowed' ] . indexOf ( name ) , - 1 ,
111
- 'FAILURE: Component ' + pkg . name + ' is not added to allowed scope. Please check package.json ' +
124
+ assert . notEqual ( REPO_PACKAGE_JSON . config [ 'validate-commit-msg' ] [ 'scope' ] [ 'allowed' ] . indexOf ( name ) , - 1 ,
125
+ 'FAILURE: Component ' + CLI_PACKAGE_JSON . name + ' is not added to allowed scope. Please check package.json ' +
112
126
'and add ' + name + ' to config["validate-commit-msg"]["scope"]["allowed"] before commit.' ) ;
113
127
}
114
128
@@ -117,7 +131,7 @@ function checkDependencyAddedInWebpackConfig() {
117
131
checkCSSDependencyAddedInWebpackConfig ( ) ;
118
132
119
133
// Check if js component has been added to webpack config
120
- if ( typeof ( pkg . main ) !== 'undefined' ) {
134
+ if ( typeof ( CLI_PACKAGE_JSON . main ) !== 'undefined' ) {
121
135
checkJSDependencyAddedInWebpackConfig ( ) ;
122
136
}
123
137
}
@@ -126,10 +140,10 @@ function checkJSDependencyAddedInWebpackConfig() {
126
140
const jsconfig = WEBPACK_CONFIG . find ( ( value ) => {
127
141
return value . name === 'main-js-a-la-carte' ;
128
142
} ) ;
129
- const nameCamel = camelCase ( pkg . name . replace ( '@material/' , '' ) ) ;
143
+ const nameCamel = camelCase ( CLI_PACKAGE_JSON . name . replace ( '@material/' , '' ) ) ;
130
144
assert . notEqual ( typeof jsconfig . entry [ nameCamel ] , 'undefined' ,
131
- 'FAILURE: Component ' + pkg . name + ' javascript dependency is not added to webpack ' +
132
- 'configuration. Please add ' + nameCamel + ' to ' + WEBPACK_CONFIG_PATH + '\'s js-components ' +
145
+ 'FAILURE: Component ' + CLI_PACKAGE_JSON . name + ' javascript dependency is not added to webpack ' +
146
+ 'configuration. Please add ' + nameCamel + ' to ' + WEBPACK_CONFIG_RELATIVE_PATH + '\'s js-components ' +
133
147
'entry before commit.' ) ;
134
148
}
135
149
@@ -139,10 +153,10 @@ function checkCSSDependencyAddedInWebpackConfig() {
139
153
const cssconfig = WEBPACK_CONFIG . find ( ( value ) => {
140
154
return value . name === 'main-css-a-la-carte' ;
141
155
} ) ;
142
- const nameMDC = pkg . name . replace ( '@material/' , 'mdc.' ) ;
156
+ const nameMDC = CLI_PACKAGE_JSON . name . replace ( '@material/' , 'mdc.' ) ;
143
157
assert . notEqual ( typeof cssconfig . entry [ nameMDC ] , 'undefined' ,
144
- 'FAILURE: Component ' + pkg . name + ' css dependency not added to webpack ' +
145
- 'configuration. Please add ' + name + ' to ' + WEBPACK_CONFIG_PATH + '\'s css ' +
158
+ 'FAILURE: Component ' + CLI_PACKAGE_JSON . name + ' css dependency not added to webpack ' +
159
+ 'configuration. Please add ' + name + ' to ' + WEBPACK_CONFIG_RELATIVE_PATH + '\'s css ' +
146
160
'entry before commit.' ) ;
147
161
}
148
162
}
@@ -160,49 +174,52 @@ function checkDependencyAddedInMDCPackage() {
160
174
161
175
function checkPkgDependencyAddedInMDCPackage ( ) {
162
176
if ( NOT_MCW_DEP . indexOf ( getPkgName ( ) ) === - 1 ) {
163
- assert . notEqual ( typeof MASTER_PKG . dependencies [ pkg . name ] , 'undefined' ,
164
- 'FAILURE: Component ' + pkg . name + ' is not a denpendency for MDC Web. ' +
165
- 'Please add ' + pkg . name + ' to ' + MASTER_PKG_PATH + '\' dependencies before commit.' ) ;
177
+ assert . notEqual ( typeof MASTER_PACKAGE_JSON . dependencies [ CLI_PACKAGE_JSON . name ] , 'undefined' ,
178
+ 'FAILURE: Component ' + CLI_PACKAGE_JSON . name + ' is not a denpendency for MDC Web. ' +
179
+ 'Please add ' + CLI_PACKAGE_JSON . name + ' to ' + MASTER_PACKAGE_JSON_RELATIVE_PATH +
180
+ '\' dependencies before commit.' ) ;
166
181
}
167
182
}
168
183
169
184
function checkCSSDependencyAddedInMDCPackage ( ) {
170
185
const name = getPkgName ( ) ;
171
186
const nameMDC = `mdc-${ name } ` ;
172
187
if ( CSS_WHITELIST . indexOf ( name ) === - 1 && NOT_MCW_DEP . indexOf ( name ) === - 1 ) {
173
- const src = fs . readFileSync ( path . join ( process . env . PWD , MASTER_CSS_PATH ) , 'utf8' ) ;
188
+ const src = fs . readFileSync ( path . join ( process . env . PWD , MASTER_CSS_RELATIVE_PATH ) , 'utf8' ) ;
174
189
const cssRules = cssom . parse ( src ) . cssRules ;
175
- const cssRule = path . join ( pkg . name , nameMDC ) ;
190
+ const cssRule = path . join ( CLI_PACKAGE_JSON . name , nameMDC ) ;
176
191
177
192
assert . notEqual ( typeof cssRules . find ( ( value ) => {
178
193
return value . href === cssRule ;
179
194
} ) , 'undefined' ,
180
- 'FAILURE: Component ' + pkg . name + ' is not being imported in MDC Web. ' +
181
- 'Please add ' + name + ' to ' + MASTER_CSS_PATH + ' import rule before commit.' ) ;
195
+ 'FAILURE: Component ' + CLI_PACKAGE_JSON . name + ' is not being imported in MDC Web. ' +
196
+ 'Please add ' + name + ' to ' + MASTER_CSS_RELATIVE_PATH + ' import rule before commit.' ) ;
182
197
}
183
198
}
184
199
185
200
function checkJSDependencyAddedInMDCPackage ( ) {
186
201
const NOT_IMPORTED = [ 'animation' ] ;
187
202
const name = getPkgName ( ) ;
188
- if ( typeof ( pkg . main ) !== 'undefined' && NOT_IMPORTED . indexOf ( name ) === - 1 && NOT_MCW_DEP . indexOf ( name ) === - 1 ) {
189
- const nameCamel = camelCase ( pkg . name . replace ( '@material/' , '' ) ) ;
190
- const src = fs . readFileSync ( path . join ( process . env . PWD , MASTER_JS_PATH ) , 'utf8' ) ;
203
+ if ( typeof ( CLI_PACKAGE_JSON . main ) !== 'undefined' &&
204
+ NOT_IMPORTED . indexOf ( name ) === - 1 &&
205
+ NOT_MCW_DEP . indexOf ( name ) === - 1 ) {
206
+ const nameCamel = camelCase ( CLI_PACKAGE_JSON . name . replace ( '@material/' , '' ) ) ;
207
+ const src = fs . readFileSync ( path . join ( process . env . PWD , MASTER_TS_RELATIVE_PATH ) , 'utf8' ) ;
191
208
const ast = recast . parse ( src , {
192
209
parser : {
193
210
parse : ( code ) => parser . parse ( code , { sourceType : 'module' } ) ,
194
211
} ,
195
212
} ) ;
196
213
assert ( checkComponentImportedAddedInMDCPackage ( ast ) , 'FAILURE: Component ' +
197
- pkg . name + ' is not being imported in MDC Web. ' + 'Please add ' + nameCamel +
198
- ' to ' + MASTER_JS_PATH + ' import rule before commit.' ) ;
214
+ CLI_PACKAGE_JSON . name + ' is not being imported in MDC Web. ' + 'Please add ' + nameCamel +
215
+ ' to ' + MASTER_TS_RELATIVE_PATH + ' import rule before commit.' ) ;
199
216
assert ( checkComponentExportedAddedInMDCPackage ( ast ) , 'FAILURE: Component ' +
200
- pkg . name + ' is not being exported in MDC Web. ' + 'Please add ' + nameCamel +
201
- ' to ' + MASTER_JS_PATH + ' export before commit.' ) ;
217
+ CLI_PACKAGE_JSON . name + ' is not being exported in MDC Web. ' + 'Please add ' + nameCamel +
218
+ ' to ' + MASTER_TS_RELATIVE_PATH + ' export before commit.' ) ;
202
219
if ( NOT_AUTOINIT . indexOf ( name ) === - 1 ) {
203
220
assert ( checkAutoInitAddedInMDCPackage ( ast ) > 0 , 'FAILURE: Component ' +
204
- pkg . name + ' seems not being auto inited in MDC Web. ' + 'Please add ' +
205
- nameCamel + ' to ' + MASTER_JS_PATH + ' autoInit statement before commit.' ) ;
221
+ CLI_PACKAGE_JSON . name + ' seems not being auto inited in MDC Web. ' + 'Please add ' +
222
+ nameCamel + ' to ' + MASTER_TS_RELATIVE_PATH + ' autoInit statement before commit.' ) ;
206
223
}
207
224
}
208
225
}
@@ -213,7 +230,7 @@ function checkComponentImportedAddedInMDCPackage(ast) {
213
230
'ImportDeclaration' ( { node} ) {
214
231
if ( node . source ) {
215
232
const source = node . source . value ;
216
- const pkgFile = pkg . name + '/index' ;
233
+ const pkgFile = CLI_PACKAGE_JSON . name + '/index' ;
217
234
// TODO: remove `.ts` when typescript rewrite is complete.
218
235
if ( source === pkgFile || source === pkgFile + '.ts' ) {
219
236
isImported = true ;
@@ -225,7 +242,7 @@ function checkComponentImportedAddedInMDCPackage(ast) {
225
242
}
226
243
227
244
function checkAutoInitAddedInMDCPackage ( ast ) {
228
- let nameCamel = camelCase ( pkg . name . replace ( '@material/' , '' ) ) ;
245
+ let nameCamel = camelCase ( CLI_PACKAGE_JSON . name . replace ( '@material/' , '' ) ) ;
229
246
if ( nameCamel === 'textfield' ) {
230
247
nameCamel = 'textField' ;
231
248
} else if ( nameCamel === 'switch' ) {
@@ -250,7 +267,7 @@ function checkAutoInitAddedInMDCPackage(ast) {
250
267
}
251
268
252
269
function checkComponentExportedAddedInMDCPackage ( ast ) {
253
- let nameCamel = camelCase ( pkg . name . replace ( '@material/' , '' ) ) ;
270
+ let nameCamel = camelCase ( CLI_PACKAGE_JSON . name . replace ( '@material/' , '' ) ) ;
254
271
if ( nameCamel === 'textfield' ) {
255
272
nameCamel = 'textField' ;
256
273
} else if ( nameCamel === 'switch' ) {
@@ -272,7 +289,7 @@ function checkComponentExportedAddedInMDCPackage(ast) {
272
289
}
273
290
274
291
function getPkgName ( ) {
275
- let name = pkg . name . split ( '/' ) [ 1 ] ;
292
+ let name = CLI_PACKAGE_JSON . name . split ( '/' ) [ 1 ] ;
276
293
if ( name === 'textfield' ) {
277
294
// Text-field now has a dash in the name. The package cannot be changed,
278
295
// since it is a lot of effort to rename npm package
0 commit comments