11#!/usr/bin/env node
22const program = require ( 'commander' )
3- const { join, resolve } = require ( 'path' )
3+ const { resolve } = require ( 'path' )
4+ const fs = require ( 'fs-extra' )
45const log = require ( '../src/utils/log' )
56const mili = require ( '../src/mili' )
67const { version } = require ( '../package.json' )
@@ -19,14 +20,15 @@ program
1920 . option ( '--no-deps' , 'Need not install dependencies' , false )
2021 . option ( '--force' )
2122 . option ( '-v --version [version]' , 'Set the template version' )
22- . option ( '--cwd [cwd]' , 'Set the current work directory' , absolutize , process . cwd ( ) )
23+ . option ( '--cwd [cwd]' , 'Set the current work directory' , absolutize )
2324 . action ( ( repository , option ) => {
2425 if ( ! repository ) program . help ( )
2526
2627 const { appName, force = false , deps = true , cwd } = option
2728
2829 let version
2930 if ( typeof option . version === 'string' ) version = option . version
31+ if ( cwd ) fs . ensureDir ( cwd )
3032
3133 return mili . init ( { cwd, name : appName , force, repository, version, noDeps : ! deps } )
3234 . then ( ( ) => log . info ( 'initialize complete' ) )
@@ -44,11 +46,14 @@ program
4446 . option ( '--force' )
4547 . option ( '--no-deps' , 'Need not install dependencies' , false )
4648 . option ( '-r, --recursive' , 'Upgrade recursive all subfolder' )
47- . option ( '--ignore [file]' , 'the folder need not search' , collect , [ ] )
48- . option ( '--cwd [cwd]' , 'Set the current work directory' , absolutize , process . cwd ( ) )
49+ . option ( '--ignore [file]' , 'the folder need not search' , collect )
50+ . option ( '--cwd [cwd]' , 'Set the current work directory' , absolutize )
4951 . action ( option => {
50- const { cwd, force = false , deps = true , recursive } = option
51- const ignore = option . ignore . map ( item => join ( cwd , item ) )
52+ const { cwd, force = false , deps = true , recursive, ignore } = option
53+ if ( cwd && ! fs . pathExistsSync ( cwd ) ) {
54+ log . error ( 'cwd' , `No such directory: ${ cwd } ` )
55+ return
56+ }
5257
5358 mili . upgrade ( { cwd, force, noDeps : ! deps , recursive, ignore } )
5459 . catch ( err => log . error ( 'program' , 'upgrade break' , err ) )
@@ -60,12 +65,16 @@ program
6065 . option ( '--force' )
6166 . option ( '-v --version [version]' , 'Set the template version' )
6267 . option ( '--no-deps' , 'Need not install dependencies' , false )
63- . option ( '--cwd [cwd]' , 'Set the current work directory' , absolutize , process . cwd ( ) )
68+ . option ( '--cwd [cwd]' , 'Set the current work directory' , absolutize )
6469 . action ( option => {
6570 const { cwd, force = false , deps = true } = option
6671
6772 let version
6873 if ( typeof option . version === 'string' ) version = option . version
74+ if ( cwd && ! fs . pathExistsSync ( cwd ) ) {
75+ log . error ( 'cwd' , `No such directory: ${ cwd } ` )
76+ return
77+ }
6978
7079 mili . update ( { cwd, force, version, noDeps : ! deps } )
7180 . then ( ( ) => log . info ( 'update complete' ) )
0 commit comments