@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
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Node, SyntaxKind } from "ts-morph";
|
|
2
|
+
import { dialectError } from "../../core/dialect-error.js";
|
|
3
|
+
import { handlePathFor } from "../../core/dialect-handle.js";
|
|
4
|
+
import { assertValidImportBinding } from "../../core/jsx-name-validator.js";
|
|
5
|
+
import { elementNameEcho } from "../../core/reject-tail.js";
|
|
6
|
+
import { elementText, tokensOf } from "./array-element.js";
|
|
7
|
+
import { parse } from "./ast.js";
|
|
8
|
+
const OPTIONS_SHAPE = '`options` must be { scope?, readonly? } with scope "public", "protected" or "private" — fix the options, or use ' +
|
|
9
|
+
"`.modify()` to make this edit directly.";
|
|
10
|
+
export function assertClassPropertyOptions(options) {
|
|
11
|
+
if (options === undefined)
|
|
12
|
+
return;
|
|
13
|
+
const valid = typeof options === "object" &&
|
|
14
|
+
options !== null &&
|
|
15
|
+
Object.keys(options).every((key) => key === "scope" || key === "readonly") &&
|
|
16
|
+
[undefined, "public", "protected", "private"].includes(options.scope) &&
|
|
17
|
+
[undefined, true, false].includes(options.readonly);
|
|
18
|
+
if (!valid)
|
|
19
|
+
throw dialectError(OPTIONS_SHAPE);
|
|
20
|
+
}
|
|
21
|
+
function declarationOf(property) {
|
|
22
|
+
const modifiers = [property.scope, property.readonly ? "readonly" : undefined].filter((m) => m !== undefined);
|
|
23
|
+
return [...modifiers, `${property.name} = ${property.initializer};`].join(" ");
|
|
24
|
+
}
|
|
25
|
+
// Tokens without a trailing `;`, so `b = 1` and `b = 1;` are the same declaration.
|
|
26
|
+
function declarationTokens(node) {
|
|
27
|
+
const tokens = tokensOf(node);
|
|
28
|
+
return tokens.at(-1) === ";" ? tokens.slice(0, -1) : tokens;
|
|
29
|
+
}
|
|
30
|
+
function sameDeclaration(member, text) {
|
|
31
|
+
const parsed = parse(`class X {\n${text}\n}`).getClassOrThrow("X").getMembers()[0];
|
|
32
|
+
const a = declarationTokens(member);
|
|
33
|
+
const b = declarationTokens(parsed);
|
|
34
|
+
return a.length === b.length && a.every((token, index) => token === b[index]);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Inserts `property` into `cls`: after its last property, else before its first member, else
|
|
38
|
+
* into its empty body. A member named `property.name` with the same declaration is a no-op;
|
|
39
|
+
* any other member with that name rejects through `fail`.
|
|
40
|
+
*/
|
|
41
|
+
export function insertClassProperty(ast, cls, property, fail) {
|
|
42
|
+
const text = declarationOf(property);
|
|
43
|
+
const members = cls.getMembers();
|
|
44
|
+
const named = members.filter((member) => Node.hasName(member) && member.getName() === property.name);
|
|
45
|
+
if (named.length > 0) {
|
|
46
|
+
if (named.length === 1 && Node.isPropertyDeclaration(named[0]) && sameDeclaration(named[0], text))
|
|
47
|
+
return;
|
|
48
|
+
fail(`the class already has a different member named "${elementNameEcho(property.name)}"`);
|
|
49
|
+
}
|
|
50
|
+
const source = ast.getFullText();
|
|
51
|
+
const indentAt = (position) => /^[ \t]*/.exec(source.slice(source.lastIndexOf("\n", position - 1) + 1))[0];
|
|
52
|
+
const anchor = members.filter((member) => Node.isPropertyDeclaration(member)).at(-1);
|
|
53
|
+
if (anchor !== undefined) {
|
|
54
|
+
const lineEnd = source.indexOf("\n", anchor.getEnd());
|
|
55
|
+
const rest = source.slice(anchor.getEnd(), lineEnd === -1 ? source.length : lineEnd);
|
|
56
|
+
// Only trivia after the property means it ends its line: the new one goes on the next line.
|
|
57
|
+
if (lineEnd !== -1 && /^\s*(\/\/.*|\/\*.*\*\/\s*)?$/.test(rest))
|
|
58
|
+
ast.insertText(lineEnd, `\n${indentAt(anchor.getStart())}${text}`);
|
|
59
|
+
else
|
|
60
|
+
ast.insertText(anchor.getEnd(), ` ${text}`);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const first = members[0];
|
|
64
|
+
if (first !== undefined) {
|
|
65
|
+
const start = first.getStart(true);
|
|
66
|
+
const open = cls.getFirstChildByKindOrThrow(SyntaxKind.OpenBraceToken);
|
|
67
|
+
if (source.slice(open.getEnd(), start).includes("\n"))
|
|
68
|
+
ast.insertText(start, `${text}\n${indentAt(start)}`);
|
|
69
|
+
else
|
|
70
|
+
ast.insertText(start, `${text} `);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const open = cls.getFirstChildByKindOrThrow(SyntaxKind.OpenBraceToken);
|
|
74
|
+
const close = cls.getLastChildByKindOrThrow(SyntaxKind.CloseBraceToken);
|
|
75
|
+
const indent = indentAt(cls.getStart());
|
|
76
|
+
ast.replaceText([open.getEnd(), close.getStart()], `\n${indent} ${text}\n${indent}`);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Adds `{scope }{readonly }name = initializer;` to the only class named `className`, after its
|
|
80
|
+
* last property, else as its first member, keeping the members' indentation. `initializer` must
|
|
81
|
+
* parse as exactly one expression and is otherwise author-trusted. A property with the same
|
|
82
|
+
* declaration is a no-op; any other member named `name` rejects.
|
|
83
|
+
*/
|
|
84
|
+
export function addClassProperty(ast, className, name, initializer, options) {
|
|
85
|
+
assertValidImportBinding(className, "className");
|
|
86
|
+
assertValidImportBinding(name, "name");
|
|
87
|
+
assertClassPropertyOptions(options);
|
|
88
|
+
const value = elementText({ expression: initializer });
|
|
89
|
+
const fail = (reason) => {
|
|
90
|
+
throw dialectError(`addClassProperty("${elementNameEcho(className)}", "${elementNameEcho(name)}") on "${handlePathFor(ast)}" — ${reason}. Edit it with .modify().`);
|
|
91
|
+
};
|
|
92
|
+
const classes = ast.getDescendantsOfKind(SyntaxKind.ClassDeclaration).filter((cls) => cls.getName() === className);
|
|
93
|
+
if (classes.length === 0)
|
|
94
|
+
fail(`no class named "${elementNameEcho(className)}" was found`);
|
|
95
|
+
if (classes.length > 1)
|
|
96
|
+
fail(`${classes.length} classes are named "${elementNameEcho(className)}"; exactly one is required`);
|
|
97
|
+
insertClassProperty(ast, classes[0], { ...options, name, initializer: value }, fail);
|
|
98
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { SourceFile } from "ts-morph";
|
|
2
2
|
import { type Handle } from "../../core/define-dialect.ts";
|
|
3
|
+
import type { ArrayElement, ArrayLocator } from "./array-element.ts";
|
|
4
|
+
import type { ClassPropertyOptions } from "./class-property.ts";
|
|
3
5
|
export * as astLibrary from "ts-morph";
|
|
4
6
|
type TypeScriptOps = {
|
|
5
7
|
addImport: (ast: SourceFile, name: string, from: string) => void;
|
|
@@ -26,13 +28,16 @@ type TypeScriptOps = {
|
|
|
26
28
|
removeExportAll: (ast: SourceFile, from: string, options?: {
|
|
27
29
|
as?: string;
|
|
28
30
|
}) => void;
|
|
31
|
+
addArrayElement: (ast: SourceFile, locator: ArrayLocator, element: ArrayElement) => void;
|
|
32
|
+
addClassProperty: (ast: SourceFile, className: string, name: string, initializer: string, options?: ClassPropertyOptions) => void;
|
|
29
33
|
};
|
|
30
34
|
/**
|
|
31
35
|
* Opens a TypeScript file for dialect-aware editing — the dialect's entry verb into a Session
|
|
32
36
|
* (REQ-DG-01.2). Returns an awaitable, chainable `Handle` exposing the universal `.modify()`
|
|
33
37
|
* escape hatch plus the dialect's named ops (`addImport`, `addDefaultImport`,
|
|
34
38
|
* `addNamespaceImport`, `addTypeImport`, `addSideEffectImport`, `addReExport`, `addExportAll`,
|
|
35
|
-
* `addFunction`, `addVariable`, `addClass`, `removeImport`, `removeExport`, `removeExportAll
|
|
39
|
+
* `addFunction`, `addVariable`, `addClass`, `removeImport`, `removeExport`, `removeExportAll`,
|
|
40
|
+
* `addArrayElement`, `addClassProperty`). Reads route through `Session.read` only (REQ-MC-03); edits
|
|
36
41
|
* coalesce into a single `modify` at flush (REQ-MC-01/02).
|
|
37
42
|
*
|
|
38
43
|
* @example
|
|
@@ -21,7 +21,8 @@ const typescriptDialect = withOps(baseDialect, opsPack);
|
|
|
21
21
|
* (REQ-DG-01.2). Returns an awaitable, chainable `Handle` exposing the universal `.modify()`
|
|
22
22
|
* escape hatch plus the dialect's named ops (`addImport`, `addDefaultImport`,
|
|
23
23
|
* `addNamespaceImport`, `addTypeImport`, `addSideEffectImport`, `addReExport`, `addExportAll`,
|
|
24
|
-
* `addFunction`, `addVariable`, `addClass`, `removeImport`, `removeExport`, `removeExportAll
|
|
24
|
+
* `addFunction`, `addVariable`, `addClass`, `removeImport`, `removeExport`, `removeExportAll`,
|
|
25
|
+
* `addArrayElement`, `addClassProperty`). Reads route through `Session.read` only (REQ-MC-03); edits
|
|
25
26
|
* coalesce into a single `modify` at flush (REQ-MC-01/02).
|
|
26
27
|
*
|
|
27
28
|
* @example
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { addImport, addDefaultImport, addNamespaceImport, addTypeImport, addSideEffectImport, addReExport, addExportAll, addFunction, addVariable, addClass, removeImport, removeExport, removeExportAll } from "./ops.ts";
|
|
2
|
+
import { addArrayElement } from "./array-element.ts";
|
|
3
|
+
import { addClassProperty } from "./class-property.ts";
|
|
2
4
|
export declare const typescriptOps: {
|
|
3
5
|
addImport: typeof addImport;
|
|
4
6
|
addDefaultImport: typeof addDefaultImport;
|
|
@@ -13,4 +15,6 @@ export declare const typescriptOps: {
|
|
|
13
15
|
removeImport: typeof removeImport;
|
|
14
16
|
removeExport: typeof removeExport;
|
|
15
17
|
removeExportAll: typeof removeExportAll;
|
|
18
|
+
addArrayElement: typeof addArrayElement;
|
|
19
|
+
addClassProperty: typeof addClassProperty;
|
|
16
20
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// The runtime op pack lives apart from index.ts so a dialect that extends TypeScript
|
|
2
2
|
// (src/dialects/nestjs) composes the same ops without importing the `find` entry point.
|
|
3
3
|
import { addImport, addDefaultImport, addNamespaceImport, addTypeImport, addSideEffectImport, addReExport, addExportAll, addFunction, addVariable, addClass, removeImport, removeExport, removeExportAll, } from "./ops.js";
|
|
4
|
+
import { addArrayElement } from "./array-element.js";
|
|
5
|
+
import { addClassProperty } from "./class-property.js";
|
|
4
6
|
export const typescriptOps = {
|
|
5
7
|
addImport,
|
|
6
8
|
addDefaultImport,
|
|
@@ -15,4 +17,6 @@ export const typescriptOps = {
|
|
|
15
17
|
removeImport,
|
|
16
18
|
removeExport,
|
|
17
19
|
removeExportAll,
|
|
20
|
+
addArrayElement,
|
|
21
|
+
addClassProperty,
|
|
18
22
|
};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"manifestVersion": 1,
|
|
3
3
|
"algorithm": "sha256",
|
|
4
4
|
"entry": "dist/bin/pbuilder-runner.js",
|
|
5
|
-
"packageVersion": "0.
|
|
5
|
+
"packageVersion": "0.10.0",
|
|
6
6
|
"files": [
|
|
7
7
|
{
|
|
8
8
|
"path": "dist/bin/pbuilder-runner.js",
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
},
|
|
99
99
|
{
|
|
100
100
|
"path": "package.json",
|
|
101
|
-
"sha256": "
|
|
101
|
+
"sha256": "ef9f722cff4d538c25daad7eedc26b2df66a2784bba63a8e97f4eef339af33f8"
|
|
102
102
|
}
|
|
103
103
|
]
|
|
104
104
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pbuilder/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "The public TypeScript/Bun authoring layer for Project Builder schematics. Authors file mutations (create, replaceContent, remove, rename, move, copy, copyIn), lowers them to IR, and talks to the Project Builder engine over framed stdio (see docs/engine-sdk-wire-spec.md).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -78,10 +78,20 @@
|
|
|
78
78
|
"import": "./dist/dialects/nestjs/index.js",
|
|
79
79
|
"default": "./dist/dialects/nestjs/index.js"
|
|
80
80
|
},
|
|
81
|
+
"./angular": {
|
|
82
|
+
"types": "./dist/dialects/angular/index.d.ts",
|
|
83
|
+
"import": "./dist/dialects/angular/index.js",
|
|
84
|
+
"default": "./dist/dialects/angular/index.js"
|
|
85
|
+
},
|
|
81
86
|
"./yaml": {
|
|
82
87
|
"types": "./dist/dialects/yaml/index.d.ts",
|
|
83
88
|
"import": "./dist/dialects/yaml/index.js",
|
|
84
89
|
"default": "./dist/dialects/yaml/index.js"
|
|
90
|
+
},
|
|
91
|
+
"./css": {
|
|
92
|
+
"types": "./dist/dialects/css/index.d.ts",
|
|
93
|
+
"import": "./dist/dialects/css/index.js",
|
|
94
|
+
"default": "./dist/dialects/css/index.js"
|
|
85
95
|
}
|
|
86
96
|
},
|
|
87
97
|
"scripts": {
|
|
@@ -107,6 +117,7 @@
|
|
|
107
117
|
},
|
|
108
118
|
"dependencies": {
|
|
109
119
|
"jsonc-parser": "3.3.1",
|
|
120
|
+
"postcss": "8.5.30",
|
|
110
121
|
"ts-morph": "28.0.0",
|
|
111
122
|
"yaml": "2.9.1"
|
|
112
123
|
}
|