-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove "find-package"-dependency from cli-script (+ refactoring)
- Use "fs.readFile" instead of require to load package.json - Wrap cli-script in a function to allow for better testability - Test for the cli-script - A hard lesson was the one that "commander" has to be cleared from the "require.cache" in every cli-test. Otherwise the "done"-callback would be called multiple times in some cases. - Fixed typo on help-message
- Loading branch information
Showing
9 changed files
with
386 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#!/usr/bin/env node | ||
/*! | ||
* thought <https://github.com/nknapp/thought> | ||
* | ||
* Copyright (c) 2015 Nils Knappmeier. | ||
* Released under the MIT license. | ||
*/ | ||
var thought = require('../') | ||
var debug = require('debug')('thought:bin') | ||
const {resolvePackageRoot} = require('../lib/utils/resolve-package-root') | ||
|
||
/** | ||
* @thought-usage | ||
* | ||
* Called by the cli-script. Exports a function for better testability | ||
* | ||
*/ | ||
module.exports = function (argv, console, done) { | ||
Error.stackTraceLimit = 0 | ||
debug('started') | ||
|
||
var program = require('commander') | ||
program | ||
.version(require('../package').version) | ||
.option('-d, --debug', 'higher stack-trace-limit, long stack-traces', function (option) { | ||
Error.stackTraceLimit = 30 | ||
require('trace-and-clarify-if-possible') | ||
}) | ||
|
||
program | ||
.command('run') | ||
.option('-a, --add-to-git', 'git-add the modified files') | ||
.description('Generate documentation from your package.json and some templates.') | ||
.action(function (options) { | ||
changeDir() | ||
.then(() => resolvePackageRoot('package.json')) | ||
.then((root) => { | ||
if (!(root.packageJson.scripts && root.packageJson.scripts.thought)) { | ||
/* eslint-disable no-console */ | ||
console.log('\nNot registered in package.json yet!\n' + | ||
'I can add a `scripts`-property to your package.json to ensure that ' + | ||
'documentation is generated automatically on version bumps.\n' + | ||
'If you want that, run `thought init`\n') | ||
/* eslint-enable no-console */ | ||
} | ||
debug('running thought') | ||
}) | ||
.then(() => thought({addToGit: options.addToGit, debug: program.debug})) | ||
.then( | ||
(filenames) => quit(null, 'The following files were updated: ' + filenames.join(', ')), | ||
quit | ||
) | ||
}) | ||
|
||
program | ||
.command('init') | ||
.description('Register scripts in the current module\'s package.json') | ||
.action(function () { | ||
changeDir() | ||
.then(require('../lib/check-engines.js')) | ||
.then(require('../lib/init.js')) | ||
.then( | ||
() => quit(null, 'OK'), | ||
quit | ||
) | ||
}) | ||
|
||
program | ||
.command('check-engines') | ||
.description('Check that all engines (such as npm) have versions that ensure Thought to run correctly') | ||
.action(function () { | ||
require('../lib/check-engines.js')() | ||
.then( | ||
() => quit(null, 'OK'), | ||
quit | ||
) | ||
}) | ||
|
||
program | ||
.command('up-to-date') | ||
.description('Perform up-to-date check of the current documentation. Exit with non-zero exit-code when thought must be run again.') | ||
.action(function () { | ||
changeDir() | ||
.then(require('../lib/up-to-date.js')) | ||
.then( | ||
() => quit(null, 'OK'), | ||
quit | ||
) | ||
}) | ||
|
||
program.parse(argv) | ||
|
||
/* istanbul ignore if: Not testable because it exits the process */ | ||
if (program.args.length === 0) { | ||
program.help() | ||
quit(null, '') | ||
} | ||
|
||
// ---------------------------------------- | ||
// chdir to local module root | ||
// ---------------------------------------- | ||
function changeDir () { | ||
return resolvePackageRoot('package.json') | ||
.then(root => { | ||
/* istanbul ignore if: Situation very hard to reproduce */ | ||
if (!root.packageJson) { | ||
throw new Error('package.json not found!\n' + | ||
'Please run me from within a node module.\n' + | ||
'My working directory is "' + process.cwd() + '".') | ||
} | ||
process.chdir(root.packageRoot) | ||
// eslint-disable-next-line no-console | ||
console.log('I\'m running inside module \'' + root.packageJson.name + '\' in \'' + process.cwd()) | ||
}) | ||
} | ||
|
||
function quit (err, message) { | ||
/* eslint-disable no-console */ | ||
if (err) { | ||
console.error(err, message) | ||
return done(1) | ||
} | ||
console.log(message) | ||
return done(0) | ||
/* eslint-enable no-console */ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.