Skip to content

Commit 0366505

Browse files
committed
feat(*): access mili commands programmatically from javascript
resolve #33
1 parent 590a66e commit 0366505

25 files changed

+285
-292
lines changed

bin/clean

Lines changed: 0 additions & 8 deletions
This file was deleted.

bin/init

Lines changed: 0 additions & 67 deletions
This file was deleted.

bin/mili

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#!/usr/bin/env node
22
const program = require('commander')
33
const log = require('../src/log')
4-
const init = require('./init')
5-
const upgrade = require('./upgrade')
6-
const update = require('./update')
7-
const clean = require('./clean')
4+
const mili = require('../src/mili')
85
const { version } = require('../package.json')
96

107

@@ -26,7 +23,8 @@ program
2623
let version
2724
if (typeof option.version === 'string') version = option.version
2825

29-
return init(appName, repository, force, version)
26+
// return init(appName, repository, force, version)
27+
return mili.init({ name: appName, force, repository, version })
3028
.then(() => log.info('initialize complete'))
3129
.catch(err => log.error('program', 'initialize break', err))
3230
})
@@ -38,7 +36,7 @@ program
3836
.action((option) => {
3937
const { force = false } = option
4038

41-
upgrade(force)
39+
mili.upgrade({ force })
4240
.then(() => log.info('upgrade complete'))
4341
.catch(err => log.error('program', 'upgrade break', err))
4442
})
@@ -54,7 +52,7 @@ program
5452
let version
5553
if (typeof option.version === 'string') version = option.version
5654

57-
update(force, version)
55+
mili.update({ force, version })
5856
.then(() => log.info('update complete'))
5957
.catch(err => log.error('program', 'update break', err))
6058
})
@@ -64,7 +62,7 @@ program
6462
.description('Clean the cache of mili')
6563
.action(() => {
6664
log.info('begin clean')
67-
clean()
65+
mili.clean()
6866
.then(() => log.info('clean complete'))
6967
.catch(err => log.error('clean', 'clean break', err))
7068
})

bin/update

Lines changed: 0 additions & 79 deletions
This file was deleted.

bin/upgrade

Lines changed: 0 additions & 77 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "mili",
33
"version": "1.6.0",
44
"description": "vue + koa isomorphic boilerplate",
5-
"main": "index.js",
5+
"main": "src/mili.js",
66
"repository": {
77
"type": "git",
88
"url": "git+https://github.com/Val-istar-Guo/vue-boilerplate.git"

src/commands/clean.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fs = require('fs-extra')
2+
const { join } = require('path')
3+
4+
5+
6+
const templatePath = join(__dirname, '../../templates')
7+
8+
module.exports = async () => {
9+
await fs.remove(templatePath)
10+
}

src/commands/init.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const getTemplate = require('../getTemplate')
2+
const loadTemplateConfig = require('../loadTemplateConfig')
3+
const extractProgectBaseInfo = require('../extractProgectBaseInfo')
4+
const genFileList = require('../genFileList')
5+
const initialFolder = require('../initialFolder')
6+
const checkAndFormatView = require('../checkAndFormatView')
7+
const genMilirc = require('../genMilirc')
8+
const copy = require('../copy')
9+
const log = require('../log')
10+
const { join, basename } = require('path')
11+
const securityCheck = require('../securityCheck')
12+
const getTemplateStorage = require('../getTemplateStorage')
13+
14+
15+
module.exports = async (options = {}) => {
16+
const {
17+
cwd = process.cwd(),
18+
// project name
19+
name,
20+
// template repository
21+
repository,
22+
// whether to skip the security check
23+
force = false,
24+
// template version expected
25+
version,
26+
} = options
27+
28+
if (!force) await securityCheck(process.cwd())
29+
30+
const templateStoragePath = await getTemplateStorage(repository)
31+
const revertTemplateRepository = await getTemplate(repository, templateStoragePath, version)
32+
33+
try {
34+
const templateConfig = await loadTemplateConfig(templateStoragePath)
35+
const baseInfo = await extractProgectBaseInfo(cwd)
36+
37+
const view = checkAndFormatView({
38+
name: name || baseInfo.name || basename(process.cwd()),
39+
repository: baseInfo.repository,
40+
remotes: baseInfo.remotes,
41+
template: { repository, version: templateConfig.version },
42+
})
43+
44+
45+
let files = await genFileList(cwd, templateConfig)
46+
files = files.map(file => ({ ...file, view }))
47+
48+
49+
log.info('initial folders...')
50+
await initialFolder(files)
51+
52+
log.info('copy files...')
53+
await Promise.all(files.map(copy))
54+
await genMilirc(cwd, view)
55+
56+
await templateConfig.hooks('afterInit')
57+
} finally {
58+
await revertTemplateRepository()
59+
}
60+
}

0 commit comments

Comments
 (0)