Skip to content

Commit 650ec83

Browse files
committed
feat(merge-handler): support merge gitignore file
1 parent d56137b commit 650ec83

File tree

5 files changed

+88
-26
lines changed

5 files changed

+88
-26
lines changed

src/apply-template/copy-files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const commentator = [
2020
].join('\n')),
2121
},
2222
{
23-
filenames: ['.gitignore', '.npmrc'],
23+
filenames: ['.npmrc'],
2424
extnames: ['.yml', '.yaml'],
2525
create: (upgrade) => ([
2626
`# mili upgrade type: ${upgrade}`,

src/handlers/merge/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const log = require('../../utils/log')
2+
const { extname, basename } = require('path')
3+
const createHandler = require('../create-handler')
4+
const readTargetFile = require('../read-target-file')
5+
const mergeJson = require('./merge-json')
6+
const mergeGitignore = require('./merge-gitignore')
7+
8+
9+
module.exports = createHandler(file => {
10+
file = readTargetFile(file)
11+
12+
if (file.targetFile.exist) {
13+
if (extname(file.targetPath) === '.json') return mergeJson(file)
14+
else if (basename(file.targetPath) === '.gitignore') return mergeGitignore(file)
15+
else {
16+
log.error('merge', [
17+
`Merge files of this type(${extname(file.targetPath)}) are not supported by mili`,
18+
'please confirm if the loaded template supports the current mili version,',
19+
'and feedback this question to the template developer.',
20+
'The current file will be overwritten directly by the template file.',
21+
`path: ${file.targetPath}`
22+
].join('\n'))
23+
}
24+
}
25+
26+
return file
27+
})
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module.exports = file => {
2+
const ignoreList = file.content.split('\n')
3+
const projectIgnoreList = file.targetFile.content.split('\n')
4+
5+
6+
let mergedList = []
7+
let lastIgnoreListIndex = 0
8+
let lastProjectIgnoreListIndex = 0
9+
10+
ignoreList.forEach((item, index) => {
11+
12+
let i = projectIgnoreList
13+
.slice(lastProjectIgnoreListIndex)
14+
.findIndex(value => value === item)
15+
i = i === -1 ? -1 : i + lastProjectIgnoreListIndex
16+
17+
if (i !== -1 && lastProjectIgnoreListIndex < i) {
18+
mergedList.push(...ignoreList.slice(lastIgnoreListIndex, index))
19+
20+
const lastProjectListFragment = projectIgnoreList
21+
.slice(lastProjectIgnoreListIndex, i)
22+
.filter(item => !ignoreList.includes(item) && item.length)
23+
mergedList.push(...lastProjectListFragment)
24+
25+
lastIgnoreListIndex = index
26+
lastProjectIgnoreListIndex = i
27+
} else if (index === ignoreList.length - 1) {
28+
mergedList.push(...ignoreList.slice(lastIgnoreListIndex))
29+
const lastProjectListFragment = projectIgnoreList
30+
.slice(lastProjectIgnoreListIndex)
31+
.filter(item => !ignoreList.includes(item) && item.length)
32+
mergedList.push(...lastProjectListFragment)
33+
}
34+
})
35+
36+
37+
mergedList = mergedList.reduce((list, item) => {
38+
if (!item.length || !list.includes(item)) list.push(item)
39+
return list
40+
}, [])
41+
42+
43+
return {
44+
...file,
45+
content: mergedList.join('\n'),
46+
}
47+
}
Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
const log = require('../utils/log')
2-
const { extname } = require('path')
3-
const createHandler = require('./create-handler')
4-
const readTargetFile = require('./read-target-file')
5-
const merge = require('merge-deep');
1+
const merge = require('merge-deep')
62

73

8-
const mergeJson = file => {
4+
module.exports = file => {
95
let content = ''
106
let targetFileContent = ''
117

@@ -36,22 +32,3 @@ const mergeJson = file => {
3632
content: JSON.stringify(merge(targetFileContent, content), null, ' ')
3733
}
3834
}
39-
40-
module.exports = createHandler(file => {
41-
file = readTargetFile(file)
42-
43-
if (file.targetFile.exist) {
44-
if (extname(file.targetPath) === '.json') return mergeJson(file)
45-
else {
46-
log.error('merge', [
47-
`Merge files of this type(${extname(file.targetPath)}) are not supported by mili`,
48-
'please confirm if the loaded template supports the current mili version,',
49-
'and feedback this question to the template developer.',
50-
'The current file will be overwritten directly by the template file.',
51-
`path: ${file.targetPath}`
52-
].join('\n'))
53-
}
54-
}
55-
56-
return file
57-
})

test/handlers/merge-gitignore.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const mergeGitignore = require('../../src/handlers/merge/merge-gitignore')
2+
3+
4+
const file = {
5+
content: 'x\na\nb\nd\ne\nt',
6+
targetFile: {
7+
content: 'b\na\nc\ne\nf',
8+
},
9+
}
10+
11+
console.log(mergeGitignore(file))

0 commit comments

Comments
 (0)