Skip to content

Commit 9ab8328

Browse files
authored
feat: rename --check-types to --[no-]types (#249)
* simplify flag * de-ess * combine `--no` option variants * include param * auto-hide flags if there's an existing `--no-` variant * changeset
1 parent 84bd24c commit 9ab8328

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

.changeset/wise-hotels-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': minor
3+
---
4+
5+
feat: rename `--check-types <typescript|checkjs|none>` to `--types <ts|js>` with a `--no-types` flag

packages/cli/commands/add/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export const add = new Command('add')
4949
.description('applies specified integrations into a project')
5050
.argument('[integration...]', 'integrations to install')
5151
.option('-C, --cwd <path>', 'path to working directory', defaultCwd)
52-
.option('--no-install', 'skips installing dependencies')
53-
.option('--no-preconditions', 'skips validating preconditions')
52+
.option('--no-install', 'skip installing dependencies')
53+
.option('--no-preconditions', 'skip validating preconditions')
5454
//.option('--community [adder...]', 'community adders to install')
5555
.configureHelp(common.helpConfig)
5656
.action((adderArgs, opts) => {

packages/cli/commands/create.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,24 @@ import * as common from '../common.js';
1515
import { runAddCommand } from './add/index.ts';
1616
import { detectSync, type AgentName } from 'package-manager-detector';
1717

18-
const langs = ['typescript', 'checkjs', 'none'] as const;
18+
const langs = ['ts', 'jsdoc'] as const;
19+
const langMap: Record<string, LanguageType | undefined> = {
20+
ts: 'typescript',
21+
jsdoc: 'checkjs',
22+
false: 'none'
23+
};
1924
const templateChoices = templates.map((t) => t.name);
20-
const langOption = new Option('--check-types <lang>', 'add type checking').choices(langs);
25+
const langOption = new Option('--types <lang>', 'add type checking').choices(langs);
2126
const templateOption = new Option('--template <type>', 'template to scaffold').choices(
2227
templateChoices
2328
);
2429

2530
const ProjectPathSchema = v.string();
2631
const OptionsSchema = v.strictObject({
27-
checkTypes: v.optional(v.picklist(langs)),
32+
types: v.pipe(
33+
v.optional(v.union([v.picklist(langs), v.boolean()])),
34+
v.transform((lang) => langMap[String(lang)])
35+
),
2836
integrations: v.boolean(),
2937
install: v.boolean(),
3038
template: v.optional(v.picklist(templateChoices))
@@ -34,10 +42,11 @@ type Options = v.InferOutput<typeof OptionsSchema>;
3442
export const create = new Command('create')
3543
.description('scaffolds a new SvelteKit project')
3644
.argument('[path]', 'where the project will be created', process.cwd())
37-
.addOption(langOption)
3845
.addOption(templateOption)
39-
.option('--no-integrations', 'skips interactive integration installer')
40-
.option('--no-install', 'skips installing dependencies')
46+
.addOption(langOption)
47+
.option('--no-types')
48+
.option('--no-integrations', 'skip interactive integration installer')
49+
.option('--no-install', 'skip installing dependencies')
4150
.configureHelp(common.helpConfig)
4251
.action((projectPath, opts) => {
4352
const cwd = v.parse(ProjectPathSchema, projectPath);
@@ -113,7 +122,7 @@ async function createProject(cwd: string, options: Options) {
113122
});
114123
},
115124
language: () => {
116-
if (options.checkTypes) return Promise.resolve(options.checkTypes);
125+
if (options.types) return Promise.resolve(options.types);
117126
return p.select<LanguageType>({
118127
message: 'Add type checking with Typescript?',
119128
initialValue: 'typescript',

packages/cli/common.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,52 @@ import { COMMANDS, constructCommand, resolveCommand } from 'package-manager-dete
88
import type { Argument, HelpConfiguration, Option } from 'commander';
99
import type { AdderWithoutExplicitArgs, Precondition } from '@sveltejs/cli-core';
1010

11+
const NO_PREFIX = '--no-';
12+
let options: readonly Option[] = [];
13+
14+
function getLongFlag(flags: string) {
15+
return flags
16+
.split(',')
17+
.map((f) => f.trim())
18+
.find((f) => f.startsWith('--'));
19+
}
20+
1121
export const helpConfig: HelpConfiguration = {
1222
argumentDescription: formatDescription,
13-
optionDescription: formatDescription
23+
optionDescription: formatDescription,
24+
visibleOptions(cmd) {
25+
// hack so that we can access existing options in `optionTerm`
26+
options = cmd.options;
27+
28+
const visible = cmd.options.filter((o) => !o.hidden);
29+
const show: Option[] = [];
30+
// hide any `--no-` flag variants if there's an existing flag of a similar name
31+
// e.g. `--types` and `--no-types` will combine into a single `--[no-]types` flag
32+
for (const option of visible) {
33+
const flag = getLongFlag(option.flags);
34+
if (flag?.startsWith(NO_PREFIX)) {
35+
const stripped = flag.slice(NO_PREFIX.length);
36+
const isNoVariant = visible.some((o) => getLongFlag(o.flags)?.startsWith(`--${stripped}`));
37+
if (isNoVariant) continue;
38+
}
39+
show.push(option);
40+
}
41+
return show;
42+
},
43+
optionTerm(option) {
44+
const longFlag = getLongFlag(option.flags);
45+
const flag = longFlag?.split(' ').at(0);
46+
if (!flag || !longFlag) return option.flags;
47+
48+
// check if there's a `--no-{flag}` variant
49+
const noVariant = `--no-${flag.slice(2)}`;
50+
const hasVariant = options.some((o) => getLongFlag(o.flags) === noVariant);
51+
if (hasVariant) {
52+
return `--[no-]${longFlag.slice(2)}`;
53+
}
54+
55+
return option.flags;
56+
}
1457
};
1558

1659
function formatDescription(arg: Option | Argument): string {

0 commit comments

Comments
 (0)