Skip to content
Merged
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
10 changes: 5 additions & 5 deletions packages/cli/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as v from 'valibot';
import { exec } from 'tinyexec';
import { Command, Option } from 'commander';
import * as p from '@svelte-cli/clack-prompts';
import * as pkg from 'empathic/package';
import pc from 'picocolors';
import {
adderCategories,
Expand All @@ -16,7 +17,6 @@ import {
import {
createOrUpdateFiles,
createWorkspace,
findUp,
installPackages,
TESTING
} from '@svelte-cli/core/internal';
Expand Down Expand Up @@ -50,7 +50,7 @@ const addersOptions = getAdderOptionFlags();
const communityDetails: AdderWithoutExplicitArgs[] = [];

// infers the workspace cwd if a `package.json` resides in a parent directory
const defaultPkgPath = findUp(process.cwd(), 'package.json');
const defaultPkgPath = pkg.up();
const defaultCwd = defaultPkgPath ? path.dirname(defaultPkgPath) : undefined;

export const add = new Command('add')
Expand Down Expand Up @@ -460,8 +460,8 @@ export async function runAddCommand(options: Options, adders: string[]): Promise
}

// install dependencies
let depsStatus;
if (options.install) {
let depsStatus: 'installed' | 'skipped' | undefined;
if (options.install && selectedAdders.length > 0) {
depsStatus = await common.suggestInstallingDependencies(options.cwd);
}

Expand Down Expand Up @@ -635,7 +635,7 @@ function getOptionChoices(details: AdderWithoutExplicitArgs) {
const groups: Record<string, string[]> = {};
const options: Record<string, unknown> = {};
for (const [id, question] of Object.entries(details.config.options)) {
let values = [];
let values: string[] = [];
const applyDefault = question.condition?.(options) !== false;
if (question.type === 'boolean') {
values = [id, `no-${id}`];
Expand Down
9 changes: 3 additions & 6 deletions packages/cli/commands/check.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { createRequire } from 'node:module';
import { execSync } from 'node:child_process';
import pc from 'picocolors';
import * as resolve from 'empathic/resolve';
import { Command } from 'commander';
import { resolveCommand } from 'package-manager-detector/commands';
import { getUserAgent } from '../common.ts';

const require = createRequire(import.meta.url);

export const check = new Command('check')
.description('a CLI for checking your Svelte code')
// flags that we'll want to pass to `svelte-check`
Expand All @@ -30,9 +28,8 @@ function runCheck(cwd: string, args: string[]) {
const pm = getUserAgent() ?? 'npm';

// validates that `svelte-check` is locally installed
try {
require.resolve('svelte-check', { paths: [cwd] });
} catch {
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(' ')}`)}`
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@svelte-cli/create": "workspace:*",
"@types/tar-fs": "^2.0.4",
"commander": "^12.1.0",
"empathic": "^1.0.0",
"package-manager-detector": "^0.2.0",
"picocolors": "^1.1.0",
"tar-fs": "^3.0.6",
Expand Down
24 changes: 0 additions & 24 deletions packages/core/files/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,3 @@ export const commonFilePaths = {
tsconfig: 'tsconfig.json',
viteConfigTS: 'vite.config.ts'
} as const;

export function findUp(searchPath: string, fileName: string, maxDepth = -1): string | undefined {
// partially sourced from https://github.com/privatenumber/get-tsconfig/blob/9e78ec52d450d58743439358dd88e2066109743f/src/utils/find-up.ts#L5
let depth = 0;
while (maxDepth < 0 || depth < maxDepth) {
const configPath = path.posix.join(searchPath, fileName);

try {
// `access` throws an exception if the file could not be found
fs.accessSync(configPath);
return configPath;
} catch {
const parentPath = path.dirname(searchPath);
if (parentPath === searchPath) {
// root directory
return;
}

searchPath = parentPath;
}

depth++;
}
}
8 changes: 5 additions & 3 deletions packages/core/files/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fs from 'node:fs';
import path from 'node:path';
import * as find from 'empathic/find';
import * as resolve from 'empathic/resolve';
import { type AstTypes, parseScript } from '@svelte-cli/ast-tooling';
import { TESTING } from '../env.ts';
import { common, object } from '../tooling/js/index.ts';
import { commonFilePaths, findUp, getPackageJson, readFile } from './utils.ts';
import { commonFilePaths, getPackageJson, readFile } from './utils.ts';
import type { OptionDefinition, OptionValues, Question } from '../adder/options.ts';

export type Workspace<Args extends OptionDefinition> = {
Expand Down Expand Up @@ -38,14 +40,14 @@ export function createWorkspace<Args extends OptionDefinition>(cwd: string): Wor
// as we might detect the monorepo `tsconfig.json` otherwise.
usesTypescript ||= fs.existsSync(path.join(cwd, commonFilePaths.tsconfig));
} else {
usesTypescript ||= findUp(cwd, commonFilePaths.tsconfig) !== undefined;
usesTypescript ||= find.up(commonFilePaths.tsconfig, { cwd }) !== undefined;
}

const { data: packageJson } = getPackageJson(workspace);

workspace.dependencies = { ...packageJson.devDependencies, ...packageJson.dependencies };
workspace.typescript = usesTypescript;
workspace.prettier = 'prettier' in workspace.dependencies;
workspace.prettier = Boolean(resolve.from(cwd, 'prettier', true));
if ('@sveltejs/kit' in workspace.dependencies) workspace.kit = parseKitOptions(workspace);
for (const [key, value] of Object.entries(workspace.dependencies)) {
// removes the version ranges (e.g. `^` is removed from: `^9.0.0`)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/internal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { installPackages, findUp } from './files/utils.ts';
export { installPackages } from './files/utils.ts';
export { createOrUpdateFiles } from './files/processors.ts';
export { createWorkspace, type Workspace } from './files/workspace.ts';
export { TESTING } from './env.ts';
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@svelte-cli/clack-prompts": "workspace:*",
"decircular": "^1.0.0",
"dedent": "^1.5.3",
"empathic": "^1.0.0",
"picocolors": "^1.1.0"
},
"dependencies": {
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.