-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
64 lines (55 loc) · 1.64 KB
/
index.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
'use strict'
const vm = require('vm')
const coffee = require('coffee-script')
const sysPath = require('path')
const keys = require('lodash').keys
const pick = require('lodash').pick
class JsenvCompiler {
constructor(config) {
if(config == null) config = {}
const plugin = config.plugins && config.plugins.jsenv || {}
}
compile(params) {
const data = params.data
const path = params.path
let sandbox = {
module: {
exports: undefined
},
require: require
}
let compiled
let result
try {
if(sysPath.extname(path) == '.coffeeenv') {
compiled = coffee.compile(data,{ bare: true })
} else {
compiled = data
}
let context = new vm.createContext(sandbox)
let script = new vm.Script("module.exports = "+compiled)
script.runInContext(context)
let envHash
let mappedEnv
if(typeof sandbox.module.exports == "function") {
mappedEnv = sandbox.module.exports(process.env)
} else {
envHash = sandbox.module.exports
let matchingEnvVars = pick(process.env,keys(sandbox.module.exports))
mappedEnv = Object.assign({},envHash,matchingEnvVars)
}
result = "module.exports = " + JSON.stringify(mappedEnv)
} catch(e) {
console.log(e)
return Promise.reject(e)
} finally {
return Promise.resolve(result)
}
}
}
JsenvCompiler.prototype.brunchPlugin = true
JsenvCompiler.prototype.type = 'javascript'
JsenvCompiler.prototype.extension = /(coffeeenv|jsenv)/
JsenvCompiler.prototype.pattern = /.(coffeeenv|jsenv)$/
JsenvCompiler.prototype.defaultEnv = "*"
module.exports = JsenvCompiler