Skip to content

Commit 8e6fccd

Browse files
committed
cleanup types
1 parent f671d24 commit 8e6fccd

File tree

12 files changed

+97
-968
lines changed

12 files changed

+97
-968
lines changed

packages/sv-utils/api-surface.md

Lines changed: 17 additions & 935 deletions
Large diffs are not rendered by default.

packages/sv-utils/src/dedent.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import dedentImpl from 'dedent';
2+
3+
/**
4+
* Template-tag or single-string dedent helper (same behavior as the `dedent` package).
5+
* Types are hand-written so the public `.d.mts` does not inline `dedent`'s full declarations.
6+
*/
7+
export type Dedent = {
8+
(strings: TemplateStringsArray, ...values: unknown[]): string;
9+
(source: string): string;
10+
};
11+
12+
const dedent: Dedent = dedentImpl as Dedent;
13+
export default dedent;

packages/sv-utils/src/index.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import {
2-
resolveCommand as _resolveCommand,
3-
type Agent,
4-
type Command
5-
} from 'package-manager-detector';
61
import {
72
parseCss,
83
parseHtml,
@@ -14,22 +9,19 @@ import {
149
} from './tooling/parsers.ts';
1510

1611
// External re-exports
17-
export { default as dedent } from 'dedent';
12+
export { default as dedent } from './dedent.ts';
1813
export * as Walker from 'zimmerframe';
14+
15+
// Package managers (delegates to `package-manager-detector`; see `pm.ts`)
1916
export {
2017
AGENTS,
2118
type AgentName,
2219
COMMANDS,
2320
constructCommand,
2421
detect,
25-
resolveCommand
26-
} from 'package-manager-detector';
27-
28-
/** Resolves a package manager command and returns it as a string array (command + args). */
29-
export function resolveCommandArray(agent: Agent, command: Command, args: string[]): string[] {
30-
const cmd = _resolveCommand(agent, command, args)!;
31-
return [cmd.command, ...cmd.args];
32-
}
22+
resolveCommand,
23+
resolveCommandArray
24+
} from './pm.ts';
3325

3426
// Parsing & language namespaces
3527
export * as css from './tooling/css/index.ts';
@@ -106,3 +98,4 @@ export { color } from './color.ts';
10698
// Types
10799
export type { Comments, AstTypes, SvelteAst } from './tooling/index.ts';
108100
export type { TransformFn } from './tooling/transforms.ts';
101+
export type { YamlDocument } from './tooling/parsers.ts';

packages/sv-utils/src/pm.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Thin wrapper around [`package-manager-detector`](https://github.com/antfu/package-manager-detector).
3+
* Only the symbols re-exported from the package root are public — we keep this module small and
4+
* avoid exposing the full upstream surface.
5+
*/
6+
import {
7+
resolveCommand as _resolveCommand,
8+
type Agent,
9+
type Command
10+
} from 'package-manager-detector';
11+
export {
12+
AGENTS,
13+
type AgentName,
14+
COMMANDS,
15+
constructCommand,
16+
detect,
17+
resolveCommand
18+
} from 'package-manager-detector';
19+
20+
export function resolveCommandArray(agent: Agent, command: Command, args: string[]): string[] {
21+
const cmd = _resolveCommand(agent, command, args)!;
22+
return [cmd.command, ...cmd.args];
23+
}

packages/sv-utils/src/tooling/parsers.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import type { TomlTable } from 'smol-toml';
22
import * as utils from './index.ts';
33

4+
/**
5+
* Minimal shape for YAML document roots from `parse.yaml` — avoids re-exporting the full `yaml` types.
6+
* At runtime this is the library’s document type; only `get` / `set` are part of the public contract.
7+
*/
8+
export type YamlDocument = {
9+
get(key: string): unknown;
10+
set(key: string, value: unknown): void;
11+
};
12+
413
type ParseBase = {
514
source: string;
615
/**
@@ -52,14 +61,13 @@ export function parseJson(source: string): { data: any } & ParseBase {
5261
return { data, source, generateCode };
5362
}
5463

55-
export function parseYaml(
56-
source: string
57-
): { data: ReturnType<typeof utils.parseYaml> } & ParseBase {
64+
export function parseYaml(source: string): { data: YamlDocument } & ParseBase {
5865
if (!source) source = '';
5966
const data = utils.parseYaml(source);
60-
const generateCode = () => utils.serializeYaml(data);
67+
const generateCode = () =>
68+
utils.serializeYaml(data as Parameters<typeof utils.serializeYaml>[0]);
6169

62-
return { data, source, generateCode };
70+
return { data: data as YamlDocument, source, generateCode };
6371
}
6472

6573
export function parseSvelte(source: string): { ast: utils.SvelteAst.Root } & ParseBase {

packages/sv-utils/src/tooling/transforms.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
parseScript,
1313
parseSvelte,
1414
parseToml,
15-
parseYaml
15+
parseYaml,
16+
type YamlDocument
1617
} from './parsers.ts';
1718
import { type RootWithInstance, ensureScript } from './svelte/index.ts';
1819
import * as svelteNs from './svelte/index.ts';
@@ -190,7 +191,7 @@ export const transforms = {
190191
* Return `false` from the callback to abort - the original content is returned unchanged.
191192
*/
192193
yaml(
193-
cb: (file: { data: ReturnType<typeof parseYaml>['data']; content: string }) => void | false,
194+
cb: (file: { data: YamlDocument; content: string }) => void | false,
194195
options?: TransformOptions
195196
): TransformFn {
196197
return (content) => {

packages/sv/src/addons/sveltekit-adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
color,
3-
resolveCommandArray,
43
text,
54
transforms,
5+
resolveCommandArray,
66
fileExists,
77
loadPackageJson,
88
sanitizeName

packages/sv/src/cli/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as p from '@clack/prompts';
2-
import { color, resolveCommandArray, commonFilePaths, loadPackageJson } from '@sveltejs/sv-utils';
2+
import { color, commonFilePaths, loadPackageJson, resolveCommandArray } from '@sveltejs/sv-utils';
33
import { Command, Option } from 'commander';
44
import fs from 'node:fs';
55
import path from 'node:path';

packages/sv/src/core/common.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as p from '@clack/prompts';
22
import {
3-
type AgentName,
43
color,
5-
resolveCommandArray,
6-
isVersionUnsupportedBelow
4+
isVersionUnsupportedBelow,
5+
type AgentName,
6+
resolveCommandArray
77
} from '@sveltejs/sv-utils';
88
import type { Argument, Command, Help, HelpConfiguration, Option } from 'commander';
99
import fs from 'node:fs';

packages/sv/src/core/engine.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import * as p from '@clack/prompts';
22
import {
33
color,
44
commonFilePaths,
5-
resolveCommand,
6-
type AgentName,
75
fileExists,
86
loadFile,
97
loadPackageJson,
10-
saveFile
8+
saveFile,
9+
resolveCommand,
10+
type AgentName
1111
} from '@sveltejs/sv-utils';
1212
import { NonZeroExitError, exec } from 'tinyexec';
1313
import { createLoadedAddon } from '../cli/add.ts';

0 commit comments

Comments
 (0)