Files
NodeBB/src/cli/package-install.js

190 lines
5.3 KiB
JavaScript
Raw Normal View History

'use strict';
const path = require('path');
const fs = require('fs');
const cproc = require('child_process');
const { paths, pluginNamePattern } = require('../constants');
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;
}, {});
}
function merge(to, from) {
// Poor man's version of _.merge()
if (Object.values(from).every(val => typeof val !== 'object')) {
return Object.assign(to, from);
}
Object.keys(from).forEach((key) => {
if (Object.getPrototypeOf(from[key]) === Object.prototype) {
to[key] = merge(to[key], from[key]);
} else {
to[key] = from[key];
}
});
return to;
}
pkgInstall.updatePackageFile = () => {
let oldPackageContents = {};
try {
oldPackageContents = JSON.parse(fs.readFileSync(paths.currentPackage, 'utf8'));
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
const defaultPackageContents = JSON.parse(fs.readFileSync(paths.installPackage, 'utf8'));
let dependencies = {};
2020-08-19 15:26:09 -04:00
Object.entries(oldPackageContents.dependencies || {}).forEach(([dep, version]) => {
if (pluginNamePattern.test(dep)) {
dependencies[dep] = version;
}
});
const { devDependencies } = defaultPackageContents;
// Sort dependencies alphabetically
2020-10-17 23:56:42 -04:00
dependencies = sortDependencies({ ...dependencies, ...defaultPackageContents.dependencies });
const packageContents = { ...merge(oldPackageContents, defaultPackageContents), dependencies, devDependencies };
fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 4));
};
pkgInstall.supportedPackageManager = [
'npm',
'cnpm',
'pnpm',
'yarn',
];
pkgInstall.getPackageManager = () => {
try {
fs.accessSync(path.join(paths.nodeModules, 'nconf/package.json'), fs.constants.R_OK);
const nconf = require('nconf');
if (!Object.keys(nconf.stores).length) {
// Quick & dirty nconf setup for when you cannot rely on nconf having been required already
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,
});
}
if (nconf.get('package_manager') && !pkgInstall.supportedPackageManager.includes(nconf.get('package_manager'))) {
nconf.clear('package_manager');
}
if (!nconf.get('package_manager')) {
// Best guess based on lockfile detection
try {
// use npm if lockfile present
fs.accessSync(path.resolve(__dirname, '../../package-lock.json'), fs.constants.R_OK);
} catch (e) {
nconf.set('package_manager', [
// no cnpm detection as it uses same lockfile as npm
'yarn.lock', 'pnpm-lock.yaml',
].reduce((result, cur) => {
if (result) {
return result;
}
try {
fs.accessSync(path.resolve(__dirname, `../../${cur}`), fs.constants.R_OK);
return cur.slice(0, 4);
} catch (e) {
return result;
}
}, undefined));
}
}
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 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;
}
}
2018-02-15 15:50:44 -05:00
try {
cproc.execSync(command, {
2018-02-15 15:50:44 -05:00
cwd: path.join(__dirname, '../../'),
stdio: [0, 1, 2],
});
} catch (e) {
console.log('Error installing dependencies!');
2021-02-03 23:59:08 -07:00
console.log(`message: ${e.message}`);
console.log(`stdout: ${e.stdout}`);
console.log(`stderr: ${e.stderr}`);
2018-02-15 15:50:44 -05:00
throw e;
}
};
pkgInstall.preserveExtraneousPlugins = () => {
// Skip if `node_modules/` is not found or inaccessible
try {
fs.accessSync(paths.nodeModules, fs.constants.R_OK);
} catch (e) {
return;
}
2020-10-17 23:56:42 -04:00
const packages = fs.readdirSync(paths.nodeModules)
.filter(pkgName => pluginNamePattern.test(pkgName));
const packageContents = JSON.parse(fs.readFileSync(paths.currentPackage, 'utf8'));
const extraneous = packages
2017-11-22 14:14:13 -05:00
// only extraneous plugins (ones not in package.json) which are not links
2021-02-04 00:01:39 -07:00
.filter((pkgName) => {
2017-11-22 14:14:13 -05:00
const extraneous = !packageContents.dependencies.hasOwnProperty(pkgName);
const isLink = fs.lstatSync(path.join(paths.nodeModules, pkgName)).isSymbolicLink();
2017-11-22 14:14:13 -05:00
return extraneous && !isLink;
})
// reduce to a map of package names to package versions
2021-02-04 00:01:39 -07:00
.reduce((map, pkgName) => {
const pkgConfig = JSON.parse(fs.readFileSync(path.join(paths.nodeModules, pkgName, 'package.json'), 'utf8'));
map[pkgName] = pkgConfig.version;
return map;
}, {});
// Add those packages to package.json
2020-10-17 23:56:42 -04:00
packageContents.dependencies = sortDependencies({ ...packageContents.dependencies, ...extraneous });
fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 4));
};