-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): Support multiple library entrypoints (#597)
* 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
Showing
8 changed files
with
818 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'`) | ||
) | ||
} |
Oops, something went wrong.