-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
executable file
·91 lines (73 loc) · 1.98 KB
/
utils.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
var marked = require('marked');
var jade = require('jade');
var stylus = require('stylus');
var fs = require('fs');
var path = require('path');
var cheerio = require('cheerio');
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false
});
function deleteFolderRecursive (path) {
var files = [];
if( fs.existsSync(path) ) {
files = fs.readdirSync(path);
files.forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
/**
* Look ma, it's cp -R.
* @param {string} src The path to the thing to copy.
* @param {string} dest The path to the new copy.
*/
var copyRecursiveSync = function(src, dest) {
var exists = fs.existsSync(src);
var stats = exists && fs.statSync(src);
var isDirectory = exists && stats.isDirectory();
if (exists && isDirectory) {
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function(childItemName) {
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
} else {
fs.linkSync(src, dest);
}
};
function mkDirIfNotExists (dirPath) {
fs.existsSync(dirPath) || fs.mkdirSync(dirPath);
}
function object_extend () {
var args = Array.prototype.slice.call(arguments);
var obj = args.shift();
args.forEach(function(o) {
Object.keys(o).forEach(function(k) {
obj[k] = o[k];
});
});
return obj;
}
module.exports = {
md : marked,
jade : jade,
stylus : stylus,
copyRecursiveSync : copyRecursiveSync,
deleteFolderRecursive : deleteFolderRecursive,
mkDirIfNotExists : mkDirIfNotExists,
extend : object_extend,
cheerio : cheerio
};