-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
73 lines (59 loc) · 2.22 KB
/
index.ts
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
import { readFile, stat } from "node:fs/promises";
import { URL } from "node:url";
import { emphasize } from "emphasize";
import meow from 'meow';
import chalk from "chalk";
import boxen from 'boxen';
const cli = meow(`
Usage
$ try <package>
Each package has additional example commands as code comments
Options
--nocode Don't print example code
Examples
$ try commander
$ try meow
$ try chalk --nocode
`, {
importMeta: import.meta,
flags: {
"nocode": {
type: "boolean",
}
}
});
const [ npmPackage ] = cli.input;
const { nocode: noCodeFlag } = cli.flags;
process.argv.splice(1, 1); // Remove 'try' from args so that other CLI libraries don't see it
process.argv = process.argv.filter((arg) => arg !== '--nocode'); // Remove 'nocode' flags so they aren't passed on
if (!npmPackage) {
throw cli.showHelp(1);
}
const sourceFileUrl: URL = await (async () => {
const doesFileExist = async (url: URL): Promise<boolean> => !!(await stat(url).catch(() => null));
const printError = (message: string) => console.error(chalk.red(message));
// Check if implementation exists for this package
for await (const extension of ['ts', 'tsx']) {
const url = new URL(`./packages/${npmPackage}.${extension}`, import.meta.url);
if (await doesFileExist(url)) {
return url;
}
}
printError(`Did not find package '${chalk.white(npmPackage)}' for this tool.`);
const issuesUrl = typeof cli.pkg.bugs === 'object' ? cli.pkg.bugs.url : cli.pkg.bugs;
printError(`If you think it should be added, open an issue here: ${chalk.underline(chalk.blue(issuesUrl))}`);
process.exit(1);
})();
// Print the source code with syntax highlighting
if (!noCodeFlag) {
const sourceCode = await readFile(sourceFileUrl);
const emphasized = emphasize.highlight('typescript', sourceCode.toString()).value;
console.log(chalk.bgBlack(boxen(emphasized, {padding: 1})));
console.log()
}
// Run the implementation
const commandString = process.argv.slice(1).join(' ');
console.log('Running command:')
console.log(`${chalk.whiteBright('$')} ${chalk.green(commandString)}`)
console.log()
import(`./packages/${npmPackage}.js`);