-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.mjs
54 lines (48 loc) · 1.51 KB
/
cli.mjs
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
#!/usr/bin/env node
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { use } from 'use-m';
import { readFileSync } from 'fs';
// Import yargs and helpers dynamically using `use`
const yargs = await use('[email protected]');
const { hideBin } = await use('[email protected]/helpers');
const currentScriptDir = dirname(fileURLToPath(import.meta.url));
// Function to get the loader path
function getLoaderPath() {
// Get the directory of the current file (cli.mjs)
try {
return resolve(currentScriptDir, 'loader.js');
} catch (err) {
throw new Error('Failed to get the loader path.', { cause: err });
}
}
// Function to get the version from package.json
function getVersion() {
try {
const packageJsonPath = resolve(currentScriptDir, 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
return packageJson.version;
} catch (err) {
throw new Error('Failed to read package.json or determine the version.', { cause: err });
}
}
yargs(hideBin(process.argv))
.scriptName('use-m')
.usage('$0 [options]')
.option('loader-path', {
alias: 'lp',
type: 'boolean',
description: 'Output the path to loader.js file',
})
.help('help')
.alias('help', 'h')
.version(getVersion()) // Use yargs' built-in version functionality
.alias('version', 'v')
.command('$0', 'Default command', () => { }, (argv) => {
if (argv.loaderPath) {
console.log(getLoaderPath());
} else {
yargs.showHelp();
}
})
.parse();