Skip to content

Commit 561b0f1

Browse files
authored
use empathic (#62)
1 parent c875925 commit 561b0f1

File tree

8 files changed

+28
-39
lines changed

8 files changed

+28
-39
lines changed

packages/cli/commands/add.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as v from 'valibot';
44
import { exec } from 'tinyexec';
55
import { Command, Option } from 'commander';
66
import * as p from '@svelte-cli/clack-prompts';
7+
import * as pkg from 'empathic/package';
78
import pc from 'picocolors';
89
import {
910
adderCategories,
@@ -16,7 +17,6 @@ import {
1617
import {
1718
createOrUpdateFiles,
1819
createWorkspace,
19-
findUp,
2020
installPackages,
2121
TESTING
2222
} from '@svelte-cli/core/internal';
@@ -50,7 +50,7 @@ const addersOptions = getAdderOptionFlags();
5050
const communityDetails: AdderWithoutExplicitArgs[] = [];
5151

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

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

462462
// install dependencies
463-
let depsStatus;
464-
if (options.install) {
463+
let depsStatus: 'installed' | 'skipped' | undefined;
464+
if (options.install && selectedAdders.length > 0) {
465465
depsStatus = await common.suggestInstallingDependencies(options.cwd);
466466
}
467467

@@ -635,7 +635,7 @@ function getOptionChoices(details: AdderWithoutExplicitArgs) {
635635
const groups: Record<string, string[]> = {};
636636
const options: Record<string, unknown> = {};
637637
for (const [id, question] of Object.entries(details.config.options)) {
638-
let values = [];
638+
let values: string[] = [];
639639
const applyDefault = question.condition?.(options) !== false;
640640
if (question.type === 'boolean') {
641641
values = [id, `no-${id}`];

packages/cli/commands/check.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { createRequire } from 'node:module';
21
import { execSync } from 'node:child_process';
32
import pc from 'picocolors';
3+
import * as resolve from 'empathic/resolve';
44
import { Command } from 'commander';
55
import { resolveCommand } from 'package-manager-detector/commands';
66
import { getUserAgent } from '../common.ts';
77

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

3230
// validates that `svelte-check` is locally installed
33-
try {
34-
require.resolve('svelte-check', { paths: [cwd] });
35-
} catch {
31+
const resolved = resolve.from(cwd, 'svelte-check', true);
32+
if (!resolved) {
3633
const cmd = resolveCommand(pm, 'add', ['-D', 'svelte-check'])!;
3734
console.error(
3835
`'svelte-check' is not installed locally. Install it with: ${pc.bold(`${cmd.command} ${cmd.args.join(' ')}`)}`

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@svelte-cli/create": "workspace:*",
3636
"@types/tar-fs": "^2.0.4",
3737
"commander": "^12.1.0",
38+
"empathic": "^1.0.0",
3839
"package-manager-detector": "^0.2.0",
3940
"picocolors": "^1.1.0",
4041
"tar-fs": "^3.0.6",

packages/core/files/utils.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -128,27 +128,3 @@ export const commonFilePaths = {
128128
tsconfig: 'tsconfig.json',
129129
viteConfigTS: 'vite.config.ts'
130130
} as const;
131-
132-
export function findUp(searchPath: string, fileName: string, maxDepth = -1): string | undefined {
133-
// partially sourced from https://github.com/privatenumber/get-tsconfig/blob/9e78ec52d450d58743439358dd88e2066109743f/src/utils/find-up.ts#L5
134-
let depth = 0;
135-
while (maxDepth < 0 || depth < maxDepth) {
136-
const configPath = path.posix.join(searchPath, fileName);
137-
138-
try {
139-
// `access` throws an exception if the file could not be found
140-
fs.accessSync(configPath);
141-
return configPath;
142-
} catch {
143-
const parentPath = path.dirname(searchPath);
144-
if (parentPath === searchPath) {
145-
// root directory
146-
return;
147-
}
148-
149-
searchPath = parentPath;
150-
}
151-
152-
depth++;
153-
}
154-
}

packages/core/files/workspace.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import * as find from 'empathic/find';
4+
import * as resolve from 'empathic/resolve';
35
import { type AstTypes, parseScript } from '@svelte-cli/ast-tooling';
46
import { TESTING } from '../env.ts';
57
import { common, object } from '../tooling/js/index.ts';
6-
import { commonFilePaths, findUp, getPackageJson, readFile } from './utils.ts';
8+
import { commonFilePaths, getPackageJson, readFile } from './utils.ts';
79
import type { OptionDefinition, OptionValues, Question } from '../adder/options.ts';
810

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

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

4648
workspace.dependencies = { ...packageJson.devDependencies, ...packageJson.dependencies };
4749
workspace.typescript = usesTypescript;
48-
workspace.prettier = 'prettier' in workspace.dependencies;
50+
workspace.prettier = Boolean(resolve.from(cwd, 'prettier', true));
4951
if ('@sveltejs/kit' in workspace.dependencies) workspace.kit = parseKitOptions(workspace);
5052
for (const [key, value] of Object.entries(workspace.dependencies)) {
5153
// removes the version ranges (e.g. `^` is removed from: `^9.0.0`)

packages/core/internal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { installPackages, findUp } from './files/utils.ts';
1+
export { installPackages } from './files/utils.ts';
22
export { createOrUpdateFiles } from './files/processors.ts';
33
export { createWorkspace, type Workspace } from './files/workspace.ts';
44
export { TESTING } from './env.ts';

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@svelte-cli/clack-prompts": "workspace:*",
4040
"decircular": "^1.0.0",
4141
"dedent": "^1.5.3",
42+
"empathic": "^1.0.0",
4243
"picocolors": "^1.1.0"
4344
},
4445
"dependencies": {

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)