From e362c342a37628ec12f914b06b9e0a1636483b7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 17 Oct 2020 23:56:42 -0400 Subject: [PATCH] fix: #8630, sort extra deps --- src/cli/package-install.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/cli/package-install.js b/src/cli/package-install.js index 8f60f656b167..d3ac543767cb 100644 --- a/src/cli/package-install.js +++ b/src/cli/package-install.js @@ -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 }; @@ -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 @@ -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;