From e6a1741c2ecfcd3e320d99c656d7e50bdc562b6f Mon Sep 17 00:00:00 2001 From: Jonson Petard <41122242+greenhat616@users.noreply.github.com> Date: Mon, 13 Apr 2020 21:59:52 +0800 Subject: [PATCH] Support package manager: cnpm and pnpm (#8222) * feat: support pnpm, cnpm * feat: store supportedPackageManager in default package.json * feat: store supportedPackageManager in src/cli/package-install.js Co-authored-by: a632079 --- src/cli/package-install.js | 58 +++++++++++++++++++++++++------------- src/cli/upgrade-plugins.js | 48 ++++++++++++++++--------------- src/plugins/install.js | 11 +++++++- 3 files changed, 74 insertions(+), 43 deletions(-) diff --git a/src/cli/package-install.js b/src/cli/package-install.js index 699a5962bd..acc61fcdb7 100644 --- a/src/cli/package-install.js +++ b/src/cli/package-install.js @@ -1,15 +1,15 @@ 'use strict'; -var path = require('path'); -var fs = require('fs'); -var cproc = require('child_process'); +const path = require('path'); +const fs = require('fs'); +const cproc = require('child_process'); -var packageFilePath = path.join(__dirname, '../../package.json'); -var packageDefaultFilePath = path.join(__dirname, '../../install/package.json'); -var modulesPath = path.join(__dirname, '../../node_modules'); +const packageFilePath = path.join(__dirname, '../../package.json'); +const packageDefaultFilePath = path.join(__dirname, '../../install/package.json'); +const modulesPath = path.join(__dirname, '../../node_modules'); function updatePackageFile() { - var oldPackageContents = {}; + let oldPackageContents = {}; try { oldPackageContents = JSON.parse(fs.readFileSync(packageFilePath, 'utf8')); @@ -19,22 +19,42 @@ function updatePackageFile() { } } - var defaultPackageContents = JSON.parse(fs.readFileSync(packageDefaultFilePath, 'utf8')); - var packageContents = { ...oldPackageContents, ...defaultPackageContents, dependencies: { ...oldPackageContents.dependencies, ...defaultPackageContents.dependencies } }; + const defaultPackageContents = JSON.parse(fs.readFileSync(packageDefaultFilePath, 'utf8')); + const packageContents = { ...oldPackageContents, ...defaultPackageContents, dependencies: { ...oldPackageContents.dependencies, ...defaultPackageContents.dependencies } }; fs.writeFileSync(packageFilePath, JSON.stringify(packageContents, null, 2)); } exports.updatePackageFile = updatePackageFile; +exports.supportedPackageManager = [ + 'npm', + 'cnpm', + 'pnpm', + 'yarn', +]; + function installAll() { - var prod = global.env !== 'development'; - var command = 'npm install'; + const prod = global.env !== 'development'; + let command = 'npm install'; try { fs.accessSync(path.join(modulesPath, 'nconf/package.json'), fs.constants.R_OK); - var packageManager = require('nconf').get('package_manager'); - if (packageManager === 'yarn') { - command = 'yarn'; + const supportedPackageManagerList = exports.supportedPackageManager; // load config from src/cli/package-install.js + const packageManager = require('nconf').get('package_manager'); + if (supportedPackageManagerList.indexOf(packageManager) >= 0) { + switch (packageManager) { + case 'yarn': + command = 'yarn'; + break; + case 'pnpm': + command = 'pnpm install'; + break; + case 'cnpm': + command = 'cnpm install'; + break; + default: + break; + } } } catch (e) { // ignore @@ -63,13 +83,13 @@ function preserveExtraneousPlugins() { return; } - var isPackage = /^nodebb-(plugin|theme|widget|reward)-\w+/; - var packages = fs.readdirSync(modulesPath).filter(function (pkgName) { + const isPackage = /^nodebb-(plugin|theme|widget|reward)-\w+/; + const packages = fs.readdirSync(modulesPath).filter(function (pkgName) { return isPackage.test(pkgName); }); - var packageContents = JSON.parse(fs.readFileSync(packageFilePath, 'utf8')); + const packageContents = JSON.parse(fs.readFileSync(packageFilePath, 'utf8')); - var extraneous = packages + const extraneous = packages // only extraneous plugins (ones not in package.json) which are not links .filter(function (pkgName) { const extraneous = !packageContents.dependencies.hasOwnProperty(pkgName); @@ -79,7 +99,7 @@ function preserveExtraneousPlugins() { }) // reduce to a map of package names to package versions .reduce(function (map, pkgName) { - var pkgConfig = JSON.parse(fs.readFileSync(path.join(modulesPath, pkgName, 'package.json'), 'utf8')); + const pkgConfig = JSON.parse(fs.readFileSync(path.join(modulesPath, pkgName, 'package.json'), 'utf8')); map[pkgName] = pkgConfig.version; return map; }, {}); diff --git a/src/cli/upgrade-plugins.js b/src/cli/upgrade-plugins.js index 7a874d5e7d..8f2c240eea 100644 --- a/src/cli/upgrade-plugins.js +++ b/src/cli/upgrade-plugins.js @@ -1,28 +1,30 @@ 'use strict'; -var async = require('async'); -var prompt = require('prompt'); -var request = require('request'); -var cproc = require('child_process'); -var semver = require('semver'); -var fs = require('fs'); -var path = require('path'); -var nconf = require('nconf'); +const async = require('async'); +const prompt = require('prompt'); +const request = require('request'); +const cproc = require('child_process'); +const semver = require('semver'); +const fs = require('fs'); +const path = require('path'); +const nconf = require('nconf'); -var paths = require('./paths'); +const paths = require('./paths'); +const packageManager = nconf.get('package_manager'); -var packageManager = nconf.get('package_manager'); -var packageManagerExecutable = packageManager === 'yarn' ? 'yarn' : 'npm'; -var packageManagerInstallArgs = packageManager === 'yarn' ? ['add'] : ['install', '--save']; +const supportedPackageManagerList = require('./package-install').supportedPackageManager; // load config from src/cli/package-install.js + +let packageManagerExecutable = supportedPackageManagerList.indexOf(packageManager) >= 0 ? packageManager : 'npm'; +const packageManagerInstallArgs = packageManager === 'yarn' ? ['add'] : ['install', '--save']; if (process.platform === 'win32') { packageManagerExecutable += '.cmd'; } -var dirname = paths.baseDir; +const dirname = paths.baseDir; function getModuleVersions(modules, callback) { - var versionHash = {}; + const versionHash = {}; async.eachLimit(modules, 50, function (module, next) { fs.readFile(path.join(dirname, 'node_modules', module, 'package.json'), { encoding: 'utf-8' }, function (err, pkg) { @@ -53,8 +55,8 @@ function getInstalledPlugins(callback) { return callback(err); } - var isNbbModule = /^nodebb-(?:plugin|theme|widget|rewards)-[\w-]+$/; - var checklist; + const isNbbModule = /^nodebb-(?:plugin|theme|widget|rewards)-[\w-]+$/; + payload.files = payload.files.filter(function (file) { return isNbbModule.test(file); @@ -75,7 +77,7 @@ function getInstalledPlugins(callback) { }); // Whittle down deps to send back only extraneously installed plugins/themes/etc - checklist = payload.deps.filter(function (pkgName) { + const checklist = payload.deps.filter(function (pkgName) { if (payload.bundled.includes(pkgName)) { return false; } @@ -119,7 +121,7 @@ function checkPlugins(standalone, callback) { version: getCurrentVersion, }), function (payload, next) { - var toCheck = Object.keys(payload.plugins); + const toCheck = Object.keys(payload.plugins); if (!toCheck.length) { process.stdout.write(' OK'.green + ''.reset); @@ -141,9 +143,9 @@ function checkPlugins(standalone, callback) { body = [body]; } - var current; - var suggested; - var upgradable = body.map(function (suggestObj) { + let current; + let suggested; + const upgradable = body.map(function (suggestObj) { current = payload.plugins[suggestObj.package]; suggested = suggestObj.version; @@ -164,7 +166,7 @@ function checkPlugins(standalone, callback) { } function upgradePlugins(callback) { - var standalone = false; + let standalone = false; if (typeof callback !== 'function') { callback = function () {}; standalone = true; @@ -203,7 +205,7 @@ function upgradePlugins(callback) { if (['y', 'Y', 'yes', 'YES'].includes(result.upgrade)) { console.log('\nUpgrading packages...'); - var args = packageManagerInstallArgs.concat(found.map(function (suggestObj) { + const args = packageManagerInstallArgs.concat(found.map(function (suggestObj) { return suggestObj.name + '@' + suggestObj.suggested; })); diff --git a/src/plugins/install.js b/src/plugins/install.js index eebb384b90..dace50b10e 100644 --- a/src/plugins/install.js +++ b/src/plugins/install.js @@ -14,7 +14,8 @@ const pubsub = require('../pubsub'); const statAsync = util.promisify(fs.stat); -const packageManager = nconf.get('package_manager') === 'yarn' ? 'yarn' : 'npm'; +const supportedPackageManagerList = require('../cli/package-install').supportedPackageManager; // load config from src/cli/package-install.js +const packageManager = supportedPackageManagerList.indexOf(nconf.get('package_manager')) >= 0 ? nconf.get('package_manager') : 'npm'; let packageManagerExecutable = packageManager; const packageManagerCommands = { yarn: { @@ -25,6 +26,14 @@ const packageManagerCommands = { install: 'install', uninstall: 'uninstall', }, + cnpm: { + install: 'install', + uninstall: 'uninstall', + }, + pnpm: { + install: 'install', + uninstall: 'uninstall', + }, }; if (process.platform === 'win32') {