@@ -28,6 +28,7 @@ optParser.addOption('b', 'binary', 'boolean', 'Pack binary');
2828optParser . addOption ( 'np' , 'no-pseudo' , 'boolean' , 'Whether use pseudo library' ) ;
2929optParser . addOption ( 'wf' , 'with-ffmpeg' , 'boolean' , 'Whether pack ffmpeg library' ) ;
3030optParser . addOption ( 'h' , 'help' , 'boolean' , 'Show help' ) ;
31+ optParser . addOption ( 'li' , 'lint' , 'boolean' , 'Whether lint code with eslint' ) ;
3132
3233const options = optParser . parseArgs ( process . argv ) ;
3334
@@ -58,6 +59,26 @@ if (options.help || Object.keys(options).length === 0) {
5859 process . exit ( 0 ) ;
5960}
6061
62+ if ( options . lint ) {
63+ // Check lint deps
64+ const lintDeps = [ 'eslint' ] ;
65+ console . log ( 'Checking lint dependencies...' ) ;
66+ const npmRoot = execSync ( `npm root -g` ) . toString ( ) . trim ( ) ;
67+ const missingLintDeps = lintDeps . filter ( ( dep ) => {
68+ return ! fs . existsSync ( path . join ( npmRoot , dep ) ) ;
69+ } ) ;
70+
71+ if ( missingLintDeps . length === 0 ) {
72+ console . log ( 'Lint dependencies OK.' ) ;
73+ } else {
74+ for ( const dep of missingLintDeps ) {
75+ console . log ( 'Installing eslint' ) ;
76+ execSync ( `npm install eslint --global --save-dev` ) ;
77+ execSync ( 'npm init --yes' ) ;
78+ }
79+ }
80+ }
81+
6182var npmInstallOption = '' ;
6283if ( process . getuid && process . getuid ( ) === 0 ) {
6384 // Running as root
@@ -237,9 +258,30 @@ function packCommon(target) {
237258 }
238259 }
239260 if ( common . files ) {
261+ let eslint ;
262+ if ( options . lint ) {
263+ const { ESLint } = require ( 'eslint' ) ;
264+ eslint = new ESLint ( { overrideConfigFile : `${ rootDir } /source/.eslintrc.json` } ) ;
265+ }
240266 // Copy common files
241267 for ( const file of common . files ) {
242268 const filePath = path . join ( packSrc , file ) ;
269+ const extname = path . extname ( filePath ) ;
270+ if ( options . lint && extname === '.js' ) {
271+ eslint . lintFiles ( filePath )
272+ . then ( ( results ) => {
273+ eslint . loadFormatter ( 'stylish' )
274+ . then ( ( formatter ) => {
275+ console . log ( formatter . format ( results ) ) ;
276+ } )
277+ . catch ( ( err ) => {
278+ console . log ( err ) ;
279+ } )
280+ } )
281+ . catch ( ( err ) => {
282+ console . log ( err ) ;
283+ } )
284+ }
243285 execSync ( `cp -a ${ filePath } ${ packDist } ` ) ;
244286 }
245287 }
0 commit comments