Skip to content

Commit 012f90e

Browse files
committed
feat: add hooks
resolve #31
1 parent f67eb62 commit 012f90e

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

bin/init

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ module.exports = async (name, repository) => {
5656
log.info('copy files')
5757
await Promise.all(files.map(copy))
5858
await genMilirc(targetPath, view)
59+
60+
await templateConfig.hooks('afterInit')
5961
} finally {
6062
await git(templateProjectPath).checkout(currentBranch )
6163
}

bin/update

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module.exports = async () => {
6060
await Promise.all(files.map(copy))
6161
await genMilirc(targetPath, view)
6262

63+
await templateConfig.hooks('afterUpdate')
6364
} finally {
6465
await git(templateProjectPath).checkout(currentBranch )
6566
}

bin/upgrade

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ module.exports = async () => {
6666
log.info('upgrading...')
6767
await Promise.all(files.map(copy))
6868
await genMilirc(targetPath, view)
69+
70+
await templateConfig.hooks('afterUpdate')
6971
} finally {
7072
await git(templateProjectPath).checkout(currentBranch )
7173
}

src/hooksEngine.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const fs = require('fs')
2+
const { exec } = require('child_process');
3+
const log = require('./log')
4+
5+
6+
module.exports = hooks => name => new Promise((resolve, reject) => {
7+
if (typeof hooks[name] !== 'string') return resolve()
8+
log.info(`run ${name} hook...`)
9+
10+
exec(hooks[name], (error, stdout, stderr) => {
11+
if (error) {
12+
log.error('hook exec error', error);
13+
return resolve()
14+
}
15+
16+
process.stdout.write(stdout)
17+
process.stderr.write(stderr)
18+
19+
resolve()
20+
});
21+
})

src/loadTemplateConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { join } = require('path')
55
const throwError = require('./throwError')
66
const sa = require('sanitization')
77
const semver = require('semver')
8+
const hooksEngine = require('./hooksEngine')
89
const { version: miliVersion } = require('../package.json')
910

1011

@@ -38,6 +39,7 @@ const checkConfig = sa.keys({
3839
handler: sa.any,
3940
handlers: sa.array,
4041
}),
42+
hooks: sa.object,
4143
})
4244

4345
module.exports = async templatePath => {
@@ -82,5 +84,7 @@ module.exports = async templatePath => {
8284
return rule
8385
})
8486

87+
config.hooks = hooksEngine(config.hooks)
88+
8589
return { ...config, version: packageJson.version }
8690
}

0 commit comments

Comments
 (0)