Skip to content

Commit 89db505

Browse files
committed
feat: create dir or throw error when cwd unexisted
resolve #67
1 parent 902c378 commit 89db505

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

bin/mili

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22
const program = require('commander')
3-
const { join, resolve } = require('path')
3+
const { resolve } = require('path')
4+
const fs = require('fs-extra')
45
const log = require('../src/utils/log')
56
const mili = require('../src/mili')
67
const { 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'))

src/commands/upgrade/upgrade-recursive.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { join } = require('path')
1+
const { join, isAbsolute } = require('path')
22
const fs = require('fs-extra')
33
const glob = require('micromatch')
44
const upgrade = require('./upgrade')
@@ -33,8 +33,13 @@ const upgradeRecursive = async(dir, ignore, options) => {
3333
module.exports = async options => {
3434
const {
3535
cwd = process.cwd(),
36-
ignore,
36+
ignore = [],
3737
} = options
3838

39-
await upgradeRecursive(cwd, ignore, options)
40-
}
39+
const absolutePathIgnored = ignore.map(item => {
40+
if (!isAbsolute(item)) return join(cwd, item)
41+
return item
42+
})
43+
44+
await upgradeRecursive(cwd, absolutePathIgnored, options)
45+
}

0 commit comments

Comments
 (0)