forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.js
98 lines (81 loc) · 2.6 KB
/
project.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
var fs = require('fs');
var path = require('path');
var _ = require('underscore');
var files = require('./files.js');
var project = exports;
_.extend(exports, {
_get_lines: function (file) {
var raw = fs.readFileSync(file, 'utf8');
var lines = raw.split(/\r*\n\r*/);
// strip blank lines at the end
while (lines.length) {
var line = lines[lines.length - 1];
if (line.match(/\S/))
break;
lines.pop();
}
return lines;
},
_get_packages_lines: function (app_dir) {
return project._get_lines(path.join(app_dir, '.meteor', 'packages'));
},
_trim_line: function (line) {
var match = line.match(/^([^#]*)#/);
if (match)
line = match[1];
line = line.replace(/^\s+|\s+$/g, ''); // leading/trailing whitespace
return line;
},
_write_packages: function (app_dir, lines) {
fs.writeFileSync(path.join(app_dir, '.meteor', 'packages'),
lines.join('\n') + '\n', 'utf8');
},
// Package names used by this project.
get_packages: function (app_dir) {
var ret = [];
// read from .meteor/packages
_.each(project._get_packages_lines(app_dir), function (line) {
line = project._trim_line(line);
if (line !== '')
ret.push(line);
});
return ret;
},
_meteorReleaseFilePath: function (appDir) {
return path.join(appDir, '.meteor', 'release');
},
getMeteorReleaseVersion: function (appDir) {
var releasePath = project._meteorReleaseFilePath(appDir);
try {
var lines = project._get_lines(releasePath);
} catch (e) {
// This is a legacy app with no '.meteor/release'
// file.
return null;
}
return project._trim_line(lines[0]);
},
writeMeteorReleaseVersion: function (appDir, release) {
var releasePath = project._meteorReleaseFilePath(appDir);
fs.writeFileSync(releasePath, release + '\n');
},
add_package: function (app_dir, name) {
var lines = project._get_packages_lines(app_dir);
// detail: if the file starts with a comment, try to keep a single
// blank line after the comment (unless the user removes it)
var current = project.get_packages(app_dir);
if (_.contains(current, name))
return;
if (!current.length && lines.length)
lines.push('');
lines.push(name);
project._write_packages(app_dir, lines);
},
remove_package: function (app_dir, name) {
// XXX assume no special regexp characters
var lines = _.reject(project._get_packages_lines(app_dir), function (line) {
return project._trim_line(line) === name;
});
project._write_packages(app_dir, lines);
}
});