-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdebug.js
58 lines (48 loc) · 1.31 KB
/
debug.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
'use strict';
const path = require('path');
const fs = require('fs-extra');
const sanitize = require('sanitize-filename');
const map = require('./map');
/**
* Writes the passed tree to disk at the root of
* the project.
*
* @example
* const myTree = someBroccoliPlugin('lib');
*
* myTree = debug(myTree);
*
* module.exports = myTree;
*
* @param {String|Object} tree The input tree to debug.
* @param {Object} options
* @property {String} options.name The name of directory you want to write to
*/
module.exports = function debug(tree, options) {
return map(tree, (contents, relativePath) => {
if (typeof options === 'string') {
options = {
name: options
};
}
if (!options) {
options = {};
}
if (!options.name && tree) {
options.name = sanitize(tree.toString());
}
if (!options.name) {
throw Error('Must pass folder name to write to. stew.debug(tree, { name: "foldername" })');
}
let debugDir;
if (options.dir) {
debugDir = options.dir + '/' + options.name;
} else {
debugDir = './DEBUG-' + options.name;
}
let debugPath = path.resolve(path.join(debugDir, relativePath));
fs.mkdirsSync(path.dirname(debugPath));
fs.writeFileSync(debugPath, contents);
return contents;
});
};