forked from DispatchMe/mgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages.js
executable file
·290 lines (237 loc) · 8.46 KB
/
packages.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
var Packages = module.exports = {};
var _ = require('lodash'),
fs = require('fs-extra'),
path = require('path'),
shell = require('shelljs');
// We rely on system git command and its configuration
// In this way it is possible to pick up .netrc
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git');
shell.exit(1);
}
var ROOT_DIR = process.cwd();
var PACKAGE_DIR = ROOT_DIR + '/packages';
function resolvePath(string) {
if (string.substr(0, 1) === '~') {
var homedir = (process.platform.substr(0, 3) === 'win') ? process.env.HOMEPATH : process.env.HOME;
string = homedir + string.substr(1);
} else if (string.substr(0, 1) !== '/') {
string = process.cwd() + '/' + string;
}
return path.resolve(string);
}
/**
* Configure the package directory.
* @param packageDir The directory to copy the packages to.
* Defaults to cwd/packages
*/
Packages.config = function (packageDir) {
PACKAGE_DIR = resolvePath(packageDir);
};
/**
* Load the packages file.
* @param [file] Defaults to cwd/git-packages.json
* @param callback
*/
Packages.fromFile = function (file, callback) {
if (_.isFunction(file)) {
callback = file;
file = null;
}
var packagesFile = file || process.cwd() + '/git-packages.json';
fs.readJson(packagesFile, function (err, packages) {
// XXX check the packages schema
if (callback) callback(err, packages);
});
};
// Create a packages document per git url.
// { gitRepo: { version: { packageName: packagePath, .. }, ... } }
var getPackagesDict = function (packages) {
var resolvedPackages = {};
_.forOwn(packages, function (definition, packageName) {
if (!definition) return;
var git = definition.git;
if (!git) return;
var repo = resolvedPackages[git] = resolvedPackages[git] || {};
// If no branch provided - checkout origin HEAD and
// don't show 'detached HEAD' notification
branch = definition.branch || '-q origin';
// If no version provided - pull HEAD
version = definition.version || 'HEAD';
repo[branch] = repo[branch] || {};
repo[branch][version] = repo[branch][version] || {};
repo[branch][version][packageName] = definition.path || '';
});
return resolvedPackages;
};
// Test if path exists and fail with error if it is not exists
var checkPathExist = function (path, errorMessage) {
if (!shell.test('-e', path)) {
shell.echo('Error: ' + errorMessage);
shell.exit(1);
}
};
/**
* Get the Meteor name of the package being added
* @param dest - path to the package
* @return string
*/
var getPackageName = function (dest) {
var packageJsPath = dest + '/package.js',
packageName = false,
lines = [];
checkPathExist(packageJsPath, 'package.js file not found.');
packageJsContents = fs.readFileSync(packageJsPath, 'utf8');
lines = packageJsContents.split(/\n/g);
for (line in lines) {
if (/name/.test(lines[line]) && (lines[line].match(/:/g).length >= 2)) {
packageName = lines[line].split(/:(.+)?/)[1].trim().replace(/\"/g, '').replace(/\'/g, '').replace(/,/g, '');
break;
}
}
return packageName;
};
/**
* Loop through the list of packages and add them to the Meteor app
* @param packagesToAddToMeteor - list of packages
*/
var addPackagesToMeteor = function(packagesToAddToMeteor) {
shell.cd(ROOT_DIR);
shell.echo("Adding the cloned packages to the Meteor app...");
for (index in packagesToAddToMeteor) {
package = packagesToAddToMeteor[index];
shell.echo("Adding package '" + package + "'...");
if (shell.exec('meteor add ' + package, {
silent: false
}).code !== 0) {
shell.echo("Error: Package '" + package + "' could not be added to the Meteor app.");
}
}
};
/**
* Create a git ignore in the package directory for the packages.
* @param packages
* @param {Function} callback
*/
Packages.ensureGitIgnore = function (packages, callback) {
var filePath = PACKAGE_DIR + '/.gitignore';
fs.ensureFileSync(filePath);
fs.readFile(filePath, 'utf8', function (err, gitIgnore) {
// Append packages to the gitignore
_.forOwn(packages, function (def, packageName) {
// Convert colons in package names to dashes for Windows
packageName = packageName.replace(/:/g, '-');
if (packageName === 'token' || gitIgnore.indexOf(packageName) > -1) return;
gitIgnore += packageName + '\n';
});
fs.writeFile(filePath, gitIgnore, callback);
});
};
/**
* Symlink local directories to the packages directory.
* @param packages The packages to symlink.
* @param {Function} callback
*/
Packages.link = function (packages, callback) {
shell.mkdir('-p', PACKAGE_DIR);
var dirLinked = _.after(_.keys(packages).length, callback);
_.forOwn(packages, function (def, packageName) {
if (!def.path || !packageName) return;
// Convert colons in package names to dashes for Windows
packageName = packageName.replace(/:/g, '-');
var dest = PACKAGE_DIR + '/' + packageName;
shell.rm('-fr', dest);
var src = resolvePath(def.path);
checkPathExist(src, 'Cannot find package ' + packageName + ' at ' + src);
shell.ln('-s', src, dest);
checkPathExist(src, 'Link failed for ' + dest);
dirLinked();
});
};
/**
* Clones repositories and copy the packages.
* @param packages The packages to load.
* @param addToGlobals whether or not to automatically add the packages with meteor add
* @param {Function} callback
*/
Packages.load = function (packages, addToGlobals, callback) {
shell.mkdir('-p', PACKAGE_DIR);
// Create a temp directory to store the tarballs
var tempDir = PACKAGE_DIR + '/temp';
shell.rm('-fr', tempDir);
shell.mkdir('-p', tempDir);
shell.cd(tempDir);
var packagesToAddToMeteor = [];
var resolvedPackages = getPackagesDict(packages);
var repoDirIndex = 0;
_.forOwn(resolvedPackages, function (repoPackages, gitRepo) {
var repoDir = tempDir + '/' + repoDirIndex;
// Change to the temp directory before cloning the repo
shell.cd(tempDir);
if (shell.exec('git clone ' + gitRepo + ' ' + repoDirIndex, {
silent: true
}).code !== 0) {
shell.echo('Error: Git clone failed: ' + gitRepo);
shell.exit(1);
}
// Change to the repo directory
shell.cd(repoDir);
repoDirIndex++;
_.forOwn(repoPackages, function (branchPackages, branch) {
if (shell.exec('git checkout -f ' + branch, {
silent: false
}).code !== 0) {
shell.echo('Error: Git checkout branch failed for ' + gitRepo + '@' + version);
shell.exit(1);
}
_.forOwn(branchPackages, function (storedPackages, version) {
_.forOwn(storedPackages, function (src, packageName) {
if (shell.exec('git reset --hard ' + version, {
silent: false
}).code !== 0) {
shell.echo('Error: Git checkout failed for ' + packageName + '@' + version);
shell.exit(1);
}
packageName = packageName.replace(/:/g, '-');
shell.echo('\nProcessing ' + packageName + ' at ' + version);
shell.echo('Cleaning up');
var dest = PACKAGE_DIR + '/' + packageName;
shell.rm('-rf', dest);
src = repoDir + '/' + src + '/';
checkPathExist(src, 'Cannot find package in repository: ' + src);
shell.echo('Copying package');
// Adding the dot after `src` forces it to copy hidden files as well
shell.cp('-rf', src + '.', dest);
checkPathExist(dest, 'Cannot copy package: ' + dest);
shell.echo('Done...\n');
if (addToGlobals) {
if (packageName = getPackageName(dest)) {
packagesToAddToMeteor.push(packageName);
}
}
});
});
});
});
// Remove the temp directory after the packages are copied.
shell.cd(process.cwd());
shell.rm('-fr', tempDir);
// add the packages to meteor
if (addToGlobals) {
addPackagesToMeteor(packagesToAddToMeteor);
}
callback();
};
/**
* Convert git ssh urls to https. This is useful for defining ssh locally and then using .netrc in build tools.
*/
Packages.toHttps = function (packages) {
packages = _.cloneDeep(packages);
_.forOwn(packages, function (definition, packageName) {
if (!definition.git)
throw new Error('No Git url defined for ' + packageName);
var gitUrl = definition.git.substring(definition.git.lastIndexOf("@") + 1, definition.git.lastIndexOf(":"));
definition.git = 'https://' + gitUrl +'/' + definition.git.substring(gitUrl.length + 5);
});
return packages;
};