Skip to content

Commit 5f3057c

Browse files
committed
feat: get information by interaction
resolve #17
1 parent 650ec83 commit 5f3057c

File tree

9 files changed

+52
-9
lines changed

9 files changed

+52
-9
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@
4747
"commander": "^2.19.0",
4848
"cosmiconfig": "^5.0.6",
4949
"fs-extra": "^7.0.0",
50+
"inquirer": "^6.2.2",
5051
"merge-deep": "^3.0.2",
5152
"micromatch": "^3.1.10",
5253
"mustache": "^3.0.0",
5354
"sanitization": "^0.3.0",
5455
"semver": "^5.6.0",
56+
"sha1": "^1.1.1",
5557
"simple-git": "^1.106.0"
5658
}
5759
}

src/apply-template/generate-milirc/template.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ mili:
33
template:
44
repository: {{{template.repository.url}}}
55
version: {{template.version.number}}
6+
interaction: {{template.interactionSHA1}}
7+
answers:
8+
{{#project.interaction}}
9+
{{key}}: {{value}}
10+
{{/project.interaction}}
11+

src/commands/init.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
const { basename } = require('path')
12
const loadConfig = require('../load-config')
23
const downloadTemplate = require('../download-template')
34
const getTemplateVersions = require('../get-template-versions')
45
const throwError = require('../utils/throw-error')
56
const applyTemplate = require('../apply-template')
67
const securityCheck = require('../security-check')
78
const checkParams = require('../check-params')
8-
const { basename } = require('path')
9+
const prompt = require('../prompt')
910

1011

1112
module.exports = async (options = {}) => {
@@ -51,6 +52,7 @@ module.exports = async (options = {}) => {
5152
].join('\n'))
5253
}
5354

55+
await prompt(config)
5456
await applyTemplate(cwd, config)
5557
await config.template.hooks('afterInit')
5658
}

src/commands/update.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const applyTemplate = require('../apply-template')
77
const securityCheck = require('../security-check')
88
const checkParams = require('../check-params')
99
const log = require('../utils/log')
10+
const prompt = require('../prompt')
1011

1112

1213
module.exports = async (options) => {
@@ -82,6 +83,8 @@ module.exports = async (options) => {
8283
config = await config.reload({
8384
templateVersion: version,
8485
})
86+
87+
await prompt(config)
8588
await applyTemplate(cwd, config)
8689
await config.template.hooks('afterUpdate')
8790
}

src/commands/upgrade.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const downloadTemplate = require('../download-template')
44
const getTemplateVersions = require('../get-template-versions')
55
const applyTemplate = require('../apply-template')
66
const securityCheck = require('../security-check')
7-
const checkParams = require('../check-params')
87
const log = require('../utils/log')
8+
const prompt = require('../prompt')
99

1010

1111
module.exports = async (options = {}) => {
@@ -32,6 +32,8 @@ module.exports = async (options = {}) => {
3232
config = await config.reload({
3333
templateVersion: version,
3434
})
35+
36+
await prompt(config)
3537
await applyTemplate(cwd, config)
3638
await config.template.hooks('afterUpgrade')
3739
}

src/load-config/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ const loadTemplateConfig = require('./load-template-config')
33
const loadProjectConfig = require('./load-project-config')
44

55

6-
const loadConfig = async ({ cwd, projectName, defaultProjectName = '', templateRepository, templateVersion }) => {
6+
const loadConfig = async ({ cwd, projectName, templateRepository, templateVersion }) => {
77
const mili = await loadMiliConfig()
8-
const project = await loadProjectConfig(cwd, projectName, defaultProjectName)
98
const template = await loadTemplateConfig(templateRepository, templateVersion)
9+
const project = await loadProjectConfig(cwd, projectName)
1010

1111

1212
return {

src/load-config/load-project-config.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ const isRepo = require('../utils/is-repository')
55
const git = require('simple-git/promise')
66

77

8-
98
module.exports = async (cwd, projectName) => {
109
const packageJson = await loadPackJson(cwd)
1110
const milirc = await loadMilirc()
12-
const interaction = milirc.interaction || {}
13-
11+
const answers = milirc.answers || {}
1412

1513
const config = {
1614
path: cwd,
1715

1816
// shell interaction result
19-
interaction,
17+
answers,
18+
interactionSHA1: milirc.interaction
2019
}
2120

2221
if (packageJson && packageJson.repository) {

src/load-config/load-template-config/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const formatRule = require('./format-rule')
1111
const throwError = require('../../utils/throw-error')
1212
const isDirectory = require('../../utils/is-directory')
1313
const searchFile = require('./search-files')
14+
const sha1 = require('sha1')
1415

1516

1617
const checkConfig = sa.keys({
@@ -25,6 +26,7 @@ const checkConfig = sa.keys({
2526
handlers: sa.array,
2627
}),
2728
hooks: sa.object,
29+
interaction: sa.array,
2830
})
2931

3032
const loadEntryFile = async path => {
@@ -74,7 +76,8 @@ module.exports = async (repository, version) => {
7476
engines: null,
7577
path: null,
7678
rules: null,
77-
interaction: null,
79+
interaction: [],
80+
interactionSHA1: '',
7881
}
7982

8083
if (!repository) return config
@@ -98,6 +101,7 @@ module.exports = async (repository, version) => {
98101
})
99102

100103
config.interaction = entryFile.interaction
104+
config.interactionSHA1 = sha1(JSON.stringify(entryFile.interaction))
101105
config.hooks = formatHooks(entryFile.hooks)
102106

103107
config.files = await searchFile(config)

src/prompt.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const inquirer = require('inquirer')
2+
3+
4+
module.exports = async (config, force = false) => {
5+
const questions = [...config.template.interaction]
6+
7+
if (!Array.isArray(questions)) return []
8+
9+
if (!force && config.template.interactionSHA1 === config.project.interactionSHA1) {
10+
config.project.interaction = Object.entries(config.project.answers)
11+
.map(item => ({ key: item[0], value: item[1] }))
12+
return []
13+
}
14+
15+
questions.map(question => {
16+
const value = config.project.answers[question.name]
17+
if (!question.default && value) question.default = value
18+
})
19+
20+
const answers = await inquirer.prompt(questions)
21+
config.project.answers = answers
22+
config.project.interaction = Object.entries(answers)
23+
.map(item => ({ key: item[0], value: item[1] }))
24+
return answers
25+
}

0 commit comments

Comments
 (0)