Skip to content

Commit 18d2cbd

Browse files
committed
feat: add a comment indicating the upgrade type of file
resolve #15
1 parent a24b479 commit 18d2cbd

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

bin/init

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ module.exports = async (name, repository) => {
4242
...file,
4343
view,
4444
// 初始化仓库需要将所有keep模式更为cover模式
45-
upgrade: file.upgrade === 'keep' ? 'cover' : file.upgrade,
4645
encoding: file.encoding || recommendFileEncoding(file.path),
4746
}))
4847
.map(formatHandlers)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
"commander": "^2.19.0",
4747
"cosmiconfig": "^5.0.6",
4848
"merge-deep": "^3.0.2",
49+
"mustache": "^3.0.0",
4950
"sanitization": "^0.3.0",
5051
"semver": "^5.6.0",
51-
"simple-git": "^1.104.0",
52-
"mustache": "^3.0.0"
52+
"simple-git": "^1.104.0"
5353
}
5454
}

src/copy.js

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
11
const fs = require('fs')
2+
const { extname, basename } = require('path')
23
const { promisify } = require('util')
3-
const { join, dirname } = require('path')
44

55

66
const readFile = promisify(fs.readFile)
77
const writeFile = promisify(fs.writeFile)
8-
const access = promisify(fs.access)
9-
const mkdir = promisify(fs.mkdir)
108

9+
const commentator = [
10+
{
11+
filenames: [],
12+
extnames: ['.js', '.ts'],
13+
create: (upgrade) => ([
14+
`// mili upgrade type: ${upgrade}`,
15+
].join('\n')),
16+
},
17+
{
18+
filenames: [],
19+
extnames: ['.md'],
20+
create: (upgrade) => ([
21+
`<!-- mili upgrade type: ${upgrade} -->`,
22+
].join('\n')),
23+
},
24+
{
25+
filenames: ['.gitignore', '.npmrc'],
26+
extnames: ['.yml', '.yaml'],
27+
create: (upgrade) => ([
28+
`# mili upgrade type: ${upgrade}`,
29+
].join('\n')),
30+
},
31+
]
1132

1233

13-
module.exports = async ({ path, view , handlers, encoding, targetPath }, root) => {
34+
const appendFileHeader = file => {
35+
const ext = extname(file.targetPath)
36+
const filename = basename(file.targetPath)
37+
const cm = commentator.find(handler => (
38+
handler.extnames.includes(ext) || handler.filenames.includes(filename)
39+
))
1440

15-
// 提前做
16-
// await ensureDirectoryExistence(targetPath)
41+
if (!cm) return file
42+
43+
if (file.upgrade === 'cover') {
44+
const comment = cm.create(file.upgrade, 'This file will be cover when upgrade template')
45+
return { ...file, content: `${comment}\n${file.content}` }
46+
} else if (file.upgrade === 'merge') {
47+
const comment = cm.create(file.upgrade, 'This file will be merge when upgrade template')
48+
return { ...file, content: `${comment}\n${file.content}` }
49+
}
50+
51+
return file
52+
}
53+
54+
55+
module.exports = async ({ upgrade, path, view , handlers, encoding, targetPath }, root) => {
1756
const content = await readFile(path, encoding)
18-
file = handlers.reduce(
57+
let file = handlers.reduce(
1958
(file, handler) => handler.genFile(file),
20-
{ path, view, content, encoding, targetPath }
59+
{ path, view, content, upgrade, encoding, targetPath }
2160
)
2261

62+
file = appendFileHeader(file)
63+
2364
await writeFile(targetPath, file.content, encoding)
2465
}

0 commit comments

Comments
 (0)