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 supportedPackageManagerList = exports.supportedPackageManager; // load config from src/cli/package-install.js const nconf = require('nconf');
const packageManager = require('nconf').get('package_manager'); const configFile = path.resolve(__dirname, '../../', nconf.any(['config', 'CONFIG']) || 'config.json');
if (supportedPackageManagerList.indexOf(packageManager) >= 0) { nconf.env().file({ // not sure why adding .argv() causes the process to terminate
switch (packageManager) { file: configFile,
case 'yarn': });
command = 'yarn';
break; return nconf.get('package_manager') || 'npm';
case 'pnpm':
command = 'pnpm install';
break;
case 'cnpm':
command = 'cnpm install';
break;
default:
break;
}
}
} catch (e) { } catch (e) {
// No error handling is required here. // nconf not install or other unexpected error/exception
// If nconf is not installed, regular package installation via npm is carried out. 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 packageManager = pkgInstall.getPackageManager();
if (supportedPackageManagerList.indexOf(packageManager) >= 0) {
switch (packageManager) {
case 'yarn':
command = `yarn${prod ? ' --production' : ''}`;
break;
case 'pnpm':
command = 'pnpm install'; // pnpm checks NODE_ENV
break;
case 'cnpm':
command = `cnpm install ${prod ? ' --production' : ''}`;
break;
default:
command += prod ? ' --production' : '';
break;
}
}
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; pkgInstall.preserveExtraneousPlugins = () => {
function 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