Skip to content

Commit 82f9dc6

Browse files
committed
feat: handler can remove file
Give the ability to handler that control whether copy file to project resolve #61
1 parent 70d5f6a commit 82f9dc6

File tree

10 files changed

+21
-11
lines changed

10 files changed

+21
-11
lines changed

src/apply-template/copy-files.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const copy = async file => {
5757
{ ...file, content, targetPath: file.targetPath }
5858
)
5959

60+
// NOTE: Handler may remove file
61+
if (!file.render) return
62+
6063
file = appendFileHeader(file)
6164

6265
await fs.writeFile(file.targetPath, file.content, file.encoding)
@@ -65,6 +68,6 @@ const copy = async file => {
6568

6669
module.exports = async config => {
6770
for (const file of config.template.files) {
68-
await copy({ ...file, view: { ...config, custom: {} } })
71+
await copy({ ...file, view: { ...config, custom: {}, answers: config.project.answers } })
6972
}
7073
}

src/apply-template/init-folder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ const { dirname } = require('path')
55
// OPTIMIZE: Analyze first, then synchronize the files that can be created synchronously.
66
module.exports = async files => {
77
for (const file of files) {
8-
await fs.ensureDir(dirname(file.targetPath))
8+
if (file.render) await fs.ensureDir(dirname(file.targetPath))
99
}
1010
}

src/commands/init.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = async(options = {}) => {
2020
if (version) checkParams.version(version)
2121

2222
let config = await loadConfig({
23+
operation: 'init',
2324
cwd,
2425
projectName: name,
2526
templateRepository: repository,

src/commands/update.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ module.exports = async options => {
2424
if (!force) await securityCheck(process.cwd())
2525
if (version) checkParams.version(version)
2626

27-
28-
let config = await loadConfig({ cwd })
29-
27+
let config = await loadConfig({ cwd, operation: 'update' })
3028

3129
if (version && semver.lt(version, config.template.version.number)) {
3230
const message = [
@@ -86,7 +84,6 @@ module.exports = async options => {
8684
checkParams.engine(config)
8785

8886
await prompt(config)
89-
config.template.files = config.template.files.filter(file => file.upgrade !== 'keep')
9087
await applyTemplate(config)
9188
await config.template.hooks('afterUpdate')
9289
}

src/commands/upgrade/upgrade.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = async(options = {}) => {
1818
} = options
1919

2020
if (!force) await securityCheck(process.cwd())
21-
let config = await loadConfig({ cwd })
21+
let config = await loadConfig({ cwd, operation: 'upgrade' })
2222

2323

2424
const versions = await getTemplateVersions(config.template.repository)
@@ -38,7 +38,6 @@ module.exports = async(options = {}) => {
3838
checkParams.engine(config)
3939

4040
await prompt(config)
41-
config.template.files = config.template.files.filter(file => file.upgrade !== 'keep')
4241
await applyTemplate(config)
4342
await config.template.hooks('afterUpgrade')
4443

src/handlers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const exist = require('./exist')
33
const mustache = require('./mustache')
44
const extractArea = require('./extract-area')
55
const ejs = require('./ejs')
6+
const removeWhen = require('./remove-when')
67

78

8-
module.exports = { merge, mustache, ejs, extractArea, exist }
9+
module.exports = { merge, mustache, ejs, extractArea, exist, removeWhen }

src/handlers/remove-when.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const createHandler = require('./create-handler')
2+
3+
module.exports = func => createHandler(file => ({ ...file, render: func(file.view, file) }))

src/load-config/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ const loadProjectConfig = require('./load-project-config')
55
const loadMilirc = require('./load-milirc')
66

77

8-
const loadConfig = async({ cwd, projectName, templateRepository, templateVersion, loadTemplate }) => {
8+
const loadConfig = async arg => {
9+
const { operation, cwd, projectName, loadTemplate } = arg
10+
let { templateRepository, templateVersion } = arg
11+
912
const milirc = await loadMilirc(cwd)
1013

1114
if (!templateRepository) {
@@ -33,10 +36,11 @@ const loadConfig = async({ cwd, projectName, templateRepository, templateVersion
3336
}))
3437

3538
return {
39+
operation,
3640
mili,
3741
template,
3842
project,
39-
reload: (parms = {}) => loadConfig({ cwd, projectName, templateRepository, templateVersion, ...parms }),
43+
reload: (parms = {}) => loadConfig({ ...arg, ...parms }),
4044
}
4145
}
4246

src/load-config/load-template-config/format-rule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = rule => {
2828

2929
if (rule.upgrade === 'merge') effectiveHandlers.push(handlers.merge)
3030
if (rule.upgrade === 'exist') effectiveHandlers.push(handlers.exist)
31+
if (rule.upgrade === 'keep') effectiveHandlers.push(handlers.removeWhen(config => config.operation === 'init'))
3132

3233
return { ...rule, handlers: effectiveHandlers }
3334
}

src/load-config/load-template-config/search-files.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ module.exports = async config => {
3939
(p, handler) => handler.genPath(p),
4040
relative(config.path, file.path)
4141
),
42+
render: true,
4243
}))
4344
}

0 commit comments

Comments
 (0)