@@ -6,6 +6,12 @@ const applyTemplate = require('@kne/apply-template');
66const path = require ( 'path' ) ;
77const env = require ( './env' ) ;
88const fs = require ( 'fs-extra' ) ;
9+ const { program} = require ( 'commander' ) ;
10+ const downloadNpmPackage = require ( '@kne/fetch-npm-package' ) ;
11+
12+ program . option ( '--template <name-to-template>' , 'specify a template for the created project' ) ;
13+ program . parse ( ) ;
14+ const options = program . opts ( ) ;
915
1016( async ( ) => {
1117 const output = await inquirer . prompt ( [ {
@@ -17,10 +23,20 @@ const fs = require('fs-extra');
1723 const tempOptions = {
1824 name, summary : output . summary , moduleAliasName : env . moduleAliasName
1925 } ;
20- await applyTemplate ( env . templateDir , outputDir , tempOptions ) ;
26+
27+ if ( env . templateDir ) {
28+ await applyTemplate ( env . templateDir , outputDir , tempOptions ) ;
29+ } else {
30+ const templatePackageName = options . template || '@kne-template/example' ;
31+ await downloadNpmPackage ( templatePackageName , null , {
32+ callback : async ( dir ) => {
33+ await applyTemplate ( dir , outputDir , tempOptions ) ;
34+ }
35+ } ) ;
36+ }
2137 if ( await fs . exists ( path . resolve ( env . appDir , 'template' ) ) ) {
2238 console . log ( '当前项目存在自定义模板,执行自定义模板覆盖' ) ;
23- await applyTemplate ( env . templateDir , outputDir , tempOptions ) ;
39+ await applyTemplate ( path . resolve ( env . appDir , 'template' ) , outputDir , tempOptions ) ;
2440 }
2541 console . log ( '完成,请开始模块开发,拜拜👋' ) ;
2642} ) ( ) . catch ( ( e ) => {
0 commit comments