-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-readme-example.js
53 lines (45 loc) · 1.45 KB
/
test-readme-example.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
'use strict'
const fs = require('fs')
const console = require('console')
const standard = require('standard')
// Load code from stdin.
let code
while (code === undefined) {
try {
code = fs.readFileSync(0, 'utf-8')
break
} catch (e) {
if (e.code === 'EAGAIN') continue
else throw e
}
}
const codeBeforeTransformations = code
// Replace requires for the npm-published name with local file
code = code
.replace(/require\(("partser"|'partser')\)/g, 'require(\'./index.js\')')
// If the first line doesn't call require, add it.
if (!code.match(/^.*require\(.*\n/)) {
code = 'const p = require(\'./index.js\')\n' + code
}
// Make console.log etc log full depth of every object (instead of their
// default depth 2).
Object.assign(console, new console.Console({
stdout: process.stdout,
stderr: process.stderr,
inspectOptions: { depth: Infinity }
}))
// Need to eval rather than new Function() because the code needs access to
// current scope, to use require etc.
eval(code) // eslint-disable-line no-eval
// Check it through standardjs
const lint = standard.lintTextSync(codeBeforeTransformations, { globals: ['p'] })
if (lint.errorCount > 0 || lint.warningCount > 0) {
for (const result of lint.results) {
for (const m of result.messages) {
console.error(`standard.js complaint at line ${m.line}, column ${m.column}:`)
console.error(` ${m.message}`)
console.error(` (rule '${m.ruleId}')`)
}
}
process.exit(0xBAD)
}