-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
124 lines (111 loc) · 2.79 KB
/
node.js
File metadata and controls
124 lines (111 loc) · 2.79 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
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
/**
* @file node
* @author Cuttle Cong
* @date 2018/2/19
* @description
*/
var cache = require('./lib/cache')
var findNodeModules = require('./lib/findNodeModules')
var nps = require('path')
var md5 = require('md5')
var os = require('os')
var cp = require('child_process')
var fs = require('fs')
var platform = os.platform()
var delimiter = ':'
if (platform === 'win32') {
delimiter = ';'
}
var modulePath = findNodeModules()
var binPath
if (modulePath) {
binPath = nps.join(modulePath, '.bin')
}
function extendedEnv(env, paths = []) {
paths = paths.map(function (p) {
return nps.resolve(p)
})
return Object.assign({}, process.env, {
PATH: paths.concat(
[binPath, (process.env.PATH || '')]
).join(delimiter)
}, env)
}
function getOptions(options) {
return Object.assign({
cache: true,
env: {},
paths: [],
cwd: 'curr' // file | path/to/cwd
}, options)
}
function getFilename(gift) {
var filesMap = gift.filesMap
var path = gift.path
return filesMap[path]
}
function getCwd(cwd, gift) {
var filename = getFilename(gift)
if (typeof cwd !== 'string') {
throw TypeError('[picidae-transformer-exec] options.cwd requires string.')
}
switch(cwd) {
case 'curr':
cwd = process.cwd()
break
case 'file':
cwd = nps.dirname(filename)
break
default:
cwd = nps.resolve(cwd)
}
if (!fs.existsSync(cwd) || !fs.statSync(cwd).isDirectory()) {
console.error('[picidae-transformer-exec] Error: ', cwd, 'is illegal.')
}
return cwd
}
/**
* @description
* Firstly, get filename as cache key from gift when cache is on.
* And calculate the hash by content to hit cache.
* If check the hash is updated, continue. otherwise break
* Then, try to match `<EXEC cmd>` placeholder.
* execute `cmd` and get the stdout to replace the placeholder
*/
function convert(cwd, options, content) {
var hitted = false
var newContent = content.replace(/<<EXEC (.+?)>>/g, function (_, command) {
hitted = true
return cp.execSync(command, {
env: extendedEnv(options.env, options.paths),
cwd: cwd
}).toString()
})
return hitted && newContent
}
exports.markdownTransformer = function (options, gift, require) {
options = getOptions(options)
var cwd = getCwd(options.cwd, gift)
var filename = getFilename(gift)
var content = gift.data
var receivedHash = md5(content)
var cached = cache[filename]
var converted
if (cached && options.cache) {
if (cached.hash === receivedHash) {
console.log('[exec]:', filename, 'cache works!')
return cached.convertedContent
}
}
converted = convert(cwd, options, content)
if (converted) {
if (options.cache) {
cache[filename] = {
hash: receivedHash,
convertedContent: converted
}
}
return converted
}
return gift.data
}