Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use creact-react-app/react-scripts instead of .d2 folder #711

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: fix linter issues
  • Loading branch information
Mohammer5 committed Mar 30, 2022
commit e08b01654dbab2b77e8247a23c2baf023edfc398
162 changes: 81 additions & 81 deletions cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,95 +27,95 @@ const handler = ({
verify,
pack: packAppOutput,
verbose,
}) => exitOnCatch(
async () => {
const paths = makePaths(cwd)
const mode = determineBuildMode(modeArg, dev)
}) =>
exitOnCatch(
async () => {
const paths = makePaths(cwd)
const mode = determineBuildMode(modeArg, dev)

loadEnvFiles(paths, mode)
loadEnvFiles(paths, mode)

reporter.print(chalk.green.bold('Build parameters:'))
printBuildParam('Mode', mode)
reporter.print(chalk.green.bold('Build parameters:'))
printBuildParam('Mode', mode)

const config = parseConfig(paths)
const config = parseConfig(paths)

if (config.type === 'app') {
setAppParameters(standalone, config)
}

await fs.remove(paths.buildOutput)
await exitWhenPackageInvalid(config, paths, verify)

reporter.info('Generating internationalization strings...')
await i18n.extractAndGenerate(paths)

reporter.info(
`Building ${config.type} ${chalk.bold(config.name)}...`
)

if (config.type === 'app') {
// Manifest generation moved here so these static assets can be
// precached by Workbox during the shell build step
reporter.info('Generating manifests...')
await generateManifests(paths, config, process.env.PUBLIC_URL)

// @TODO: Figure out how to do this
// const { injectPrecacheManifest } = require('../lib/pwa')
// if (config.pwa.enabled) {
// reporter.info('Injecting precache manifest...')
// await injectPrecacheManifest(paths, config)
// }

reporter.info('Ensuring that a build folder exists')
fs.ensureDirSync(paths.buildOutput)

// CRA Manages service worker compilation here
reporter.info('Creating a production build...')
await craBuild({ config, verbose, cwd })

if (!fs.pathExistsSync(paths.buildAppOutput)) {
reporter.error('No build output found')
process.exit(1)
if (config.type === 'app') {
setAppParameters(standalone, config)
}

if (packAppOutput) {
const bundle = path.parse(paths.buildAppBundle)

await fs.remove(paths.buildAppBundleOutput)

// update bundle archive
await pack({
destination: path.resolve(cwd, bundle.dir),
filename: bundle.base,
})
await fs.remove(paths.buildOutput)
await exitWhenPackageInvalid(config, paths, verify)

reporter.info('Generating internationalization strings...')
await i18n.extractAndGenerate(paths)

reporter.info(
`Building ${config.type} ${chalk.bold(config.name)}...`
)

if (config.type === 'app') {
// Manifest generation moved here so these static assets can be
// precached by Workbox during the shell build step
reporter.info('Generating manifests...')
await generateManifests(paths, config, process.env.PUBLIC_URL)

// @TODO: Figure out how to do this
// const { injectPrecacheManifest } = require('../lib/pwa')
// if (config.pwa.enabled) {
// reporter.info('Injecting precache manifest...')
// await injectPrecacheManifest(paths, config)
// }

reporter.info('Ensuring that a build folder exists')
fs.ensureDirSync(paths.buildOutput)

// CRA Manages service worker compilation here
reporter.info('Creating a production build...')
await craBuild({ config, verbose, cwd })

if (!fs.pathExistsSync(paths.buildAppOutput)) {
reporter.error('No build output found')
process.exit(1)
}

if (packAppOutput) {
const bundle = path.parse(paths.buildAppBundle)

await fs.remove(paths.buildAppBundleOutput)

// update bundle archive
await pack({
destination: path.resolve(cwd, bundle.dir),
filename: bundle.base,
})
}

reporter.print(chalk.green('\n**** DONE! ****'))
} else {
await Promise.all([
compile({
config,
paths,
moduleType: 'es',
mode,
watch,
}),
compile({
config,
paths,
moduleType: 'cjs',
mode,
watch,
}),
])
}

reporter.print(chalk.green('\n**** DONE! ****'))
} else {
await Promise.all([
compile({
config,
paths,
moduleType: 'es',
mode,
watch,
}),
compile({
config,
paths,
moduleType: 'cjs',
mode,
watch,
}),
])
},
{
name: 'build',
onError: () => reporter.error('Build script failed'),
}
},
{
name: 'build',
onError: () => reporter.error('Build script failed'),
}
)

)

const command = {
aliases: 'b',
Expand Down
7 changes: 2 additions & 5 deletions cli/src/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const { initApp, initLibrary } = require('../lib/index.js')

const handler = async options => await (
options.lib
? initLibrary(options)
: initApp(options)
)
const handler = async options =>
await (options.lib ? initLibrary(options) : initApp(options))

const command = {
command: 'init <name>',
Expand Down
86 changes: 43 additions & 43 deletions cli/src/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,60 @@ const handler = ({
proxy,
proxyPort,
verbose,
}) => exitOnCatch(
async () => {
const mode = 'development'
const paths = makePaths(cwd)
}) =>
exitOnCatch(
async () => {
const mode = 'development'
const paths = makePaths(cwd)

loadEnvFiles(paths, mode)
loadEnvFiles(paths, mode)

const config = parseConfig(paths)
const config = parseConfig(paths)

verifyIsApp(config)
verifyIsApp(config)

const appPort = await detectPort(port)
const appPort = await detectPort(port)

await createLocalProxyServer({ proxy, proxyPort, appPort })
await notifyWhenPackageInvalid(config, paths)
await createLocalProxyServer({ proxy, proxyPort, appPort })
await notifyWhenPackageInvalid(config, paths)

reporter.info('Generating internationalization strings...')
await i18n.extractAndGenerate(paths)
reporter.info('Generating internationalization strings...')
await i18n.extractAndGenerate(paths)

// Manifests added here so app has access to manifest.json for pwa
reporter.info('Generating manifests...')
await generateManifests(paths, config, process.env.PUBLIC_URL)
// Manifests added here so app has access to manifest.json for pwa
reporter.info('Generating manifests...')
await generateManifests(paths, config, process.env.PUBLIC_URL)

if (String(appPort) !== String(port)) {
reporter.print('')
reporter.warn(
`Something is already running on port ${port}, using ${appPort} instead.`
)
}
if (String(appPort) !== String(port)) {
reporter.print('')
reporter.warn(
`Something is already running on port ${port}, using ${appPort} instead.`
)
}

// @TODO: Figure out how to do this properly
// const { compileServiceWorker } = require('../lib/pwa')
// if (config.pwa.enabled) {
// reporter.info('Compiling service worker...')
// await compileServiceWorker({
// config,
// paths,
// mode: 'development',
// })
// }
// @TODO: Figure out how to do this properly
// const { compileServiceWorker } = require('../lib/pwa')
// if (config.pwa.enabled) {
// reporter.info('Compiling service worker...')
// await compileServiceWorker({
// config,
// paths,
// mode: 'development',
// })
// }

try {
await craStart({ port, cwd: paths.base, config, verbose })
} catch (code) {
process.exit(code)
try {
await craStart({ port, cwd: paths.base, config, verbose })
} catch (code) {
process.exit(code)
}
},
{
name: 'start',
onError: () =>
reporter.error('Start script exited with non-zero exit code'),
}
},
{
name: 'start',
onError: () =>
reporter.error('Start script exited with non-zero exit code'),
}
)

)

const command = {
command: 'start',
Expand Down
13 changes: 5 additions & 8 deletions cli/src/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ const handler = async ({ cwd }) => {

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

await exitOnCatch(
async () => await craTest({ cwd, testArgs }),
{
name: 'test',
onError: () =>
reporter.error('Test script exited with non-zero exit code'),
}
)
await exitOnCatch(async () => await craTest({ cwd, testArgs }), {
name: 'test',
onError: () =>
reporter.error('Test script exited with non-zero exit code'),
})
}

const command = {
Expand Down
11 changes: 4 additions & 7 deletions cli/src/lib/init/init-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const { craInit } = require('../shell')
// See https://github.com/facebook/create-react-app/issues/9849
function addMissingFieldsToPackageJson(appFolder) {
const packageJsonPath = path.join(appFolder, 'package.json')
const packageJsonRaw = fs.readFileSync(packageJsonPath, { encoding: 'utf8' })
const packageJsonRaw = fs.readFileSync(packageJsonPath, {
encoding: 'utf8',
})
const packageJsonContents = JSON.parse(packageJsonRaw)
const packageJsonPatched = {
...packageJsonContents,
Expand All @@ -24,11 +26,7 @@ function addMissingFieldsToPackageJson(appFolder) {
fs.writeFileSync(packageJsonPath, packageJsonPatchedString)
}

module.exports = async function initApp({
cwd: cwdArg,
verbose,
name,
}) {
module.exports = async function initApp({ cwd: cwdArg, verbose, name }) {
const cwd = cwdArg || process.cwd()
const appFolder = path.join(cwd, name)

Expand All @@ -41,5 +39,4 @@ module.exports = async function initApp({
reporter.info(
`Successfully created the dhis2 app. You can now navigate to \`${appFolder}\` and run \`yarn start\``
)

}
5 changes: 1 addition & 4 deletions cli/src/lib/init/init-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ module.exports = async function initLibrary({ force, name, cwdArg }) {
)
} else {
reporter.info('Importing d2.config.js defaults')
fs.copyFileSync(
paths.configDefaultsLib,
paths.config
)
fs.copyFileSync(paths.configDefaultsLib, paths.config)
}

if (!fs.existsSync(paths.package)) {
Expand Down
4 changes: 1 addition & 3 deletions cli/src/lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ const fs = require('fs')
const path = require('path')
const { reporter } = require('@dhis2/cli-helpers-engine')

const shellSource = path.dirname(
require.resolve('@dhis2/app-shell')
)
const shellSource = path.dirname(require.resolve('@dhis2/app-shell'))
const shellAppDirname = 'src/D2App'

const findYarnLock = base => {
Expand Down
Loading