@cargo-ai/cdk 1.0.5 → 1.0.7
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 +59 -9
- package/build/src/cli/commands/from.d.ts +65 -0
- package/build/src/cli/commands/from.d.ts.map +1 -0
- package/build/src/cli/commands/from.js +275 -0
- package/build/src/cli/commands/init.d.ts +1 -1
- package/build/src/cli/commands/init.d.ts.map +1 -1
- package/build/src/cli/commands/init.js +32 -13
- package/build/src/cli/commands/types.d.ts.map +1 -1
- package/build/src/cli/commands/types.js +75 -37
- package/build/src/core.d.ts +1 -1
- package/build/src/core.d.ts.map +1 -1
- package/build/src/deploy/apply.d.ts +6 -0
- package/build/src/deploy/apply.d.ts.map +1 -1
- package/build/src/deploy/apply.js +4 -0
- package/build/src/deploy/destroy.d.ts.map +1 -1
- package/build/src/deploy/destroy.js +33 -0
- package/build/src/deploy/executors.live.d.ts.map +1 -1
- package/build/src/deploy/executors.live.js +157 -1
- package/build/src/deploy/readers.live.d.ts.map +1 -1
- package/build/src/deploy/readers.live.js +34 -1
- package/build/src/index.d.ts +11 -7
- package/build/src/index.d.ts.map +1 -1
- package/build/src/index.js +6 -4
- package/build/src/refs.d.ts +22 -0
- package/build/src/refs.d.ts.map +1 -1
- package/build/src/refs.js +11 -2
- package/build/src/resources/actions.d.ts +39 -19
- package/build/src/resources/actions.d.ts.map +1 -1
- package/build/src/resources/actions.js +60 -33
- package/build/src/resources/agent.d.ts +33 -23
- package/build/src/resources/agent.d.ts.map +1 -1
- package/build/src/resources/agent.js +24 -24
- package/build/src/resources/app.d.ts +3 -1
- package/build/src/resources/app.d.ts.map +1 -1
- package/build/src/resources/app.js +2 -1
- package/build/src/resources/capacity.d.ts +11 -0
- package/build/src/resources/capacity.d.ts.map +1 -1
- package/build/src/resources/capacity.js +81 -0
- package/build/src/resources/connector.d.ts +10 -0
- package/build/src/resources/connector.d.ts.map +1 -1
- package/build/src/resources/connector.js +18 -1
- package/build/src/resources/customIntegration.d.ts +12 -0
- package/build/src/resources/customIntegration.d.ts.map +1 -0
- package/build/src/resources/customIntegration.js +18 -0
- package/build/src/resources/mcpServer.d.ts +8 -10
- package/build/src/resources/mcpServer.d.ts.map +1 -1
- package/build/src/resources/mcpServer.js +4 -7
- package/build/src/resources/model.d.ts +47 -4
- package/build/src/resources/model.d.ts.map +1 -1
- package/build/src/resources/model.js +38 -1
- package/build/src/resources/relationship.d.ts +21 -0
- package/build/src/resources/relationship.d.ts.map +1 -0
- package/build/src/resources/relationship.js +39 -0
- package/build/src/resources/worker.d.ts +13 -1
- package/build/src/resources/worker.d.ts.map +1 -1
- package/build/src/resources/worker.js +3 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/templates/blank/cargo.ts +17 -22
- package/templates/full/README.md +7 -4
- package/templates/full/agents/sdr.ts +12 -8
- package/templates/full/connectors/hunter.ts +10 -0
- package/templates/full/mcp/crm.ts +9 -6
- package/templates/full/tools/enrich.ts +28 -9
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Define your entire Cargo workspace in code — connectors, models, plays, tools,
|
|
4
4
|
agents, MCP servers, context, segments, capacities, territories, folders, files, workers and apps — and deploy it
|
|
5
5
|
declaratively, the same way you'd manage cloud infrastructure with Pulumi or the
|
|
6
|
-
AWS CDK. For a complete overview, see the [Cargo CDK docs](https://docs.getcargo.ai/
|
|
6
|
+
AWS CDK. For a complete overview, see the [Cargo CDK docs](https://docs.getcargo.ai/get-started/project-layout).
|
|
7
7
|
|
|
8
8
|
## Requirements
|
|
9
9
|
|
|
@@ -51,6 +51,9 @@ no global install:
|
|
|
51
51
|
npx @cargo-ai/cdk init my-workspace --template full # every resource type, wired up
|
|
52
52
|
# or: npx @cargo-ai/cdk init my-workspace # minimal 'blank' template
|
|
53
53
|
# or: npx @cargo-ai/cdk init --list-templates # see all templates
|
|
54
|
+
# or: npx @cargo-ai/cdk init my-tam --from getcargohq/cargo-cookbooks/signal-based-tam
|
|
55
|
+
# scaffold from a GitHub source (owner/repo[/folder][@ref]); a cookbook
|
|
56
|
+
# folder also pulls its required siblings via the repo's cargo.scaffold.json
|
|
54
57
|
cd my-workspace && npm install
|
|
55
58
|
```
|
|
56
59
|
|
|
@@ -130,15 +133,19 @@ const flow = defineWorkflow("enrich", { input, output }, ({ input, ai }) => …)
|
|
|
130
133
|
export const enrich = defineTool("enrich", { workflow: flow, emojiSlug: "mag" });
|
|
131
134
|
|
|
132
135
|
export const sdr = defineAgent("sdr", {
|
|
133
|
-
connector: openai, //
|
|
136
|
+
connector: openai, // the LLM connector (or connectorRef("…"))
|
|
134
137
|
languageModel: "gpt-4o",
|
|
135
138
|
systemPrompt: "Qualify inbound leads.",
|
|
136
|
-
|
|
137
|
-
|
|
139
|
+
// everything the agent can call or read — one array, kind inferred per handle
|
|
140
|
+
uses: [
|
|
141
|
+
{ ref: contacts, readOnly: true }, // a data model (handle + options)
|
|
142
|
+
enrich, // a tool (bare handle)
|
|
143
|
+
hunter.actions.findEmail, // an action off a connector handle
|
|
144
|
+
],
|
|
138
145
|
});
|
|
139
146
|
```
|
|
140
147
|
|
|
141
|
-
See [Defining Resources](https://docs.getcargo.ai/
|
|
148
|
+
See [Defining Resources](https://docs.getcargo.ai/connectors/overview) for every
|
|
142
149
|
builder (connectors, models, plays, tools, agents, MCP servers, context, segments,
|
|
143
150
|
capacities, territories, folders, files, workers, apps) and the slug rules.
|
|
144
151
|
|
|
@@ -161,6 +168,49 @@ There is one context repo per workspace, so this is a singleton. On deploy it
|
|
|
161
168
|
writes each changed file into the repo via the runtime — **additive**: files
|
|
162
169
|
added elsewhere (the UI, other tooling) are left in place.
|
|
163
170
|
|
|
171
|
+
### Unification
|
|
172
|
+
|
|
173
|
+
A model can declare how it feeds Cargo's unified `Account` / `Contact` /
|
|
174
|
+
`AccountEvent` / `ContactEvent` models. A connector model defaults to its
|
|
175
|
+
integration's built-in mapping (`{ source: "integration" }`), so you only pass
|
|
176
|
+
`unification` to override it or to unify a model the integration doesn't map on
|
|
177
|
+
its own. A `custom` unification maps columns to shared references (`domain`,
|
|
178
|
+
`email`, `linkedinId`, …) and can link a child to its parent by pointing at
|
|
179
|
+
another model handle:
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
import { defineModel } from "@cargo-ai/cdk";
|
|
183
|
+
|
|
184
|
+
export const accounts = defineModel("accounts", {
|
|
185
|
+
dataset: warehouse,
|
|
186
|
+
extractSlug: "runQuery",
|
|
187
|
+
unification: {
|
|
188
|
+
source: "custom",
|
|
189
|
+
type: "account",
|
|
190
|
+
uniqueColumns: [{ slug: "website", reference: "domain" }],
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
export const contacts = defineModel("contacts", {
|
|
195
|
+
dataset: warehouse,
|
|
196
|
+
extractSlug: "runQuery",
|
|
197
|
+
unification: {
|
|
198
|
+
source: "custom",
|
|
199
|
+
type: "contact",
|
|
200
|
+
uniqueColumns: [{ slug: "work_email", reference: "workEmail" }],
|
|
201
|
+
// link each contact to its unified account — pass the model handle, not a uuid
|
|
202
|
+
parent: { kind: "model", columnSlug: "account_id", parent: accounts },
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Unification isn't part of the create call, so the CDK applies it with a
|
|
208
|
+
follow-up update once the model exists — including models the deploy adopts.
|
|
209
|
+
Passing a parent handle makes the child depend on the parent, so the parent is
|
|
210
|
+
created first and its uuid is injected. See
|
|
211
|
+
[Unification](https://docs.getcargo.ai/models/unification) for the reference and
|
|
212
|
+
priority rules.
|
|
213
|
+
|
|
164
214
|
## Commands
|
|
165
215
|
|
|
166
216
|
In a scaffolded project, the everyday commands are `npm run` scripts:
|
|
@@ -174,7 +224,7 @@ npm run deploy # create/update resources, write cargo.state.json
|
|
|
174
224
|
Run any command — including the ones without a script — with `npx @cargo-ai/cdk`:
|
|
175
225
|
|
|
176
226
|
```bash
|
|
177
|
-
npx @cargo-ai/cdk init <directory> # scaffold from a template (--template, --list-templates)
|
|
227
|
+
npx @cargo-ai/cdk init <directory> # scaffold from a template (--template, --list-templates) or GitHub (--from owner/repo[/folder][@ref])
|
|
178
228
|
npx @cargo-ai/cdk deploy --prune # also delete resources removed from code
|
|
179
229
|
npx @cargo-ai/cdk deploy --refresh # re-read live resources, re-apply out-of-band changes
|
|
180
230
|
npx @cargo-ai/cdk refresh # read-only: report resources that drifted from code
|
|
@@ -189,7 +239,7 @@ CLI installed — same engine, same login.
|
|
|
189
239
|
|
|
190
240
|
Common flags: `--dir <path>`, `--yes` (skip the prompt — required in CI),
|
|
191
241
|
`--json`, and `--force` (steal a stale state lock). See
|
|
192
|
-
[Deploying](https://docs.getcargo.ai/
|
|
242
|
+
[Deploying](https://docs.getcargo.ai/deploy/deploying).
|
|
193
243
|
|
|
194
244
|
## State & drift
|
|
195
245
|
|
|
@@ -210,8 +260,8 @@ cargo.state.audit.jsonl
|
|
|
210
260
|
|
|
211
261
|
`npx @cargo-ai/cdk refresh` reports resources changed or deleted outside the CDK
|
|
212
262
|
(e.g. edited in the Cargo UI); `npx @cargo-ai/cdk deploy --refresh` re-applies your
|
|
213
|
-
code over them. See [State & Drift](https://docs.getcargo.ai/
|
|
263
|
+
code over them. See [State & Drift](https://docs.getcargo.ai/deploy/state-and-drift).
|
|
214
264
|
|
|
215
265
|
## Documentation
|
|
216
266
|
|
|
217
|
-
Full documentation: [docs.getcargo.ai/cdk](https://docs.getcargo.ai/
|
|
267
|
+
Full documentation: [docs.getcargo.ai/cdk](https://docs.getcargo.ai/get-started/project-layout).
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export type FromRef = {
|
|
3
|
+
owner: string;
|
|
4
|
+
repo: string;
|
|
5
|
+
/** Repo-relative folder to scaffold ("" = the whole repo). */
|
|
6
|
+
subPath: string;
|
|
7
|
+
/** Branch or tag ("" = the repo's default branch). */
|
|
8
|
+
gitRef: string;
|
|
9
|
+
};
|
|
10
|
+
/** Reject absolute paths, `..`, `.`, and empty segments in repo-relative paths. */
|
|
11
|
+
export declare function assertSafeRepoRelativePath(repoRelativePath: string, label: string): void;
|
|
12
|
+
/** Join `relativePath` under `baseDir` and ensure the result cannot escape it. */
|
|
13
|
+
export declare function resolveWithin(baseDir: string, relativePath: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Parse a `--from` source. Accepts `owner/repo[/sub/path][@ref]`, the same
|
|
16
|
+
* with a `github.com` / `git@github.com:` prefix, and GitHub tree URLs
|
|
17
|
+
* (`github.com/owner/repo/tree/<ref>/<sub/path>`). Returns `undefined` when
|
|
18
|
+
* the source doesn't name a GitHub repo.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseFromRef(raw: string): FromRef | undefined;
|
|
21
|
+
declare const manifestSchema: z.ZodObject<{
|
|
22
|
+
shared: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
23
|
+
folders: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
24
|
+
requires: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
25
|
+
}, z.core.$strip>>>;
|
|
26
|
+
}, z.core.$strip>;
|
|
27
|
+
export type ScaffoldManifest = z.infer<typeof manifestSchema>;
|
|
28
|
+
export declare const MANIFEST_FILENAME = "cargo.scaffold.json";
|
|
29
|
+
/** Read and validate `cargo.scaffold.json` at the repo root, if present. */
|
|
30
|
+
export declare function readManifest(repoDir: string): Promise<ScaffoldManifest | undefined>;
|
|
31
|
+
export type CopyPlan = {
|
|
32
|
+
/** Repo-relative folders to copy, keeping their path under the target. */
|
|
33
|
+
folders: string[];
|
|
34
|
+
/** Repo-relative files to copy, keeping their path under the target. */
|
|
35
|
+
files: string[];
|
|
36
|
+
/** When true, the single folder's CONTENTS land at the target root. */
|
|
37
|
+
flatten: boolean;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Decide what to copy. A manifest-declared folder keeps the repo layout and
|
|
41
|
+
* brings its transitive `requires` plus the shared root files; anything else
|
|
42
|
+
* is a flat copy of the requested folder (or the whole repo).
|
|
43
|
+
*/
|
|
44
|
+
export declare function planCopies(manifest: ScaffoldManifest | undefined, subPath: string): CopyPlan;
|
|
45
|
+
/**
|
|
46
|
+
* Shallow-clone the repo into a temp dir. Returns the dir and its cleanup.
|
|
47
|
+
* `remoteUrl` overrides the GitHub URL (tests clone from a local repo).
|
|
48
|
+
*/
|
|
49
|
+
export declare function cloneGitHubRepo(ref: FromRef, remoteUrl?: string): Promise<{
|
|
50
|
+
dir: string;
|
|
51
|
+
cleanup: () => Promise<void>;
|
|
52
|
+
}>;
|
|
53
|
+
/**
|
|
54
|
+
* Fetch the source and copy the planned folders/files into `targetDir`,
|
|
55
|
+
* substituting `__APP_NAME__`. Returns the copied repo-relative paths.
|
|
56
|
+
*/
|
|
57
|
+
export declare function scaffoldFromGitHub(payload: {
|
|
58
|
+
ref: FromRef;
|
|
59
|
+
targetDir: string;
|
|
60
|
+
appName: string;
|
|
61
|
+
/** Test-only override — clone from this URL/path instead of github.com. */
|
|
62
|
+
remoteUrl?: string;
|
|
63
|
+
}): Promise<string[]>;
|
|
64
|
+
export {};
|
|
65
|
+
//# sourceMappingURL=from.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"from.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/from.ts"],"names":[],"mappings":"AA2BA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB,MAAM,MAAM,OAAO,GAAG;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAC;IAChB,sDAAsD;IACtD,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAIF,mFAAmF;AACnF,wBAAgB,0BAA0B,CACxC,gBAAgB,EAAE,MAAM,EACxB,KAAK,EAAE,MAAM,GACZ,IAAI,CAeN;AAED,kFAAkF;AAClF,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,CAW3E;AA0BD;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,CAqD7D;AAED,QAAA,MAAM,cAAc;;;;;iBAKlB,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC;AAE9D,eAAO,MAAM,iBAAiB,wBAAwB,CAAC;AAEvD,4EAA4E;AAC5E,wBAAsB,YAAY,CAChC,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAoCvC;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,0EAA0E;IAC1E,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,wEAAwE;IACxE,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,uEAAuE;IACvE,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,UAAU,CACxB,QAAQ,EAAE,gBAAgB,GAAG,SAAS,EACtC,OAAO,EAAE,MAAM,GACd,QAAQ,CAkBV;AAED;;;GAGG;AACH,wBAAsB,eAAe,CACnC,GAAG,EAAE,OAAO,EACZ,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAAE,CAAC,CA8BxD;AAED;;;GAGG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,EAAE;IAChD,GAAG,EAAE,OAAO,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,2EAA2E;IAC3E,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAsDpB"}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
// `cdk init <dir> --from <source>` — scaffold from a GitHub repo instead of a
|
|
2
|
+
// bundled template. The source names a repo or a folder inside one
|
|
3
|
+
// (`owner/repo`, `owner/repo/folder`, `owner/repo/folder@ref`, or a
|
|
4
|
+
// github.com URL). The repo is fetched with a shallow `git clone`, so private
|
|
5
|
+
// sources work through the user's existing git auth.
|
|
6
|
+
//
|
|
7
|
+
// The manifest is OPTIONAL — any repo/folder scaffolds without it. A repo of
|
|
8
|
+
// composable folders (e.g. cargo-cookbooks) can ship a `cargo.scaffold.json`
|
|
9
|
+
// at its root to declare cross-folder dependencies:
|
|
10
|
+
//
|
|
11
|
+
// {
|
|
12
|
+
// "shared": ["package.json", "tsconfig.json"],
|
|
13
|
+
// "folders": { "signal-based-tam": { "requires": ["base-gtm"] } }
|
|
14
|
+
// }
|
|
15
|
+
//
|
|
16
|
+
// When the requested folder is declared there, the scaffold keeps the repo's
|
|
17
|
+
// folder layout and also copies its `requires` folders (transitively) plus the
|
|
18
|
+
// `shared` root files — so cross-folder imports like
|
|
19
|
+
// `../base-gtm/models/accounts` keep resolving. Any other source copies the
|
|
20
|
+
// folder's contents flat into the target directory.
|
|
21
|
+
import { execFile } from "node:child_process";
|
|
22
|
+
import { promises as fs } from "node:fs";
|
|
23
|
+
import { tmpdir } from "node:os";
|
|
24
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
25
|
+
import { promisify } from "node:util";
|
|
26
|
+
import { z } from "zod";
|
|
27
|
+
import { copyDirectory } from "./templates.js";
|
|
28
|
+
const execFileAsync = promisify(execFile);
|
|
29
|
+
const SEGMENT_PATTERN = /^[\w][\w.-]*$/;
|
|
30
|
+
/** Reject absolute paths, `..`, `.`, and empty segments in repo-relative paths. */
|
|
31
|
+
export function assertSafeRepoRelativePath(repoRelativePath, label) {
|
|
32
|
+
if (repoRelativePath === "") {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (isAbsolute(repoRelativePath)) {
|
|
36
|
+
throw new Error(`Invalid ${label}: absolute paths are not allowed.`);
|
|
37
|
+
}
|
|
38
|
+
const segments = repoRelativePath.split(/[/\\]/);
|
|
39
|
+
for (const segment of segments) {
|
|
40
|
+
if (segment === "" || segment === "." || segment === "..") {
|
|
41
|
+
throw new Error(`Invalid ${label}: "${repoRelativePath}" contains an unsafe path segment.`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** Join `relativePath` under `baseDir` and ensure the result cannot escape it. */
|
|
46
|
+
export function resolveWithin(baseDir, relativePath) {
|
|
47
|
+
const resolvedBase = resolve(baseDir);
|
|
48
|
+
const resolved = resolve(resolvedBase, relativePath);
|
|
49
|
+
const rel = relative(resolvedBase, resolved);
|
|
50
|
+
if (rel === "" || rel === ".") {
|
|
51
|
+
return resolvedBase;
|
|
52
|
+
}
|
|
53
|
+
if (rel.startsWith("..") || isAbsolute(rel)) {
|
|
54
|
+
throw new Error(`Path "${relativePath}" escapes "${baseDir}".`);
|
|
55
|
+
}
|
|
56
|
+
return resolved;
|
|
57
|
+
}
|
|
58
|
+
function assertSafeSubPath(subPath) {
|
|
59
|
+
try {
|
|
60
|
+
assertSafeRepoRelativePath(subPath, "source folder");
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function validateManifestPaths(manifest) {
|
|
68
|
+
for (const file of manifest.shared) {
|
|
69
|
+
assertSafeRepoRelativePath(file, `${MANIFEST_FILENAME} shared entry`);
|
|
70
|
+
}
|
|
71
|
+
for (const [folder, entry] of Object.entries(manifest.folders)) {
|
|
72
|
+
assertSafeRepoRelativePath(folder, `${MANIFEST_FILENAME} folder key`);
|
|
73
|
+
for (const required of entry.requires) {
|
|
74
|
+
assertSafeRepoRelativePath(required, `${MANIFEST_FILENAME} requires entry for "${folder}"`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Parse a `--from` source. Accepts `owner/repo[/sub/path][@ref]`, the same
|
|
80
|
+
* with a `github.com` / `git@github.com:` prefix, and GitHub tree URLs
|
|
81
|
+
* (`github.com/owner/repo/tree/<ref>/<sub/path>`). Returns `undefined` when
|
|
82
|
+
* the source doesn't name a GitHub repo.
|
|
83
|
+
*/
|
|
84
|
+
export function parseFromRef(raw) {
|
|
85
|
+
const trimmed = raw.trim();
|
|
86
|
+
const isGitHubUrl = /^https?:\/\/(www\.)?github\.com\//.test(trimmed) ||
|
|
87
|
+
/^git@github\.com:/.test(trimmed);
|
|
88
|
+
const stripped = trimmed
|
|
89
|
+
.replace(/^git@github\.com:/, "")
|
|
90
|
+
.replace(/^https?:\/\/(www\.)?github\.com\//, "")
|
|
91
|
+
.replace(/\/+$/, "");
|
|
92
|
+
if (stripped.includes("://") || stripped.startsWith("git@")) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const atIndex = stripped.lastIndexOf("@");
|
|
96
|
+
const path = atIndex === -1 ? stripped : stripped.slice(0, atIndex);
|
|
97
|
+
const refSuffix = atIndex === -1 ? "" : stripped.slice(atIndex + 1);
|
|
98
|
+
if (atIndex !== -1 && refSuffix === "")
|
|
99
|
+
return undefined;
|
|
100
|
+
const segments = path.split("/").filter((segment) => segment !== "");
|
|
101
|
+
const [owner, repoRaw] = segments;
|
|
102
|
+
if (owner === undefined || repoRaw === undefined)
|
|
103
|
+
return undefined;
|
|
104
|
+
const repo = repoRaw.replace(/\.git$/, "");
|
|
105
|
+
if (!SEGMENT_PATTERN.test(owner) || !SEGMENT_PATTERN.test(repo)) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
// A tree URL pins its ref in the path: owner/repo/tree/<ref>/<sub/path>.
|
|
109
|
+
// Only apply for full GitHub URLs — otherwise "tree" may be a real folder
|
|
110
|
+
// (e.g. owner/repo/tree/my-feature).
|
|
111
|
+
if (isGitHubUrl && segments[2] === "tree") {
|
|
112
|
+
const treeRef = segments[3];
|
|
113
|
+
if (treeRef === undefined || refSuffix !== "")
|
|
114
|
+
return undefined;
|
|
115
|
+
const subPath = segments.slice(4).join("/");
|
|
116
|
+
if (assertSafeSubPath(subPath) === false)
|
|
117
|
+
return undefined;
|
|
118
|
+
return {
|
|
119
|
+
owner,
|
|
120
|
+
repo,
|
|
121
|
+
subPath,
|
|
122
|
+
gitRef: treeRef,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
const subPath = segments.slice(2).join("/");
|
|
126
|
+
if (assertSafeSubPath(subPath) === false)
|
|
127
|
+
return undefined;
|
|
128
|
+
return {
|
|
129
|
+
owner,
|
|
130
|
+
repo,
|
|
131
|
+
subPath,
|
|
132
|
+
gitRef: refSuffix,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const manifestSchema = z.object({
|
|
136
|
+
shared: z.array(z.string()).default([]),
|
|
137
|
+
folders: z
|
|
138
|
+
.record(z.string(), z.object({ requires: z.array(z.string()).default([]) }))
|
|
139
|
+
.default({}),
|
|
140
|
+
});
|
|
141
|
+
export const MANIFEST_FILENAME = "cargo.scaffold.json";
|
|
142
|
+
/** Read and validate `cargo.scaffold.json` at the repo root, if present. */
|
|
143
|
+
export async function readManifest(repoDir) {
|
|
144
|
+
const manifestPath = join(repoDir, MANIFEST_FILENAME);
|
|
145
|
+
let raw;
|
|
146
|
+
try {
|
|
147
|
+
raw = await fs.readFile(manifestPath, "utf-8");
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
if (error !== null &&
|
|
151
|
+
typeof error === "object" &&
|
|
152
|
+
"code" in error &&
|
|
153
|
+
error.code === "ENOENT") {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
157
|
+
throw new Error(`Could not read ${MANIFEST_FILENAME} in the source repo: ${message}`);
|
|
158
|
+
}
|
|
159
|
+
let json;
|
|
160
|
+
try {
|
|
161
|
+
json = JSON.parse(raw);
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
165
|
+
throw new Error(`Invalid ${MANIFEST_FILENAME} in the source repo: ${message}`);
|
|
166
|
+
}
|
|
167
|
+
const parsed = manifestSchema.safeParse(json);
|
|
168
|
+
if (parsed.success === false) {
|
|
169
|
+
throw new Error(`Invalid ${MANIFEST_FILENAME} in the source repo: ${z.prettifyError(parsed.error)}`);
|
|
170
|
+
}
|
|
171
|
+
validateManifestPaths(parsed.data);
|
|
172
|
+
return parsed.data;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Decide what to copy. A manifest-declared folder keeps the repo layout and
|
|
176
|
+
* brings its transitive `requires` plus the shared root files; anything else
|
|
177
|
+
* is a flat copy of the requested folder (or the whole repo).
|
|
178
|
+
*/
|
|
179
|
+
export function planCopies(manifest, subPath) {
|
|
180
|
+
const declared = manifest === undefined ? undefined : manifest.folders[subPath];
|
|
181
|
+
if (manifest === undefined || declared === undefined || subPath === "") {
|
|
182
|
+
return { folders: [subPath], files: [], flatten: true };
|
|
183
|
+
}
|
|
184
|
+
const folders = [];
|
|
185
|
+
const queue = [subPath];
|
|
186
|
+
while (queue.length > 0) {
|
|
187
|
+
const current = queue.shift();
|
|
188
|
+
if (current === undefined || folders.includes(current))
|
|
189
|
+
continue;
|
|
190
|
+
folders.push(current);
|
|
191
|
+
const entry = manifest.folders[current];
|
|
192
|
+
if (entry !== undefined)
|
|
193
|
+
queue.push(...entry.requires);
|
|
194
|
+
}
|
|
195
|
+
return { folders, files: manifest.shared, flatten: false };
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Shallow-clone the repo into a temp dir. Returns the dir and its cleanup.
|
|
199
|
+
* `remoteUrl` overrides the GitHub URL (tests clone from a local repo).
|
|
200
|
+
*/
|
|
201
|
+
export async function cloneGitHubRepo(ref, remoteUrl) {
|
|
202
|
+
const dir = await fs.mkdtemp(join(tmpdir(), "cargo-cdk-from-"));
|
|
203
|
+
const url = remoteUrl === undefined
|
|
204
|
+
? `https://github.com/${ref.owner}/${ref.repo}.git`
|
|
205
|
+
: remoteUrl;
|
|
206
|
+
const branchArgs = ref.gitRef === "" ? [] : ["--branch", ref.gitRef];
|
|
207
|
+
try {
|
|
208
|
+
await execFileAsync("git", [
|
|
209
|
+
"clone",
|
|
210
|
+
"--depth",
|
|
211
|
+
"1",
|
|
212
|
+
"--quiet",
|
|
213
|
+
...branchArgs,
|
|
214
|
+
url,
|
|
215
|
+
dir,
|
|
216
|
+
]);
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
220
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
221
|
+
throw new Error(`Could not clone ${url}${ref.gitRef === "" ? "" : ` (ref ${ref.gitRef})`}. Is git installed and the repo accessible?\n${message}`);
|
|
222
|
+
}
|
|
223
|
+
// Drop the git metadata so a whole-repo copy never carries it over.
|
|
224
|
+
await fs.rm(join(dir, ".git"), { recursive: true, force: true });
|
|
225
|
+
return {
|
|
226
|
+
dir,
|
|
227
|
+
cleanup: () => fs.rm(dir, { recursive: true, force: true }),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Fetch the source and copy the planned folders/files into `targetDir`,
|
|
232
|
+
* substituting `__APP_NAME__`. Returns the copied repo-relative paths.
|
|
233
|
+
*/
|
|
234
|
+
export async function scaffoldFromGitHub(payload) {
|
|
235
|
+
const { ref, targetDir, appName, remoteUrl } = payload;
|
|
236
|
+
const rename = [{ from: "__APP_NAME__", to: appName }];
|
|
237
|
+
const { dir, cleanup } = await cloneGitHubRepo(ref, remoteUrl);
|
|
238
|
+
try {
|
|
239
|
+
if (ref.subPath !== "") {
|
|
240
|
+
assertSafeRepoRelativePath(ref.subPath, "source folder");
|
|
241
|
+
const subPathSrc = resolveWithin(dir, ref.subPath);
|
|
242
|
+
const stat = await fs.stat(subPathSrc).catch(() => undefined);
|
|
243
|
+
if (stat === undefined || stat.isDirectory() === false) {
|
|
244
|
+
throw new Error(`"${ref.subPath}" is not a folder in ${ref.owner}/${ref.repo}.`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const manifest = await readManifest(dir);
|
|
248
|
+
const plan = planCopies(manifest, ref.subPath);
|
|
249
|
+
for (const folder of plan.folders) {
|
|
250
|
+
const src = resolveWithin(dir, folder);
|
|
251
|
+
const dest = plan.flatten ? targetDir : resolveWithin(targetDir, folder);
|
|
252
|
+
const stat = await fs.stat(src).catch(() => undefined);
|
|
253
|
+
if (stat === undefined || stat.isDirectory() === false) {
|
|
254
|
+
throw new Error(`Manifest folder "${folder}" is missing from ${ref.owner}/${ref.repo}.`);
|
|
255
|
+
}
|
|
256
|
+
await copyDirectory(src, dest, rename);
|
|
257
|
+
}
|
|
258
|
+
for (const file of plan.files) {
|
|
259
|
+
const src = resolveWithin(dir, file);
|
|
260
|
+
const stat = await fs.stat(src).catch(() => undefined);
|
|
261
|
+
if (stat === undefined || stat.isFile() === false) {
|
|
262
|
+
throw new Error(`Manifest shared file "${file}" is missing from ${ref.owner}/${ref.repo}.`);
|
|
263
|
+
}
|
|
264
|
+
const raw = await fs.readFile(src, "utf-8");
|
|
265
|
+
const replaced = rename.reduce((current, { from, to }) => current.split(from).join(to), raw);
|
|
266
|
+
const dest = resolveWithin(targetDir, file);
|
|
267
|
+
await fs.mkdir(dirname(dest), { recursive: true });
|
|
268
|
+
await fs.writeFile(dest, replaced, "utf-8");
|
|
269
|
+
}
|
|
270
|
+
return [...plan.folders, ...plan.files].filter((path) => path !== "");
|
|
271
|
+
}
|
|
272
|
+
finally {
|
|
273
|
+
await cleanup();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/init.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/init.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,OAAO,EAAU,MAAM,WAAW,CAAC;AAmBjD,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,CAoGzD"}
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { existsSync, readdirSync } from "node:fs";
|
|
2
2
|
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { Option } from "commander";
|
|
4
5
|
import { ExitCodes, failWith, info, success } from "../io.js";
|
|
6
|
+
import { parseFromRef, scaffoldFromGitHub } from "./from.js";
|
|
5
7
|
import { copyDirectory, listTemplates } from "./templates.js";
|
|
6
|
-
// `cdk init <directory> [--template <slug>]` — scaffold a
|
|
7
|
-
// copying one of `@cargo-ai/cdk`'s bundled templates
|
|
8
|
-
//
|
|
8
|
+
// `cdk init <directory> [--template <slug>] [--from <source>]` — scaffold a
|
|
9
|
+
// starter CDK project by copying one of `@cargo-ai/cdk`'s bundled templates
|
|
10
|
+
// (see `--list-templates`) or fetching a GitHub source — e.g. a cookbook:
|
|
11
|
+
// `cdk init my-tam --from getcargohq/cargo-cookbooks/signal-based-tam` — substituting
|
|
12
|
+
// `__APP_NAME__`. Cookbook sources pull their required sibling folders via the
|
|
13
|
+
// repo's `cargo.scaffold.json` (see `./from.ts`).
|
|
9
14
|
const TEMPLATE_DESCRIPTIONS = {
|
|
10
15
|
blank: "Minimal starter — one connector + model + a workflow-backed tool. Good starting point.",
|
|
11
16
|
full: "The full example — every resource type wired into a GTM growth workspace: connectors, models, plays, agents, an MCP server, context, tools, a worker, and a hosted app.",
|
|
@@ -14,7 +19,10 @@ export function registerInitCommand(parent) {
|
|
|
14
19
|
parent
|
|
15
20
|
.command("init <directory>")
|
|
16
21
|
.description("Scaffold a starter Cargo CDK project locally from a template (ready to plan/deploy).")
|
|
17
|
-
.
|
|
22
|
+
.addOption(new Option("--template <slug>", "Template slug (default: blank)")
|
|
23
|
+
.default("blank")
|
|
24
|
+
.conflicts("from"))
|
|
25
|
+
.option("--from <source>", "GitHub source to scaffold from instead of a template — owner/repo[/folder][@ref] or a github.com URL. Cookbook folders also pull their required siblings (repo's cargo.scaffold.json).")
|
|
18
26
|
.option("--name <name>", "Project name written into package.json")
|
|
19
27
|
.option("--list-templates", "Print available templates and exit")
|
|
20
28
|
.option("--force", "Write into a non-empty directory")
|
|
@@ -28,11 +36,6 @@ export function registerInitCommand(parent) {
|
|
|
28
36
|
}
|
|
29
37
|
return;
|
|
30
38
|
}
|
|
31
|
-
const templateDir = join(templatesRoot, opts.template);
|
|
32
|
-
if (!existsSync(templateDir)) {
|
|
33
|
-
const slugs = await listTemplates(templatesRoot);
|
|
34
|
-
failWith(`Unknown template "${opts.template}". Available: ${slugs.join(", ")}`, { code: ExitCodes.GenericError });
|
|
35
|
-
}
|
|
36
39
|
const targetDir = resolve(process.cwd(), directory);
|
|
37
40
|
if (existsSync(targetDir) &&
|
|
38
41
|
readdirSync(targetDir).length > 0 &&
|
|
@@ -40,10 +43,26 @@ export function registerInitCommand(parent) {
|
|
|
40
43
|
failWith(`Directory ${directory} is not empty. Pass --force to scaffold into it anyway.`, { code: ExitCodes.GenericError });
|
|
41
44
|
}
|
|
42
45
|
const appName = opts.name !== undefined ? opts.name : basename(targetDir);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
if (opts.from !== undefined) {
|
|
47
|
+
const ref = parseFromRef(opts.from);
|
|
48
|
+
if (ref === undefined) {
|
|
49
|
+
failWith(`Could not parse --from "${opts.from}". Expected owner/repo[/folder][@ref] or a github.com URL.`, { code: ExitCodes.GenericError });
|
|
50
|
+
}
|
|
51
|
+
const copied = await scaffoldFromGitHub({ ref, targetDir, appName });
|
|
52
|
+
const detail = copied.length > 0 ? ` (${copied.join(", ")})` : "";
|
|
53
|
+
success(`Scaffolded ${directory} from ${ref.owner}/${ref.repo}${ref.subPath === "" ? "" : `/${ref.subPath}`}${detail}`);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const templateDir = join(templatesRoot, opts.template);
|
|
57
|
+
if (!existsSync(templateDir)) {
|
|
58
|
+
const slugs = await listTemplates(templatesRoot);
|
|
59
|
+
failWith(`Unknown template "${opts.template}". Available: ${slugs.join(", ")}`, { code: ExitCodes.GenericError });
|
|
60
|
+
}
|
|
61
|
+
await copyDirectory(templateDir, targetDir, [
|
|
62
|
+
{ from: "__APP_NAME__", to: appName },
|
|
63
|
+
]);
|
|
64
|
+
success(`Scaffolded ${directory} from the "${opts.template}" template`);
|
|
65
|
+
}
|
|
47
66
|
info(``);
|
|
48
67
|
info([
|
|
49
68
|
`Next steps:`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAiHzC,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG,IAAI,CAgE7E"}
|