Skip to content

Commit d56137b

Browse files
committed
refactor(*): split module functionality to reduce duplicate code
The new code structure is easier to understand, and it's easier for newcomers to contribute code. BREAKING CHANGE: View structure and milirc config structure was changed.Template need to be upgraded to support new view.And project need to change milirc manually.
1 parent 21e6c0f commit d56137b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+702
-624
lines changed

bin/mili

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
const program = require('commander')
3-
const log = require('../src/log')
3+
const log = require('../src/utils/log')
44
const mili = require('../src/mili')
55
const { version } = require('../package.json')
66

docs/zh-cn/template-render.md

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,64 @@ view的结构是mili获取当前用户项目的各种信息组合成的一个结
3737

3838
```js
3939
const view = {
40-
// 项目名称,来自用户项目目录下package.json中的name字段,初始化时可能是由用户指定或者目录名。
41-
name: 'project name',
42-
43-
// 项目远程仓库
44-
repository: {
45-
// 仓库的url,从用户项目目录下package.json中的repository字段获取。
46-
// 如果字段不存在,则获取项目本地仓库关联的远程仓库的列表中的第一个仓库地址。
47-
url: '',
48-
// 仓库的类型,枚举值 ['git', 'github']
49-
type: '',
50-
// 仓库的归属用户,目前仅支持github的git链接识别。无法从url上识别得到则为空
51-
user: '',
52-
// 仓库名,目前仅支持github的git链接识别。无法从url上识别得到则为空
53-
name: '',
40+
mili: {
41+
// 运行的mili版本
42+
version: '1.10.0',
43+
},
44+
project: {
45+
// 项目的本地路径
46+
path: 'path_to_project',
47+
// 项目名称,来自用户项目目录下package.json中的name字段,初始化时可能是由用户指定或者目录名。
48+
name: 'project name',
49+
50+
// 项目远程仓库
51+
repository: {
52+
// 仓库的类型,
53+
type: 'git/local',
54+
// 仓库托管的服务商
55+
service: 'github',
56+
// 仓库的url,从用户项目目录下package.json中的repository字段获取。
57+
// 如果字段不存在,则获取项目本地仓库关联的远程仓库的列表中的第一个仓库地址。
58+
url: 'https://github.com/Val-istar-Guo/mili.git',
59+
// 仓库的归属用户,目前仅支持github的git链接识别。无法从url上识别得到则为null
60+
owner: 'Val-istar-Guo',
61+
// 仓库名,目前仅支持github的git链接识别。无法从url上识别得到则为null
62+
name: 'mili',
63+
},
5464
},
5565

56-
// 项目本地仓库关联的远程仓库列表,仅在repository.url无法从package.json中获取的时候存在该字段
57-
remotes: [],
58-
59-
// 模版信息(这就是从你模版的package.json中提取的部分信息)
66+
// 模版信息
6067
template: {
6168
// 模版的远程仓库地址
62-
repository: 'template_remote_repository_url',
69+
repository: {
70+
// 仓库的类型,
71+
type: 'git/local',
72+
// 仓库托管的服务商
73+
service: 'github',
74+
// 仓库的url,从用户项目目录下package.json中的repository字段获取。
75+
// 如果字段不存在,则获取项目本地仓库关联的远程仓库的列表中的第一个仓库地址。
76+
url: 'https://github.com/Val-istar-Guo/mili.git',
77+
// 仓库的归属用户,目前仅支持github的git链接识别。无法从url上识别得到则为null
78+
owner: 'Val-istar-Guo',
79+
// 仓库名,目前仅支持github的git链接识别。无法从url上识别得到则为null
80+
name: 'mili',
81+
},
6382
// 模版的版本
64-
version: 'template_version',
83+
version: {
84+
commit: 'ad67824adbef....',
85+
ref: 'ref/tags/v1.2.3',
86+
number: '1.2.3',
87+
},
88+
89+
// 以下为模版的入口/配置文件相关配置信息
90+
path: '',
91+
encoding: '',
92+
engines: '',
93+
rules: [],
94+
hooks: {},
6595
},
6696

67-
// 通过handler注入到view中的其他信息
97+
// 通过handler注入到view中的其他信息,每个文件都不一样
6898
custom: {},
6999
}
70100
```

src/copy.js renamed to src/apply-template/copy-files.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs-extra')
2+
const { join } = require('path')
23
const { extname, basename } = require('path')
34

45

@@ -49,14 +50,22 @@ const appendFileHeader = file => {
4950
}
5051

5152

52-
module.exports = async ({ upgrade, path, view , handlers, encoding, targetPath }, root) => {
53-
const content = await fs.readFile(path, encoding)
54-
let file = handlers.reduce(
53+
const copy = async (file, to) => {
54+
const content = await fs.readFile(file.path, file.encoding)
55+
56+
file = file.handlers.reduce(
5557
(file, handler) => handler.genFile(file),
56-
{ path, view, content, upgrade, encoding, targetPath }
58+
{ ...file, content, targetPath: join(to, file.targetPath) }
5759
)
5860

5961
file = appendFileHeader(file)
6062

61-
await fs.writeFile(targetPath, file.content, encoding)
63+
await fs.writeFile(file.targetPath, file.content, file.encoding)
64+
}
65+
66+
67+
module.exports = async (config) => {
68+
for (let file of config.template.files) {
69+
await copy({ ...file, view: { ...config, custom: {} } }, config.project.path)
70+
}
6271
}

src/gen-milirc/index.js renamed to src/apply-template/generate-milirc/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const { join } = require('path')
33
const mustache = require('mustache')
44

55

6-
module.exports = async (targetPath, view) => {
6+
module.exports = async (cwd, config) => {
77
const template = await fs.readFile(join(__dirname, 'template.mustache'), 'utf8')
8-
const milirc = mustache.render(template, view)
8+
const milirc = mustache.render(template, config)
99

10-
await fs.writeFile(join(targetPath, '.milirc.yml'), milirc, 'utf8')
10+
await fs.writeFile(join(cwd, '.milirc.yml'), milirc, 'utf8')
1111
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mili:
2+
version: {{mili.version}}
3+
template:
4+
repository: {{{template.repository.url}}}
5+
version: {{template.version.number}}

src/apply-template/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const log = require('../utils/log')
2+
const genMilirc = require('./generate-milirc')
3+
const copyFiles = require('./copy-files')
4+
const initFolder = require('./init-folder')
5+
6+
7+
module.exports = async (cwd, config) => {
8+
log.info('initial folders...')
9+
await initFolder(config.template.files)
10+
log.info('copy files...')
11+
await copyFiles(config)
12+
await genMilirc(cwd, config)
13+
}
File renamed without changes.

src/check-and-format-view.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/check-params.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const semver = require('semver')
2+
3+
4+
module.exports = {
5+
version: version => {
6+
if (!semver.valid(version)) {
7+
throwError([
8+
'Version number is wrong',
9+
'Make sure the version number conforms to the semver specification',
10+
'Semantic Versioning: https://semver.org',
11+
].join('\n'))
12+
}
13+
}
14+
}

src/commands/clean.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const fs = require('fs-extra')
22
const { join } = require('path')
33

44

5-
65
const templatePath = join(__dirname, '../../templates')
76

87
module.exports = async () => {

0 commit comments

Comments
 (0)