Skip to content

Commit 94e6827

Browse files
committed
fix: different output when project file is same
1 parent c84b0d1 commit 94e6827

File tree

3 files changed

+85
-42
lines changed

3 files changed

+85
-42
lines changed

src/handlers/merge/merge-ignore.js

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,85 @@
1-
module.exports = file => {
2-
const ignoreList = file.content.split('\n')
3-
const projectIgnoreList = file.targetFile.content.split('\n')
1+
const defaultBucketName = Symbol()
2+
const classifyIgnore = list => {
3+
let last = { name: defaultBucketName, values: [] }
4+
const bucket = [last]
45

6+
list.forEach(item => {
7+
if (/^#/.test(item)) {
8+
const name = item.substring(1)
9+
/** comment adjacent comments */
10+
if (!last.values.length && last.name !== defaultBucketName) {
11+
last.name = `${last.name}\n${name}`
12+
return
13+
}
514

6-
let mergedList = []
7-
let lastIgnoreListIndex = 0
8-
let lastProjectIgnoreListIndex = 0
9-
10-
ignoreList.forEach((item, index) => {
11-
let i = projectIgnoreList
12-
.slice(lastProjectIgnoreListIndex)
13-
.findIndex(value => value === item)
14-
i = i === -1 ? -1 : i + lastProjectIgnoreListIndex
15-
16-
if (i !== -1 && lastProjectIgnoreListIndex < i) {
17-
mergedList.push(...ignoreList.slice(lastIgnoreListIndex, index))
18-
19-
const lastProjectListFragment = projectIgnoreList
20-
.slice(lastProjectIgnoreListIndex, i)
21-
.filter(item => !ignoreList.includes(item) && item.length)
22-
mergedList.push(...lastProjectListFragment)
23-
24-
lastIgnoreListIndex = index
25-
lastProjectIgnoreListIndex = i
26-
} else if (index === ignoreList.length - 1) {
27-
mergedList.push(...ignoreList.slice(lastIgnoreListIndex))
28-
const lastProjectListFragment = projectIgnoreList
29-
.slice(lastProjectIgnoreListIndex)
30-
.filter(item => !ignoreList.includes(item) && item.length)
31-
mergedList.push(...lastProjectListFragment)
15+
const pair = bucket.find(item => item.name === name)
16+
if (pair) {
17+
last = pair
18+
} else {
19+
last = { name, values: [] }
20+
bucket.push(last)
21+
}
22+
} else if (item) {
23+
if (!last.values.includes(item)) last.values.push(item)
3224
}
3325
})
3426

35-
if (lastIgnoreListIndex < projectIgnoreList.length) {
36-
const lastProjectListFragment = projectIgnoreList
37-
.slice(lastProjectIgnoreListIndex, projectIgnoreList.length)
38-
.filter(item => !ignoreList.includes(item) && item.length)
39-
mergedList.push(...lastProjectListFragment)
40-
}
27+
return bucket
28+
}
29+
30+
const mergeBucket = (b1, b2) => {
31+
const bucket = b1.map(item => ({ ...item }))
32+
33+
b2.forEach(pair => {
34+
const sameOne = bucket.find(item => item.name === pair.name)
35+
if (!sameOne) {
36+
bucket.push(pair)
37+
} else {
38+
pair.values.forEach(value => {
39+
if (!(sameOne.values.includes(value))) sameOne.values = sameOne.values.concat(value)
40+
})
41+
}
42+
})
43+
44+
return bucket
45+
}
46+
47+
const uniqBucket = (b1, b2) => {
48+
const allValues = [].concat(...b2.map(item => item.values))
49+
return b1.map(pair => {
50+
const values = pair.values.filter(item => !allValues.includes(item))
51+
return { name: pair.name, values }
52+
})
53+
}
54+
55+
const renderBucket = bucket => bucket
56+
.filter(pair => (pair.name !== defaultBucketName || pair.values.length))
57+
.map(({ name, values }) => {
58+
let str = ''
59+
if (typeof name === 'string') str += name.replace(/^(.*)(\n|$)/mg, '#$1\n')
60+
str += values.join('\n')
61+
62+
return str
63+
})
64+
.join('\n\n')
65+
66+
module.exports = file => {
67+
const templateIgnoreList = file.content.split('\n')
68+
const projectIgnoreList = file.targetFile.content.split('\n')
69+
70+
const templateIgnoreBucket = classifyIgnore(templateIgnoreList)
71+
const projectIgnoreBucket = classifyIgnore(projectIgnoreList)
72+
73+
const uniquedBucket = uniqBucket(projectIgnoreBucket, templateIgnoreBucket)
4174

42-
mergedList = mergedList.reduce((list, item) => {
43-
if (!item.length || !list.includes(item)) list.push(item)
44-
return list
45-
}, [])
75+
const bucket = mergeBucket(templateIgnoreBucket, uniquedBucket)
76+
const result = renderBucket(bucket)
4677

78+
const beginBlank = file.content.match(/^\s*/g)[0]
79+
const endBlank = file.content.match(/\s*$/g)[0]
4780

4881
return {
4982
...file,
50-
content: mergedList.join('\n'),
83+
content: `${beginBlank}${result}${endBlank}`,
5184
}
5285
}

src/handlers/merge/merge-json.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ module.exports = file => {
2828
return file
2929
}
3030

31+
const beginBlank = file.content.match(/^\s*/g)[0]
32+
const endBlank = file.content.match(/\s*$/g)[0]
33+
34+
const result = JSON.stringify(merge(targetFileContent, content), null, ' ')
35+
3136
return {
3237
...file,
33-
content: JSON.stringify(merge(targetFileContent, content), null, ' '),
38+
content: `${beginBlank}${result}${endBlank}`,
3439
}
3540
}

src/handlers/merge/merge-yaml.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ module.exports = file => {
2929
return file
3030
}
3131

32+
const beginBlank = file.content.match(/^\s*/g)[0]
33+
const endBlank = file.content.match(/\s*$/g)[0]
34+
35+
const result = yaml.dump(merge(targetFileContent, content))
36+
3237
return {
3338
...file,
34-
content: yaml.dump(merge(targetFileContent, content)),
39+
content: `${beginBlank}${result}${endBlank}`,
3540
}
3641
}

0 commit comments

Comments
 (0)