Skip to content

Commit

Permalink
fix: restore test command and deal with standard deps (#13)
Browse files Browse the repository at this point in the history
* fix: restore test command and deal with standard deps

* fix: local ref standard-lib

* fix: revert to explicit duplication of standard libs

* docs: complete barebones documentation

* chore: move version to top of root package.json

* chore: remove circular dep in build process

* Update package.json
  • Loading branch information
amcgee committed Aug 28, 2019
1 parent 85f7b1a commit 5745c21
Show file tree
Hide file tree
Showing 32 changed files with 7,653 additions and 287 deletions.
15 changes: 9 additions & 6 deletions cli/config/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ const browsersList = require('./.browserlistrc')

module.exports = {
presets: [
'@babel/preset-react',
'@babel/preset-typescript',
['@babel/preset-env', { modules: false, targets: browsersList }],
require('@babel/preset-react'),
require('@babel/preset-typescript'),
[
require('@babel/preset-env'),
{ modules: 'auto', targets: browsersList },
],
],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread',
require('@babel/plugin-proposal-class-properties'),
require('@babel/plugin-proposal-object-rest-spread'),

// Always build in "production" mode even when styled-jsx runtime may select "development"
['styled-jsx/babel', { optimizeForSpeed: true }],
[require('styled-jsx/babel'), { optimizeForSpeed: true }],
],
}
9 changes: 9 additions & 0 deletions cli/config/init.App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'

it('renders without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<App />, div)
ReactDOM.unmountComponentAtNode(div)
})
10 changes: 5 additions & 5 deletions cli/config/init.package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "",
"version": "1.0.0",
"description": "",
"license": "BSD-3-Clause",
"private": true
"name": "",
"version": "1.0.0",
"description": "",
"license": "BSD-3-Clause",
"private": true
}
6 changes: 6 additions & 0 deletions cli/config/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
verbose: true,
transform: JSON.stringify({
'^.+\\.[t|j]sx?$': require.resolve('./jest.transform.js'),
}),
}
2 changes: 2 additions & 0 deletions cli/config/jest.transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const babelConfig = require('./babel.config')
module.exports = require('babel-jest').createTransformer(babelConfig)
24 changes: 9 additions & 15 deletions cli/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@ const json = require('rollup-plugin-json')

const { reporter } = require('@dhis2/cli-helpers-engine')

const standardExternals = [
'@dhis2/app-runtime',
'@dhis2/d2-i18n',
'@dhis2/ui-core',
'classnames',
'moment',
'prop-types',
'react',
'react-dom',
'styled-jsx',
].reduce((out, dep) => {
out[dep] = true
return out
}, {})
const standardLibs = require('./standard-libs.json')

// Exclude local app-shell dependencies
Object.entries(standardLibs).forEach(([dep, version]) => {
if (version.startsWith('file:') || version.startsWith('link:')) {
delete standardLibs[dep]
}
})

const bundle = ({
entryPointName,
Expand All @@ -35,7 +29,7 @@ const bundle = ({
}) => {
const externals = new RegExp(
Object.keys({
...standardExternals,
...standardLibs,
...pkg.peerDependencies,
}).join('|')
)
Expand Down
11 changes: 11 additions & 0 deletions cli/config/standard-libs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"@dhis2/app-runtime": "^1.5.1",
"@dhis2/d2-i18n": "^1.0.5",
"@dhis2/ui-core": "^3.7.1",
"classnames": "^2.2.6",
"moment": "^2.24.0",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"styled-jsx": "^3.2.1"
}
12 changes: 11 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,31 @@
"src"
],
"dependencies": {
"//": "****** Includes standard libraries from app-shell, so that tests work ******",
"@babel/core": "^7.3.3",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/plugin-proposal-object-rest-spread": "^7.4.4",
"@babel/preset-env": "^7.3.1",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
"@dhis2/app-runtime": "^1.5.1",
"@dhis2/cli-helpers-engine": "^1.4.0",
"@dhis2/d2-i18n": "^1.0.5",
"@dhis2/ui-core": "^3.7.1",
"babel-jest": "^24.9.0",
"chalk": "^2.4.2",
"classnames": "^2.2.6",
"fs-extra": "^8.1.0",
"gaze": "^1.1.3",
"handlebars": "^4.1.2",
"i18next-conv": "^9",
"i18next-scanner": "^2.9.1",
"jest-cli": "^24.9.0",
"lodash": "^4.17.11",
"react": "*",
"moment": "^2.24.0",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"rollup": "^1.20.2",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^10.1.0",
Expand Down
17 changes: 12 additions & 5 deletions cli/src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const handler = async ({ force, name, cwd, lib }) => {
)
} else {
pkg.scripts = pkg.scripts || {}
pkg.scripts.build = 'yarn run d2-app-scripts build'
pkg.scripts.build = 'd2-app-scripts build'
}

if (pkg.scripts && pkg.scripts.start && !force) {
Expand All @@ -53,7 +53,7 @@ const handler = async ({ force, name, cwd, lib }) => {
)
} else {
pkg.scripts = pkg.scripts || {}
pkg.scripts.start = 'yarn run d2-app-scripts start'
pkg.scripts.start = 'd2-app-scripts start'
}

if (pkg.scripts && pkg.scripts.test && !force) {
Expand All @@ -62,7 +62,7 @@ const handler = async ({ force, name, cwd, lib }) => {
)
} else {
pkg.scripts = pkg.scripts || {}
pkg.scripts.test = 'yarn run d2-app-scripts test'
pkg.scripts.test = 'd2-app-scripts test'
}

fs.writeJSONSync(paths.package, pkg, {
Expand Down Expand Up @@ -115,9 +115,9 @@ const handler = async ({ force, name, cwd, lib }) => {

const entrypoint = lib ? 'src/index.js' : 'src/App.js'

if (!force && fs.existsSync(path.join(paths.base, entrypoint))) {
if (fs.existsSync(path.join(paths.base, entrypoint))) {
reporter.warn(
`An entrypoint file at ${entrypoint} already exists, use --force to overwrite it`
`An entrypoint file at ${entrypoint} already exists, remove it to create the sample entrypoint`
)
} else {
reporter.info(`Creating entrypoint ${chalk.bold(entrypoint)}`)
Expand All @@ -126,6 +126,13 @@ const handler = async ({ force, name, cwd, lib }) => {
path.join(__dirname, '../../config/init.entrypoint.js'),
path.join(paths.base, entrypoint)
)

if (!lib) {
fs.copyFileSync(
path.join(__dirname, '../../config/init.App.test.js'),
path.join(paths.base, 'src/App.test.js')
)
}
}

reporter.print('')
Expand Down
47 changes: 30 additions & 17 deletions cli/src/commands/test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,56 @@
const { reporter } = require('@dhis2/cli-helpers-engine')

const i18n = require('../lib/i18n')
const compile = require('../lib/compile')
const fs = require('fs-extra')
const path = require('path')

const makePaths = require('../lib/paths')
const makeShell = require('../lib/shell')
const parseConfig = require('../lib/parseConfig')
const exitOnCatch = require('../lib/exitOnCatch')

const { runCLI } = require('jest-cli')

const handler = async ({ cwd, force, shell: shellSource }) => {
const paths = makePaths(cwd)
const config = parseConfig(paths).app
const shell = makeShell({ config, paths })
process.env.BABEL_ENV = 'test'
process.env.NODE_ENV = 'test'

await shell.bootstrap({ force, shell: shellSource })
const paths = makePaths(cwd)

reporter.info('Running tests...')

await exitOnCatch(
async () => {
const compilePromise = compile({
mode: 'development',
paths,
watch: false,
})
const testPromise = shell.test()
await Promise.all([compilePromise, testPromise])
const defaultJestConfig = require(paths.jestConfigDefaults)
const appJestConfig = fs.existsSync(paths.jestConfig)
? require(paths.jestConfig)
: {}

const jestConfig = {
roots: ['./src'],
...defaultJestConfig,
...appJestConfig,
}

const result = await runCLI(jestConfig, [paths.base])

if (result.results.success) {
reporter.info(`Tests completed`)
} else {
reporter.error(`Tests failed`)
process.exit(1)
}
},
{
name: 'start',
name: 'test',
onError: () =>
reporter.error('Start script exited with non-zero exit code'),
reporter.error('Test script exited with non-zero exit code'),
}
)
}

const command = {
command: 'test',
aliases: 't',
desc: 'Run app-shell and application tests',
desc: 'Run application unit tests',
handler,
}

Expand Down
10 changes: 6 additions & 4 deletions cli/src/lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ const compile = async ({
path.join(outDir, `es/${config.type}.js`),
path.join(paths.shellApp, `${config.type}.js`)
)
await fs.copy(
path.join(outDir, `es/${config.type}.js.map`),
path.join(paths.shellApp, `${config.type}.js.map`)
)
if (mode === 'production') {
await fs.copy(
path.join(outDir, `es/${config.type}.js.map`),
path.join(paths.shellApp, `${config.type}.js.map`)
)
}
}

module.exports = compile
2 changes: 2 additions & 0 deletions cli/src/lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = makePaths = (cwd = process.cwd()) => {
__dirname,
'../../config/d2.config.lib.js'
),
jestConfigDefaults: path.join(__dirname, '../../config/jest.config.js'),

shellSource: path.join(__dirname, '../../assets/shell'),

Expand All @@ -23,6 +24,7 @@ module.exports = makePaths = (cwd = process.cwd()) => {
config: path.join(base, './d2.config.js'),
src: path.join(base, './src'),
appEntry: path.join(base, './src/App.js'),
jestConfig: path.join(base, 'jest.config.js'),
i18nStrings: path.join(base, './i18n'),
i18nLocales: path.join(base, './src/locales'),

Expand Down
6 changes: 6 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@
Unified application architecture and build pipeline to simplify and standardize application development within the DHIS2 ecosystem.

See the [JIRA epic](https://jira.dhis2.org/browse/TECH-179) for project status.

---

&nbsp;

[<button style="color:#EFEFEF;font-size:1.2rem;background-color:#2196f3;border: 1px black solid;border-radius: 5px;padding: 15px 30px;width: 300px;cursor:pointer;">Get Started!</button>](getting-started)
2 changes: 0 additions & 2 deletions docs/_navbar.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
- [Tutorials](tutorials/)
- |
- [**Platform**](/)
- [Runtime](https://runtime.dhis2.nu)
9 changes: 5 additions & 4 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
- [**Getting Started**](getting-started.md)
- [**CLI**](cli)
- [`d2 app build`](cli/build.md)
- [`d2 app start`](cli/start.md)
- [`d2 app test`](cli/test.md)
- [`d2-app-scripts init`](cli/init.md)
- [`d2-app-scripts build`](cli/build.md)
- [`d2-app-scripts start`](cli/start.md)
- [`d2-app-scripts test`](cli/test.md)
- [**Configuration**](config.md)
- [Types - `app`, `lib`](config/apps-and-libs)
- [Types - `app`, `lib`](config/types)
- [`d2.config.js` Reference](config/d2-config-js-reference.md)
- [**Architecture**](architecture.md)
- &nbsp;
Expand Down
8 changes: 5 additions & 3 deletions docs/cli.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# App Platform CLI

```sh
> d2 app build
```
The application platform comes with a CLI. This CLI is analogous to `react-scripts` in that it is the **only required dependency** for a functional and modern DHIS2 application. It can be used standalone, usually for the purposes of standing up new application repositories, or (more commonly) added as a dev dependency in the application's package.json.

To run the `init` command of the CLI as a stand-alone tool, either install the DHIS2 cli globally (`yarn global add @dhis2/cli`) and run `d2 app scripts init myapp`, or run it dynamically with `npx` (`npx @dhis2/cli-app-scripts init myapp`)

To install the CLI as a dev dependency (this is done for you by the `init` command) run `yarn add --dev @dhis2/cli-app-scripts`.
20 changes: 20 additions & 0 deletions docs/cli/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# d2 app scripts build

Create a standalone production app build in the `./build` directory

```sh
> d2 app scripts build --help
d2-app-scripts build

Build a production app bundle for use with the DHIS2 app-shell

Options:
--cwd working directory to use (defaults to cwd)
--version Show version number [boolean]
--config Path to JSON config file
--mode Specify the target build environment
[choices: "development", "production"] [default: production]
--dev Build in development mode [boolean]
--watch Watch source files for changes [boolean] [default: false]
-h, --help Show help [boolean]
```
19 changes: 19 additions & 0 deletions docs/cli/init.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# d2 app scripts init

Bootraps a new (or updates an existing) DHIS2 application in the directory `./<name>`, installing all required dependencies.

```sh
> d2 app scripts init --help
d2-app-scripts init <name>

Initialize a DHIS2 app or library for use with the DHIS2 app platform

Options:
--cwd working directory to use (defaults to cwd)
--version Show version number [boolean]
--config Path to JSON config file
--force Overwrite existing files and configurations
[boolean] [default: false]
--lib Create a library [boolean] [default: false]
-h, --help Show help [boolean]
```
Loading

0 comments on commit 5745c21

Please sign in to comment.