fix: , package merging should deep merge nested objects

+ tests
isekai-main
Julian Lam
parent a0a3870611
commit 689c125cd3

@ -3,6 +3,7 @@
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const cproc = require('child_process'); const cproc = require('child_process');
const _ = require('lodash');
const { paths, pluginNamePattern } = require('../constants'); const { paths, pluginNamePattern } = require('../constants');
@ -40,7 +41,7 @@ pkgInstall.updatePackageFile = () => {
// Sort dependencies alphabetically // Sort dependencies alphabetically
dependencies = sortDependencies({ ...dependencies, ...defaultPackageContents.dependencies }); dependencies = sortDependencies({ ...dependencies, ...defaultPackageContents.dependencies });
const packageContents = { ...oldPackageContents, ...defaultPackageContents, dependencies: dependencies }; const packageContents = _.merge(oldPackageContents, defaultPackageContents, { dependencies });
fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 2)); fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 2));
}; };

@ -2,33 +2,98 @@
const { execSync } = require('child_process'); const { execSync } = require('child_process');
const path = require('path'); const path = require('path');
const { readFile } = require('fs').promises; const fs = require('fs').promises;
const assert = require('assert'); const assert = require('assert');
describe('Package install', () => { const pkgInstall = require('../src/cli/package-install');
it('should remove non-`nodebb-` modules not specified in `install/package.json`', async () => {
const oldValue = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
const packageFilePath = path.join(__dirname, '../package.json');
// install an extra package describe('Package install lib', () => {
// chose dotenv because it's a popular package describe('updatePackageFile()', () => {
// and we use nconf instead let source;
execSync('npm install dotenv --save'); const sourcePackagePath = path.resolve(__dirname, '../install/package.json');
const packageFilePath = path.resolve(__dirname, '../package.json');
// assert it saves in package.json before(async () => {
const packageWithExtras = JSON.parse(await readFile(packageFilePath, 'utf8')); // Move `install/package.json` and `package.json` out of the way for now
assert(packageWithExtras.dependencies.dotenv, 'dependency did not save'); await fs.copyFile(sourcePackagePath, path.resolve(__dirname, '../install/package.json.bak')); // safekeeping
await fs.copyFile(packageFilePath, path.resolve(__dirname, '../package.json.bak')); // safekeeping
await fs.copyFile(sourcePackagePath, packageFilePath); // match files for testing
});
// update the package file beforeEach(async () => {
require('../src/cli/package-install').updatePackageFile(); await fs.copyFile(path.resolve(__dirname, '../install/package.json.bak'), sourcePackagePath);
await fs.copyFile(path.resolve(__dirname, '../package.json.bak'), packageFilePath);
source = JSON.parse(await fs.readFile(sourcePackagePath));
});
// assert it removed the extra package it('should remove non-`nodebb-` modules not specified in `install/package.json`', async () => {
const packageCleaned = JSON.parse(await readFile(packageFilePath, 'utf8')); const oldValue = process.env.NODE_ENV;
assert(!packageCleaned.dependencies.dotenv, 'dependency was not removed'); process.env.NODE_ENV = 'development';
process.env.NODE_ENV = oldValue;
}); // install an extra package
// chose dotenv because it's a popular package
// and we use nconf instead
execSync('npm install dotenv --save');
// assert it saves in package.json
const packageWithExtras = JSON.parse(await fs.readFile(packageFilePath, 'utf8'));
assert(packageWithExtras.dependencies.dotenv, 'dependency did not save');
// update the package file
pkgInstall.updatePackageFile();
// assert it removed the extra package
const packageCleaned = JSON.parse(await fs.readFile(packageFilePath, 'utf8'));
assert(!packageCleaned.dependencies.dotenv, 'dependency was not removed');
process.env.NODE_ENV = oldValue;
});
it('should merge new root level properties from `install/package.json` into `package.json`', async () => {
source.bin = './nodebb';
await fs.writeFile(sourcePackagePath, JSON.stringify(source, null, 4));
pkgInstall.updatePackageFile();
const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8'));
assert.deepStrictEqual(updated, source);
});
// it('should ') it('should add new dependencies', async () => {
source.dependencies['nodebb-plugin-foobar'] = '1.0.0';
await fs.writeFile(sourcePackagePath, JSON.stringify(source, null, 4));
pkgInstall.updatePackageFile();
const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8'));
assert.deepStrictEqual(updated, source);
});
it('should update version on dependencies', async () => {
source.dependencies['nodebb-plugin-mentions'] = '1.0.0';
await fs.writeFile(sourcePackagePath, JSON.stringify(source, null, 4));
pkgInstall.updatePackageFile();
const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8'));
assert.deepStrictEqual(updated, source);
});
it('should deep merge nested objects', async () => {
source.scripts.postinstall = 'echo "I am a silly bean";';
await fs.writeFile(packageFilePath, JSON.stringify(source, null, 4));
delete source.scripts.postinstall;
source.scripts.preinstall = 'echo "What are you?";';
await fs.writeFile(sourcePackagePath, JSON.stringify(source, null, 4));
source.scripts.postinstall = 'echo "I am a silly bean";';
pkgInstall.updatePackageFile();
const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8'));
assert.deepStrictEqual(updated, source);
assert.strictEqual(updated.scripts.postinstall, 'echo "I am a silly bean";');
assert.strictEqual(updated.scripts.preinstall, 'echo "What are you?";');
});
after(async () => {
// Clean up
await fs.rename(path.resolve(__dirname, '../install/package.json.bak'), sourcePackagePath);
await fs.rename(path.resolve(__dirname, '../package.json.bak'), packageFilePath);
});
});
}); });

Loading…
Cancel
Save