Skip to content

Commit 57c9dd9

Browse files
committed
feat(handler): merge handler support yaml/yml file
1 parent b673770 commit 57c9dd9

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"merge-deep": "^3.0.2",
6363
"micromatch": "^4.0.2",
6464
"mustache": "^3.0.1",
65+
"node-yaml": "^3.2.0",
6566
"sanitization": "^0.3.0",
6667
"semver": "^6.0.0",
6768
"sha1": "^1.1.1",

src/handlers/merge/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const createHandler = require('../create-handler')
44
const readTargetFile = require('../read-target-file')
55
const mergeJson = require('./merge-json')
66
const mergeIgnore = require('./merge-ignore')
7+
const mergeYaml = require('./merge-yaml')
78

89

910
module.exports = createHandler(file => {
@@ -12,6 +13,8 @@ module.exports = createHandler(file => {
1213
if (file.targetFile.exist) {
1314
if (extname(file.targetPath) === '.json') {
1415
return mergeJson(file)
16+
} else if (['.yaml', '.yml'].includes(extname(file.targetPath))) {
17+
return mergeYaml(file)
1518
} else if (['.gitignore', '.npmignore'].includes(basename(file.targetPath))) {
1619
return mergeIgnore(file)
1720
} else {

src/handlers/merge/merge-yaml.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const yaml = require('node-yaml')
2+
const merge = require('merge-deep')
3+
const log = require('../../utils/log')
4+
5+
6+
module.exports = file => {
7+
let content = ''
8+
let targetFileContent = ''
9+
10+
try {
11+
content = yaml.parse(file.content)
12+
} catch (e) {
13+
log.error('merge', [
14+
'The template file and the project file failed to merge due to a json syntax error in the template file.',
15+
'The project file will be overwritten directly by the template file.',
16+
`path: ${file.targetPath}`,
17+
].join('\n'))
18+
return file
19+
}
20+
21+
try {
22+
targetFileContent = yaml.parse(file.targetFile.content)
23+
} catch (e) {
24+
log.error('merge', [
25+
'The template file and the project file failed to merge due to a yaml syntax error in the project file.',
26+
'The project file will be overwritten directly by the template file.',
27+
`path: ${file.targetPath}`,
28+
].join('\n'))
29+
return file
30+
}
31+
32+
return {
33+
...file,
34+
content: yaml.dump(merge(targetFileContent, content)),
35+
}
36+
}

0 commit comments

Comments
 (0)