@pbuilder/sdk 0.5.1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -2
- package/dist/core/define-dialect.d.ts +6 -2
- package/dist/core/define-dialect.js +1 -0
- package/dist/core/dialect-handle.d.ts +14 -1
- package/dist/core/dialect-handle.js +47 -2
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +1 -1
- package/dist/core/jsx-name-validator.d.ts +6 -1
- package/dist/core/jsx-name-validator.js +12 -4
- package/dist/dialects/angular/index.d.ts +63 -0
- package/dist/dialects/angular/index.js +68 -0
- package/dist/dialects/angular/ops.d.ts +110 -0
- package/dist/dialects/angular/ops.js +342 -0
- package/dist/dialects/css/ast.d.ts +7 -0
- package/dist/dialects/css/ast.js +16 -0
- package/dist/dialects/css/index.d.ts +40 -0
- package/dist/dialects/css/index.js +38 -0
- package/dist/dialects/css/ops.d.ts +44 -0
- package/dist/dialects/css/ops.js +216 -0
- package/dist/dialects/nestjs/ops.js +1 -26
- package/dist/dialects/typescript/array-element.d.ts +41 -0
- package/dist/dialects/typescript/array-element.js +323 -0
- package/dist/dialects/typescript/class-property.d.ts +24 -0
- package/dist/dialects/typescript/class-property.js +98 -0
- package/dist/dialects/typescript/index.d.ts +6 -1
- package/dist/dialects/typescript/index.js +2 -1
- package/dist/dialects/typescript/pack.d.ts +4 -0
- package/dist/dialects/typescript/pack.js +4 -0
- package/dist/runner-manifest.json +2 -2
- package/package.json +12 -1
package/README.md
CHANGED
|
@@ -559,9 +559,9 @@ conformance-tests a *dialect or op-pack* implementation — not the same audienc
|
|
|
559
559
|
|
|
560
560
|
When string-level mutation isn't enough — "add an import to this module", "set a prop on
|
|
561
561
|
this JSX element" — **dialects** provide structured, AST-aware operations for one file
|
|
562
|
-
type: see [authoring a dialect](./docs/authoring-a-dialect.md) for the full story.
|
|
562
|
+
type: see [authoring a dialect](./docs/authoring-a-dialect.md) for the full story. Seven
|
|
563
563
|
ship today: `@pbuilder/sdk/typescript`, `@pbuilder/sdk/react`, `@pbuilder/sdk/nestjs`,
|
|
564
|
-
`@pbuilder/sdk/json`, and `@pbuilder/sdk/
|
|
564
|
+
`@pbuilder/sdk/angular`, `@pbuilder/sdk/json`, `@pbuilder/sdk/yaml`, and `@pbuilder/sdk/css`.
|
|
565
565
|
|
|
566
566
|
A dialect's entry point is its own `find(path)`: it opens a **coalescing, awaitable
|
|
567
567
|
handle**. Every op you chain mutates the same live AST, and the handle serializes to
|
|
@@ -590,6 +590,8 @@ await ts.find("src/index.ts")
|
|
|
590
590
|
| `removeImport(name, from)` | Removes the named binding; deletes the whole statement when it was the last one. Idempotent on an absent binding. |
|
|
591
591
|
| `removeExport(name, from)` | Removes a named re-export; deletes the statement when it was the last one. Idempotent on an absent one. |
|
|
592
592
|
| `removeExportAll(from, { as? })` | Removes `export * from "from"` (or the `as` form). Idempotent on an absent one. |
|
|
593
|
+
| `addArrayElement(locator, element)` | Appends to the array literal `locator` points at (a variable, a call argument, or a decorator argument, then a `path` of keys and `{ key: "value" }` matches), keeping its layout. Creates missing keys; a no-op when the element is already listed. `element` is an identifier, or `{ expression }` for code. |
|
|
594
|
+
| `addClassProperty(className, name, initializer, opts?)` | Adds `{scope }{readonly }name = initializer;` to the only class named `className`, after its last property. A no-op when the same property exists; any other member named `name` rejects. |
|
|
593
595
|
| `addFunction(name, source, opts?)` | Appends a top-level function. `source` **includes** the braces (`"(): void {}"`). |
|
|
594
596
|
| `addVariable(name, initializer, opts?)` | Appends a top-level variable (`kind` defaults to `const`). |
|
|
595
597
|
| `addClass(name, source, opts?)` | Appends a top-level class. `source` **excludes** the braces — the op adds them. |
|
|
@@ -668,6 +670,26 @@ Extra named ops: `setComment`, `removeComment`, `setAnchor`, `removeAnchor`, `se
|
|
|
668
670
|
`addMergeKey`, `setStyle`, `setTag`, `addDocument`, `removeDocument`. `.modify(fn)` exposes
|
|
669
671
|
[yaml](https://eemeli.org/yaml/). See [the YAML dialect guide](./docs/yaml-dialect.md).
|
|
670
672
|
|
|
673
|
+
### `@pbuilder/sdk/css`
|
|
674
|
+
|
|
675
|
+
Sets and removes declarations in `.css` files, such as design tokens synced into a file that
|
|
676
|
+
also holds hand-written rules. A target names one block (`{ selector }`, `{ atRule }`, or
|
|
677
|
+
both); a missing block is created at the end of the file, and everything else keeps its bytes.
|
|
678
|
+
|
|
679
|
+
```ts
|
|
680
|
+
import * as css from "@pbuilder/sdk/css";
|
|
681
|
+
|
|
682
|
+
await css
|
|
683
|
+
.find("tokens.css")
|
|
684
|
+
.setDeclaration({ atRule: "theme" }, "--color-primary", "#1974f9")
|
|
685
|
+
.removeDeclaration({ selector: ":root" }, "--legacy-blue");
|
|
686
|
+
```
|
|
687
|
+
|
|
688
|
+
Named ops: `setDeclaration`, `removeDeclaration`, and the query `getDeclaration(target, property)`,
|
|
689
|
+
which ends the chain and resolves to `{ value, important }` (also exported as
|
|
690
|
+
`getDeclaration(root, target, property)` for use inside `.modify(fn)`). `.modify(fn)` exposes
|
|
691
|
+
[postcss](https://postcss.org/). See [the CSS dialect guide](./docs/css-dialect.md).
|
|
692
|
+
|
|
671
693
|
### `@pbuilder/sdk/nestjs`
|
|
672
694
|
|
|
673
695
|
Every `@pbuilder/sdk/typescript` op, plus ops that import a class and register it in the
|
|
@@ -685,6 +707,28 @@ await nestjs
|
|
|
685
707
|
Extra named ops: `addProvider`, `addController`, `addModuleImport`, `addExport`. `.modify(fn)`
|
|
686
708
|
exposes ts-morph. See [the NestJS dialect guide](./docs/nestjs-dialect.md).
|
|
687
709
|
|
|
710
|
+
### `@pbuilder/sdk/angular`
|
|
711
|
+
|
|
712
|
+
Every `@pbuilder/sdk/typescript` op, plus ops for standalone components, the application's root
|
|
713
|
+
providers, routes, signal inputs, outputs and state, injected dependencies, and `@NgModule`
|
|
714
|
+
metadata. Each op adds the imports its code needs.
|
|
715
|
+
|
|
716
|
+
```ts
|
|
717
|
+
import * as angular from "@pbuilder/sdk/angular";
|
|
718
|
+
|
|
719
|
+
await angular.find("src/app/app.config.ts").addRootProvider({ expression: "provideHttpClient(withFetch())" }, [
|
|
720
|
+
{ name: "provideHttpClient", from: "@angular/common/http" },
|
|
721
|
+
{ name: "withFetch", from: "@angular/common/http" },
|
|
722
|
+
]);
|
|
723
|
+
await angular.find("src/app/app.routes.ts").addLazyRoute({ path: "admin", loadChildren: "./admin/admin.routes" });
|
|
724
|
+
```
|
|
725
|
+
|
|
726
|
+
Extra named ops: `addComponentImport`, `addComponentProvider`, `addRootProvider`, `addRoute`,
|
|
727
|
+
`addLazyRoute`, `addInjection`, `addInput`, `addModel`, `addOutput`, `addSignal`, `addComputed`,
|
|
728
|
+
`addNgModuleImport`, `addDeclaration`, `addNgModuleProvider`, `addNgModuleExport`, `addBootstrap`.
|
|
729
|
+
`.modify(fn)` exposes ts-morph. See
|
|
730
|
+
[the Angular dialect guide](./docs/angular-dialect.md).
|
|
731
|
+
|
|
688
732
|
### `.modify()` — the universal escape hatch
|
|
689
733
|
|
|
690
734
|
Every dialect handle carries one universal op alongside its named ops: `.modify(ast => …)`.
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { ReadOps } from "./base-handle.ts";
|
|
2
|
+
export { query } from "./dialect-handle.ts";
|
|
2
3
|
export type Op<Ast = any> = (ast: Ast, ...args: never[]) => void;
|
|
4
|
+
declare const QUERY: unique symbol;
|
|
5
|
+
export type Query<Ast = any, A extends unknown[] = any[], R = unknown> = ((ast: Ast, ...args: A) => R) & {
|
|
6
|
+
readonly [QUERY]: true;
|
|
7
|
+
};
|
|
3
8
|
export type OpPack<Ast = any> = Record<string, Op<Ast>>;
|
|
4
9
|
export interface DialectDescriptor<Ast = any, Ops extends OpPack<Ast> = OpPack<Ast>> {
|
|
5
10
|
extensions: string[];
|
|
@@ -12,7 +17,7 @@ export interface DialectDescriptor<Ast = any, Ops extends OpPack<Ast> = OpPack<A
|
|
|
12
17
|
type Bound<Ast, F> = F extends (ast: Ast, ...rest: infer R) => void ? (...rest: R) => void : never;
|
|
13
18
|
type Edited<Ast, Ops extends OpPack<Ast>> = Handle<"writable", Ast, Ops>;
|
|
14
19
|
type OpMethods<Ast, Ops extends OpPack<Ast>> = {
|
|
15
|
-
[K in keyof Ops]: (...a: Parameters<Bound<Ast, Ops[K]>>) => Edited<Ast, Ops>;
|
|
20
|
+
[K in keyof Ops]: Ops[K] extends Query<any, infer A, infer R> ? (...a: A) => Promise<Awaited<R>> : (...a: Parameters<Bound<Ast, Ops[K]>>) => Edited<Ast, Ops>;
|
|
16
21
|
};
|
|
17
22
|
type DialectWriteOps<Ast, Ops extends OpPack<Ast>> = {
|
|
18
23
|
replaceContent(content: string): Edited<Ast, Ops>;
|
|
@@ -91,4 +96,3 @@ export declare const RESERVED_HANDLE_NAMES: readonly ["then", "read", "raw", "mo
|
|
|
91
96
|
* const tsDialect = withOps(baseTsDialect, addImportPack);
|
|
92
97
|
*/
|
|
93
98
|
export declare function withOps<Ast, B extends OpPack<Ast>, P extends OpPack<Ast>[]>(base: Dialect<Ast, B>, ...packs: P): Dialect<Ast, B & UnionToIntersection<P[number]>>;
|
|
94
|
-
export {};
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import { basename } from "node:path";
|
|
15
15
|
import { createDialectHandle } from "./dialect-handle.js";
|
|
16
16
|
import { dialectError } from "./dialect-error.js";
|
|
17
|
+
export { query } from "./dialect-handle.js";
|
|
17
18
|
/**
|
|
18
19
|
* Declares a named collection of ops over one AST type — usable standalone (REQ-DG-02.3),
|
|
19
20
|
* e.g. shareable across two dialects targeting the same AST type, before any `withOps`
|
|
@@ -1,8 +1,21 @@
|
|
|
1
|
-
import type { OpPack, Handle } from "./define-dialect.ts";
|
|
1
|
+
import type { OpPack, Handle, Query } from "./define-dialect.ts";
|
|
2
2
|
interface DialectAst<Ast> {
|
|
3
3
|
parse(source: string): Ast;
|
|
4
4
|
print(ast: Ast): string;
|
|
5
5
|
}
|
|
6
|
+
/**
|
|
7
|
+
* Marks an op-pack entry as a query: on the handle it ends the chain and resolves to `fn`'s
|
|
8
|
+
* result instead of returning the handle. It runs in chain order on the live AST, so it sees
|
|
9
|
+
* every op chained before it; it emits nothing, and it rejects if it changes the AST.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const pack = defineOpPack({
|
|
13
|
+
* push(ast: string[], line: string) { ast.push(line); },
|
|
14
|
+
* count: query((ast: string[]) => ast.length),
|
|
15
|
+
* });
|
|
16
|
+
* // await dialect.find("a.lines").push("x").count() -> number of lines, "x" included
|
|
17
|
+
*/
|
|
18
|
+
export declare function query<Ast, A extends unknown[], R>(fn: (ast: Ast, ...args: A) => R): Query<Ast, A, R>;
|
|
6
19
|
/**
|
|
7
20
|
* Recovers the author-facing handle path for a live AST instance previously registered by
|
|
8
21
|
* `#ensureLive` — the ONLY channel through which an op function can reach the path for its
|
|
@@ -21,6 +21,29 @@ function isThenable(value) {
|
|
|
21
21
|
// WeakMap entry lives exactly as long as the AST instance it's keyed on (set once in
|
|
22
22
|
// #ensureLive, alongside `#live` itself) — no separate cleanup needed.
|
|
23
23
|
const astHandlePaths = new WeakMap();
|
|
24
|
+
// The dispatcher must decide synchronously whether a method returns the handle or a promise,
|
|
25
|
+
// and a function's return type is invisible at runtime — so `query()` brands the function.
|
|
26
|
+
// It lives here, not in define-dialect.ts, because define-dialect.ts already imports this
|
|
27
|
+
// module: the reverse runtime import would be circular.
|
|
28
|
+
const queries = new WeakSet();
|
|
29
|
+
/**
|
|
30
|
+
* Marks an op-pack entry as a query: on the handle it ends the chain and resolves to `fn`'s
|
|
31
|
+
* result instead of returning the handle. It runs in chain order on the live AST, so it sees
|
|
32
|
+
* every op chained before it; it emits nothing, and it rejects if it changes the AST.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const pack = defineOpPack({
|
|
36
|
+
* push(ast: string[], line: string) { ast.push(line); },
|
|
37
|
+
* count: query((ast: string[]) => ast.length),
|
|
38
|
+
* });
|
|
39
|
+
* // await dialect.find("a.lines").push("x").count() -> number of lines, "x" included
|
|
40
|
+
*/
|
|
41
|
+
export function query(fn) {
|
|
42
|
+
// A fresh wrapper, so `fn` itself stays a plain op wherever it is reused unwrapped.
|
|
43
|
+
const branded = (ast, ...args) => fn(ast, ...args);
|
|
44
|
+
queries.add(branded);
|
|
45
|
+
return branded;
|
|
46
|
+
}
|
|
24
47
|
/**
|
|
25
48
|
* Recovers the author-facing handle path for a live AST instance previously registered by
|
|
26
49
|
* `#ensureLive` — the ONLY channel through which an op function can reach the path for its
|
|
@@ -257,6 +280,24 @@ class DialectHandleController {
|
|
|
257
280
|
this.#ensureOpen();
|
|
258
281
|
});
|
|
259
282
|
}
|
|
283
|
+
// Sequenced on #tail like an op, but it never calls #ensureOpen: a query only reads the live
|
|
284
|
+
// AST, so it registers no directive (#ensureLive's first read still drains, as for an op).
|
|
285
|
+
// The print comparison is what keeps it read-only: a query that edits the AST would
|
|
286
|
+
// otherwise smuggle its edit into the next op's directive.
|
|
287
|
+
runQuery(fn, args, name) {
|
|
288
|
+
return this.#chain(async () => {
|
|
289
|
+
await this.#ensureLive();
|
|
290
|
+
const before = this.#printContained();
|
|
291
|
+
let result;
|
|
292
|
+
await this.#invokeContained(async () => {
|
|
293
|
+
result = await fn(this.#live, ...args);
|
|
294
|
+
}, `${name}() on "${this.#path}" threw`);
|
|
295
|
+
if (this.#printContained() !== before) {
|
|
296
|
+
throw dialectError(`${name}() is a query and must not change "${this.#path}"`);
|
|
297
|
+
}
|
|
298
|
+
return result;
|
|
299
|
+
});
|
|
300
|
+
}
|
|
260
301
|
// Council fix pass (security note 1, ADR-0037): an ASYNC `fn` is awaited INSIDE the SAME
|
|
261
302
|
// shared containment `runOp` uses (`#invokeContained`) — the enqueued step already runs
|
|
262
303
|
// in the async `#tail` chain, so awaiting is natural. Without this, `fn`'s returned
|
|
@@ -369,11 +410,15 @@ export function createDialectHandle(dialectAst, ops, path) {
|
|
|
369
410
|
then: (onfulfilled, onrejected) => controller.then(onfulfilled, onrejected),
|
|
370
411
|
};
|
|
371
412
|
// The op-derived members — one bound dispatcher per key in `ops` (exactly what
|
|
372
|
-
// `OpMethods<Ast, Ops>` describes at the type level)
|
|
413
|
+
// `OpMethods<Ast, Ops>` describes at the type level): a query resolves to its value, any
|
|
414
|
+
// other op chains.
|
|
373
415
|
const opMethods = Object.fromEntries(Object.keys(ops).map((opName) => [
|
|
374
416
|
opName,
|
|
375
417
|
(...args) => {
|
|
376
|
-
|
|
418
|
+
const op = ops[opName];
|
|
419
|
+
if (queries.has(op))
|
|
420
|
+
return controller.runQuery(op, args, opName);
|
|
421
|
+
controller.runOp(op, args, opName);
|
|
377
422
|
return handle;
|
|
378
423
|
},
|
|
379
424
|
]));
|
package/dist/core/index.d.ts
CHANGED
|
@@ -6,5 +6,5 @@ export type { SessionContext } from "./context.ts";
|
|
|
6
6
|
export { defineFactory, currentContext } from "./context.ts";
|
|
7
7
|
export type { ReadOps, WriteOps, WritableHandleRef } from "./base-handle.ts";
|
|
8
8
|
export type { FoundHandle, WritableHandle } from "./handle-state.ts";
|
|
9
|
-
export { defineDialect, defineOpPack, withOps } from "./define-dialect.ts";
|
|
10
|
-
export type { Dialect, OpPack } from "./define-dialect.ts";
|
|
9
|
+
export { defineDialect, defineOpPack, query, withOps } from "./define-dialect.ts";
|
|
10
|
+
export type { Dialect, OpPack, Query } from "./define-dialect.ts";
|
package/dist/core/index.js
CHANGED
|
@@ -3,4 +3,4 @@
|
|
|
3
3
|
export { Session } from "./session.js";
|
|
4
4
|
export { DirectiveFactory } from "./directive-factory.js";
|
|
5
5
|
export { defineFactory, currentContext } from "./context.js";
|
|
6
|
-
export { defineDialect, defineOpPack, withOps } from "./define-dialect.js";
|
|
6
|
+
export { defineDialect, defineOpPack, query, withOps } from "./define-dialect.js";
|
|
@@ -10,7 +10,12 @@ export declare function assertValidElementName(elementName: string): void;
|
|
|
10
10
|
* `IdentifierName` (including reserved words) — `import { name }` requires a `BindingIdentifier`,
|
|
11
11
|
* so a reserved-word check (V5, SEC-1) runs alongside the denylist.
|
|
12
12
|
*/
|
|
13
|
-
export declare function assertValidImportBinding(name: string): void;
|
|
13
|
+
export declare function assertValidImportBinding(name: string, argName?: string): void;
|
|
14
|
+
/**
|
|
15
|
+
* A plain-identifier object key. Reserved words are legal keys (`{ default: 1 }`), so only the
|
|
16
|
+
* grammar and the denylist apply: a created `__proto__: [...]` would set the prototype.
|
|
17
|
+
*/
|
|
18
|
+
export declare function assertValidPropertyKey(key: string, argName: string): void;
|
|
14
19
|
/**
|
|
15
20
|
* The single validate-before-mutate chokepoint (ADR-02): `validators` runs first over the
|
|
16
21
|
* op's full argument tuple and throws via `dialectError` on any failure — `body` (the actual
|
|
@@ -86,10 +86,18 @@ export function assertValidElementName(elementName) {
|
|
|
86
86
|
* `IdentifierName` (including reserved words) — `import { name }` requires a `BindingIdentifier`,
|
|
87
87
|
* so a reserved-word check (V5, SEC-1) runs alongside the denylist.
|
|
88
88
|
*/
|
|
89
|
-
export function assertValidImportBinding(name) {
|
|
90
|
-
assertGrammar(
|
|
91
|
-
assertNotReservedWord(
|
|
92
|
-
assertNotDenylisted(
|
|
89
|
+
export function assertValidImportBinding(name, argName = "name") {
|
|
90
|
+
assertGrammar(argName, name, IMPORT_BINDING_GRAMMAR, "be a valid JavaScript identifier");
|
|
91
|
+
assertNotReservedWord(argName, name);
|
|
92
|
+
assertNotDenylisted(argName, name);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* A plain-identifier object key. Reserved words are legal keys (`{ default: 1 }`), so only the
|
|
96
|
+
* grammar and the denylist apply: a created `__proto__: [...]` would set the prototype.
|
|
97
|
+
*/
|
|
98
|
+
export function assertValidPropertyKey(key, argName) {
|
|
99
|
+
assertGrammar(argName, key, IMPORT_BINDING_GRAMMAR, "use object keys that are valid JavaScript identifiers");
|
|
100
|
+
assertNotDenylisted(argName, key);
|
|
93
101
|
}
|
|
94
102
|
/**
|
|
95
103
|
* The single validate-before-mutate chokepoint (ADR-02): `validators` runs first over the
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { SourceFile } from "ts-morph";
|
|
2
|
+
import { type Handle } from "../../core/define-dialect.ts";
|
|
3
|
+
import { typescriptOps } from "../typescript/pack.ts";
|
|
4
|
+
import type { ArrayElement } from "../typescript/array-element.ts";
|
|
5
|
+
import { type InjectionOptions, type InputOptions, type LazyRouteSpec, type NamedImport, type OutputOptions, type RouteSpec, type StateOptions } from "./ops.ts";
|
|
6
|
+
export * as astLibrary from "ts-morph";
|
|
7
|
+
export type { InjectionOptions, InputOptions, LazyRouteSpec, NamedImport, OutputOptions, RouteSpec, StateOptions };
|
|
8
|
+
type InsertOp = (ast: SourceFile, element: ArrayElement, imports?: readonly NamedImport[]) => void;
|
|
9
|
+
type AngularOps = {
|
|
10
|
+
addComponentImport: InsertOp;
|
|
11
|
+
addComponentProvider: InsertOp;
|
|
12
|
+
addRootProvider: InsertOp;
|
|
13
|
+
addRoute: (ast: SourceFile, route: RouteSpec) => void;
|
|
14
|
+
addLazyRoute: (ast: SourceFile, route: LazyRouteSpec) => void;
|
|
15
|
+
addInjection: (ast: SourceFile, name: string, token: ArrayElement, options?: InjectionOptions) => void;
|
|
16
|
+
addInput: (ast: SourceFile, name: string, options?: InputOptions) => void;
|
|
17
|
+
addModel: (ast: SourceFile, name: string, options?: InputOptions) => void;
|
|
18
|
+
addOutput: (ast: SourceFile, name: string, options?: OutputOptions) => void;
|
|
19
|
+
addSignal: (ast: SourceFile, name: string, initial: string, options?: StateOptions) => void;
|
|
20
|
+
addComputed: (ast: SourceFile, name: string, expression: string, options?: StateOptions) => void;
|
|
21
|
+
addNgModuleImport: InsertOp;
|
|
22
|
+
addDeclaration: InsertOp;
|
|
23
|
+
addNgModuleProvider: InsertOp;
|
|
24
|
+
addNgModuleExport: InsertOp;
|
|
25
|
+
addBootstrap: InsertOp;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Opens a `.ts` file of an Angular project for editing. The handle carries every
|
|
29
|
+
* `@pbuilder/sdk/typescript` op plus the Angular ops below, chained on one handle and written
|
|
30
|
+
* once.
|
|
31
|
+
*
|
|
32
|
+
* Each op takes `(element, imports?)`: `element` is an identifier, or `{ expression }` for
|
|
33
|
+
* code such as `provideHttpClient(withFetch())`, and `imports` lists the `{ name, from }`
|
|
34
|
+
* named imports that code needs, each added with `addImport`'s rules. An element already
|
|
35
|
+
* listed is a no-op.
|
|
36
|
+
*
|
|
37
|
+
* - `addComponentImport` / `addComponentProvider` append to `imports` / `providers` of the
|
|
38
|
+
* file's only `@Component`, creating the metadata object or the array when missing.
|
|
39
|
+
* - `addRootProvider` appends to the root `providers` of the file's only variable typed
|
|
40
|
+
* `ApplicationConfig`, or of the only `bootstrapApplication(...)` config object.
|
|
41
|
+
* - `addRoute({ path, component, from })` imports `component` and adds an eager route;
|
|
42
|
+
* `addLazyRoute({ path, loadComponent | loadChildren, export? })` adds a lazy one. Both go
|
|
43
|
+
* to the file's only variable typed `Routes` or `Route[]`, before a `**` route when there is
|
|
44
|
+
* one, since the first matching route wins. A different route with the same `path` rejects.
|
|
45
|
+
*
|
|
46
|
+
* They reject, leaving the file unchanged, when the target is missing, ambiguous, or not a
|
|
47
|
+
* literal. Use `.modify(fn)` for those files.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* import * as angular from "@pbuilder/sdk/angular";
|
|
51
|
+
*
|
|
52
|
+
* // src/app/app.config.ts before:
|
|
53
|
+
* // export const appConfig: ApplicationConfig = { providers: [provideRouter(routes)] };
|
|
54
|
+
* export const run = async () => {
|
|
55
|
+
* await angular.find("src/app/app.config.ts").addRootProvider({ expression: "provideHttpClient(withFetch())" }, [
|
|
56
|
+
* { name: "provideHttpClient", from: "@angular/common/http" },
|
|
57
|
+
* { name: "withFetch", from: "@angular/common/http" },
|
|
58
|
+
* ]);
|
|
59
|
+
* };
|
|
60
|
+
* // -> import { provideHttpClient, withFetch } from "@angular/common/http";
|
|
61
|
+
* // -> export const appConfig: ApplicationConfig = { providers: [provideRouter(routes), provideHttpClient(withFetch())] };
|
|
62
|
+
*/
|
|
63
|
+
export declare function find(path: string): Handle<"found", SourceFile, AngularOps & typeof typescriptOps>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { defineDialect, defineOpPack, withOps } from "../../core/define-dialect.js";
|
|
2
|
+
import { parse, print } from "../typescript/ast.js";
|
|
3
|
+
import { typescriptOps } from "../typescript/pack.js";
|
|
4
|
+
import { addBootstrap, addComponentImport, addComponentProvider, addComputed, addDeclaration, addInjection, addInput, addLazyRoute, addModel, addNgModuleExport, addNgModuleImport, addNgModuleProvider, addOutput, addRootProvider, addRoute, addSignal, } from "./ops.js";
|
|
5
|
+
export * as astLibrary from "ts-morph";
|
|
6
|
+
const angularOps = defineOpPack({
|
|
7
|
+
addComponentImport,
|
|
8
|
+
addComponentProvider,
|
|
9
|
+
addRootProvider,
|
|
10
|
+
addRoute,
|
|
11
|
+
addLazyRoute,
|
|
12
|
+
addInjection,
|
|
13
|
+
addInput,
|
|
14
|
+
addModel,
|
|
15
|
+
addOutput,
|
|
16
|
+
addSignal,
|
|
17
|
+
addComputed,
|
|
18
|
+
addNgModuleImport,
|
|
19
|
+
addDeclaration,
|
|
20
|
+
addNgModuleProvider,
|
|
21
|
+
addNgModuleExport,
|
|
22
|
+
addBootstrap,
|
|
23
|
+
});
|
|
24
|
+
const baseDialect = defineDialect({
|
|
25
|
+
extensions: [".ts"],
|
|
26
|
+
ast: { parse, print },
|
|
27
|
+
ops: {},
|
|
28
|
+
});
|
|
29
|
+
const angularDialect = withOps(baseDialect, typescriptOps, angularOps);
|
|
30
|
+
/**
|
|
31
|
+
* Opens a `.ts` file of an Angular project for editing. The handle carries every
|
|
32
|
+
* `@pbuilder/sdk/typescript` op plus the Angular ops below, chained on one handle and written
|
|
33
|
+
* once.
|
|
34
|
+
*
|
|
35
|
+
* Each op takes `(element, imports?)`: `element` is an identifier, or `{ expression }` for
|
|
36
|
+
* code such as `provideHttpClient(withFetch())`, and `imports` lists the `{ name, from }`
|
|
37
|
+
* named imports that code needs, each added with `addImport`'s rules. An element already
|
|
38
|
+
* listed is a no-op.
|
|
39
|
+
*
|
|
40
|
+
* - `addComponentImport` / `addComponentProvider` append to `imports` / `providers` of the
|
|
41
|
+
* file's only `@Component`, creating the metadata object or the array when missing.
|
|
42
|
+
* - `addRootProvider` appends to the root `providers` of the file's only variable typed
|
|
43
|
+
* `ApplicationConfig`, or of the only `bootstrapApplication(...)` config object.
|
|
44
|
+
* - `addRoute({ path, component, from })` imports `component` and adds an eager route;
|
|
45
|
+
* `addLazyRoute({ path, loadComponent | loadChildren, export? })` adds a lazy one. Both go
|
|
46
|
+
* to the file's only variable typed `Routes` or `Route[]`, before a `**` route when there is
|
|
47
|
+
* one, since the first matching route wins. A different route with the same `path` rejects.
|
|
48
|
+
*
|
|
49
|
+
* They reject, leaving the file unchanged, when the target is missing, ambiguous, or not a
|
|
50
|
+
* literal. Use `.modify(fn)` for those files.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* import * as angular from "@pbuilder/sdk/angular";
|
|
54
|
+
*
|
|
55
|
+
* // src/app/app.config.ts before:
|
|
56
|
+
* // export const appConfig: ApplicationConfig = { providers: [provideRouter(routes)] };
|
|
57
|
+
* export const run = async () => {
|
|
58
|
+
* await angular.find("src/app/app.config.ts").addRootProvider({ expression: "provideHttpClient(withFetch())" }, [
|
|
59
|
+
* { name: "provideHttpClient", from: "@angular/common/http" },
|
|
60
|
+
* { name: "withFetch", from: "@angular/common/http" },
|
|
61
|
+
* ]);
|
|
62
|
+
* };
|
|
63
|
+
* // -> import { provideHttpClient, withFetch } from "@angular/common/http";
|
|
64
|
+
* // -> export const appConfig: ApplicationConfig = { providers: [provideRouter(routes), provideHttpClient(withFetch())] };
|
|
65
|
+
*/
|
|
66
|
+
export function find(path) {
|
|
67
|
+
return angularDialect.find(path);
|
|
68
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { type SourceFile } from "ts-morph";
|
|
2
|
+
import { type ArrayElement } from "../typescript/array-element.ts";
|
|
3
|
+
import { type ClassPropertyOptions } from "../typescript/class-property.ts";
|
|
4
|
+
/** A named import an inserted element needs: `import { name } from "from"`. */
|
|
5
|
+
export type NamedImport = {
|
|
6
|
+
name: string;
|
|
7
|
+
from: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Appends `element` to the `imports` of the file's only `@Component`, creating the metadata
|
|
11
|
+
* object or the array when missing, then adds each of `imports`. An equal element is a no-op.
|
|
12
|
+
*/
|
|
13
|
+
export declare const addComponentImport: (ast: SourceFile, element: ArrayElement, imports?: readonly NamedImport[]) => void;
|
|
14
|
+
/** Same as `addComponentImport`, for the component's `providers`. */
|
|
15
|
+
export declare const addComponentProvider: (ast: SourceFile, element: ArrayElement, imports?: readonly NamedImport[]) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Appends `element` to the application's root `providers`: those of the file's only top-level
|
|
18
|
+
* variable typed `ApplicationConfig`, else those of the only `bootstrapApplication(...)` call's
|
|
19
|
+
* config object, created when missing. A config passed by name rejects with the module to edit.
|
|
20
|
+
*/
|
|
21
|
+
export declare const addRootProvider: (ast: SourceFile, element: ArrayElement, imports?: readonly NamedImport[]) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Appends `element` to `imports` of the file's only `@NgModule`, creating the metadata object
|
|
24
|
+
* or the array when missing, then adds each of `imports`. An equal element is a no-op.
|
|
25
|
+
*/
|
|
26
|
+
export declare const addNgModuleImport: (ast: SourceFile, element: ArrayElement, imports?: readonly NamedImport[]) => void;
|
|
27
|
+
/** Same as `addNgModuleImport`, for `declarations`: components, directives and pipes declared `standalone: false`. */
|
|
28
|
+
export declare const addDeclaration: (ast: SourceFile, element: ArrayElement, imports?: readonly NamedImport[]) => void;
|
|
29
|
+
/** Same as `addNgModuleImport`, for `providers`. */
|
|
30
|
+
export declare const addNgModuleProvider: (ast: SourceFile, element: ArrayElement, imports?: readonly NamedImport[]) => void;
|
|
31
|
+
/** Same as `addNgModuleImport`, for `exports`. */
|
|
32
|
+
export declare const addNgModuleExport: (ast: SourceFile, element: ArrayElement, imports?: readonly NamedImport[]) => void;
|
|
33
|
+
/** Same as `addNgModuleImport`, for `bootstrap`. */
|
|
34
|
+
export declare const addBootstrap: (ast: SourceFile, element: ArrayElement, imports?: readonly NamedImport[]) => void;
|
|
35
|
+
/** An eager route: `{ path, component }`, with `component` imported from `from`. */
|
|
36
|
+
export type RouteSpec = {
|
|
37
|
+
path: string;
|
|
38
|
+
component: string;
|
|
39
|
+
from: string;
|
|
40
|
+
};
|
|
41
|
+
/** A lazy route: `loadComponent` or `loadChildren` is the module to import; `export` names its export, else the default one. */
|
|
42
|
+
export type LazyRouteSpec = {
|
|
43
|
+
path: string;
|
|
44
|
+
loadComponent?: string;
|
|
45
|
+
loadChildren?: string;
|
|
46
|
+
export?: string;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Imports `route.component` from `route.from` and adds `{ path, component }` to the file's only
|
|
50
|
+
* top-level variable typed `Routes` or `Route[]`, before a `**` route when there is one. An
|
|
51
|
+
* equal route is a no-op; a different route with the same `path` rejects.
|
|
52
|
+
*/
|
|
53
|
+
export declare function addRoute(ast: SourceFile, route: RouteSpec): void;
|
|
54
|
+
/**
|
|
55
|
+
* Same placement and duplicate rules as `addRoute`, for a lazy route:
|
|
56
|
+
* `{ path, loadComponent: () => import("<module>") }`, or `loadChildren`, followed by
|
|
57
|
+
* `.then((m) => m.<export>)` when `export` is given. Adds no import.
|
|
58
|
+
*/
|
|
59
|
+
export declare function addLazyRoute(ast: SourceFile, route: LazyRouteSpec): void;
|
|
60
|
+
type Scope = NonNullable<ClassPropertyOptions["scope"]>;
|
|
61
|
+
/** Options of `addInput` and `addModel`; `type` is the generic argument, `initial` the default value. */
|
|
62
|
+
export type InputOptions = {
|
|
63
|
+
type?: string;
|
|
64
|
+
initial?: string;
|
|
65
|
+
required?: boolean;
|
|
66
|
+
imports?: readonly NamedImport[];
|
|
67
|
+
};
|
|
68
|
+
/** Options of `addOutput`. */
|
|
69
|
+
export type OutputOptions = {
|
|
70
|
+
type?: string;
|
|
71
|
+
imports?: readonly NamedImport[];
|
|
72
|
+
};
|
|
73
|
+
/** Options of `addInjection`; `scope` defaults to `private`. */
|
|
74
|
+
export type InjectionOptions = {
|
|
75
|
+
scope?: Scope;
|
|
76
|
+
imports?: readonly NamedImport[];
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Adds `readonly name = input<type>(initial)`, or `input.required<type>()` with `required`, to
|
|
80
|
+
* the file's only `@Component` or `@Directive` class, after its last property, and imports
|
|
81
|
+
* `input`. The same declaration already there is a no-op.
|
|
82
|
+
*/
|
|
83
|
+
export declare const addInput: (ast: SourceFile, name: string, options?: InputOptions) => void;
|
|
84
|
+
/** Same as `addInput`, with `model()` for a two-way bound property. */
|
|
85
|
+
export declare const addModel: (ast: SourceFile, name: string, options?: InputOptions) => void;
|
|
86
|
+
/** Adds `readonly name = output<type>()` to the file's only `@Component` or `@Directive` class and imports `output`. */
|
|
87
|
+
export declare function addOutput(ast: SourceFile, name: string, options?: OutputOptions): void;
|
|
88
|
+
/**
|
|
89
|
+
* Adds `private readonly name = inject(token)` to the file's only class decorated with
|
|
90
|
+
* `@Component`, `@Directive`, `@Pipe`, `@Injectable` or `@Service`, and imports `inject`.
|
|
91
|
+
* `token` is an identifier or `{ expression }`; `options.scope` replaces `private`.
|
|
92
|
+
*/
|
|
93
|
+
export declare function addInjection(ast: SourceFile, name: string, token: ArrayElement, options?: InjectionOptions): void;
|
|
94
|
+
/** Options of `addSignal` and `addComputed`; `scope` defaults to `protected`. */
|
|
95
|
+
export type StateOptions = {
|
|
96
|
+
type?: string;
|
|
97
|
+
scope?: Scope;
|
|
98
|
+
imports?: readonly NamedImport[];
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Adds `protected readonly name = signal<type>(initial)` to the file's only `@Component` or
|
|
102
|
+
* `@Directive` class and imports `signal`. `options.scope` replaces `protected`.
|
|
103
|
+
*/
|
|
104
|
+
export declare function addSignal(ast: SourceFile, name: string, initial: string, options?: StateOptions): void;
|
|
105
|
+
/**
|
|
106
|
+
* Adds `protected readonly name = computed<type>(() => expression)` to the file's only
|
|
107
|
+
* `@Component` or `@Directive` class and imports `computed`. `options.scope` replaces `protected`.
|
|
108
|
+
*/
|
|
109
|
+
export declare function addComputed(ast: SourceFile, name: string, expression: string, options?: StateOptions): void;
|
|
110
|
+
export {};
|