-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.ts
More file actions
53 lines (45 loc) · 1.5 KB
/
Copy pathcli.ts
File metadata and controls
53 lines (45 loc) · 1.5 KB
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
#!/usr/bin/env node
import {program as commander} from 'commander';
import fs from 'node:fs/promises';
import path from 'node:path';
import {PublishFlat} from './PublishFlat.js';
interface PackageJson {
bin: Record<string, string>;
description: string;
version: string;
}
const __dirname = import.meta.dirname;
const packageJsonPath = path.join(__dirname, '../package.json');
const {bin, description, version}: PackageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8'));
const [toolName] = Object.keys(bin);
commander
.name(toolName)
.version(version)
.description(description)
.option('-y, --yarn', 'Use yarn for publishing (default: false)')
.option('-f, --flatten <dir>', 'Which directory to flatten', 'dist')
.option('-o, --output <dir>', 'Set the output directory (default: temp directory)')
.option('-p, --publish', 'Publish (default: false)')
.arguments('[dir]')
.allowUnknownOption()
.parse(process.argv);
const commanderOptions = commander.opts();
const flatPublisher = new PublishFlat({
dirToFlatten: commanderOptions.flatten,
outputDir: commanderOptions.output,
packageDir: commanderOptions.dir || '.',
publishArguments: commanderOptions.args,
useYarn: commanderOptions.yarn || false,
});
void (async () => {
try {
const outputDir = await flatPublisher.build();
if (commanderOptions.publish && outputDir) {
await flatPublisher.publish(outputDir);
}
process.exit();
} catch (error) {
console.error(error);
process.exit(1);
}
})();