From 195e4dbfe02849f3f749d4d7b624bbcd816b6cab Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Tue, 18 Sep 2018 01:25:10 -0400 Subject: [PATCH] Refactor CLI and main to allow programmatic usage (#43) --- cli.js | 27 ++------------------------- index.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/cli.js b/cli.js index 9bb76cc..3c1cd71 100755 --- a/cli.js +++ b/cli.js @@ -1,11 +1,6 @@ #!/usr/bin/env node 'use strict'; -const gitClone = require('git-clone'); -const isUrl = require('is-url-superb'); const meow = require('meow'); -const pify = require('pify'); -const rmfr = require('rmfr'); -const tempy = require('tempy'); const findReadmeFile = require('./lib/find-readme-file'); const awesomeLint = require('.'); @@ -18,32 +13,14 @@ const main = async () => { const options = { }; const input = cli.input[0]; - let temp = null; if (input) { - if (isUrl(input)) { - temp = tempy.directory(); - await pify(gitClone)(input, temp); - - const readme = findReadmeFile(temp); - if (!readme) { - await rmfr(temp); - throw new Error(`Unable to find valid readme for "${input}"`); - } - - options.filename = readme; - } else { - options.filename = input; - } + options.filename = input; } else { - options.filename = 'readme.md'; + options.filename = findReadmeFile(process.cwd()); } await awesomeLint.report(options); - - if (temp) { - await rmfr(temp); - } }; main(); diff --git a/index.js b/index.js index c1a6955..0116a3f 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,17 @@ 'use strict'; const path = require('path'); +const isUrl = require('is-url-superb'); const ora = require('ora'); const remark = require('remark'); +const gitClone = require('git-clone'); const globby = require('globby'); const pify = require('pify'); +const rmfr = require('rmfr'); +const tempy = require('tempy'); const toVfile = require('to-vfile'); const vfileReporterPretty = require('vfile-reporter-pretty'); const config = require('./config'); +const findReadmeFile = require('./lib/find-readme-file'); const lint = options => { options = { @@ -29,9 +34,28 @@ const lint = options => { lint.report = async options => { const spinner = ora('Linting').start(); + let temp = null; + + if (isUrl(options.filename)) { + temp = tempy.directory(); + await pify(gitClone)(options.filename, temp); + + const readme = findReadmeFile(temp); + if (!readme) { + await rmfr(temp); + throw new Error(`Unable to find valid readme for "${options.filename}"`); + } + + options.filename = readme; + } + const file = await lint(options); const {messages} = file; + if (temp) { + await rmfr(temp); + } + if (messages.length === 0) { spinner.succeed(); return;