Skip to content

Commit 01b8fed

Browse files
committed
feat: add update command
resolve #16
1 parent 18d2cbd commit 01b8fed

File tree

6 files changed

+190
-76
lines changed

6 files changed

+190
-76
lines changed

bin/init

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,50 @@ const genMilirc = require('../src/genMilirc')
1313
const copy = require('../src/copy')
1414
const log = require('../src/log')
1515
const { join, basename } = require('path')
16+
const git = require('simple-git/promise')
1617

1718

1819
module.exports = async (name, repository) => {
1920
// const miliConfig = await loadMiliConfig()
20-
const templateProjectPath = await cloneRepository(repository)
21-
const templateConfig = await loadTemplateConfig(templateProjectPath)
22-
const templatePath = join(templateProjectPath, templateConfig.path)
23-
const baseInfo = await extractProgectBaseInfo(join(process.cwd()))
24-
25-
// basename(process.cwd())
26-
// OPTIMIZE: Check mili version and remind user
27-
// if (templateConfig.version > mili.version)
28-
29-
let files = await genFileList({ path: templatePath, upgrade: 'cover', handlers: [] }, templateConfig.rules)
30-
31-
const view = checkAndFormatView({
32-
name: name || baseInfo.name || basename(process.cwd()),
33-
repository: baseInfo.repository,
34-
remotes: baseInfo.remotes,
35-
template: { repository, version: templateConfig.version },
36-
})
37-
38-
39-
const targetPath = process.cwd()
40-
files = files
41-
.map(file => ({
42-
...file,
43-
view,
44-
// 初始化仓库需要将所有keep模式更为cover模式
45-
encoding: file.encoding || recommendFileEncoding(file.path),
46-
}))
47-
.map(formatHandlers)
48-
.map(genTargetPath(templatePath, targetPath))
49-
50-
51-
log.info('initial folder')
52-
await initialFolder(files)
53-
54-
log.info('copy files')
55-
await Promise.all(files.map(copy))
56-
await genMilirc(targetPath, view)
21+
const { templatePath: templateProjectPath, currentBranch } = await cloneRepository(repository)
22+
try {
23+
const templateConfig = await loadTemplateConfig(templateProjectPath)
24+
const templatePath = join(templateProjectPath, templateConfig.path)
25+
const baseInfo = await extractProgectBaseInfo(join(process.cwd()))
26+
27+
// basename(process.cwd())
28+
// OPTIMIZE: Check mili version and remind user
29+
// if (templateConfig.version > mili.version)
30+
31+
let files = await genFileList({ path: templatePath, upgrade: 'cover', handlers: [] }, templateConfig.rules)
32+
33+
const view = checkAndFormatView({
34+
name: name || baseInfo.name || basename(process.cwd()),
35+
repository: baseInfo.repository,
36+
remotes: baseInfo.remotes,
37+
template: { repository, version: templateConfig.version },
38+
})
39+
40+
41+
const targetPath = process.cwd()
42+
files = files
43+
.map(file => ({
44+
...file,
45+
view,
46+
// 初始化仓库需要将所有keep模式更为cover模式
47+
encoding: file.encoding || recommendFileEncoding(file.path),
48+
}))
49+
.map(formatHandlers)
50+
.map(genTargetPath(templatePath, targetPath))
51+
52+
53+
log.info('initial folder')
54+
await initialFolder(files)
55+
56+
log.info('copy files')
57+
await Promise.all(files.map(copy))
58+
await genMilirc(targetPath, view)
59+
} finally {
60+
await git(templateProjectPath).checkout(currentBranch )
61+
}
5762
}

bin/mili

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const program = require('commander')
66
const log = require('../src/log')
77
const init = require('./init')
88
const upgrade = require('./upgrade')
9+
const update = require('./update')
910
const { version } = require('../package.json')
1011

1112

@@ -39,6 +40,16 @@ program
3940
.catch(err => log.error('program', 'upgrade break', err))
4041
})
4142

43+
program
44+
.command('update')
45+
.description('Update the project with the current version of the template')
46+
.action(() => {
47+
log.info('prepare update')
48+
update()
49+
.then(() => log.info('update complete'))
50+
.catch(err => log.error('program', 'update break', err))
51+
})
52+
4253

4354
// error on unknown commands
4455
program.on('command:*', function () {

bin/update

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env node
2+
const loadMiliConfig = require('../src/loadMiliConfig')
3+
const cloneRepository = require('../src/cloneRepository')
4+
const loadTemplateConfig = require('../src/loadTemplateConfig')
5+
const extractProgectBaseInfo = require('../src/extractProgectBaseInfo')
6+
const recommendFileEncoding = require('../src/recommendFileEncoding')
7+
const genFileList = require('../src/genFileList')
8+
const formatHandlers = require('../src/formatHandlers')
9+
const genTargetPath = require('../src/genTargetPath')
10+
const initialFolder = require('../src/initialFolder')
11+
const checkAndFormatView = require('../src/checkAndFormatView')
12+
const genMilirc = require('../src/genMilirc')
13+
const copy = require('../src/copy')
14+
const log = require('../src/log')
15+
const { join, basename } = require('path')
16+
const git = require('simple-git/promise')
17+
18+
19+
module.exports = async () => {
20+
const miliConfig = await loadMiliConfig()
21+
const repository = miliConfig.repository
22+
23+
const { templatePath: templateProjectPath, currentBranch } = await cloneRepository(repository, miliConfig.version)
24+
try {
25+
const templateConfig = await loadTemplateConfig(templateProjectPath)
26+
27+
const templatePath = join(templateProjectPath, templateConfig.path)
28+
const baseInfo = await extractProgectBaseInfo(join(process.cwd()))
29+
30+
// basename(process.cwd())
31+
// OPTIMIZE: Check mili version and remind user
32+
// if (templateConfig.version > mili.version)
33+
34+
let files = await genFileList({ path: templatePath, upgrade: 'cover', handlers: [] }, templateConfig.rules)
35+
36+
const view = checkAndFormatView({
37+
name: baseInfo.name || basename(process.cwd()),
38+
repository: baseInfo.repository,
39+
remotes: baseInfo.remotes,
40+
template: { repository, version: templateConfig.version },
41+
})
42+
43+
44+
const targetPath = process.cwd()
45+
files = files
46+
.filter(file => file.upgrade !== 'keep')
47+
.map(file => ({
48+
...file,
49+
view,
50+
encoding: file.encoding || recommendFileEncoding(file.path),
51+
}))
52+
.map(formatHandlers)
53+
.map(genTargetPath(templatePath, targetPath))
54+
55+
56+
log.info('check folder')
57+
await initialFolder(files)
58+
59+
log.info('upgrading...')
60+
await Promise.all(files.map(copy))
61+
await genMilirc(targetPath, view)
62+
63+
} finally {
64+
await git(templateProjectPath).checkout(currentBranch )
65+
}
66+
}

bin/upgrade

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,59 @@ const copy = require('../src/copy')
1414
const log = require('../src/log')
1515
const semver = require('semver')
1616
const { join, basename } = require('path')
17+
const git = require('simple-git/promise')
1718

1819

1920
module.exports = async () => {
2021
const miliConfig = await loadMiliConfig()
2122
const repository = miliConfig.repository
2223

23-
console.log(miliConfig)
24-
const templateProjectPath = await cloneRepository(repository)
25-
const templateConfig = await loadTemplateConfig(templateProjectPath)
24+
const { templatePath: templateProjectPath, currentBranch } = await cloneRepository(repository)
25+
try {
26+
const templateConfig = await loadTemplateConfig(templateProjectPath)
2627

27-
// miliConfig
28-
if (semver.gte(miliConfig.version, templateConfig.version)) {
29-
log.info('The template is already the latest version')
30-
return
31-
}
28+
// miliConfig
29+
if (semver.gte(miliConfig.version, templateConfig.version)) {
30+
log.info('The template is already the latest version')
31+
return
32+
}
3233

33-
const templatePath = join(templateProjectPath, templateConfig.path)
34-
const baseInfo = await extractProgectBaseInfo(join(process.cwd()))
34+
const templatePath = join(templateProjectPath, templateConfig.path)
35+
const baseInfo = await extractProgectBaseInfo(join(process.cwd()))
3536

36-
// basename(process.cwd())
37-
// OPTIMIZE: Check mili version and remind user
38-
// if (templateConfig.version > mili.version)
37+
// basename(process.cwd())
38+
// OPTIMIZE: Check mili version and remind user
39+
// if (templateConfig.version > mili.version)
3940

40-
let files = await genFileList({ path: templatePath, upgrade: 'cover', handlers: [] }, templateConfig.rules)
41+
let files = await genFileList({ path: templatePath, upgrade: 'cover', handlers: [] }, templateConfig.rules)
4142

42-
const view = checkAndFormatView({
43-
name: baseInfo.name || basename(process.cwd()),
44-
repository: baseInfo.repository,
45-
remotes: baseInfo.remotes,
46-
template: { repository, version: templateConfig.version },
47-
})
43+
const view = checkAndFormatView({
44+
name: baseInfo.name || basename(process.cwd()),
45+
repository: baseInfo.repository,
46+
remotes: baseInfo.remotes,
47+
template: { repository, version: templateConfig.version },
48+
})
4849

4950

50-
const targetPath = process.cwd()
51-
files = files
52-
.filter(file => file.upgrade !== 'keep')
53-
.map(file => ({
54-
...file,
55-
view,
56-
encoding: file.encoding || recommendFileEncoding(file.path),
57-
}))
58-
.map(formatHandlers)
59-
.map(genTargetPath(templatePath, targetPath))
51+
const targetPath = process.cwd()
52+
files = files
53+
.filter(file => file.upgrade !== 'keep')
54+
.map(file => ({
55+
...file,
56+
view,
57+
encoding: file.encoding || recommendFileEncoding(file.path),
58+
}))
59+
.map(formatHandlers)
60+
.map(genTargetPath(templatePath, targetPath))
6061

6162

62-
log.info('check folder')
63-
await initialFolder(files)
63+
log.info('check folder')
64+
await initialFolder(files)
6465

65-
log.info('upgrading...')
66-
await Promise.all(files.map(copy))
67-
await genMilirc(targetPath, view)
66+
log.info('upgrading...')
67+
await Promise.all(files.map(copy))
68+
await genMilirc(targetPath, view)
69+
} finally {
70+
await git(templateProjectPath).checkout(currentBranch )
71+
}
6872
}

src/cloneRepository.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,44 @@ const git = require('simple-git/promise')
33
const { join } = require('path')
44
const paths = require('./paths')
55
const { promisify } = require('util')
6+
const throwError = require('./throwError')
67

78

89
const access = promisify(fs.access)
910

1011

11-
module.exports = async repository => {
12+
module.exports = async (repository, version) => {
1213
const templatePath = join(paths.templates, repository)
1314

1415
try {
1516
await access(templatePath, fs.constants.F_OK)
1617
await git(templatePath).pull()
17-
// pull new commit
1818
} catch(err) {
19+
// BUG: need delete folder
1920
await git().clone(repository, templatePath)
2021
}
2122

22-
return templatePath
23+
const gitT = git(templatePath)
24+
const tags = await gitT.tags()
25+
26+
const branchSummary = await gitT.branch()
27+
const currentBranch = branchSummary.current
28+
29+
30+
if (version) {
31+
version = `v${version}`
32+
if (!tags.all.includes(version)) {
33+
throwError([
34+
'No corresponding template version was found',
35+
'Please confirm that the version number exists in the tags of the template repository.',
36+
`Expected template version: ${version}`
37+
].join('\n'))
38+
}
39+
40+
gitT.checkout(version)
41+
} else if (tags.latest) {
42+
await gitT.checkout(tags.latest)
43+
}
44+
45+
return { templatePath, currentBranch }
2346
}

src/loadMiliConfig.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ module.exports = async () => {
1010
if (result) config = result.config || {}
1111

1212

13-
if (!config.version || !semver.valid(config.version)) throwError([
13+
if (!config.version) throwError([
14+
'Unable to get template version from .milirc',
15+
'Please check if the .milirc file is configured correctly.',
16+
].join('\n'))
17+
18+
if (!semver.valid(config.version)) throwError([
1419
'The version number of the template finded in .milirc does not conform to the semver specification',
1520
'Please check if the .milirc file is configured correctly.',
1621
].join('\n'))

0 commit comments

Comments
 (0)