Skip to content

Commit ef6009c

Browse files
committed
feat: publish at d2-app-scripts, add init command
1 parent 09b923f commit ef6009c

File tree

13 files changed

+243
-42
lines changed

13 files changed

+243
-42
lines changed

cli/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# @dhis2/cli-app
1+
# @dhis2/cli-app-scripts
22

33
Single-dependency build, start, and test scripts for the DHIS2 App Platform
44

55
See [platform.dhis2.nu](https://platform.dhis2.nu) for complete documentation.
66

77
```sh
8-
> yarn add --dev @dhis2/cli-app
8+
> yarn add --dev @dhis2/cli-app-scripts
99

1010
# OR
1111

12-
> npm install --save-dev @dhis2/cli-app
12+
> npm install --save-dev @dhis2/cli-app-scripts
1313
```
File renamed without changes.

cli/config/d2.config.app.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const config = {
2+
type: 'app',
3+
4+
entryPoints: {
5+
app: './src/App',
6+
},
7+
}
8+
9+
module.exports = config

cli/config/d2.config.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

cli/config/d2.config.lib.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const config = {
2+
type: 'lib',
3+
4+
entryPoints: {
5+
lib: './src/index',
6+
},
7+
}
8+
9+
module.exports = config

cli/config/init.package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "",
3+
"version": "1.0.0",
4+
"description": "",
5+
"license": "BSD-3-Clause",
6+
"private": true
7+
}

cli/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@dhis2/cli-app",
2+
"name": "@dhis2/cli-app-scripts",
33
"version": "0.9.1",
44
"main": "src/index.js",
55
"repository": "https://github.com/amcgee/dhis2-app-platform",
@@ -35,7 +35,7 @@
3535
"styled-jsx": "^3.2.1"
3636
},
3737
"bin": {
38-
"d2-app": "./bin/d2-app"
38+
"d2-app-scripts": "./bin/d2-app-scripts"
3939
},
4040
"scripts": {
4141
"copy:shell": "mkdir -p assets && rm -rf assets/shell && cp -rf ../shell assets/shell && rm -rf assets/shell/node_modules",

cli/src/commands/init.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
const { reporter, exec } = require('@dhis2/cli-helpers-engine')
2+
const path = require('path')
3+
const fs = require('fs-extra')
4+
const chalk = require('chalk')
5+
6+
const makePaths = require('../lib/paths')
7+
8+
const handler = async ({ force, name, title, cwd, lib }) => {
9+
const paths = makePaths(cwd)
10+
11+
if (fs.existsSync(paths.config) && !force) {
12+
reporter.warn(
13+
'A config file already exists, use --force to overwrite it'
14+
)
15+
} else {
16+
reporter.info('Importing d2.config.js defaults')
17+
fs.copyFileSync(
18+
lib ? paths.configDefaultsLib : paths.configDefaultsApp,
19+
paths.config
20+
)
21+
}
22+
23+
if (!fs.existsSync(paths.package)) {
24+
reporter.info('No package.json found, creating one...')
25+
// await exec({
26+
// cmd: 'yarn',
27+
// args: [
28+
// 'init'
29+
// ],
30+
// cwd: target || cwd,
31+
// stdio: 'inherit',
32+
// pipe: true
33+
// })
34+
35+
const pkg = require(path.join(
36+
__dirname,
37+
'../../config/init.package.json'
38+
))
39+
pkg.name = name
40+
fs.writeJSONSync(paths.package, pkg, {
41+
spaces: 2,
42+
})
43+
44+
// await exec({
45+
// cmd: 'yarn',
46+
// args: [
47+
// 'install',
48+
// '--pnp' // Let's be FANCY!
49+
// ],
50+
// cwd: paths.base
51+
// })
52+
}
53+
54+
reporter.info('Creating package scripts...')
55+
const pkg = require(paths.package)
56+
if (pkg.scripts && pkg.scripts.build && !force) {
57+
reporter.warn(
58+
'A script called "build" already exists, use --force to overwrite it'
59+
)
60+
} else {
61+
pkg.scripts = pkg.scripts || {}
62+
pkg.scripts.build = 'yarn run d2-app-scripts build'
63+
}
64+
65+
if (pkg.scripts && pkg.scripts.start && !force) {
66+
reporter.warn(
67+
'A script called "start" already exists, use --force to overwrite it'
68+
)
69+
} else {
70+
pkg.scripts = pkg.scripts || {}
71+
pkg.scripts.start = 'yarn run d2-app-scripts start'
72+
}
73+
74+
if (pkg.scripts && pkg.scripts.test && !force) {
75+
reporter.warn(
76+
'A script called "test" already exists, use --force to overwrite it'
77+
)
78+
} else {
79+
pkg.scripts = pkg.scripts || {}
80+
pkg.scripts.test = 'yarn run d2-app-scripts test'
81+
}
82+
83+
fs.writeJSONSync(paths.package, pkg, {
84+
spaces: 2,
85+
})
86+
87+
if (
88+
!force &&
89+
((pkg.devDependencies &&
90+
Object.keys(pkg.devDependencies).includes(
91+
'@dhis2/cli-app-scripts'
92+
)) ||
93+
(pkg.dependencies &&
94+
Object.keys(pkg.dependencies).includes(
95+
'@dhis2/cli-app-scripts'
96+
)))
97+
) {
98+
reporter.warn(
99+
'A version of `@dhis2/cli-app-scripts` is already listed as a dependency, use --force to overwrite it'
100+
)
101+
} else {
102+
reporter.info('Installing @dhis2/cli-app-scripts...')
103+
await exec({
104+
cmd: 'yarn',
105+
args: ['add', '--dev', '@dhis2/cli-app-scripts'],
106+
cwd: paths.base,
107+
})
108+
}
109+
110+
if (
111+
!force &&
112+
((pkg.dependencies &&
113+
Object.keys(pkg.dependencies).includes('@dhis2/app-runtime')) ||
114+
(pkg.peerDependencies &&
115+
Object.keys(pkg.peerDependencies).includes(
116+
'@dhis2/app-runtime'
117+
)))
118+
) {
119+
reporter.warn(
120+
'A version of `@dhis2/app-runtime` is already listed as a dependency, use --force to overwrite it'
121+
)
122+
} else {
123+
reporter.info('Installing @dhis2/app-runtime...')
124+
await exec({
125+
cmd: 'yarn',
126+
args: ['add', '@dhis2/app-runtime'],
127+
cwd: paths.base,
128+
})
129+
}
130+
131+
const entrypoint = lib ? 'src/index.js' : 'src/App.js'
132+
133+
if (!force && fs.existsSync(path.join(paths.base, entrypoint))) {
134+
reporter.warn(
135+
`An entrypoint file at ${entrypoint} already exists, use --force to overwrite it`
136+
)
137+
} else {
138+
reporter.info(`Creating entrypoint ${chalk.bold(entrypoint)}`)
139+
fs.mkdirpSync(path.join(paths.base, 'src'))
140+
fs.writeFileSync(
141+
path.join(paths.base, entrypoint),
142+
"export default () => 'Welcome to DHIS2!'"
143+
)
144+
}
145+
146+
reporter.print('')
147+
reporter.info('SUCCESS!')
148+
reporter.print('Run `yarn start` to launch your new DHIS2 application')
149+
}
150+
151+
const command = {
152+
command: 'init <name>',
153+
desc: 'Setup an app ',
154+
builder: {
155+
force: {
156+
description: 'Overwrite existing files and configurations',
157+
type: 'boolean',
158+
default: false,
159+
},
160+
lib: {
161+
description: 'Create a library',
162+
type: 'boolean',
163+
default: false,
164+
},
165+
title: {
166+
description:
167+
'The human-readable title of the application or library',
168+
type: 'string',
169+
},
170+
},
171+
handler,
172+
}
173+
174+
module.exports = command

cli/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { namespace } = require('@dhis2/cli-helpers-engine')
22

3-
module.exports = namespace('app', {
3+
module.exports = namespace('scripts', {
44
desc: 'Scripts for development of DHIS2 applications',
55
builder: yargs => {
66
yargs.option('cwd', {

cli/src/lib/parseConfig.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const { reporter } = require('@dhis2/cli-helpers-engine')
22
const { defaultsDeep, has } = require('lodash')
3+
const fs = require('fs-extra')
34
const chalk = require('chalk')
45

5-
const defaultConfig = require('../../config/d2.config')
6-
76
const requiredConfigFields = {
87
app: ['name', 'title', 'entryPoints.app'],
98
lib: ['name', 'entryPoints.lib'],
@@ -32,10 +31,29 @@ const validateConfig = config => {
3231

3332
const parseConfig = paths => {
3433
try {
35-
reporter.debug('Load d2 config at', paths.config)
34+
let config = {}
35+
// if (!fs.existsSync(paths.config)) {
36+
// reporter.error('Config file d2.config.js not found - use the init command to create one!')
37+
// process.exit(1)
38+
// }
39+
40+
if (fs.existsSync(paths.config)) {
41+
reporter.debug('Loading d2 config at', paths.config)
42+
config = require(paths.config)
43+
reporter.debug('loaded', config)
44+
}
3645

37-
let config = require(paths.config)
38-
config = defaultsDeep(config, defaultConfig)
46+
const type = config.type || 'app'
47+
reporter.debug(`Type identified : ${chalk.bold(type)}`)
48+
49+
const defaults =
50+
type === 'lib' ? paths.configDefaultsLib : paths.configDefaultsApp
51+
config = defaultsDeep(config, require(defaults))
52+
53+
if (fs.existsSync(paths.package)) {
54+
config.name = config.name || require(paths.package).name
55+
}
56+
config.title = config.title || config.name
3957

4058
validateConfig(config)
4159

0 commit comments

Comments
 (0)