-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcli.js
76 lines (70 loc) · 1.86 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
import {readFile} from 'node:fs';
import {format} from 'node:util';
import {obj} from 'through2';
import _yargs from 'yargs/yargs';
import getPkgRepo from './index.js';
const 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;
const input = yargs._;
if (process.stdin.isTTY) {
if (input.length > 0) {
input.forEach(path => {
let repo;
readFile(path, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
try {
repo = getPkgRepo(JSON.parse(data));
console.log(repo);
} catch (e) {
console.error(`${path}: ${e.toString()}`);
}
});
});
} else {
process.stdin
.pipe(
obj((chunk, enc, cb) => {
let repo;
const pkgData = {
repository: chunk.toString(),
};
try {
repo = getPkgRepo(pkgData);
cb(null, format(repo) + '\n');
} catch (e) {
console.error(e.toString());
cb();
}
}),
)
.pipe(process.stdout);
}
} else {
process.stdin
.pipe(
obj((chunk, enc, cb) => {
let repo;
try {
repo = getPkgRepo(JSON.parse(chunk.toString()));
} catch (e) {
console.error(e.toString());
process.exit(1);
}
cb(null, format(repo) + '\n');
}),
)
.pipe(process.stdout);
}