Skip to content

Commit

Permalink
feat: create dir or throw error when cwd is unexist
Browse files Browse the repository at this point in the history
resolve #67
  • Loading branch information
Val-istar-Guo committed May 15, 2019
1 parent 902c378 commit 857a4c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
23 changes: 16 additions & 7 deletions bin/mili
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
const program = require('commander')
const { join, resolve } = require('path')
const { resolve } = require('path')
const fs = require('fs-extra')
const log = require('../src/utils/log')
const mili = require('../src/mili')
const { version } = require('../package.json')
Expand All @@ -19,14 +20,15 @@ program
.option('--no-deps', 'Need not install dependencies', false)
.option('--force')
.option('-v --version [version]', 'Set the template version')
.option('--cwd [cwd]', 'Set the current work directory', absolutize, process.cwd())
.option('--cwd [cwd]', 'Set the current work directory', absolutize)
.action((repository, option) => {
if (!repository) program.help()

const { appName, force = false, deps = true, cwd } = option

let version
if (typeof option.version === 'string') version = option.version
if (cwd) fs.ensureDir(cwd)

return mili.init({ cwd, name: appName, force, repository, version, noDeps: !deps })
.then(() => log.info('initialize complete'))
Expand All @@ -44,11 +46,14 @@ program
.option('--force')
.option('--no-deps', 'Need not install dependencies', false)
.option('-r, --recursive', 'Upgrade recursive all subfolder')
.option('--ignore [file]', 'the folder need not search', collect, [])
.option('--cwd [cwd]', 'Set the current work directory', absolutize, process.cwd())
.option('--ignore [file]', 'the folder need not search', collect)
.option('--cwd [cwd]', 'Set the current work directory', absolutize)
.action(option => {
const { cwd, force = false, deps = true, recursive } = option
const ignore = option.ignore.map(item => join(cwd, item))
const { cwd, force = false, deps = true, recursive, ignore } = option
if (cwd && !fs.pathExistsSync(cwd)) {
log.error('cwd', `No such directory: ${cwd}`)
return
}

mili.upgrade({ cwd, force, noDeps: !deps, recursive, ignore })
.catch(err => log.error('program', 'upgrade break', err))
Expand All @@ -60,12 +65,16 @@ program
.option('--force')
.option('-v --version [version]', 'Set the template version')
.option('--no-deps', 'Need not install dependencies', false)
.option('--cwd [cwd]', 'Set the current work directory', absolutize, process.cwd())
.option('--cwd [cwd]', 'Set the current work directory', absolutize)
.action(option => {
const { cwd, force = false, deps = true } = option

let version
if (typeof option.version === 'string') version = option.version
if (cwd && !fs.pathExistsSync(cwd)) {
log.error('cwd', `No such directory: ${cwd}`)
return
}

mili.update({ cwd, force, version, noDeps: !deps })
.then(() => log.info('update complete'))
Expand Down
13 changes: 9 additions & 4 deletions src/commands/upgrade/upgrade-recursive.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { join } = require('path')
const { join, isAbsolute } = require('path')
const fs = require('fs-extra')
const glob = require('micromatch')
const upgrade = require('./upgrade')
Expand Down Expand Up @@ -33,8 +33,13 @@ const upgradeRecursive = async(dir, ignore, options) => {
module.exports = async options => {
const {
cwd = process.cwd(),
ignore,
ignore = [],
} = options

await upgradeRecursive(cwd, ignore, options)
}
const absolutePathIgnored = ignore.map(item => {
if (!isAbsolute(item)) return join(cwd, item)
return item
})

await upgradeRecursive(cwd, absolutePathIgnored, options)
}

0 comments on commit 857a4c7

Please sign in to comment.