Skip to content

Commit c875925

Browse files
authored
feat: add check command (#59)
1 parent 6e1d153 commit c875925

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

packages/cli/bin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { program } from 'commander';
55
import { add } from './commands/add.js';
66
import { create } from './commands/create.js';
77
import { migrate } from './commands/migrate.js';
8+
import { check } from './commands/check.js';
89
import { helpConfig } from './common.js';
910

1011
program.name(pkg.name).version(pkg.version, '-v').configureHelp(helpConfig);
11-
program.addCommand(create).addCommand(add).addCommand(migrate);
12+
program.addCommand(create).addCommand(add).addCommand(migrate).addCommand(check);
1213
program.parse();

packages/cli/commands/add.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const defaultPkgPath = findUp(process.cwd(), 'package.json');
5454
const defaultCwd = defaultPkgPath ? path.dirname(defaultPkgPath) : undefined;
5555

5656
export const add = new Command('add')
57-
.description('Applies specified adders into a project')
57+
.description('applies specified adders into a project')
5858
.argument('[adder...]', 'adders to install')
5959
.option('-C, --cwd <path>', 'path to working directory', defaultCwd)
6060
.option('--no-install', 'skips installing dependencies')

packages/cli/commands/check.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { createRequire } from 'node:module';
2+
import { execSync } from 'node:child_process';
3+
import pc from 'picocolors';
4+
import { Command } from 'commander';
5+
import { resolveCommand } from 'package-manager-detector/commands';
6+
import { getUserAgent } from '../common.ts';
7+
8+
const require = createRequire(import.meta.url);
9+
10+
export const check = new Command('check')
11+
.description('a CLI for checking your Svelte code')
12+
// flags that we'll want to pass to `svelte-check`
13+
.allowUnknownOption(true)
14+
.option('-C, --cwd <path>', 'path to working directory', process.cwd())
15+
.configureHelp({
16+
formatHelp() {
17+
// we'll pass the responsibility of presenting the help menu over to `svelte-check`
18+
runCheck(process.cwd(), ['--help']);
19+
return '';
20+
}
21+
})
22+
.action((options, check: Command) => {
23+
const cwd: string = options.cwd;
24+
const args: string[] = check.args;
25+
26+
runCheck(cwd, args);
27+
});
28+
29+
function runCheck(cwd: string, args: string[]) {
30+
const pm = getUserAgent() ?? 'npm';
31+
32+
// validates that `svelte-check` is locally installed
33+
try {
34+
require.resolve('svelte-check', { paths: [cwd] });
35+
} catch {
36+
const cmd = resolveCommand(pm, 'add', ['-D', 'svelte-check'])!;
37+
console.error(
38+
`'svelte-check' is not installed locally. Install it with: ${pc.bold(`${cmd.command} ${cmd.args.join(' ')}`)}`
39+
);
40+
process.exit(1);
41+
}
42+
43+
// avoids printing the stack trace for `sv` when `svelte-check` exits with an error code
44+
try {
45+
execSync(`npx svelte-check ${args.join(' ')}`, { stdio: 'inherit', cwd });
46+
} catch {}
47+
}

packages/cli/commands/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const create = new Command('create')
4545
const highlight = (str: string) => pc.bold(pc.cyan(str));
4646

4747
let i = 1;
48-
const initialSteps = [];
48+
const initialSteps: string[] = [];
4949
const relative = path.relative(process.cwd(), directory);
5050
const pm = await common.guessPackageManager(cwd);
5151
if (relative !== '') {

packages/cli/commands/migrate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { execSync } from 'node:child_process';
22
import { Command } from 'commander';
33

44
export const migrate = new Command('migrate')
5-
.description('A CLI for migrating Svelte(Kit) codebases')
5+
.description('a CLI for migrating Svelte(Kit) codebases')
66
.argument('<migration>', 'migration to run')
77
.option('-C, --cwd <path>', 'path to working directory', process.cwd())
88
.configureHelp({

packages/cli/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export async function guessPackageManager(cwd: string): Promise<AgentName> {
9696
return pm?.name ?? getUserAgent() ?? 'npm';
9797
}
9898

99-
function getUserAgent() {
99+
export function getUserAgent() {
100100
const userAgent = process.env.npm_config_user_agent;
101101
if (!userAgent) return undefined;
102102
const pmSpec = userAgent.split(' ')[0];

0 commit comments

Comments
 (0)