Skip to content

Commit

Permalink
feat(cli): use Yargs in stead of meow (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: Ruben de Wit <[email protected]>
  • Loading branch information
githrdw and Ruben de Wit authored Aug 4, 2021
1 parent abaf00a commit 8017a91
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 252 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"dependencies": {
"@hutson/parse-repository-url": "^3.0.0",
"hosted-git-info": "^4.0.0",
"meow": "^7.0.0",
"through2": "^2.0.0"
"through2": "^2.0.0",
"yargs": "^17.0.1"
},
"devDependencies": {
"@hutson/conventional-changelog-config": "^2.0.0",
Expand Down
82 changes: 40 additions & 42 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,28 @@

const fs = require(`fs`);
const getPkgRepo = require(`../`);
const meow = require(`meow`);
const through = require(`through2`);
const util = require(`util`);

const cli = meow({
help:
`Practice writing repository URL or validate the repository in a package.json file.
If used without specifying a package.json file path, you will enter an interactive shell.
Otherwise, the repository info in package.json is printed.
const yargs = require('yargs/yargs')(process.argv.slice(2))
.usage(
'\nPractice writing repository URL or validate the repository in a package.json file. If used without specifying a package.json file path, you will enter an interactive shell. Otherwise, the repository info in package.json is printed.'
)
.scriptName('get-pkg-repo')
.command('$0')
.command('<path> [<path> ...]')
.example('get-pkg-repo')
.example('get-pkg-repo package.json')
.example('cat package.json | get-pkg-repo')
.help().argv;

Usage
get-pkg-repo
get-pkg-repo <path> [<path> ...]
cat <path> | get-pkg-repo
Examples
get-pkg-repo
get-pkg-repo package.json
cat package.json | get-pkg-repo`,
});

const {input} = cli;
const input = yargs._;

if (process.stdin.isTTY) {
if (input.length > 0) {
input.forEach(path => {
let repo;
fs.readFile(path, `utf8`, (err, data) => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
Expand All @@ -47,33 +41,37 @@ if (process.stdin.isTTY) {
});
} else {
process.stdin
.pipe(through.obj((chunk, enc, cb) => {
let repo;
const pkgData = {
repository: chunk.toString(),
};
.pipe(
through.obj((chunk, enc, cb) => {
let repo;
const pkgData = {
repository: chunk.toString(),
};

try {
repo = getPkgRepo(pkgData);
cb(null, util.format(repo) + `\n`);
} catch (e) {
console.error(e.toString());
cb();
}
}))
try {
repo = getPkgRepo(pkgData);
cb(null, util.format(repo) + '\n');
} catch (e) {
console.error(e.toString());
cb();
}
})
)
.pipe(process.stdout);
}
} else {
process.stdin
.pipe(through.obj((chunk, enc, cb) => {
let repo;
try {
repo = getPkgRepo(JSON.parse(chunk.toString()));
} catch (e) {
console.error(e.toString());
process.exit(1);
}
cb(null, `${util.format(repo)}\n`);
}))
.pipe(
through.obj((chunk, enc, cb) => {
let repo;
try {
repo = getPkgRepo(JSON.parse(chunk.toString()));
} catch (e) {
console.error(e.toString());
process.exit(1);
}
cb(null, util.format(repo) + '\n');
})
)
.pipe(process.stdout);
}
Loading

0 comments on commit 8017a91

Please sign in to comment.