-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathinstallation-checker.js
68 lines (57 loc) · 1.88 KB
/
installation-checker.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
var debug = require('debug')('ember-cli:installation-checker');
var fs = require('fs');
var path = require('path');
var SilentError = require('silent-error');
function existsSync(path) {
try {
fs.accessSync(path);
return true;
}
catch (e) {
return false;
}
}
module.exports = InstallationChecker;
function InstallationChecker(options) {
this.project = options.project;
}
/**
* Check if npm directories are present,
* and raise an error message with instructions on how to proceed.
*
* If some of these package managers aren't being used in the project
* we just ignore them. Their usage is considered by checking the
* presence of your manifest files: package.json for npm.
*/
InstallationChecker.prototype.checkInstallations = function() {
var commands = [];
if (this.usingNpm() && this.npmDependenciesNotPresent()) {
debug('npm dependencies not installed');
commands.push('`npm install`');
}
if (commands.length) {
var commandText = commands.join(' and ');
throw new SilentError('No dependencies installed. Run ' + commandText + ' to install missing dependencies.');
}
};
function hasDependencies(pkg) {
return (pkg.dependencies && pkg.dependencies.length) ||
(pkg.devDependencies && pkg.devDependencies.length);
}
function readJSON(path) {
try {
return JSON.parse(fs.readFileSync(path).toString());
} catch (e) {
throw new SilentError('InstallationChecker: Unable to parse: ' + path);
}
}
InstallationChecker.prototype.hasNpmDeps = function() {
return hasDependencies(readJSON(path.join(this.project.root, 'package.json')));
};
InstallationChecker.prototype.usingNpm = function() {
return existsSync(path.join(this.project.root, 'package.json')) && this.hasNpmDeps();
};
InstallationChecker.prototype.npmDependenciesNotPresent = function() {
return !existsSync(this.project.nodeModulesPath);
};