-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
67 lines (53 loc) · 1.59 KB
/
cli.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
import fs from 'node:fs/promises';
import process from 'node:process';
import { cli } from 'cleye';
import { isAbsolute, resolve } from 'pathe';
import { findPackageJSON, genJsrFromPackageJson, readPkgJSON, writeJsr } from '.';
import { description, name, version } from '../package.json';
import { _throwError, logger } from './logger';
const LOG_LEVEL_SILENT = 0; // Fatal and Error
const LOG_LEVEL_NORMAL = 3; // Informational logs, success, fail, ready, start, ...
function resolveJsrPath(root: string): string {
return resolve(root, 'jsr.json');
}
export async function main(): Promise<void> {
const argv = cli({
name,
version,
flags: {
root: {
type: String,
alias: 'r',
description: 'root directory including package.json',
placeholder: '[path]',
},
silent: {
type: Boolean,
alias: 's',
description: 'Suppress non-error logs',
},
},
help: {
description,
},
});
const { root, silent } = argv.flags;
logger.level = silent === true ? LOG_LEVEL_SILENT : LOG_LEVEL_NORMAL;
/** current working directory or maybe file */
const cwd
= isAbsolute(root as string)
? root as string
: root != null
? resolve(process.cwd(), root)
: process.cwd();
/* check if root is a directory */
if (!(await fs.lstat(cwd)).isDirectory()) {
_throwError(`${root} is not a valid root directory`);
}
const pkgJSONPath = await findPackageJSON({ cwd });
const jsrPath = resolveJsrPath(cwd);
const pkgJSON = await readPkgJSON(pkgJSONPath);
const jsr = genJsrFromPackageJson({ pkgJSON });
await writeJsr(jsrPath, jsr);
logger.success(`Generated ${jsrPath}`);
}