-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathutils.js
More file actions
48 lines (42 loc) · 1.17 KB
/
utils.js
File metadata and controls
48 lines (42 loc) · 1.17 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
var tmp = require("tmp");
var fs = require("fs");
module.exports = {
unindent: function(text) {
var indentation = text.split("\n")
.map(line => {
var match = line.match(/([\s]+)[^\s]+/);
return match && match[1];
})
.find(value => value);
return indentation
? text.replace(new RegExp(`^${indentation}`, "gm"), "")
: text;
},
writeFile: function(path, content) {
return new Promise((resolve, reject) => {
return fs.writeFile(path, content, err => (err ? reject(err) : resolve()));
});
},
withTmpDir: function() {
return new Promise(function(resolve, reject) {
tmp.dir({ unsafeCleanup: true }, function(err, tmpDirPath) {
if (err) {
reject(err);
} else {
resolve(tmpDirPath);
}
});
});
},
assertKeysPresent: function(object, requiredKeys, missingCallback) {
var providedKeys = Object.keys(object || {});
var missingKeys = requiredKeys.filter(key => {
return (
providedKeys.indexOf(key) === -1 || providedKeys[key] === ""
);
});
if (missingKeys.length > 0) {
missingCallback(missingKeys);
}
}
};