-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
171 lines (158 loc) · 4.1 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import cac from 'cac';
import type { OptionConfig } from 'cac/deno/Option.js';
import semver from 'semver';
import { pubm } from './index.js';
import { requiredMissingInformationTasks } from './tasks/required-missing-information.js';
import type { Options } from './types/options.js';
import { version } from './utils/package-json.js';
const { RELEASE_TYPES } = semver;
interface CliOptions {
version: string;
testScript: string;
preview?: boolean;
branch: string;
anyBranch?: boolean;
preCheck: boolean;
conditionCheck: boolean;
cleanup: boolean;
tests: boolean;
build: boolean;
publish: boolean;
releaseDraft: boolean;
yolo?: boolean;
tag: string;
packageManager?: string;
contents?: string;
registry?: string;
}
const options: {
rawName: string;
description: string;
options?: OptionConfig;
}[] = [
{
rawName: '--test-script <script>',
description: 'The npm script to run tests before publishing',
options: { default: 'test', type: String },
},
{
rawName: '-p, --preview',
description: 'Show tasks without actually executing publish',
options: { type: Boolean },
},
{
rawName: '-b, --branch <name>',
description: 'Name of the release branch',
options: { default: 'main', type: String },
},
{
rawName: '-a, --any-branch',
description: 'Show tasks without actually executing publish',
options: { type: Boolean },
},
{
rawName: '--no-pre-check',
description: 'Skip prerequisites check task',
options: { type: Boolean },
},
{
rawName: '--no-condition-check',
description: 'Skip required conditions check task',
options: { type: Boolean },
},
{
rawName: '--no-cleanup',
description: 'Skip cleaning the `node_modules` directory',
options: { type: Boolean },
},
{
rawName: '--no-tests',
description: 'Skip running tests before publishing',
options: { type: Boolean },
},
{
rawName: '--no-build',
description: 'Skip build before publishing',
options: { type: Boolean },
},
{
rawName: '--no-publish',
description: 'Skip publishing task',
options: { type: Boolean },
},
{
rawName: '--no-release-draft',
description: 'Skip creating a GitHub release draft',
options: { type: Boolean },
},
{
rawName: '-y, --yolo',
description: 'Skip both cleanup and tests',
options: { type: Boolean },
},
{
rawName: '-t, --tag <name>',
description: 'Publish under a specific dist-tag',
options: { default: 'latest', type: String },
},
{
rawName: '--package-manager <name>',
description: `Use a specific package manager 'packageManager' field in package.json or package manager configuration file`,
options: { type: String },
},
{
rawName: '-c, --contents <path>',
description: 'Subdirectory to publish',
options: { type: String },
},
{
rawName: '--registry <...registries>',
description:
'Target registries for publish\nregistry can be npm | jsr | https://url.for.private-registries',
options: { type: String, default: 'npm,jsr' },
},
];
const cli = cac('pubm');
for (const option of options) {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
cli.option(option.rawName, option.description, option.options as any);
}
function resolveCliOptions(options: CliOptions): Options {
return {
...options,
skipCleanup: !options.cleanup,
skipPublish: !options.publish,
skipReleaseDraft: !options.releaseDraft,
skipTests: !options.tests,
skipBuild: !options.build,
registries: options.registry?.split(','),
skipPrerequisitesCheck: !options.preCheck,
skipConditionsCheck: !options.conditionCheck,
};
}
cli
.command('[version]')
.action(async (nextVersion, options: Omit<CliOptions, 'version'>) => {
console.clear();
const context = {
version: nextVersion,
tag: options.tag,
};
await requiredMissingInformationTasks().run(context);
await pubm(
resolveCliOptions({
...options,
version: context.version,
tag: context.tag,
}),
);
});
cli.help((sections) => {
sections[1].body += `\n\n Version can be:\n ${RELEASE_TYPES.join(' | ')} | 1.2.3`;
sections.splice(2, 2);
sections.push({ body: '\n' });
});
(async () => {
cli.version(await version({ cwd: import.meta.dirname }));
cli.parse();
})();