@pina-rs/codama-renderer-cpi 0.0.0 → 0.13.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 +36 -1
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/dist/types/index.d.ts +14 -0
- package/package.json +51 -1
- package/CHANGELOG.md +0 -3
- package/LICENSE +0 -1
package/README.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
1
|
# @pina-rs/codama-renderer-cpi
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Codama visitor that renders a standalone, `no_std` Pina CPI crate from the pipeline's current `RootNode`.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add @pina-rs/codama-renderer-cpi codama
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Use it in `codama.json`. Codama accepts both Codama and Anchor IDLs and performs Anchor normalization before visitors run.
|
|
10
|
+
|
|
11
|
+
```json
|
|
12
|
+
{
|
|
13
|
+
"idl": "./target/idl/counter.json",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"cpi": {
|
|
16
|
+
"from": "@pina-rs/codama-renderer-cpi",
|
|
17
|
+
"args": ["./clients/counter-cpi"]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
codama run cpi
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The visitor uses the native Pina CLI bundled by `@pina-rs/cli` and sends the current pipeline root over standard input. The generated crate exposes typed account sets and instruction builders with `.invoke()` and `.invoke_signed()` methods backed by Pina's validated, allocator-free CPI context.
|
|
28
|
+
|
|
29
|
+
Pass visitor options from a programmatic Codama pipeline when you need explicit destination handling:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
await codama.accept(renderVisitor("./clients/counter-cpi", {
|
|
33
|
+
mode: "update",
|
|
34
|
+
scaffold: false,
|
|
35
|
+
}));
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`mode` accepts `auto` (the default), `create`, `update`, or `overwrite`. Updates replace only `src/generated` and preserve an existing `Cargo.toml` and `src/lib.rs`. `scaffold: false` generates only source files; `overwrite` removes the complete destination before rendering.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { spawnSync } from "child_process";
|
|
3
|
+
import { createRequire } from "module";
|
|
4
|
+
import { rootNodeVisitor } from "codama";
|
|
5
|
+
var MAX_OUTPUT_BYTES = 16 * 1024;
|
|
6
|
+
function renderVisitor(outputDir, options = {}) {
|
|
7
|
+
return rootNodeVisitor((root) => {
|
|
8
|
+
renderRoot(root, outputDir, options);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function renderRoot(root, outputDir, options = {}) {
|
|
12
|
+
const [command, ...prefixArgs] = options.pinaCommand ?? defaultPinaCommand();
|
|
13
|
+
const args = [
|
|
14
|
+
...prefixArgs,
|
|
15
|
+
"cpi",
|
|
16
|
+
"--stdin",
|
|
17
|
+
"--output",
|
|
18
|
+
outputDir,
|
|
19
|
+
"--mode",
|
|
20
|
+
options.mode ?? "auto"
|
|
21
|
+
];
|
|
22
|
+
if (options.scaffold === false) {
|
|
23
|
+
args.push("--no-scaffold");
|
|
24
|
+
}
|
|
25
|
+
const result = spawnSync(
|
|
26
|
+
command,
|
|
27
|
+
args,
|
|
28
|
+
{
|
|
29
|
+
encoding: "utf8",
|
|
30
|
+
input: JSON.stringify(root),
|
|
31
|
+
maxBuffer: MAX_OUTPUT_BYTES,
|
|
32
|
+
windowsHide: true
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
if (result.error) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Failed to run the Pina CPI renderer: ${result.error.message}`,
|
|
38
|
+
{
|
|
39
|
+
cause: result.error
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
if (result.status !== 0) {
|
|
44
|
+
const detail = diagnosticText(result.stderr) || diagnosticText(result.stdout);
|
|
45
|
+
const suffix = detail ? `: ${detail}` : "";
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Pina CPI renderer exited with status ${result.status ?? "unknown"}${suffix}`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function defaultPinaCommand() {
|
|
52
|
+
const require2 = createRequire(import.meta.url);
|
|
53
|
+
const launcher = require2.resolve("@pina-rs/cli/bin/pina.cjs");
|
|
54
|
+
return [process.execPath, launcher];
|
|
55
|
+
}
|
|
56
|
+
function diagnosticText(value) {
|
|
57
|
+
return (value ?? "").slice(-MAX_OUTPUT_BYTES).trim().replaceAll(
|
|
58
|
+
/[\u0000-\u001f\u007f]/g,
|
|
59
|
+
(character) => JSON.stringify(character).slice(1, -1)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
var index_default = renderVisitor;
|
|
63
|
+
export {
|
|
64
|
+
index_default as default,
|
|
65
|
+
renderRoot,
|
|
66
|
+
renderVisitor
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { spawnSync } from \"node:child_process\";\nimport { createRequire } from \"node:module\";\n\nimport type { RootNode, Visitor } from \"codama\";\nimport { rootNodeVisitor } from \"codama\";\n\nconst MAX_OUTPUT_BYTES = 16 * 1024;\n\nexport interface RenderOptions {\n\t/** Override the Pina command and any arguments placed before `cpi`. */\n\tpinaCommand?: readonly [string, ...string[]];\n\t/** Control whether the destination is created, updated, or replaced. */\n\tmode?: \"auto\" | \"create\" | \"update\" | \"overwrite\";\n\t/** Create Cargo.toml and src/lib.rs when they are missing. */\n\tscaffold?: boolean;\n}\n\n/** Creates a Codama visitor that renders a standalone, `no_std` Pina CPI crate. */\nexport function renderVisitor(\n\toutputDir: string,\n\toptions: RenderOptions = {},\n): Visitor<void, \"rootNode\"> {\n\treturn rootNodeVisitor((root: RootNode) => {\n\t\trenderRoot(root, outputDir, options);\n\t});\n}\n\n/** Renders one normalized Codama root through the native Pina renderer. */\nexport function renderRoot(\n\troot: RootNode,\n\toutputDir: string,\n\toptions: RenderOptions = {},\n): void {\n\tconst [command, ...prefixArgs] = options.pinaCommand ?? defaultPinaCommand();\n\tconst args = [\n\t\t...prefixArgs,\n\t\t\"cpi\",\n\t\t\"--stdin\",\n\t\t\"--output\",\n\t\toutputDir,\n\t\t\"--mode\",\n\t\toptions.mode ?? \"auto\",\n\t];\n\tif (options.scaffold === false) {\n\t\targs.push(\"--no-scaffold\");\n\t}\n\tconst result = spawnSync(\n\t\tcommand,\n\t\targs,\n\t\t{\n\t\t\tencoding: \"utf8\",\n\t\t\tinput: JSON.stringify(root),\n\t\t\tmaxBuffer: MAX_OUTPUT_BYTES,\n\t\t\twindowsHide: true,\n\t\t},\n\t);\n\n\tif (result.error) {\n\t\tthrow new Error(\n\t\t\t`Failed to run the Pina CPI renderer: ${result.error.message}`,\n\t\t\t{\n\t\t\t\tcause: result.error,\n\t\t\t},\n\t\t);\n\t}\n\n\tif (result.status !== 0) {\n\t\tconst detail = diagnosticText(result.stderr) ||\n\t\t\tdiagnosticText(result.stdout);\n\t\tconst suffix = detail ? `: ${detail}` : \"\";\n\t\tthrow new Error(\n\t\t\t`Pina CPI renderer exited with status ${\n\t\t\t\tresult.status ?? \"unknown\"\n\t\t\t}${suffix}`,\n\t\t);\n\t}\n}\n\nfunction defaultPinaCommand(): readonly [string, ...string[]] {\n\tconst require = createRequire(import.meta.url);\n\tconst launcher = require.resolve(\"@pina-rs/cli/bin/pina.cjs\");\n\n\treturn [process.execPath, launcher];\n}\n\nfunction diagnosticText(value: string | null): string {\n\treturn (value ?? \"\")\n\t\t.slice(-MAX_OUTPUT_BYTES)\n\t\t.trim()\n\t\t.replaceAll(\n\t\t\t/[\\u0000-\\u001f\\u007f]/g,\n\t\t\t(character) => JSON.stringify(character).slice(1,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,UAAU,QAAQ;AACnC;AAEA,SAAS,eAAe,OAA8B;AACrD,UAAQ,SAAS,IACf,MAAM,CAAC,gBAAgB,EACvB,KAAK,EACL;AAAA,IACA;AAAA,IACA,CAAC,cAAc,KAAK,UAAU,SAAS,EAAE,MAAM,GAAG,EAAE;AAAA,EACrD;AACF;AAEA,IAAO,gBAAQ;","names":["require"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RootNode, Visitor } from "codama";
|
|
2
|
+
export interface RenderOptions {
|
|
3
|
+
/** Override the Pina command and any arguments placed before `cpi`. */
|
|
4
|
+
pinaCommand?: readonly [string, ...string[]];
|
|
5
|
+
/** Control whether the destination is created, updated, or replaced. */
|
|
6
|
+
mode?: "auto" | "create" | "update" | "overwrite";
|
|
7
|
+
/** Create Cargo.toml and src/lib.rs when they are missing. */
|
|
8
|
+
scaffold?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** Creates a Codama visitor that renders a standalone, `no_std` Pina CPI crate. */
|
|
11
|
+
export declare function renderVisitor(outputDir: string, options?: RenderOptions): Visitor<void, "rootNode">;
|
|
12
|
+
/** Renders one normalized Codama root through the native Pina renderer. */
|
|
13
|
+
export declare function renderRoot(root: RootNode, outputDir: string, options?: RenderOptions): void;
|
|
14
|
+
export default renderVisitor;
|
package/package.json
CHANGED
|
@@ -1 +1,51 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@pina-rs/codama-renderer-cpi",
|
|
3
|
+
"version": "0.13.0",
|
|
4
|
+
"description": "Codama visitor that renders standalone Pina CPI crates",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://pina.rs",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/pina-rs/pina/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/pina-rs/pina.git",
|
|
13
|
+
"directory": "packages/codama-renderer-cpi"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"types": "./dist/types/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
"types": "./dist/types/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"./dist",
|
|
25
|
+
"./README.md"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"provenance": true
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "rimraf dist && tsup && tsc -p ./tsconfig.declarations.json",
|
|
33
|
+
"test": "pnpm test:types && pnpm test:unit",
|
|
34
|
+
"test:types": "tsc --noEmit",
|
|
35
|
+
"test:unit": "vitest run"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@pina-rs/cli": "^0.13.0",
|
|
39
|
+
"codama": "^1.10.2"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "^26.4.0",
|
|
43
|
+
"rimraf": "^6.1.3",
|
|
44
|
+
"tsup": "^8.5.1",
|
|
45
|
+
"typescript": "^7.0.2",
|
|
46
|
+
"vitest": "^4.1.11"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=18"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/CHANGELOG.md
DELETED
package/LICENSE
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
Placeholder package published by monochange. See the source repository for license terms.
|