-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-install.js
More file actions
executable file
·82 lines (66 loc) · 1.85 KB
/
auto-install.js
File metadata and controls
executable file
·82 lines (66 loc) · 1.85 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env node
'use strict';
var fs = require('fs'),
path = require('path');
function readJSONFile ( filepath ) {
return JSON.parse( fs.readFileSync( filepath, { encoding: 'utf8' }) );
}
function getDir (pkgName, defaultDir) {
try {
return readJSONFile( '.' + pkgName + 'rc').directory;
} catch (err) {
return defaultDir;
}
}
function pathExists (spath) {
try {
return fs.statSync( path.join(__dirname, spath) );
} catch(err) {
return false;
}
}
function checkDependencies (dependencies, dirlib) {
for( var dependence in dependencies ) {
if( !pathExists( path.join( dirlib, dependence ) ) ) {
return false;
}
}
return true;
}
function missingDependencies (json, dirlib) {
if( json.dependencies && !checkDependencies(json.dependencies, dirlib) ) {
return true;
}
if( json.devDependencies && !checkDependencies(json.devDependencies, dirlib) ) {
return true;
}
return false;
}
function nitroVersionOK () {
var currentVersion = readJSONFile('package.json').devDependencies.nitro,
installedVersion;
try {
installedVersion = readJSONFile('node_modules/nitro/package.json').version;
} catch(err) {
return false;
}
return currentVersion.replace(/^[~^]/, '') === installedVersion;
}
function needsInstall () {
var dir = {
npm: getDir('npm', 'node_modules'),
bower: getDir('bower', 'bower_components')
},
install = [];
if( !nitroVersionOK() || !pathExists(dir.npm) || missingDependencies( require('./package'), dir.npm ) ) {
install.push('npm');
}
if( pathExists('./bower.json') && missingDependencies( require('./bower'), dir.bower ) ) {
install.push('bower');
}
return install;
}
needsInstall().forEach(function (libname) {
console.log('\nrequired', libname + ' install\n');
require('child_process').execSync(libname + ' install');
});