Skip to content

Commit b42b1cb

Browse files
committed
feat(*): add project status check before init, upgrade, update
resolve #26
1 parent 7cda6f7 commit b42b1cb

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

bin/init

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ const copy = require('../src/copy')
1414
const log = require('../src/log')
1515
const { join, basename } = require('path')
1616
const git = require('simple-git/promise')
17+
const statusCheck = require('../src/statusCheck')
1718

1819

19-
module.exports = async (name, repository) => {
20+
module.exports = async (name, repository, force) => {
21+
if (!force) await statusCheck(process.cwd())
22+
2023
// const miliConfig = await loadMiliConfig()
2124
const { templatePath: templateProjectPath, currentBranch } = await cloneRepository(repository)
2225
try {

bin/mili

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#!/usr/bin/env node
2-
3-
const { readdirSync } = require('fs')
4-
const { join, basename } = require('path')
52
const program = require('commander')
63
const log = require('../src/log')
74
const init = require('./init')
@@ -19,34 +16,41 @@ program
1916
.usage('[options] <repository>')
2017
.description('initialize the project')
2118
.option('-n --app-name [app_name]', `Set your app name.`)
19+
.option('--force')
2220
.action((repository, option) => {
2321
if (!repository) program.help()
2422

25-
const { appName } = option
23+
const { appName, force = false } = option
2624

2725
log.info('initialize project ...')
2826

29-
return init(appName, repository)
27+
return init(appName, repository, force)
3028
.then(() => log.info('initialize complete'))
3129
.catch(err => log.error('program', 'initialize break', err))
3230
})
3331

3432
program
3533
.command('upgrade')
3634
.description('upgrade the template')
37-
.action(() => {
35+
.option('--force')
36+
.action((option) => {
37+
const { force = false } = option
38+
3839
log.info('prepare upgrade')
39-
upgrade()
40+
upgrade(force)
4041
.then(() => log.info('upgrade complete'))
4142
.catch(err => log.error('program', 'upgrade break', err))
4243
})
4344

4445
program
4546
.command('update')
4647
.description('Update the project with the current version of the template')
47-
.action(() => {
48+
.option('--force')
49+
.action((option) => {
50+
const { force = false } = option
51+
4852
log.info('prepare update')
49-
update()
53+
update(force)
5054
.then(() => log.info('update complete'))
5155
.catch(err => log.error('program', 'update break', err))
5256
})

bin/update

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ const copy = require('../src/copy')
1414
const log = require('../src/log')
1515
const { join, basename } = require('path')
1616
const git = require('simple-git/promise')
17+
const statusCheck = require('../src/statusCheck')
1718

1819

19-
module.exports = async () => {
20+
module.exports = async (force) => {
21+
if (!force) await statusCheck(process.cwd())
22+
2023
const miliConfig = await loadMiliConfig()
2124
const repository = miliConfig.repository
2225

bin/upgrade

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ const log = require('../src/log')
1515
const semver = require('semver')
1616
const { join, basename } = require('path')
1717
const git = require('simple-git/promise')
18+
const statusCheck = require('../src/statusCheck')
1819

1920

20-
module.exports = async () => {
21+
module.exports = async (force) => {
22+
if (!force) await statusCheck(process.cwd())
23+
2124
const miliConfig = await loadMiliConfig()
2225
const repository = miliConfig.repository
2326

src/statusCheck.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const fs = require('fs-extra')
2+
const git = require('simple-git/promise')
3+
const throwError = require('./throwError')
4+
5+
6+
const isEmpty = async path => {
7+
const files = await fs.readdir(path)
8+
if (files.length) return false
9+
10+
return true
11+
}
12+
13+
const reminder = [
14+
'This command may cause some files to be overwritten',
15+
"If you're sure you want to run this command, rerun it with --force.",
16+
].join('\n')
17+
module.exports = async path => {
18+
if (!(await isEmpty(path))) {
19+
throwError([
20+
'The project directory is not a git repository and is a non-empty folder.',
21+
reminder,
22+
].join('\n'))
23+
} else if (await git(path).checkIsRepo()) {
24+
throwError([
25+
'Git working directory not clean',
26+
reminder,
27+
].join('\n'))
28+
}
29+
}

0 commit comments

Comments
 (0)