Skip to content

Commit e5ce2c4

Browse files
committed
fix: use templates from file system not zip
1 parent 1d22072 commit e5ce2c4

File tree

7 files changed

+90
-291
lines changed

7 files changed

+90
-291
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages/create-app/templates

packages/create-app/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"dependencies": {
1313
"@dhis2/cli-helpers-engine": "^3.2.2",
1414
"@inquirer/prompts": "^7.8.4",
15-
"decompress": "^4.2.1",
1615
"fast-glob": "^3.3.3",
1716
"fs-extra": "^11.3.3"
1817
},

packages/create-app/src/index.js

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
const { execSync } = require('child_process')
12
const path = require('path')
2-
const { reporter, exec } = require('@dhis2/cli-helpers-engine')
3+
const { reporter, exec, chalk } = require('@dhis2/cli-helpers-engine')
34
const { input, select } = require('@inquirer/prompts')
4-
const decompress = require('decompress')
55
const fg = require('fast-glob')
66
const fs = require('fs-extra')
7+
const { default: getPackageManager } = require('./utils/getPackageManager')
78

89
process.on('uncaughtException', (error) => {
910
if (error instanceof Error && error.name === 'ExitPromptError') {
@@ -15,14 +16,15 @@ process.on('uncaughtException', (error) => {
1516
})
1617

1718
const 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+
5961
const 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-
7267
const 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
},
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const getPackageManager = () => {
2+
const userAgent = process.env.npm_config_user_agent || ''
3+
4+
if (userAgent.startsWith('yarn')) {
5+
return 'yarn'
6+
}
7+
8+
if (userAgent.startsWith('npm')) {
9+
return 'npm'
10+
}
11+
12+
return 'pnpm'
13+
}
14+
15+
export default getPackageManager

packages/create-app/templates/template-ts-dataelements-react-router/src/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const MyApp: FC = () => {
4242
<a
4343
target="_blank"
4444
href="https://developers.dhis2.org/docs/guides"
45+
rel="noreferrer"
4546
>
4647
Web development guides
4748
</a>
@@ -50,6 +51,7 @@ const MyApp: FC = () => {
5051
<a
5152
target="_blank"
5253
href="https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html"
54+
rel="noreferrer"
5355
>
5456
Documentation for the API{' '}
5557
</a>
@@ -58,6 +60,7 @@ const MyApp: FC = () => {
5860
<a
5961
target="_blank"
6062
href="https://developers.dhis2.org/docs/tutorials/app-runtime-query/"
63+
rel="noreferrer"
6164
>
6265
AppRuntime and useDataQuery hook
6366
</a>
@@ -66,6 +69,7 @@ const MyApp: FC = () => {
6669
<a
6770
target="_blank"
6871
href="https://developers.dhis2.org/docs/ui/webcomponents"
72+
rel="noreferrer"
6973
>
7074
UI Library
7175
</a>
@@ -74,6 +78,7 @@ const MyApp: FC = () => {
7478
<a
7579
target="_blank"
7680
href="https://developers.dhis2.org/demo/"
81+
rel="noreferrer"
7782
>
7883
UI library demo
7984
</a>

packages/create-app/templates/template-ts-dataelements/src/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const MyApp: FC = () => {
4343
<a
4444
target="_blank"
4545
href="https://developers.dhis2.org/docs/guides"
46+
rel="noreferrer"
4647
>
4748
Web development guides
4849
</a>
@@ -51,6 +52,7 @@ const MyApp: FC = () => {
5152
<a
5253
target="_blank"
5354
href="https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html"
55+
rel="noreferrer"
5456
>
5557
Documentation for the API{' '}
5658
</a>
@@ -59,6 +61,7 @@ const MyApp: FC = () => {
5961
<a
6062
target="_blank"
6163
href="https://developers.dhis2.org/docs/tutorials/app-runtime-query/"
64+
rel="noreferrer"
6265
>
6366
AppRuntime and useDataQuery hook
6467
</a>
@@ -67,6 +70,7 @@ const MyApp: FC = () => {
6770
<a
6871
target="_blank"
6972
href="https://developers.dhis2.org/docs/ui/webcomponents"
73+
rel="noreferrer"
7074
>
7175
UI Library
7276
</a>
@@ -75,6 +79,7 @@ const MyApp: FC = () => {
7579
<a
7680
target="_blank"
7781
href="https://developers.dhis2.org/demo/"
82+
rel="noreferrer"
7883
>
7984
UI library demo
8085
</a>

0 commit comments

Comments
 (0)