Skip to content

Commit

Permalink
Refactor CLI and main to allow programmatic usage (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit authored and sindresorhus committed Sep 18, 2018
1 parent ab15c07 commit 195e4db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
27 changes: 2 additions & 25 deletions 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('.');
Expand All @@ -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();
24 changes: 24 additions & 0 deletions 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 = {
Expand All @@ -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;
Expand Down

0 comments on commit 195e4db

Please sign in to comment.