From 9587e07296c4ab46ec404e1044ab07528bab45cb Mon Sep 17 00:00:00 2001 From: Izel Nakri Date: Mon, 17 Feb 2020 22:36:43 +0100 Subject: [PATCH] fastboot enchancements --- .circleci/config.yml | 2 +- cli.js | 61 ++++++++++----------- lib/builders/build-fastboot-package-json.js | 5 +- lib/utils/fastboot-express-middleware.js | 8 +-- 4 files changed, 37 insertions(+), 39 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1e7c1bed..e53b9104 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -31,7 +31,7 @@ jobs: DOCKER_TAG=$(echo mber:${CIRCLE_BRANCH} | tr '/' '_') docker pull ${HUB_USERNAME}/${DOCKER_TAG} docker run -t -d --name="mber" ${HUB_USERNAME}/${DOCKER_TAG} /bin/bash - - run: docker exec -it -e NODE_OPTIONS=--experimental-modules mber npm test + - run: docker exec -it mber npm test workflows: version: 2 diff --git a/cli.js b/cli.js index 25b184d3..17b7681e 100755 --- a/cli.js +++ b/cli.js @@ -1,16 +1,6 @@ #! /usr/bin/env node - import chalk from 'ansi-colors'; import Console from './lib/utils/console.js'; -import printCommand from './lib/commands/index.js'; -import compileCommand from './lib/commands/compile.js'; -import serveCommand from './lib/commands/serve.js'; -import testCommand from './lib/commands/test.js'; -import buildCommand from './lib/commands/build.js'; -import consoleCommand from './lib/commands/console.js'; -import newCommand from './lib/commands/new.js'; -import generateCommand from './lib/commands/generate.js'; -import deleteCommand from './lib/commands/delete.js'; process.title = 'mber'; global.mainContext = global; // NOTE: needed for ember-template-compiler @@ -18,41 +8,48 @@ global.mainContext = global; // NOTE: needed for ember-template-compiler let shouldRunCommand = false; const CLI = { - default(commandHandler) { + async default(commandHandler) { if (!process.argv[2]) { shouldRunCommand = true; - return commandHandler(); + return await commandHandler(); } }, - command(commandName, commandHandler) { + async command(commandName, commandHandler) { const commandMatchesArray = Array.isArray(commandName) && commandName.includes(process.argv[2]); if (commandMatchesArray || commandName === process.argv[2]) { shouldRunCommand = true; - return commandHandler(); + return await commandHandler(); } } }; -CLI.default(() => printCommand()); -CLI.command(['serve', 'server', 's'], () => serveCommand()); // TODO: add proxy -CLI.command(['test', 't'], () => testCommand()); // TODO: add --proxy -CLI.command(['build', 'b'], () => buildCommand()); // TODO: add --proxy -CLI.command(['compile', 'transpile', 'c'], () => compileCommand()); -CLI.command(['console'], () => consoleCommand()); -CLI.command(['help', 'h', 'print', 'p'], () => printCommand()); -CLI.command(['init', 'new'], () => newCommand()); -CLI.command(['generate', 'g', 'create'], () => generateCommand(process.argv[3], process.argv[4])); -CLI.command(['delete', 'd', 'destroy', 'remove'], () => - deleteCommand(process.argv[3], process.argv[4]) -); - -if (!shouldRunCommand) { - Console.log(chalk.red('unknown command. Available options are:')); - printCommand(); - setTimeout(() => process.exit(1), 100); -} +(async () => { + CLI.default(async () => await runCommand('./lib/commands/index.js')); + CLI.command(['serve', 'server', 's'], async () => await runCommand('./lib/commands/serve.js')); // TODO: add proxy + CLI.command(['test', 't'], async () => await runCommand('./lib/commands/test.js')); // TODO: add --proxy + CLI.command(['build', 'b'], async () => await runCommand('./lib/commands/build.js')); // TODO: add --proxy + CLI.command(['compile', 'transpile', 'c'], async () => await runCommand('./lib/commands/compile.js')); + CLI.command(['console'], async () => await runCommand('./lib/commands/console.js')); + CLI.command(['help', 'h', 'print', 'p'], async () => await runCommand('./lib/commands/index.js')); + CLI.command(['init', 'new'], async () => await runCommand('./lib/commands/new.js')); + CLI.command(['generate', 'g', 'create'], async () => { + return (await import('./lib/commands/generate.js')).default(process.argv[3], process.argv[4]) + }); + CLI.command(['delete', 'd', 'destroy', 'remove'], async () => { + (await import('./lib/commands/delete.js')).default(process.argv[3], process.argv[4]); + }); + + if (!shouldRunCommand) { + Console.log(chalk.red('unknown command. Available options are:')); + await runCommand('./lib/commands/index.js'); + setTimeout(() => process.exit(1), 100); + } +})(); +async function runCommand(commandPath) { + return (await import(commandPath)).default(); +} // NOTE: maybe merge server and console commands in future? diff --git a/lib/builders/build-fastboot-package-json.js b/lib/builders/build-fastboot-package-json.js index 8a13a0c6..e1673f37 100644 --- a/lib/builders/build-fastboot-package-json.js +++ b/lib/builders/build-fastboot-package-json.js @@ -22,6 +22,7 @@ export default async function( const applicationName = ENV.modulePrefix || 'frontend'; const applicationPath = assetMap['assets/application.js']; const projectRoot = buildConfig.projectRoot || (await findProjectRoot()); + const packageJSON = JSON.parse(await fs.readFile(`${projectRoot}/package.json`)).toString(); return fs.writeFile( `${projectRoot}/${distPath}/package.json`, @@ -54,11 +55,11 @@ export default async function( htmlFile: 'index.html', vendorFiles: [assetMap['assets/vendor.js']] }, - moduleWhitelist: [ + moduleWhitelist: (packageJSON.fastbootDependencies || []).concat([ 'node-fetch', 'abortcontroller-polyfill', 'abortcontroller-polyfill/dist/cjs-ponyfill' - ], + ]), schemaVersion: 3 } }, diff --git a/lib/utils/fastboot-express-middleware.js b/lib/utils/fastboot-express-middleware.js index ef9a1c63..8aca6219 100644 --- a/lib/utils/fastboot-express-middleware.js +++ b/lib/utils/fastboot-express-middleware.js @@ -21,15 +21,15 @@ export default function(distPath, opts={}) { let headers = result.headers; let statusMessage = result.error ? 'NOT OK ' : 'OK '; - for (var pair of headers.entries()) { - response.set(pair[0], pair[1]); - } - if (result.error) { console.log('RESILIENT MODE CAUGHT:', result.error.stack); console.log(result.error); next(result.error); + } else { + for (var pair of headers.entries()) { + response.set(pair[0], pair[1]); + } } // TODO: on debug, show fastboot render times