@@ -14,55 +14,59 @@ const copy = require('../src/copy')
1414const log = require ( '../src/log' )
1515const semver = require ( 'semver' )
1616const { join, basename } = require ( 'path' )
17+ const git = require ( 'simple-git/promise' )
1718
1819
1920module . exports = async ( ) => {
2021 const miliConfig = await loadMiliConfig ( )
2122 const repository = miliConfig . repository
2223
23- console . log ( miliConfig )
24- const templateProjectPath = await cloneRepository ( repository )
25- const templateConfig = await loadTemplateConfig ( templateProjectPath )
24+ const { templatePath : templateProjectPath , currentBranch } = await cloneRepository ( repository )
25+ try {
26+ const templateConfig = await loadTemplateConfig ( templateProjectPath )
2627
27- // miliConfig
28- if ( semver . gte ( miliConfig . version , templateConfig . version ) ) {
29- log . info ( 'The template is already the latest version' )
30- return
31- }
28+ // miliConfig
29+ if ( semver . gte ( miliConfig . version , templateConfig . version ) ) {
30+ log . info ( 'The template is already the latest version' )
31+ return
32+ }
3233
33- const templatePath = join ( templateProjectPath , templateConfig . path )
34- const baseInfo = await extractProgectBaseInfo ( join ( process . cwd ( ) ) )
34+ const templatePath = join ( templateProjectPath , templateConfig . path )
35+ const baseInfo = await extractProgectBaseInfo ( join ( process . cwd ( ) ) )
3536
36- // basename(process.cwd())
37- // OPTIMIZE: Check mili version and remind user
38- // if (templateConfig.version > mili.version)
37+ // basename(process.cwd())
38+ // OPTIMIZE: Check mili version and remind user
39+ // if (templateConfig.version > mili.version)
3940
40- let files = await genFileList ( { path : templatePath , upgrade : 'cover' , handlers : [ ] } , templateConfig . rules )
41+ let files = await genFileList ( { path : templatePath , upgrade : 'cover' , handlers : [ ] } , templateConfig . rules )
4142
42- const view = checkAndFormatView ( {
43- name : baseInfo . name || basename ( process . cwd ( ) ) ,
44- repository : baseInfo . repository ,
45- remotes : baseInfo . remotes ,
46- template : { repository, version : templateConfig . version } ,
47- } )
43+ const view = checkAndFormatView ( {
44+ name : baseInfo . name || basename ( process . cwd ( ) ) ,
45+ repository : baseInfo . repository ,
46+ remotes : baseInfo . remotes ,
47+ template : { repository, version : templateConfig . version } ,
48+ } )
4849
4950
50- const targetPath = process . cwd ( )
51- files = files
52- . filter ( file => file . upgrade !== 'keep' )
53- . map ( file => ( {
54- ...file ,
55- view,
56- encoding : file . encoding || recommendFileEncoding ( file . path ) ,
57- } ) )
58- . map ( formatHandlers )
59- . map ( genTargetPath ( templatePath , targetPath ) )
51+ const targetPath = process . cwd ( )
52+ files = files
53+ . filter ( file => file . upgrade !== 'keep' )
54+ . map ( file => ( {
55+ ...file ,
56+ view,
57+ encoding : file . encoding || recommendFileEncoding ( file . path ) ,
58+ } ) )
59+ . map ( formatHandlers )
60+ . map ( genTargetPath ( templatePath , targetPath ) )
6061
6162
62- log . info ( 'check folder' )
63- await initialFolder ( files )
63+ log . info ( 'check folder' )
64+ await initialFolder ( files )
6465
65- log . info ( 'upgrading...' )
66- await Promise . all ( files . map ( copy ) )
67- await genMilirc ( targetPath , view )
66+ log . info ( 'upgrading...' )
67+ await Promise . all ( files . map ( copy ) )
68+ await genMilirc ( targetPath , view )
69+ } finally {
70+ await git ( templateProjectPath ) . checkout ( currentBranch )
71+ }
6872}
0 commit comments