1+ const { execSync } = require ( 'child_process' )
12const path = require ( 'path' )
2- const { reporter, exec } = require ( '@dhis2/cli-helpers-engine' )
3+ const { reporter, exec, chalk } = require ( '@dhis2/cli-helpers-engine' )
34const { input, select } = require ( '@inquirer/prompts' )
4- const decompress = require ( 'decompress' )
55const fg = require ( 'fast-glob' )
66const fs = require ( 'fs-extra' )
7+ const { default : getPackageManager } = require ( './utils/getPackageManager' )
78
89process . on ( 'uncaughtException' , ( error ) => {
910 if ( error instanceof Error && error . name === 'ExitPromptError' ) {
@@ -15,14 +16,15 @@ process.on('uncaughtException', (error) => {
1516} )
1617
1718const templates = {
19+ templateRoot : path . join ( __dirname , '../templates' ) ,
1820 templateWithList : path . join (
1921 __dirname ,
20- '../templates/template-ts-dataelements.zip '
22+ '../templates/template-ts-dataelements'
2123 ) ,
2224
2325 templateWithReactRouter : path . join (
2426 __dirname ,
25- '../templates/template-ts-dataelements-react-router.zip '
27+ '../templates/template-ts-dataelements-react-router'
2628 ) ,
2729}
2830
@@ -51,24 +53,17 @@ const commandHandler = {
5153 packageManager : {
5254 description : 'Package Manager' ,
5355 type : 'string' ,
54- default : 'pnpm' ,
5556 alias : [ 'package' , 'packagemanager' ] ,
5657 } ,
5758 } ,
5859}
60+
5961const getTemplateFile = ( templateName ) => {
6062 return templateName === 'react-router'
6163 ? templates . templateWithReactRouter
6264 : templates . templateWithList
6365}
6466
65- const defaultOptions = {
66- typeScript : true ,
67- templateName : 'basic' ,
68- template : getTemplateFile ( 'basic' ) ,
69- packageManager : 'pnpm' ,
70- }
71-
7267const command = {
7368 command : '[app]' ,
7469 builder : ( yargs ) => {
@@ -77,11 +72,6 @@ const command = {
7772 handler : async ( argv ) => {
7873 let name = argv . _ [ 0 ] || argv . name
7974
80- const selectedOptions = {
81- ...defaultOptions ,
82- }
83-
84- reporter . debug ( `running "npm create @dhis2/app" (or npx) command"` )
8575 const useDefauls = argv . yes
8676
8777 if ( ! name ) {
@@ -94,10 +84,11 @@ const command = {
9484 reporter . log ( `name of project: ${ name } ` )
9585 }
9686
97- selectedOptions . typeScript = argv . typescript
98- selectedOptions . packageManager = argv . packageManager
99- selectedOptions . templateName = argv . template
100- selectedOptions . template = getTemplateFile ( argv . template )
87+ const selectedOptions = {
88+ typeScript : argv . typescript ,
89+ packageManager : argv . packageManager ?? getPackageManager ( ) ,
90+ templateName : argv . template ,
91+ }
10192
10293 if ( ! useDefauls ) {
10394 const packageManager = await select ( {
@@ -124,7 +115,7 @@ const command = {
124115 selectedOptions . typeScript = language === 'ts'
125116
126117 const template = await select ( {
127- message : 'Select a teamplate ' ,
118+ message : 'Select a template ' ,
128119 default : 'ts' ,
129120 choices : [
130121 { name : 'Basic Template' , value : 'basic' } ,
@@ -136,23 +127,15 @@ const command = {
136127 } )
137128
138129 selectedOptions . templateName = template
139- selectedOptions . template = getTemplateFile ( template )
140130 }
141131
142- // let pkgManager = 'yarn'
143- // if (pnpm) {
144- // pkgManager = 'pnpm'
145- // } else if (npm) {
146- // pkgManager = 'npm'
147- // }
148-
149132 reporter . info (
150133 `Initialising a new project using "${ selectedOptions . packageManager } " as a package manager.`
151134 )
152135
153- if ( selectedOptions . packageManager !== 'pnpm ') {
136+ if ( selectedOptions . packageManager === 'yarn ') {
154137 reporter . warn (
155- 'We recommend using "pnpm" as a package manager for new projects. You can do so by passing the argument --pnpm (i.e. d2 app scripts init --pnpm). This will become the default in future versions of d2 CLI.'
138+ 'We recommend using "pnpm" as a package manager for new projects. Yarn 1 will be deprecated in future versions of the CLI.'
156139 )
157140 }
158141 // create the folder where the template will be generated
@@ -166,9 +149,26 @@ const command = {
166149 process . exit ( 1 )
167150 }
168151
169- reporter . info ( `selected options: ${ JSON . stringify ( selectedOptions ) } ` )
152+ reporter . info ( `${ chalk . cyan ( 'Selected options:' ) } ` )
153+ for ( const [ x , y ] of Object . entries ( selectedOptions ) ) {
154+ reporter . info ( ` ${ x } : ${ y } ` )
155+ }
156+
157+ try {
158+ // Running git clean on templates folder
159+ // This is useful in local development if you have built the templates for testing
160+ // and want to make sure to delete d2 and node_modules folders before copying the template
161+ execSync ( `git clean -X -f -- ${ templates . templateRoot } ` , {
162+ stdio : 'ignore' ,
163+ } )
164+ reporter . debug ( 'Successfully ran git clean' )
165+ } catch ( err ) {
166+ reporter . debug ( err )
167+ }
170168
171- await decompress ( selectedOptions . template , cwd )
169+ reporter . info ( 'Copying template files' )
170+ const templateFiles = getTemplateFile ( selectedOptions . templateName )
171+ fs . copySync ( templateFiles , cwd )
172172
173173 const paths = {
174174 base : cwd ,
@@ -235,6 +235,8 @@ const command = {
235235
236236 // copy correct lock file for npm/yarn
237237 if ( ! pnpm ) {
238+ // ? copying yarn.lock or package-lock speeds installation a bit
239+ // ? but we could also just run "yarn install" and generate the lock file
238240 if ( fs . existsSync ( paths . pnpmLock ) ) {
239241 fs . removeSync ( paths . pnpmLock )
240242 }
@@ -304,35 +306,30 @@ const command = {
304306
305307 reporter . info ( `Running '${ pkgManager } install'` )
306308
307- reporter . debug ( `Upgrading @dhis2 dependencies to latest` )
308- await exec ( {
309- cmd : pkgManager ,
310- args : [
311- npm ? 'install --save' : 'upgrade' ,
312- '@dhis2/app-runtime@latest' ,
313- '@dhis2/cli-app-scripts@latest' ,
314- '@dhis2/ui@latest' ,
315- ] ,
316- cwd : paths . base ,
317- pipe : argv . debug ,
318- } )
319-
320309 await exec ( {
321310 cmd : pkgManager ,
322311 args : [ 'install' ] ,
323312 cwd : paths . base ,
324313 pipe : argv . debug ,
325314 } )
326315
327- reporter . debug ( `Running '${ pkgManager } format'` )
316+ // ToDo: setup the formatting on CLI project properly and avoid running it on scaffolding
317+ reporter . debug ( `Running '${ pkgManager } format' (prettier)` )
328318 await exec ( {
329319 cmd : pkgManager ,
330320 args : npm ? [ 'run' , 'format' ] : [ 'format' ] ,
331321 cwd : paths . base ,
332322 pipe : argv . debug ,
333323 } )
334324
335- reporter . info ( 'Done!' )
325+ reporter . info ( `${ chalk . greenBright ( 'Done!' ) } ` )
326+
327+ const cdCmd = name != '.' ? `cd ${ name } && ` : ''
328+ reporter . print (
329+ `Run ${ chalk . bold (
330+ `${ cdCmd } ${ pkgManager } start`
331+ ) } to launch your new DHIS2 application`
332+ )
336333
337334 return
338335 } ,
0 commit comments