Skip to content

Commit

Permalink
fix: NodeBB#8630, sort extra deps
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 18, 2020
1 parent d9a1685 commit e362c34
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/cli/package-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ function updatePackageFile() {
});

// Sort dependencies alphabetically
dependencies = Object.entries({ ...dependencies, ...defaultPackageContents.dependencies }).sort((a, b) => (a < b ? -1 : 1)).reduce((memo, pkg) => {
memo[pkg[0]] = pkg[1];
return memo;
}, {});
dependencies = sortDependencies({ ...dependencies, ...defaultPackageContents.dependencies });

const packageContents = { ...oldPackageContents, ...defaultPackageContents, dependencies: dependencies };

Expand Down Expand Up @@ -95,9 +92,9 @@ function preserveExtraneousPlugins() {
return;
}

const packages = fs.readdirSync(paths.nodeModules).filter(function (pkgName) {
return pluginNamePattern.test(pkgName);
});
const packages = fs.readdirSync(paths.nodeModules)
.filter(pkgName => pluginNamePattern.test(pkgName));

const packageContents = JSON.parse(fs.readFileSync(paths.currentPackage, 'utf8'));

const extraneous = packages
Expand All @@ -116,8 +113,18 @@ function preserveExtraneousPlugins() {
}, {});

// Add those packages to package.json
Object.assign(packageContents.dependencies, extraneous);
packageContents.dependencies = sortDependencies({ ...packageContents.dependencies, ...extraneous });

fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 2));
}

function sortDependencies(dependencies) {
return Object.entries(dependencies)
.sort((a, b) => (a < b ? -1 : 1))
.reduce((memo, pkg) => {
memo[pkg[0]] = pkg[1];
return memo;
}, {});
}

exports.preserveExtraneousPlugins = preserveExtraneousPlugins;

0 comments on commit e362c34

Please sign in to comment.