|
| 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 | +} |
0 commit comments