diff --git a/.yarnrc.yml b/.yarnrc.yml index e264a3875..65d637172 100644 --- a/.yarnrc.yml +++ b/.yarnrc.yml @@ -6,7 +6,6 @@ packageExtensions: dependencies: react: "*" react-dom: "*" - classnames: "*" rc-checkbox@*: dependencies: react: "*" diff --git a/packages/cli/src/commands/build/build.js b/packages/cli/src/commands/build/build.js index 282908581..16e0b07fd 100644 --- a/packages/cli/src/commands/build/build.js +++ b/packages/cli/src/commands/build/build.js @@ -15,17 +15,23 @@ */ import getServer from '../../utils/getServer.js'; -import installServer from './installServer.js'; -import runLowdefyBuild from './runLowdefyBuild.js'; -import runNextBuild from './runNextBuild.js'; +import installServer from '../../utils/installServer.js'; +import mergePackageJson from '../../utils/mergePackageJson.js'; +import runLowdefyBuild from '../../utils/runLowdefyBuild.js'; +import runNextBuild from '../../utils/runNextBuild.js'; async function build({ context }) { context.print.info('Starting build.'); + const directory = context.directory.server; await getServer({ context, packageName: '@lowdefy/server' }); - await installServer({ context }); - await runLowdefyBuild({ context }); - await installServer({ context }); - await runNextBuild({ context }); + await mergePackageJson({ + context, + serverDirectory: directory, + }); + await installServer({ context, directory }); + await runLowdefyBuild({ context, directory }); + await installServer({ context, directory }); + await runNextBuild({ context, directory }); await context.sendTelemetry({ sendTypes: true }); context.print.succeed(`Build successful.`); } diff --git a/packages/cli/src/commands/build/installServer.js b/packages/cli/src/commands/build/installServer.js deleted file mode 100644 index 4b2f3ffde..000000000 --- a/packages/cli/src/commands/build/installServer.js +++ /dev/null @@ -1,43 +0,0 @@ -/* - Copyright 2020-2021 Lowdefy, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -import { spawnProcess } from '@lowdefy/node-utils'; - -const args = { - npm: ['install', '--legacy-peer-deps'], - yarn: ['install'], -}; - -async function installServer({ context }) { - context.print.spin(`Running ${context.packageManager} install.`); - try { - await spawnProcess({ - logger: context.print, - command: context.packageManager, // npm or yarn - args: args[context.packageManager], - processOptions: { - cwd: context.directories.server, - }, - silent: false, - }); - } catch (error) { - console.log(error); - throw new Error(`${context.packageManager} install failed.`); - } - context.print.log(`${context.packageManager} install successful.`); -} - -export default installServer; diff --git a/packages/cli/src/commands/dev/dev.js b/packages/cli/src/commands/dev/dev.js index b4976076c..49fef0c62 100644 --- a/packages/cli/src/commands/dev/dev.js +++ b/packages/cli/src/commands/dev/dev.js @@ -14,16 +14,22 @@ limitations under the License. */ -import installServer from './installServer.js'; +import installServer from '../../utils/installServer.js'; +import mergePackageJson from '../../utils/mergePackageJson.js'; import runDevServer from './runDevServer.js'; import getServer from '../../utils/getServer.js'; async function dev({ context }) { + const directory = context.directory.dev; context.print.info('Starting development server.'); await getServer({ context, packageName: '@lowdefy/server-dev' }); - await installServer({ context }); + await mergePackageJson({ + context, + serverDirectory: directory, + }); + await installServer({ context, directory }); context.sendTelemetry(); - await runDevServer({ context }); + await runDevServer({ context, directory }); } export default dev; diff --git a/packages/cli/src/commands/dev/runDevServer.js b/packages/cli/src/commands/dev/runDevServer.js index fdf96dd5e..504d7f2d2 100644 --- a/packages/cli/src/commands/dev/runDevServer.js +++ b/packages/cli/src/commands/dev/runDevServer.js @@ -16,13 +16,13 @@ import { spawnProcess } from '@lowdefy/node-utils'; -async function runDevServer({ context }) { +async function runDevServer({ context, directory }) { await spawnProcess({ logger: context.print, args: ['run', 'start'], command: context.packageManager, // npm or yarn processOptions: { - cwd: context.directories.dev, + cwd: directory, env: { ...process.env, LOWDEFY_BUILD_REF_RESOLVER: context.options.refResolver, diff --git a/packages/cli/src/commands/start/runStart.js b/packages/cli/src/commands/start/runStart.js index 2d785998f..2f20d39e9 100644 --- a/packages/cli/src/commands/start/runStart.js +++ b/packages/cli/src/commands/start/runStart.js @@ -16,14 +16,14 @@ import { spawnProcess } from '@lowdefy/node-utils'; -async function runStart({ context }) { +async function runStart({ context, directory }) { context.print.spin(`Running "${context.packageManager} run start".`); await spawnProcess({ logger: context.print, args: ['run', 'start'], command: context.packageManager, // npm or yarn processOptions: { - cwd: context.directories.server, + cwd: directory, env: { ...process.env, LOWDEFY_DIRECTORY_CONFIG: context.directories.config, diff --git a/packages/cli/src/commands/start/start.js b/packages/cli/src/commands/start/start.js index 80add2abf..c3ed933ed 100644 --- a/packages/cli/src/commands/start/start.js +++ b/packages/cli/src/commands/start/start.js @@ -21,7 +21,7 @@ import runStart from './runStart.js'; async function build({ context }) { context.print.info('Starting server.'); context.sendTelemetry({ sendTypes: true }); - await runStart({ context }); + await runStart({ context, directory: context.directory.server }); } export default build; diff --git a/packages/cli/src/utils/getServer.js b/packages/cli/src/utils/getServer.js index c029115dd..915c9099f 100644 --- a/packages/cli/src/utils/getServer.js +++ b/packages/cli/src/utils/getServer.js @@ -19,7 +19,7 @@ import path from 'path'; import { cleanDirectory, readFile } from '@lowdefy/node-utils'; import fetchNpmTarball from './fetchNpmTarball.js'; -async function getServer({ context, packageName }) { +async function getServer({ context, packageName, directory }) { if (context.lowdefyVersion === 'local') { context.print.warn(`Running local ${packageName}.`); return; @@ -27,28 +27,26 @@ async function getServer({ context, packageName }) { let fetchServer = false; - const serverExists = fs.existsSync(path.join(context.directories.server, 'package.json')); + const serverExists = fs.existsSync(path.join(directory, 'package.json')); if (!serverExists) fetchServer = true; if (serverExists) { - const serverPackageConfig = JSON.parse( - await readFile(path.join(context.directories.server, 'package.json')) - ); + const serverPackageConfig = JSON.parse(await readFile(path.join(directory, 'package.json'))); if (serverPackageConfig.version !== context.lowdefyVersion) { fetchServer = true; - context.print.warn(`Removing @lowdefy/server with version ${serverPackageConfig.version}`); - await cleanDirectory(context.directories.server); + context.print.warn(`Removing ${packageName} with version ${serverPackageConfig.version}`); + await cleanDirectory(directory); } } if (fetchServer) { - context.print.spin('Fetching @lowdefy/server from npm.'); + context.print.spin(`Fetching ${packageName} from npm.`); await fetchNpmTarball({ packageName, version: context.lowdefyVersion, - directory: context.directories.server, + directory: directory, }); - context.print.log('Fetched @lowdefy/server from npm.'); + context.print.log(`Fetched ${packageName} from npm.`); } } diff --git a/packages/cli/src/commands/dev/installServer.js b/packages/cli/src/utils/installServer.js similarity index 93% rename from packages/cli/src/commands/dev/installServer.js rename to packages/cli/src/utils/installServer.js index 3452a470e..3014b24b6 100644 --- a/packages/cli/src/commands/dev/installServer.js +++ b/packages/cli/src/utils/installServer.js @@ -21,7 +21,7 @@ const args = { yarn: ['install'], }; -async function installServer({ context }) { +async function installServer({ context, directory }) { context.print.spin(`Running ${context.packageManager} install.`); try { await spawnProcess({ @@ -29,7 +29,7 @@ async function installServer({ context }) { command: context.packageManager, // npm or yarn args: args[context.packageManager], processOptions: { - cwd: context.directories.dev, + cwd: directory, }, silent: false, }); diff --git a/packages/cli/src/utils/mergePackageJson.js b/packages/cli/src/utils/mergePackageJson.js new file mode 100644 index 000000000..1b30cf424 --- /dev/null +++ b/packages/cli/src/utils/mergePackageJson.js @@ -0,0 +1,34 @@ +/* + Copyright 2020-2021 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import path from 'path'; +import { mergeObjects } from '@lowdefy/helpers'; +import { readFile, writeFile } from '@lowdefy/node-utils'; + +async function mergePackageJson({ context, serverDirectory }) { + const serverPackageJsonPath = path.join(serverDirectory, 'package.json'); + const serverPackageJson = JSON.parse(await readFile(serverPackageJsonPath)); + const configPackageJson = JSON.parse( + await readFile(path.join(context.directories.config, 'package.json')) + ); + + const mergedPackageJson = mergeObjects([serverPackageJson, configPackageJson]); + + const mergedPackageJsonContent = JSON.stringify(mergedPackageJson, null, 2).concat('\n'); + await writeFile(serverPackageJsonPath, mergedPackageJsonContent); +} + +export default mergePackageJson; diff --git a/packages/cli/src/commands/build/runLowdefyBuild.js b/packages/cli/src/utils/runLowdefyBuild.js similarity index 94% rename from packages/cli/src/commands/build/runLowdefyBuild.js rename to packages/cli/src/utils/runLowdefyBuild.js index e1d062ec2..745c7ccfd 100644 --- a/packages/cli/src/commands/build/runLowdefyBuild.js +++ b/packages/cli/src/utils/runLowdefyBuild.js @@ -16,7 +16,7 @@ import { spawnProcess } from '@lowdefy/node-utils'; -async function runLowdefyBuild({ context }) { +async function runLowdefyBuild({ context, directory }) { context.print.log('Running Lowdefy build.'); try { await spawnProcess({ @@ -24,7 +24,7 @@ async function runLowdefyBuild({ context }) { command: context.packageManager, // npm or yarn args: ['run', 'build:lowdefy'], processOptions: { - cwd: context.directories.server, + cwd: directory, env: { ...process.env, LOWDEFY_BUILD_REF_RESOLVER: context.options.refResolver, diff --git a/packages/cli/src/commands/build/runNextBuild.js b/packages/cli/src/utils/runNextBuild.js similarity index 92% rename from packages/cli/src/commands/build/runNextBuild.js rename to packages/cli/src/utils/runNextBuild.js index f2c428944..4eb5efd46 100644 --- a/packages/cli/src/commands/build/runNextBuild.js +++ b/packages/cli/src/utils/runNextBuild.js @@ -16,7 +16,7 @@ import { spawnProcess } from '@lowdefy/node-utils'; -async function runNextBuild({ context }) { +async function runNextBuild({ context, directory }) { context.print.log('Running Next build.'); try { await spawnProcess({ @@ -24,7 +24,7 @@ async function runNextBuild({ context }) { command: context.packageManager, // npm or yarn args: ['run', 'build:next'], processOptions: { - cwd: context.directories.server, + cwd: directory, }, silent: false, }); diff --git a/packages/utils/helpers/src/mergeObjects.test.js b/packages/utils/helpers/src/mergeObjects.test.js index 1fa62182b..ad763cfb0 100644 --- a/packages/utils/helpers/src/mergeObjects.test.js +++ b/packages/utils/helpers/src/mergeObjects.test.js @@ -96,3 +96,13 @@ test('merge list of objects, larger indices overwrite smaller', () => { } `); }); + +test('merge objects with null', () => { + const obj = { + a: 'a', + b: 1, + c: { a: 'b' }, + }; + expect(mergeObjects([obj, null])).toEqual(obj); + expect(mergeObjects([null, obj])).toEqual(obj); +});