Skip to content

Commit

Permalink
feat(util): use child_process to exec npm-view directly
Browse files Browse the repository at this point in the history
avoid use npm api: #191
  • Loading branch information
xuchaoying committed Feb 23, 2021
1 parent f9ff8db commit 8ceaf86
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 225 deletions.
62 changes: 0 additions & 62 deletions packages/svrx-util/__tests__/spec/npm.test.js

This file was deleted.

9 changes: 0 additions & 9 deletions packages/svrx-util/lib/npm/devnull.js

This file was deleted.

58 changes: 0 additions & 58 deletions packages/svrx-util/lib/npm/index.js

This file was deleted.

115 changes: 58 additions & 57 deletions packages/svrx-util/lib/package-manager/package-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ const mkdirp = require('mkdirp');
const fs = require('fs-extra');
const tmp = require('tmp');
const libPath = require('path');
const { fork } = require('child_process');
const semver = require('semver');
const rimraf = require('rimraf');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const npm = require('../npm');

const requireEnsure = (path) => {
delete require.cache[path];
Expand Down Expand Up @@ -143,7 +141,6 @@ class PackageManager {
}

/**
*
* @param {string|undefined} version
* @returns {Promise<void>}
*/
Expand Down Expand Up @@ -213,23 +210,27 @@ class PackageManager {

async getRemotePackages() {
const { packageName, registry } = this;
const task = fork(libPath.join(__dirname, './tasks/view.js'), {
silent: true,
});

try {
return new Promise((resolve, reject) => {
task.on('error', reject);
task.on('message', (ret) => {
if (ret.error) reject(new Error(`install error: package '${packageName}' not found: ${ret.error}`));
else resolve(ret);
});
task.send({
packageName, registry,
});
});
const registryCmd = registry ? `--registry=${registry}` : '';
const versionCmd = `npm view '${packageName}@*' version ${registryCmd} --json`;
const engineCmd = `npm view '${packageName}@*' engines ${registryCmd} --json`;

const { stdout: versionStr, stderr: versionErr } = await exec(versionCmd);
const { stdout: engineStr, stderr: engineErr } = await exec(engineCmd);

if (!versionStr || !engineStr || versionErr || engineErr) {
throw new Error(versionErr || engineErr);
}

const versions = JSON.parse(versionStr);
const engines = JSON.parse(engineStr);
// assume each version contains 'engines' field
return versions.map((version, index) => ({
version,
pattern: engines[index] && engines[index].svrx,
}));
} catch (e) {
throw new Error(`install error: package '${packageName}' not found: ${e.message}`);
throw new Error(`install error: package '${packageName}' not found: ${e}`);
}
}

Expand Down Expand Up @@ -343,44 +344,44 @@ class PackageManager {
}
}

PackageManager.getInstallTask = async ({
packageName, version, root, registry,
}) => {
const tmpPath = tmp.dirSync().name;

const options = {
name: packageName,
nameReal: packageName,
version,
path: tmpPath,
registry,
global: true,
};

const result = await npm.install(options);
const tmpFolder = libPath.join(tmpPath, 'node_modules', packageName);
const destFolder = libPath.join(root, result.version);

fs.copySync(tmpFolder, destFolder, {
dereference: true, // ensure linked folder is copied too
});

return result.version;
};

PackageManager.getViewTask = async ({
packageName, registry,
}) => {
const viewResult = await npm.view([
`${packageName}@*`,
'engines',
], {
registry,
});
return Object.keys(viewResult).map((v) => ({
version: v,
pattern: (viewResult[v].engines && viewResult[v].engines.svrx) || '*',
}));
};
// PackageManager.getInstallTask = async ({
// packageName, version, root, registry,
// }) => {
// const tmpPath = tmp.dirSync().name;
//
// const options = {
// name: packageName,
// nameReal: packageName,
// version,
// path: tmpPath,
// registry,
// global: true,
// };
//
// const result = await npm.install(options);
// const tmpFolder = libPath.join(tmpPath, 'node_modules', packageName);
// const destFolder = libPath.join(root, result.version);
//
// fs.copySync(tmpFolder, destFolder, {
// dereference: true, // ensure linked folder is copied too
// });
//
// return result.version;
// };
//
// PackageManager.getViewTask = async ({
// packageName, registry,
// }) => {
// const viewResult = await npm.view([
// `${packageName}@*`,
// 'engines',
// ], {
// registry,
// });
// return Object.keys(viewResult).map((v) => ({
// version: v,
// pattern: (viewResult[v].engines && viewResult[v].engines.svrx) || '*',
// }));
// };

module.exports = PackageManager;
19 changes: 0 additions & 19 deletions packages/svrx-util/lib/package-manager/tasks/install.js

This file was deleted.

19 changes: 0 additions & 19 deletions packages/svrx-util/lib/package-manager/tasks/view.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/svrx-util/lib/svrx-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ exports.PackageManagerCreator = require('./package-manager');
exports.nameFormatter = require('./name-formatter');
exports.rcFileRead = require('./rc-file-read');
exports.logger = require('./logger');
exports.npm = require('./npm');
exports.c2k = require('./c2k');

0 comments on commit 8ceaf86

Please sign in to comment.