This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
npm.js
116 lines (106 loc) · 3.36 KB
/
npm.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var fs = require('fs');
var npm = require('npm');
var pathExtra = require('path-extra');
var request = require('request-json-light');
var logger = require('./logger');
var nodeHelpers = require('./node');
var configHelpers = require('../config');
module.exports = npmHelpers = {
/**
* Directory where node modules will be installed.
*/
initialWd: '',
/**
* Fetch given app source and dependencies from NPM registry.
*
* Config file is ~/.cozy-light/.config
*
* @param {String} app App to fetch from NPM.
* @param {Function} callback Callback to run once work is done.
*/
install: function (app, callback) {
var options = {
dir: configHelpers.getModulesPath(),
prefix: ''
};
npm.load(options, function () {
npm.commands.install([app], callback);
});
},
/**
* Link given app source and dependencies from local file system.
*
* @param {String} app Path to the module to link.
* @param {Function} callback Callback to run once work is done.
*/
link: function (app, callback) {
var options = {
local: true,
dir: configHelpers.getModulesPath(),
prefix: ''
};
npm.load(options, function () {
app = pathExtra.resolve(npmHelpers.initialWd, app);
npm.commands.link([app], callback);
});
},
/**
* Remove application source and dependencies using NPM lib.
*
* @param {String} app App to fetch from NPM.
* @param {Function} callback Callback to run once work is done.
*/
uninstall: function (app, callback) {
var options = {
dir: configHelpers.getModulesPath(),
prefix: ''
};
npm.load(options, function () {
npm.commands.uninstall([app], callback);
});
},
/**
* Fetch application or plugin manifest from an url or a path
*
* @param {String} app App or Plugin name to fetch from url or path.
* @param {Function} callback Termination.
*/
fetchManifest: function (app, callback) {
var appPath = pathExtra.resolve(npmHelpers.initialWd, app);
var manifestPath = pathExtra.join(appPath, 'package.json');
if (fs.existsSync(appPath) && fs.existsSync(manifestPath)) {
fs.readFile(manifestPath, function checkError (err, manifest) {
if (err) {
logger.error(err);
nodeHelpers.invoke(callback, err);
} else {
nodeHelpers.invoke(callback, err, JSON.parse(manifest), 'file');
}
});
} else {
var client = request.newClient('https://raw.githubusercontent.com/');
/* Checking if a branch of the github repo is specified
* within the application path parameter (separated with a @).
*/
if (app.indexOf('@') > 1) {
// if a branch is specified, we build the url with it.
var manifestUrl = app.split('@')[0] + '/' + app.split('@')[1] + '/package.json';
} else {
// if no branch is specified, we build the url with master branch.
var manifestUrl = app + '/master/package.json';
}
console.log(manifestUrl);
client.get(manifestUrl, function (err, res, manifest) {
if (err) {
logger.error(err);
nodeHelpers.invoke(callback, err);
} else if (res.statusCode !== 200) {
logger.error(err);
nodeHelpers.invoke(callback, err);
} else {
nodeHelpers.invoke(callback, err, manifest, 'url');
}
});
}
}
};