refactor: updated package-install.js exports style, new exported method 'getPackageManager' for use in cases where nconf is unreliable, fix bug where nconf was not correctly set up in cli tools, proper installation of dev dependencies based on global env value

isekai-main
Julian Lam 3 years ago
parent c526a34605
commit 9a1690857d

@ -6,7 +6,18 @@ const cproc = require('child_process');
const { paths, pluginNamePattern } = require('../constants'); const { paths, pluginNamePattern } = require('../constants');
function updatePackageFile() { const pkgInstall = module.exports;
function sortDependencies(dependencies) {
return Object.entries(dependencies)
.sort((a, b) => (a < b ? -1 : 1))
.reduce((memo, pkg) => {
memo[pkg[0]] = pkg[1];
return memo;
}, {});
}
pkgInstall.updatePackageFile = () => {
let oldPackageContents = {}; let oldPackageContents = {};
try { try {
@ -32,45 +43,58 @@ function updatePackageFile() {
const packageContents = { ...oldPackageContents, ...defaultPackageContents, dependencies: dependencies }; const packageContents = { ...oldPackageContents, ...defaultPackageContents, dependencies: dependencies };
fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 2)); fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 2));
} };
exports.updatePackageFile = updatePackageFile;
exports.supportedPackageManager = [ pkgInstall.supportedPackageManager = [
'npm', 'npm',
'cnpm', 'cnpm',
'pnpm', 'pnpm',
'yarn', 'yarn',
]; ];
function installAll() { pkgInstall.getPackageManager = () => {
const prod = global.env !== 'development'; // Use this method if you can't reliably require('nconf') directly
let command = 'npm install';
try { try {
// Quick & dirty nconf setup
fs.accessSync(path.join(paths.nodeModules, 'nconf/package.json'), fs.constants.R_OK); fs.accessSync(path.join(paths.nodeModules, 'nconf/package.json'), fs.constants.R_OK);
const nconf = require('nconf');
const configFile = path.resolve(__dirname, '../../', nconf.any(['config', 'CONFIG']) || 'config.json');
nconf.env().file({ // not sure why adding .argv() causes the process to terminate
file: configFile,
});
return nconf.get('package_manager') || 'npm';
} catch (e) {
// nconf not install or other unexpected error/exception
return 'npm';
}
};
pkgInstall.installAll = () => {
const prod = process.env.NODE_ENV !== 'development';
let command = 'npm install';
const supportedPackageManagerList = exports.supportedPackageManager; // load config from src/cli/package-install.js const supportedPackageManagerList = exports.supportedPackageManager; // load config from src/cli/package-install.js
const packageManager = require('nconf').get('package_manager'); const packageManager = pkgInstall.getPackageManager();
if (supportedPackageManagerList.indexOf(packageManager) >= 0) { if (supportedPackageManagerList.indexOf(packageManager) >= 0) {
switch (packageManager) { switch (packageManager) {
case 'yarn': case 'yarn':
command = 'yarn'; command = `yarn${prod ? ' --production' : ''}`;
break; break;
case 'pnpm': case 'pnpm':
command = 'pnpm install'; command = 'pnpm install'; // pnpm checks NODE_ENV
break; break;
case 'cnpm': case 'cnpm':
command = 'cnpm install'; command = `cnpm install ${prod ? ' --production' : ''}`;
break; break;
default: default:
command += prod ? ' --production' : '';
break; break;
} }
} }
} catch (e) {
// No error handling is required here.
// If nconf is not installed, regular package installation via npm is carried out.
}
try { try {
cproc.execSync(command + (prod ? ' --production' : ''), { cproc.execSync(command, {
cwd: path.join(__dirname, '../../'), cwd: path.join(__dirname, '../../'),
stdio: [0, 1, 2], stdio: [0, 1, 2],
}); });
@ -81,11 +105,9 @@ function installAll() {
console.log(`stderr: ${e.stderr}`); console.log(`stderr: ${e.stderr}`);
throw e; throw e;
} }
} };
exports.installAll = installAll;
function preserveExtraneousPlugins() { pkgInstall.preserveExtraneousPlugins = () => {
// Skip if `node_modules/` is not found or inaccessible // Skip if `node_modules/` is not found or inaccessible
try { try {
fs.accessSync(paths.nodeModules, fs.constants.R_OK); fs.accessSync(paths.nodeModules, fs.constants.R_OK);
@ -117,15 +139,4 @@ function preserveExtraneousPlugins() {
packageContents.dependencies = sortDependencies({ ...packageContents.dependencies, ...extraneous }); packageContents.dependencies = sortDependencies({ ...packageContents.dependencies, ...extraneous });
fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 2)); 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;

Loading…
Cancel
Save