Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sv kit as alias for svelte-kit command #397

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-sheep-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': minor
---

feat: add `sv kit` as alias for `svelte-kit` command
24 changes: 24 additions & 0 deletions documentation/docs/20-commands/50-sv-kit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: sv kit
---

`sv kit` is a tiny CLI tool that helps you initialize and update your SvelteKit project.

As SvelteKit projects use [Vite](https://vitejs.dev/), you'll mostly be using Vite's CLI commands to build and run your project:
- `vite dev` — start a development server
- `vite build` — build a production version of your app
- `vite preview` — run the production version locally

## Installation

`sv kit` is available in SvelteKit projects, i.e. you will need the `@sveltejs/kit` package to use it. You can use `sv create` to [create and set up](sv-create) a new SvelteKit project. You can also check out SvelteKit's [documentation](/docs/kit) for more information.

## Usage

```bash
npx sv kit sync
```

`sv kit sync` creates the `tsconfig.json` and all generated types (which you can import as `./$types` inside routing files) for your project. When you create a new project, it is listed as the `prepare` script and will be run automatically as part of the npm lifecycle, so you should not ordinarily have to run this command.

You can run this command manually to ensure the generated types and `tsconfig.json` are up to date with your project and configuration. It may also be necessary to run this command as part of your CI pipeline.
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 {}
}
Loading