Skip to content

Commit

Permalink
Add kit command
Browse files Browse the repository at this point in the history
  • Loading branch information
rChaoz committed Jan 15, 2025
1 parent 679ff83 commit 11344b2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 47 deletions.
3 changes: 2 additions & 1 deletion packages/cli/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { add } from './commands/add/index.ts';
import { create } from './commands/create.ts';
import { migrate } from './commands/migrate.ts';
import { check } from './commands/check.ts';
import { kit } from './commands/kit.ts';
import { helpConfig } from './utils/common.ts';

program.name(pkg.name).version(pkg.version, '-v, --version').configureHelp(helpConfig);
program.addCommand(create).addCommand(add).addCommand(migrate).addCommand(check);
program.addCommand(create).addCommand(add).addCommand(migrate).addCommand(check).addCommand(kit);
program.parse();
54 changes: 8 additions & 46 deletions packages/cli/commands/check.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,8 @@
import process from 'node:process';
import { execSync } from 'node:child_process';
import pc from 'picocolors';
import { Command } from 'commander';
import * as resolve from 'empathic/resolve';
import { resolveCommand } from 'package-manager-detector/commands';
import { getUserAgent } from '../utils/package-manager.ts';

export const check = new Command('check')
.description('a CLI for checking your Svelte code')
// flags that we'll want to pass to `svelte-check`
.allowUnknownOption(true)
.option('-C, --cwd <path>', 'path to working directory', process.cwd())
.configureHelp({
formatHelp() {
// we'll pass the responsibility of presenting the help menu over to `svelte-check`
runCheck(process.cwd(), ['--help']);
return '';
}
})
.action((options, check: Command) => {
const cwd: string = options.cwd;
const args: string[] = check.args;

runCheck(cwd, args);
});

function runCheck(cwd: string, args: string[]) {
const pm = getUserAgent() ?? 'npm';

// validates that `svelte-check` is locally installed
const resolved = resolve.from(cwd, 'svelte-check', true);
if (!resolved) {
const cmd = resolveCommand(pm, 'add', ['-D', 'svelte-check'])!;
console.error(
`'svelte-check' is not installed locally. Install it with: ${pc.bold(`${cmd.command} ${cmd.args.join(' ')}`)}`
);
process.exit(1);
}

// avoids printing the stack trace for `sv` when `svelte-check` exits with an error code
try {
const cmd = resolveCommand(pm, 'execute-local', ['svelte-check', ...args])!;
execSync(`${cmd.command} ${cmd.args.join(' ')}`, { stdio: 'inherit', cwd });
} catch {}
}
import { createCommand } from '../utils/external-package.js';

export const check = createCommand(
'check',
'svelte-check',
'svelte-check',
'a CLI for checking your Svelte code'
);
8 changes: 8 additions & 0 deletions packages/cli/commands/kit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createCommand } from '../utils/external-package.js';

export const kit = createCommand(
'kit',
'@sveltejs/kit',
'svelte-kit',
'a CLI for working with your SvelteKit project'
);
56 changes: 56 additions & 0 deletions packages/cli/utils/external-package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import process from 'node:process';
import { execSync } from 'node:child_process';
import pc from 'picocolors';
import { Command } from 'commander';
import * as resolve from 'empathic/resolve';
import { resolveCommand } from 'package-manager-detector/commands';
import { getUserAgent } from './package-manager.ts';

export function createCommand(
name: string,
package_name: string,
package_binary: string,
description: string
) {
return (
new Command(name)
.description(description)
// allow options for the external package
.allowUnknownOption(true)
.option('-C, --cwd <path>', 'path to working directory', process.cwd())
.configureHelp({
formatHelp() {
// pass the responsibility of presenting the help menu over to the external package
runPackage(package_name, package_binary, process.cwd(), ['--help']);
return '';
}
})
.action((options, check: Command) => {
const cwd: string = options.cwd;
const args: string[] = check.args;

runPackage(package_name, package_binary, cwd, args);
})
);
}

function runPackage(name: string, binary: string, cwd: string, args: string[]) {
const pm = getUserAgent() ?? 'npm';

// validates that the package is locally installed
// try to find package.json as package might not have a main export
const resolved = resolve.from(cwd, `${name}/package.json`, true);
if (!resolved) {
const cmd = resolveCommand(pm, 'add', ['-D', name])!;
console.error(
`'${name}' is not installed locally. Install it with: ${pc.bold(`${cmd.command} ${cmd.args.join(' ')}`)}`
);
process.exit(1);
}

// avoids printing the stack trace for `sv` when the external package exits with an error code
try {
const cmd = resolveCommand(pm, 'execute-local', [binary, ...args])!;
execSync(`${cmd.command} ${cmd.args.join(' ')}`, { stdio: 'inherit', cwd });
} catch {}
}

0 comments on commit 11344b2

Please sign in to comment.