This repository has been archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathignore.js
164 lines (158 loc) · 4.66 KB
/
ignore.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var path = require("path");
var fs = require("fs-promise");
var when = require("when");
var Minimatch = require("minimatch").Minimatch;
var utils = require("./utils");
var console = utils.console;
/**
* Look for .jpmignore in the given directory and use it to filter files
* and sub-directories fallback to use default filter rules if .jpmignore
* doesn't exist.
*
* @param {String} dir
* @param {Object} options
* @return {Promise}
*/
function ignore(dir, options) {
var jpmignore = path.join(dir, ".jpmignore");
var defaultRules = ["*.zip", ".*", "test/", ".jpmignore", "*.xpi"];
return utils.getManifest({addonDir: options.addonDir})
.then(function(manifest) {
return fs.exists(jpmignore);
})
.then(function(exists) {
if (exists) {
if (options.verbose) {
console.log(".jpmignore found");
}
return fs.stat(jpmignore)
.then(function(stat) {
if (stat.isFile()) {
return fs.readFile(jpmignore)
.then(function(data) {
return data.toString().replace(/[\r\n]/, "\n").split("\n");
});
} else {
console.warn(
".jpmignore is not a file, fallback to use " +
"default filter rules");
return defaultRules;
}
});
} else {
if (options.verbose) {
console.warn(
".jpmignore does not exist, fallback to use default filter rules");
}
return defaultRules;
}
})
.then(function(lines) {
// Add "manifest.json" to always exclude it from the xpi (See
// https://github.com/mozilla-jetpack/jpm/pull/566 for rationale),
// but ensure the "webextension/manifest.json", that is used for the
// webextension embedded in an SDK hybrid add-on, is always included
// (See https://github.com/mozilla-jetpack/jpm/pull/578 for rationale).
lines = ["manifest.json", "!webextension/manifest.json"].concat(lines);
var rules = lines.filter(function(e) {
// exclude blank lines and comments
return !/^\s*(#|$)/.test(e);
});
// http://git-scm.com/docs/gitignore
// the last matching pattern decides the outcome
// reverse rules and break at first matched rule when filtering
return rules.reverse().map(function(rule) {
return new Minimatch(rule, {
matchBase: true,
dot: true,
flipNegate: true
});
});
})
.then(function(rules) {
return listdir(dir, rules, dir, true);
});
}
module.exports = ignore;
/**
* filter a dir based on filter rules
*
* @param {String} dir
* @param {Array} rules
* @param {Array} root
* @param {Boolean} included
* @return {Promise}
*/
function listdir(dir, rules, root, included) {
return fs.readdir(dir)
.then(function(files) {
return when.all(files.map(function(f) {
f = path.join(dir, f);
return when(fs.stat(f), function(stat) {
return {
path: f,
isDirectory: stat.isDirectory()
};
});
}));
})
.then(function(arr) {
var files = [];
var subdirs = [];
arr.forEach(function(e) {
if (e.isDirectory) {
subdirs.push(e.path);
} else {
files.push(e.path);
}
});
files = files.filter(function(f) {
return filter(f, rules, root, false, included);
});
return when.all(subdirs.map(function(d) {
return listdir(d, rules, root, filter(d, rules, root, true, included));
}))
.then(function(list) {
list = list.reduce(function(ret, i) {
return ret.concat(i);
}, files);
// include current dir only if at least one of its children is included
if (list.length > 0) {
list.push(dir);
}
return list;
});
});
}
/**
* check if a given file or dir should be kept
*
* @param {String} p
* @param {Array} rules
* @param {String} root
* @param {Boolean} isDirectory
* @param {Boolean} included
* @return {Boolean}
*/
function filter(p, rules, root, isDirectory, included) {
p = path.relative(root, p);
for (var i in rules) {
var rule = rules[i];
if (isDirectory) {
if (rule.match(p) || rule.match("/" + p) ||
rule.match(p + "/") || rule.match("/" + p + "/")
) {
return rule.negate;
}
} else {
if (rule.match(p) || rule.match("/" + p)) {
return rule.negate;
}
}
}
return included;
}