-
Notifications
You must be signed in to change notification settings - Fork 20
/
check-npm-package.js
44 lines (37 loc) · 1.6 KB
/
check-npm-package.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Adapted from https://github.com/tmeasday/check-npm-versions
*/
import fs from 'fs';
import semver from 'semver';
import colors from 'colors';
import logger from './logger';
import ImportPathHelpers from './helpers/import-path-helpers';
export default function checkNpmPackage(packageWithVersion) {
const [ packageName, packageVersion ] = packageWithVersion.split('@');
if (!verifyPackageExists(packageName, packageVersion)) {
return false;
}
return checkNpmVersion(packageName, packageVersion);
}
function verifyPackageExists(packageName, packageVersion) {
const packagePath = `${ImportPathHelpers.basePath}/node_modules/${packageName}`;
const doesPackageExist = fs.existsSync(packagePath);
if (!doesPackageExist) {
logger.error(colors.red.bold(`Error checking npm module: ${packageName}@${packageVersion} (required by nathantreid:css-modules): module not found. Please ensure you have installed the module; here is the command:\n meteor npm install ${packageName} --save-dev\n`));
}
return doesPackageExist;
}
function checkNpmVersion(name, version) {
try {
const installedVersion = require(`${name}/package.json`).version;
if (semver.satisfies(installedVersion, version)) {
return true;
} else {
logger.warn(colors.yellow.bold(`WARNING: version mismatch for ${name}; installed version is ${installedVersion}, but version ${version} is required by nathantreid:css-modules)`));
return true;
}
} catch (e) {
logger.error(colors.red.bold(`Error checking package: ${name}@${version} (required by nathantreid:css-modules): ${e.message}`));
return false;
}
}