Skip to content

Commit

Permalink
fix(cli): Support multiple library entrypoints (#597)
Browse files Browse the repository at this point in the history
* test: setup jest for @dhis2/cli-app-scripts

* fix(cli): update compiler entrypoints verification to support multiple libary entrypoints

* fix(cli): support multiple library entrypoints

* docs: update d2.config.js reference to mention multiple library entrypoints
  • Loading branch information
mediremi committed Jul 27, 2021
1 parent 56c06df commit a95be81
Show file tree
Hide file tree
Showing 8 changed files with 818 additions and 119 deletions.
8 changes: 8 additions & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,13 @@
},
"bin": {
"d2-app-scripts": "./bin/d2-app-scripts"
},
"scripts": {
"test": "d2-app-scripts test"
},
"jest": {
"testPathIgnorePatterns": [
"src/commands/test.js"
]
}
}
55 changes: 14 additions & 41 deletions cli/src/lib/compiler/compile.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,18 @@
const path = require('path')
const babel = require('@babel/core')
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const { reporter } = require('@dhis2/cli-helpers-engine')
const chokidar = require('chokidar')
const fs = require('fs-extra')
const makeBabelConfig = require('../../../config/makeBabelConfig.js')
const {
verifyEntrypoints,
overwriteAppEntrypoint,
} = require('./entrypoints.js')
const {
extensionPattern,
normalizeExtension,
} = require('./extensionHelpers.js')

const overwriteEntrypoint = async ({ config, paths }) => {
const isApp = config.type === 'app'
const entrypoint = isApp ? config.entryPoints.app : config.entryPoints.lib
if (!entrypoint.match(/^(\.\/)?src\//)) {
const msg = `App entrypoint ${chalk.bold(
entrypoint
)} must be located within the ${chalk.bold('./src')} directory`
reporter.error(msg)
throw new Error(msg)
}

const relativeEntrypoint = entrypoint.replace(/^(\.\/)?src\//, '')

try {
require.resolve(path.join(paths.base, entrypoint))
} catch (e) {
const msg = `Could not resolve app entrypoint ${chalk.bold(entrypoint)}`
reporter.error(msg)
throw new Error(msg)
}

const outRelativeEntrypoint = normalizeExtension(relativeEntrypoint)

if (isApp) {
const shellAppSource = await fs.readFile(paths.shellSourceEntrypoint)
await fs.writeFile(
paths.shellAppEntrypoint,
shellAppSource
.toString()
.replace(
/'.\/D2App\/app'/g,
`'./D2App/${outRelativeEntrypoint}'`
)
)
}
}

const watchFiles = ({ inputDir, outputDir, processFileCallback, watch }) => {
const compileFile = async source => {
const relative = normalizeExtension(path.relative(inputDir, source))
Expand Down Expand Up @@ -99,13 +66,19 @@ const compile = async ({
mode = 'development',
watch = false,
}) => {
await overwriteEntrypoint({ config, paths })

const isApp = config.type === 'app'

verifyEntrypoints({ config, paths })
if (isApp) {
await overwriteAppEntrypoint({
entrypoint: config.entryPoints.app,
paths,
})
}

const outDir = isApp
? paths.shellApp
: path.join(paths.buildOutput, moduleType)

fs.removeSync(outDir)
fs.ensureDirSync(outDir)

Expand Down
72 changes: 72 additions & 0 deletions cli/src/lib/compiler/entrypoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const path = require('path')
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const fs = require('fs-extra')
const { normalizeExtension } = require('./extensionHelpers.js')

const verifyEntrypoint = ({ entrypoint, basePath, resolveModule }) => {
if (!entrypoint.match(/^(\.\/)?src\//)) {
const msg = `Entrypoint ${chalk.bold(
entrypoint
)} must be located within the ${chalk.bold('./src')} directory`
reporter.error(msg)
throw new Error(msg)
}

try {
resolveModule(path.join(basePath, entrypoint))
} catch (e) {
const msg = `Could not resolve entrypoint ${chalk.bold(entrypoint)}`
reporter.error(msg)
throw new Error(msg)
}
}

exports.verifyEntrypoints = ({
config,
paths,
resolveModule = require.resolve,
}) => {
if (config.type === 'app') {
verifyEntrypoint({
entrypoint: config.entryPoints.app,
basePath: paths.base,
resolveModule,
})
return
}

const verifyLibraryEntrypoint = entrypoint => {
switch (typeof entrypoint) {
case 'string':
verifyEntrypoint({
entrypoint,
basePath: paths.base,
resolveModule,
})
return
case 'object':
Object.values(entrypoint).forEach(verifyLibraryEntrypoint)
return
default: {
const msg = `${chalk.bold(
entrypoint
)} is not a valid entrypoint`
reporter.error(msg)
throw new Error(msg)
}
}
}
verifyLibraryEntrypoint(config.entryPoints.lib)
}

exports.overwriteAppEntrypoint = async ({ entrypoint, paths }) => {
const relativeEntrypoint = entrypoint.replace(/^(\.\/)?src\//, '')
const outRelativeEntrypoint = normalizeExtension(relativeEntrypoint)
const shellAppSource = await fs.readFile(paths.shellSourceEntrypoint)
await fs.writeFile(
paths.shellAppEntrypoint,
shellAppSource
.toString()
.replace(/'.\/D2App\/app'/g, `'./D2App/${outRelativeEntrypoint}'`)
)
}
Loading

0 comments on commit a95be81

Please sign in to comment.