-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·138 lines (130 loc) · 4.58 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const childProcess = require('child_process')
const path = require('path')
const babelJest = require('babel-jest')
function loadConfig (filePath, jestConfig) {
// Default Config
const config = {
application: 'ruby',
args: {
runner: undefined,
windowsRunner: undefined,
transformer: path.join(__dirname, 'erb_transformer.rb'),
engine: 'erb',
delimiter: '__JEST_ERB_TRANSFORMER__'
},
timeout: 5000,
stdio: ['pipe', 'pipe', 'pipe'],
babelConfig: false
}
// User options
const userOptions = {
application: {
tester: new RegExp(`^(rails|${config.application})$`),
applyToConfig: value => {
if (/^win/.test(process.platform) && value === 'rails') {
config.args.runner = 'bin\\rails'
config.args.windowsRunner = 'runner'
} else if (value === 'rails') {
config.application = 'bin/rails'
config.args.runner = 'runner'
} else {
config.args.application = value
}
}
},
engine: {
tester: new RegExp(`^(erubi|${config.args.engine})$`),
applyToConfig: value => {
config.args.engine = value
}
},
timeout: {
tester: { test: value => /^(\d+(?:\.\d*)?)$/.test(value.toString()) },
applyToConfig: userTimeout => {
config.timeout = parseInt(userTimeout)
}
},
babelConfig: {
tester: { test: value => ['boolean', 'string', 'object'].includes(typeof value) },
applyToConfig: userBabelConfig => {
if (userBabelConfig.toString() === 'true') {
config.babelConfig = {}
} else if (typeof userBabelConfig === 'string') {
config.babelConfig = {
configFile: userBabelConfig
}
} else if (typeof userBabelConfig === 'object') {
config.babelConfig = userBabelConfig
} else {
config.babelConfig = false
}
}
}
}
// Get jest v27 config
const configObj = jestConfig.config ? jestConfig.config : jestConfig
// Load user config
const erbTransformers = configObj.transform.filter(e => e[1] === __filename)
const userConfig = erbTransformers.find(e => (new RegExp(e[0])).test(filePath))[2]
if (userConfig === undefined) {
console.warn('WARNING - User Configuration could not be loaded, please check configuration is correct and report to the maintainers!')
} else {
// Apply user config
for (const [key, value] of Object.entries(userConfig)) {
const selectedOption = userOptions[key]
if (selectedOption === undefined) {
console.warn(`WARNING - User Configuration: "${key}" is not a valid configuration key and will be ignored!`)
} else {
const isValidValue = selectedOption.tester.test(value)
if (isValidValue) {
selectedOption.applyToConfig(value)
} else {
console.warn(`WARNING - User Configuration: "${key}": "${value}" is not a valid "${key}" value, using default value instead!`)
}
}
}
}
return config
}
function bufferToString (transformerOutput, delimiter) {
const stringOutput = transformerOutput.toString()
const fileContentRegex = new RegExp(`${delimiter}([\\s\\S]*)${delimiter}`)
return stringOutput.match(fileContentRegex)[1]
}
function erbTransformer (fileContent, filePath, config) {
const child = childProcess.spawnSync(
config.application,
Object.values(config.args).filter(e => e !== undefined),
{
timeout: config.timeout,
stdio: config.stdio,
input: fileContent
}
)
if (child.status !== 0) {
if (child.error && child.error.code === 'ETIMEDOUT') {
throw new Error(`Compilation of '${filePath}' timed out after ${config.timeout}ms!`)
} else {
throw new Error(`Error compiling '${filePath}', status: '${child.status}', signal: '${child.signal}', error: ${child.stderr ? child.stderr.toString() : 'undefined'}`)
}
}
const compiledFile = bufferToString(child.stdout, config.args.delimiter)
return compiledFile
}
function processFile (fileContent, filePath, config) {
let processedContent = String(erbTransformer(fileContent, filePath, config))
if (config.babelConfig) {
const babelTransformer = babelJest.default.createTransformer(config.babelConfig)
const transformOptions = { config: process.cwd() }
processedContent = babelTransformer.process(processedContent, filePath, transformOptions).code
}
return processedContent
}
module.exports = {
process (fileContent, filePath, jestConfig) {
const config = loadConfig(filePath, jestConfig)
return {
code: processFile(fileContent, filePath, config)
}
}
}