@antora/cli
Advanced tools
Comparing version 1.0.0-alpha.6 to 1.0.0-alpha.7
@@ -11,2 +11,3 @@ #!/usr/bin/env node | ||
const DOT_RELATIVE_RX = new RegExp(`^\\.{1,2}[${[...new Set(['/', ospath.sep])].join('').replace('\\', '\\\\')}]`) | ||
const VERSION = require('../package.json').version | ||
@@ -21,11 +22,23 @@ | ||
function requireSiteGenerator (name, playbookDir) { | ||
try { | ||
// QUESTION should we remove the leading ./ ? (makes it a broader search) | ||
const searchPath = '.' + ospath.sep + ospath.relative('.', ospath.join(playbookDir, 'node_modules')) | ||
name = require.resolve(name, { paths: [searchPath] }) | ||
} catch (e) {} | ||
return require(name) | ||
function exitWithError (err, showStack, message = undefined) { | ||
console.error(showStack ? err.stack : `error: ${message || err.message}`) | ||
process.exit(1) | ||
} | ||
function requireLibraries (requirePaths) { | ||
if (requirePaths) requirePaths.forEach((requirePath) => requireLibrary(requirePath)) | ||
} | ||
function requireLibrary (requirePath, cwd = process.cwd()) { | ||
if (requirePath.charAt() === '.' && 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) | ||
} | ||
cli | ||
@@ -36,2 +49,4 @@ .name('antora') | ||
.usage('[options] [[command] [args]]') | ||
.option('-r, --require <library>', 'Require library (aka node module) or script before executing command.') | ||
.on('option:require', (requirePath) => (cli.requirePaths = (cli.requirePaths || []).concat(requirePath))) | ||
.option('--stacktrace', 'Print the stacktrace to the console if the application fails.') | ||
@@ -44,9 +59,14 @@ | ||
.action(async (playbookFile, command) => { | ||
try { | ||
requireLibraries(cli.requirePaths) | ||
} catch (err) { | ||
exitWithError(err, cli.stacktrace) | ||
} | ||
let generateSite | ||
try { | ||
// TODO honor --generator option (or auto-detect) | ||
generateSite = requireSiteGenerator('@antora/site-generator-default', ospath.resolve(playbookFile, '..')) | ||
} catch (e) { | ||
console.error('error: No site generator found. Try installing @antora/site-generator-default.') | ||
process.exit(1) | ||
generateSite = requireLibrary('@antora/site-generator-default', ospath.resolve(playbookFile, '..')) | ||
} catch (err) { | ||
const msg = 'error: Generator not found or failed to load. Try installing @antora/site-generator-default.' | ||
exitWithError(err, cli.stacktrace, msg) | ||
} | ||
@@ -56,6 +76,3 @@ const args = cli.rawArgs.slice(cli.rawArgs.indexOf(command.name()) + 1) | ||
// TODO support passing a preloaded convict config as third option; gets new args and env | ||
cli._promise = generateSite(args, process.env).catch((err) => { | ||
console.error(cli.stacktrace ? err.stack : 'error: ' + err.message) | ||
process.exit(1) | ||
}) | ||
cli._promise = generateSite(args, process.env).catch((err) => exitWithError(err, cli.stacktrace)) | ||
}) | ||
@@ -62,0 +79,0 @@ .options.sort((a, b) => a.long.localeCompare(b.long)) |
{ | ||
"name": "@antora/cli", | ||
"version": "1.0.0-alpha.6", | ||
"version": "1.0.0-alpha.7", | ||
"description": "The command line interface for Antora.", | ||
@@ -21,4 +21,4 @@ "license": "MPL-2.0", | ||
"dependencies": { | ||
"@antora/playbook-builder": "1.0.0-alpha.6", | ||
"commander": "^2.13.0" | ||
"@antora/playbook-builder": "1.0.0-alpha.7", | ||
"commander": "^2.14.1" | ||
}, | ||
@@ -25,0 +25,0 @@ "devDependencies": { |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
10148
176
+ Added@antora/playbook-builder@1.0.0-alpha.7(transitive)
+ Addedcoffeescript@1.12.7(transitive)
+ Addedcson-parser@3.0.0(transitive)
- Removed@antora/playbook-builder@1.0.0-alpha.6(transitive)
- Removedcoffee-script@1.12.7(transitive)
- Removedcson-parser@2.0.1(transitive)
Updatedcommander@^2.14.1