Skip to content

Commit cd7321a

Browse files
committed
feat: new option to upgrade recursive
resolve #47
1 parent 69c3685 commit cd7321a

File tree

14 files changed

+122
-40
lines changed

14 files changed

+122
-40
lines changed

bin/mili

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22
const program = require('commander')
3+
const { join } = require('path')
34
const log = require('../src/utils/log')
45
const mili = require('../src/mili')
56
const { version } = require('../package.json')
@@ -30,17 +31,25 @@ program
3031
.catch(err => log.error('program', 'initialize break', err))
3132
})
3233

34+
const collect = (val, memo) => {
35+
memo.push(val);
36+
return memo;
37+
}
38+
3339
program
3440
.command('upgrade')
3541
.description('upgrade the template')
3642
.option('--force')
3743
.option('--no-deps', 'Need not install dependencies', false)
44+
.option('-r, --recursive', 'Upgrade recursive all subfolder')
45+
.option('--ignore [file]', 'the folder need not search', collect, [])
3846
.action((option) => {
39-
const { force = false, deps = true } = option
47+
const { force = false, deps = true, recursive } = option
48+
const cwd = process.cwd()
49+
const ignore = option.ignore.map(item => join(cwd, item))
4050

41-
mili.upgrade({ force, noDeps: !deps })
42-
.then(() => log.info('upgrade complete'))
43-
.catch(err => log.error('program', 'upgrade break', err))
51+
mili.upgrade({ force, noDeps: !deps, recursive, ignore })
52+
.catch(err => log.error('program', 'upgrade break', err))
4453
})
4554

4655
program

src/apply-template/copy-files.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ const appendFileHeader = file => {
5050
}
5151

5252

53-
const copy = async (file, to) => {
53+
const copy = async (file) => {
5454
const content = await fs.readFile(file.path, file.encoding)
5555

5656
file = file.handlers.reduce(
5757
(file, handler) => handler.genFile(file),
58-
{ ...file, content, targetPath: join(to, file.targetPath) }
58+
{ ...file, content, targetPath: file.targetPath }
5959
)
6060

6161
file = appendFileHeader(file)
@@ -66,6 +66,6 @@ const copy = async (file, to) => {
6666

6767
module.exports = async (config) => {
6868
for (let file of config.template.files) {
69-
await copy({ ...file, view: { ...config, custom: {} } }, config.project.path)
69+
await copy({ ...file, view: { ...config, custom: {} } })
7070
}
7171
}

src/apply-template/generate-milirc/index.js

Lines changed: 2 additions & 2 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 (cwd, config) => {
6+
module.exports = async config => {
77
const template = await fs.readFile(join(__dirname, 'template.mustache'), 'utf8')
88
const milirc = mustache.render(template, config)
99

10-
await fs.writeFile(join(cwd, '.milirc.yml'), milirc, 'utf8')
10+
await fs.writeFile(join(config.project.path, '.milirc.yml'), milirc, 'utf8')
1111
}

src/apply-template/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const copyFiles = require('./copy-files')
44
const initFolder = require('./init-folder')
55

66

7-
module.exports = async (cwd, config) => {
7+
module.exports = async (config) => {
88
log.info('initial folders...')
99
await initFolder(config.template.files)
1010
log.info('copy files...')
1111
await copyFiles(config)
12-
await genMilirc(cwd, config)
12+
await genMilirc(config)
1313
}

src/commands/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ module.exports = async (options = {}) => {
5757
}
5858

5959
await prompt(config)
60-
await applyTemplate(cwd, config)
60+
await applyTemplate(config)
6161
await config.template.hooks('afterInit')
6262
}

src/commands/update.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,6 @@ module.exports = async (options) => {
8989

9090
await prompt(config)
9191
config.template.files = config.template.files.filter(file => file.upgrade !== 'keep')
92-
await applyTemplate(cwd, config)
92+
await applyTemplate(config)
9393
await config.template.hooks('afterUpdate')
9494
}

src/commands/upgrade/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const upgradeRecursive = require('./upgrade-recursive')
2+
const upgrade = require('./upgrade')
3+
4+
5+
module.exports = async options => {
6+
if (options.recursive) {
7+
await upgradeRecursive(options)
8+
} else {
9+
await upgrade(options)
10+
log.info('upgrade complete')
11+
}
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { join } = require('path')
2+
const fs = require('fs-extra')
3+
const glob = require('micromatch')
4+
const upgrade = require('./upgrade')
5+
const log = require('../../utils/log')
6+
7+
8+
const upgradeRecursive = async (dir, ignore, options) => {
9+
const stats = await fs.stat(dir)
10+
if (!stats.isDirectory()) return
11+
12+
if (await fs.pathExists(join(dir, '.milirc.yml'))) {
13+
log.info(`upgrading ${dir}`)
14+
await upgrade({ ...options, cwd: dir })
15+
console.log('\n ')
16+
}
17+
18+
let folders = await fs.readdir(dir)
19+
folders = folders
20+
.map(filename => join(dir, filename))
21+
.filter(filepath => !glob.isMatch(filepath, ignore))
22+
23+
/**
24+
* NOTE: There should upgrade one by one,
25+
* because it's possible that two of these projects were used same template,
26+
* resulting in template download conflict.
27+
*/
28+
for (let folder of folders) {
29+
await upgradeRecursive(folder, ignore, options)
30+
}
31+
}
32+
33+
module.exports = async options => {
34+
const {
35+
cwd = process.cwd(),
36+
ignore,
37+
} = options
38+
39+
await upgradeRecursive(cwd, ignore, options)
40+
}

src/commands/upgrade.js renamed to src/commands/upgrade/upgrade.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const loadConfig = require('../load-config')
1+
const loadConfig = require('../../load-config')
22
const semver = require('semver')
3-
const downloadTemplate = require('../download-template')
4-
const getTemplateVersions = require('../get-template-versions')
5-
const applyTemplate = require('../apply-template')
6-
const securityCheck = require('../security-check')
7-
const log = require('../utils/log')
8-
const prompt = require('../prompt')
9-
const checkParams = require('../check-params')
3+
const downloadTemplate = require('../../download-template')
4+
const getTemplateVersions = require('../../get-template-versions')
5+
const applyTemplate = require('../../apply-template')
6+
const securityCheck = require('../../security-check')
7+
const log = require('../../utils/log')
8+
const prompt = require('../../prompt')
9+
const checkParams = require('../../check-params')
1010

1111

1212
module.exports = async (options = {}) => {
@@ -39,6 +39,8 @@ module.exports = async (options = {}) => {
3939

4040
await prompt(config)
4141
config.template.files = config.template.files.filter(file => file.upgrade !== 'keep')
42-
await applyTemplate(cwd, config)
42+
await applyTemplate(config)
4343
await config.template.hooks('afterUpgrade')
44+
45+
log.info('upgrade complete')
4446
}

src/load-config/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
1+
const { join } = require('path')
12
const loadMiliConfig = require('./load-mili-config')
23
const loadTemplateConfig = require('./load-template-config')
34
const loadProjectConfig = require('./load-project-config')
5+
const loadMilirc = require('./load-milirc')
46

57

68
const loadConfig = async ({ cwd, projectName, templateRepository, templateVersion, loadTemplate }) => {
9+
const milirc = await loadMilirc(cwd)
10+
11+
if (!templateRepository) {
12+
if (milirc.template && milirc.template.repository) templateRepository = milirc.template.repository
13+
else throw new Error('Unable to find template repository, please check whether milirc is configured correctly')
14+
}
15+
16+
if (!templateVersion && milirc.template && milirc.template.version) {
17+
templateVersion = {
18+
number: milirc.template.version
19+
}
20+
}
21+
722
const mili = await loadMiliConfig()
823
const template = await loadTemplateConfig(templateRepository, templateVersion, loadTemplate)
924
const project = await loadProjectConfig(cwd, projectName)
1025

26+
// NOTE: generate the path that relative to the output folder
27+
if (typeof template.repository.path === 'function') template.repository.path = template.repository.path(cwd)
28+
29+
// NOTE: The files of template's targetPath should point to project
30+
template.files = template.files.map(file => ({
31+
...file,
32+
targetPath: join(project.path, file.targetPath),
33+
}))
1134

1235
return {
1336
mili,

0 commit comments

Comments
 (0)