feat(cli): Merge user package json into server package json.

This commit is contained in:
Sam 2022-02-22 17:33:33 +02:00
parent 754241b36d
commit 899a15f6c5
No known key found for this signature in database
GPG Key ID: D004126FCD1A6DF0
13 changed files with 85 additions and 75 deletions

View File

@ -6,7 +6,6 @@ packageExtensions:
dependencies:
react: "*"
react-dom: "*"
classnames: "*"
rc-checkbox@*:
dependencies:
react: "*"

View File

@ -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.`);
}

View File

@ -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;

View File

@ -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;

View File

@ -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,

View File

@ -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,

View File

@ -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;

View File

@ -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.`);
}
}

View File

@ -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,
});

View File

@ -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;

View File

@ -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,

View File

@ -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,
});

View File

@ -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);
});