-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinvoker.js
144 lines (130 loc) · 4.94 KB
/
invoker.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
139
140
141
142
143
144
const fs = require('fs')
const ospath = require('path')
const asciidoctor = require('@asciidoctor/core')()
const pkg = require('../package.json')
const stdin = require('./stdin')
const DOT_RELATIVE_RX = new RegExp(`^\\.{1,2}[/${ospath.sep.replace('/', '').replace('\\', '\\\\')}]`)
class Invoker {
constructor (options) {
this.options = options
}
async invoke () {
const processArgs = this.options.argv.slice(2)
const { args } = this.options
const { verbose, version, files } = args
if (version || (verbose && processArgs.length === 1)) {
this.showVersion()
process.exit(0)
}
if (files.length === 0 && !this.options.stdin) {
// nothing to do...
this.showHelp()
process.exit(0)
}
Invoker.prepareProcessor(args, asciidoctor)
const options = this.options.options
const failureLevel = options['failure_level']
try {
if (this.options.stdin) {
await Invoker.convertFromStdin(options, args)
} else if (files && files.length > 0) {
Invoker.convertFiles(files, verbose, args['timings'], options)
}
Invoker.exit(failureLevel)
} catch (e) {
if (e && e.name === 'NotImplementedError' && e.message === `asciidoctor: FAILED: missing converter for backend '${options.backend}'. Processing aborted.`) {
console.error(`> Error: missing converter for backend '${options.backend}'. Processing aborted.`)
console.error(`> You might want to require a Node.js package with --require option to support this backend.`)
process.exit(1)
}
throw e
}
}
showHelp () {
if (this.options.args['help'] === 'syntax') {
console.log(fs.readFileSync(ospath.join(__dirname, '..', 'data', 'reference', 'syntax.adoc'), 'utf8'))
} else {
this.options.yargs.showHelp()
}
}
showVersion () {
console.log(this.version())
}
version () {
const releaseName = process.release ? process.release.name : 'node'
return `Asciidoctor.js ${asciidoctor.getVersion()} (Asciidoctor ${asciidoctor.getCoreVersion()}) [https://asciidoctor.org]
Runtime Environment (${releaseName} ${process.version} on ${process.platform})
CLI version ${pkg.version}`
}
/**
* @deprecated Use {#showVersion}. Will be removed in version 4.0.
*/
static printVersion () {
console.log(new Invoker().version())
}
static async readFromStdin () {
return stdin.read()
}
static async convertFromStdin (options, args) {
const data = await Invoker.readFromStdin()
if (args['timings']) {
const timings = asciidoctor.Timings.create()
const instanceOptions = Object.assign({}, options, { timings })
asciidoctor.convert(data, instanceOptions)
timings.printReport(process.stderr, '-')
} else {
asciidoctor.convert(data, options)
}
}
static convertFiles (files, verbose, timings, options) {
files.forEach((file) => {
if (verbose) {
console.log(`converting file ${file}`)
}
if (timings) {
const timings = asciidoctor.Timings.create()
const instanceOptions = Object.assign({}, options, { timings })
asciidoctor.convertFile(file, instanceOptions)
timings.printReport(process.stderr, file)
} else {
asciidoctor.convertFile(file, options)
}
})
}
static requireLibrary (requirePath, cwd = process.cwd()) {
if (requirePath.charAt(0) === '.' && DOT_RELATIVE_RX.test(requirePath)) {
// NOTE require resolves a dot-relative path relative to current file; resolve relative to cwd instead
requirePath = ospath.resolve(requirePath)
} else if (!ospath.isAbsolute(requirePath)) {
// NOTE appending node_modules prevents require from looking elsewhere before looking in these paths
const paths = [cwd, ospath.dirname(__dirname)].map((start) => ospath.join(start, 'node_modules'))
requirePath = require.resolve(requirePath, { paths })
}
return require(requirePath)
}
static prepareProcessor (argv, asciidoctor) {
const requirePaths = argv['require']
if (requirePaths) {
requirePaths.forEach((requirePath) => {
const lib = Invoker.requireLibrary(requirePath)
if (lib && typeof lib.register === 'function') {
// REMIND: it could be an extension or a converter.
// the register function on a converter does not take any argument
// but the register function on an extension expects one argument (the extension registry)
// Until we revisit the API for extension and converter, we pass the registry as the first argument
lib.register(asciidoctor.Extensions)
}
})
}
}
static exit (failureLevel) {
let code = 0
const logger = asciidoctor.LoggerManager.getLogger()
if (logger && typeof logger.getMaxSeverity === 'function' && logger.getMaxSeverity() && logger.getMaxSeverity() >= failureLevel) {
code = 1
}
process.exit(code)
}
}
module.exports = Invoker
module.exports.asciidoctor = asciidoctor