@decohere/create 0.0.1-dev.20261106.139 → 0.0.1-dev.20261106.158
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 +40 -19
- package/es6/src/cli.js +2 -2
- package/es6/src/index.js +2 -1
- package/es6/src/resolve-launch.js +2 -2
- package/es6/src/runner.js +257 -63
- package/es6/src/tools.js +9 -0
- package/package.json +1 -1
- package/types/src/index.d.ts +2 -1
- package/types/src/resolve-launch.d.ts +2 -1
- package/types/src/runner.d.ts +9 -3
- package/types/src/tools.d.ts +5 -0
package/README.md
CHANGED
|
@@ -2,7 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
Scaffold a new Decohere project — built with Forge, served with Flux.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Scaffolding always runs somewhere that already has its tools, so nothing gets installed on your machine.
|
|
6
|
+
|
|
7
|
+
| Where you run it | `create` | `add`, `auth0` |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| A Decohere Dev Container | Runs right there | Runs right there |
|
|
10
|
+
| Your machine, or another container with Docker access | Runs in the `ghcr.io/typesmiths/decohere/create` image, then copies the project out | Says to open the project's Dev Container |
|
|
11
|
+
| Another container with no Docker access | Says Docker is needed | Says to open the project's Dev Container |
|
|
12
|
+
|
|
13
|
+
The scaffolder image gets no folders from your machine, and it can't see `~/.npmrc` or your Auth0 login. It works in its own folder, and the project is copied out only if it succeeds, so a failed run or Ctrl-C leaves your folder as it was. The container is always removed. A new project needs an empty folder (a `.git` folder is fine). This works the same from your machine, from inside a container, and against a remote Docker.
|
|
14
|
+
|
|
15
|
+
With Auth0 and Postgres, `create` also makes an admin user with a generated password. The password isn't printed or kept in the project, and it's only for logging in. On Linux with a key store (GNOME Keyring or KWallet, through `secret-tool`), it's saved there, and `create` prints the command to read it. Anywhere else it goes in `~/.config/decohere/<project>.env`, readable only by you. Run inside a container, that file is in the container, so copy it if the container may be rebuilt.
|
|
16
|
+
|
|
17
|
+
Choosing Auth0 during `create` means logging in there. That login goes away with the container, so your first `auth0` command in the Dev Container asks you to log in once more.
|
|
6
18
|
|
|
7
19
|
## Usage
|
|
8
20
|
|
|
@@ -105,37 +117,45 @@ Each API is a separate deploy: package it (`pnpm run package:billing`) and build
|
|
|
105
117
|
|
|
106
118
|
### `auth0`
|
|
107
119
|
|
|
108
|
-
|
|
120
|
+
Run these in the project's Dev Container, which has the Auth0 CLI. Each project keeps its login in its own folder on your machine: `$XDG_RUNTIME_DIR/decohere-auth0/<project>` on Linux, which is held in memory and readable only by you, and `~/.config/decohere/decohere-auth0/<project>` elsewhere. Rebuilding the Dev Container doesn't log you out, but logging out of your machine or rebooting does on Linux. `configure` and `add auth0` stop if you're logged in to a different tenant from the one in `.env`. Use this after a project exists, when a token expires, or when the login app needs to be created or updated.
|
|
109
121
|
|
|
110
122
|
```bash
|
|
111
123
|
# Device/URL login — open the printed URL in your browser
|
|
112
|
-
|
|
124
|
+
pnpm decohere auth0 login
|
|
113
125
|
|
|
114
|
-
# Update the Auth0 API and
|
|
115
|
-
|
|
116
|
-
npx @decohere/create auth0 configure -d my-app
|
|
126
|
+
# Update the Auth0 API, login app and token app for this workspace
|
|
127
|
+
pnpm decohere auth0 configure
|
|
117
128
|
|
|
118
|
-
#
|
|
119
|
-
decohere auth0
|
|
120
|
-
decohere auth0 configure
|
|
129
|
+
# Print a token for calling the API in development
|
|
130
|
+
pnpm decohere auth0 token
|
|
121
131
|
```
|
|
122
132
|
|
|
123
|
-
`configure` does **not** rewrite application source. It ensures a `<project> Dev` login app (written to `.env` as `AUTH0_CLIENT_ID`), scans `projects/*/src` for `@Authorize("action", "Subject")`, adds those permissions to the Auth0 API (keeping any already on the API) and assigns them to `AUTH0_ADMIN_USER_SUB` when set.
|
|
133
|
+
`configure` does **not** rewrite application source. It ensures a `<project> Dev` login app for your web app (written to `.env` as `AUTH0_CLIENT_ID`, and allowing `http://localhost:<port>` for each API), scans `projects/*/src` for `@Authorize("action", "Subject")`, adds those permissions to the Auth0 API (keeping any already on the API) and assigns them to `AUTH0_ADMIN_USER_SUB` when set.
|
|
124
134
|
|
|
125
135
|
#### Add Auth0 to an existing project
|
|
126
136
|
|
|
137
|
+
In the project's Dev Container:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
pnpm decohere add auth0
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
This logs in if needed, creates the Auth0 API and a `<project> Dev` login app, writes `AUTH0_ISSUER_BASE_URL`, `AUTH0_AUDIENCE` and `AUTH0_CLIENT_ID` to `.env`, adds the `auth` section to `config/config.yaml`, and adds `.withAuth()` to each API's `main.ts`. Running it again changes nothing. If a `main.ts` no longer has the generated shape, the command leaves it alone and prints the lines to add. It also creates a `<project> Token` machine app (written to `.env` as `AUTH0_TOKEN_CLIENT_ID`). It doesn't add the local user profile a new project with Postgres gets.
|
|
144
|
+
|
|
145
|
+
Once Auth0 is added, **every route needs a token** unless it has `@AllowAnonymous()`, because `withAuth()` is secure by default. To call one, in the Dev Container:
|
|
146
|
+
|
|
127
147
|
```bash
|
|
128
|
-
decohere
|
|
148
|
+
curl -H "Authorization: Bearer $(pnpm -s decohere auth0 token)" localhost:3000/hello/1
|
|
129
149
|
```
|
|
130
150
|
|
|
131
|
-
|
|
151
|
+
`auth0 token` prints a short-lived token from the machine app, with no browser, and it's allowed every permission on the API. Keep it to development. Your Auth0 login has to be current, because the Auth0 CLI fetches the token.
|
|
132
152
|
|
|
133
153
|
#### Keep Auth0 in sync with controllers
|
|
134
154
|
|
|
135
155
|
`@Authorize("read", "Invoice")` becomes Auth0 API permission `read:Invoice`. After adding a controller:
|
|
136
156
|
|
|
137
157
|
```bash
|
|
138
|
-
|
|
158
|
+
pnpm decohere auth0 configure
|
|
139
159
|
```
|
|
140
160
|
|
|
141
161
|
Then get a new access token so it includes the new permissions.
|
|
@@ -195,9 +215,9 @@ override `defineAbilitiesFor` for product CASL rules, and `mapPrincipal` for IdP
|
|
|
195
215
|
(including the `AUTH0_ADMIN_USER_SUB` admin bootstrap). `@decohere/http`'s default already maps
|
|
196
216
|
`admin` → `manage`/`all` and `action:Subject` scopes.
|
|
197
217
|
|
|
198
|
-
####
|
|
218
|
+
#### Try the API
|
|
199
219
|
|
|
200
|
-
While the API is running,
|
|
220
|
+
While the API is running, `http://localhost:3000/openapi.json` describes every route.
|
|
201
221
|
|
|
202
222
|
## Options
|
|
203
223
|
|
|
@@ -205,10 +225,11 @@ While the API is running, choose Import in Postman and paste `http://localhost:3
|
|
|
205
225
|
|---|---|---|
|
|
206
226
|
| `[directory]` / `-d <directory>` | `webapi` / global | Target directory (positional wins; default `.`) |
|
|
207
227
|
| `--name <slug>` | `webapi` | API directory under `projects/` (repeatable; default `api`) |
|
|
208
|
-
| `add api <name>` | `decohere add api`
|
|
209
|
-
| `add auth0` | `decohere add auth0`
|
|
210
|
-
| `auth0 login` | `decohere auth0`
|
|
211
|
-
| `auth0 configure` | `decohere auth0`
|
|
228
|
+
| `add api <name>` | `decohere add api` | Add an API to an existing Decohere workspace |
|
|
229
|
+
| `add auth0` | `decohere add auth0` | Add Auth0 to an existing workspace |
|
|
230
|
+
| `auth0 login` | `decohere auth0` | Device/URL login; keeps tokens in `~/.config/auth0` |
|
|
231
|
+
| `auth0 configure` | `decohere auth0` | Update the Auth0 API, login app and token app for an existing workspace |
|
|
232
|
+
| `auth0 token` | `decohere auth0` | Print an access token for calling the API in development |
|
|
212
233
|
| `--database <provider>` | `webapi` / `add` | `none` \| `postgres/typeorm` |
|
|
213
234
|
| `--auth <provider>` | `webapi` / `add` | `none` \| `auth0` |
|
|
214
235
|
| `-y, --yes` | `webapi` / `add` | Install dependencies without prompting |
|
package/es6/src/cli.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { launch } from "./runner.js";
|
|
3
|
+
await launch(process.argv.slice(2));
|
package/es6/src/index.js
CHANGED
|
@@ -62,7 +62,7 @@ export function containerArgvFromHost(argv) {
|
|
|
62
62
|
targetDir: cwd,
|
|
63
63
|
containerArgv: argv,
|
|
64
64
|
projectName: basename(cwd),
|
|
65
|
-
kind: "
|
|
65
|
+
kind: "create"
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
let directoryFlag;
|
|
@@ -97,6 +97,6 @@ export function containerArgvFromHost(argv) {
|
|
|
97
97
|
"."
|
|
98
98
|
],
|
|
99
99
|
projectName: basename(targetDir),
|
|
100
|
-
kind: product[0] === "auth0" ? "
|
|
100
|
+
kind: product[0] === "auth0" || isAdd ? "project" : "create"
|
|
101
101
|
};
|
|
102
102
|
}
|
package/es6/src/runner.js
CHANGED
|
@@ -1,18 +1,146 @@
|
|
|
1
|
-
import { execFileSync, spawnSync } from "node:child_process";
|
|
2
|
-
import { existsSync, mkdirSync } from "node:fs";
|
|
1
|
+
import { execFileSync, spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
|
-
import {
|
|
4
|
+
import { randomBytes } from "node:crypto";
|
|
5
|
+
import { join } from "node:path";
|
|
5
6
|
import { resolveLaunch } from "./resolve-launch.js";
|
|
7
|
+
import { hasDecohereTools, isInContainer, OPEN_DEV_CONTAINER_MESSAGE } from "./tools.js";
|
|
6
8
|
import { CREATE_IMAGE_REPOSITORY, CREATE_VERSION } from "./version.js";
|
|
7
9
|
const BUILDER_IMAGE = `${CREATE_IMAGE_REPOSITORY}:${CREATE_VERSION}`;
|
|
10
|
+
const SCAFFOLD_PACKAGE = `@decohere/scaffold@${CREATE_VERSION}`;
|
|
11
|
+
const WORKSPACE = "/workspace";
|
|
12
|
+
/** Passed by name only, so the value is never on a command line. */ const ADMIN_PASSWORD_ENV = "DECOHERE_ADMIN_PASSWORD";
|
|
13
|
+
const SIGNAL_EXIT_CODES = {
|
|
14
|
+
SIGINT: 130,
|
|
15
|
+
SIGTERM: 143,
|
|
16
|
+
SIGHUP: 129
|
|
17
|
+
};
|
|
8
18
|
/**
|
|
9
|
-
*
|
|
10
|
-
* the
|
|
11
|
-
*/ export function
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
* Where a command runs: right here if this container has Decohere's tools, otherwise in
|
|
20
|
+
* the scaffolder image. Commands on an existing project need that project's Dev Container.
|
|
21
|
+
*/ export async function launch(argv) {
|
|
22
|
+
const plan = resolveLaunch(argv);
|
|
23
|
+
if (hasDecohereTools()) runHere(plan);
|
|
24
|
+
if (plan.kind === "project") {
|
|
25
|
+
console.error(OPEN_DEV_CONTAINER_MESSAGE);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
return runInBuilder(argv);
|
|
29
|
+
}
|
|
30
|
+
/** Help and version print and leave; nothing is written, so there's nothing to copy out. */ function writesProject({ containerArgv }) {
|
|
31
|
+
return !containerArgv.some((arg)=>[
|
|
32
|
+
"--help",
|
|
33
|
+
"-h",
|
|
34
|
+
"--version"
|
|
35
|
+
].includes(arg));
|
|
36
|
+
}
|
|
37
|
+
/** A new project only goes into an empty folder, so a copy can never overwrite someone's files. */ function refuseFolderWithFiles(targetDir) {
|
|
38
|
+
if (!existsSync(targetDir)) return;
|
|
39
|
+
const files = readdirSync(targetDir).filter((name)=>name !== ".git");
|
|
40
|
+
if (files.length === 0) return;
|
|
41
|
+
console.error(`\n ${targetDir} already has files in it.\n` + " Choose a new folder. To change an existing project, run `pnpm decohere add` in its Dev Container.\n");
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
/** Meets Auth0's password rules whichever policy the tenant uses. */ function generateAdminPassword() {
|
|
45
|
+
return `${randomBytes(18).toString("base64url")}aA1!`;
|
|
46
|
+
}
|
|
47
|
+
/** Store it in the Linux key store, if there is one. The value goes over stdin, never in argv. */ function saveToKeyStore(projectName, password) {
|
|
48
|
+
if (process.platform !== "linux") return false;
|
|
49
|
+
const stored = spawnSync("secret-tool", [
|
|
50
|
+
"store",
|
|
51
|
+
`--label=Decohere ${projectName} Auth0 admin`,
|
|
52
|
+
...keyStoreAttributes(projectName)
|
|
53
|
+
], {
|
|
54
|
+
input: password,
|
|
55
|
+
stdio: [
|
|
56
|
+
"pipe",
|
|
57
|
+
"ignore",
|
|
58
|
+
"ignore"
|
|
59
|
+
]
|
|
60
|
+
});
|
|
61
|
+
return stored.error == null && stored.status === 0;
|
|
62
|
+
}
|
|
63
|
+
function keyStoreAttributes(projectName) {
|
|
64
|
+
return [
|
|
65
|
+
"service",
|
|
66
|
+
"decohere",
|
|
67
|
+
"project",
|
|
68
|
+
projectName,
|
|
69
|
+
"account",
|
|
70
|
+
"auth0-admin"
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* After a successful run, save the generated password: in the OS key store where there is one,
|
|
75
|
+
* otherwise in one private file outside the project. It's only for logging in, so nothing in
|
|
76
|
+
* the app reads it. Nothing is saved if no admin was made.
|
|
77
|
+
*/ function saveAdminPassword(targetDir, projectName, password) {
|
|
78
|
+
const env = join(targetDir, ".env");
|
|
79
|
+
const email = existsSync(env) ? readFileSync(env, "utf8").match(/^AUTH0_ADMIN_USER_EMAIL=(.+)$/m)?.[1] : undefined;
|
|
80
|
+
if (email == null) return;
|
|
81
|
+
const folder = join(homedir(), ".config", "decohere");
|
|
82
|
+
const file = join(folder, `${projectName}.env`);
|
|
83
|
+
if (saveToKeyStore(projectName, password)) {
|
|
84
|
+
// A file from an earlier run would hold a password that no longer works.
|
|
85
|
+
rmSync(file, {
|
|
86
|
+
force: true
|
|
87
|
+
});
|
|
88
|
+
console.log("\n Auth0 admin password saved in your key store. To read it:");
|
|
89
|
+
console.log(`\n secret-tool lookup ${keyStoreAttributes(projectName).join(" ")}`);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
mkdirSync(folder, {
|
|
93
|
+
recursive: true,
|
|
94
|
+
mode: 0o700
|
|
95
|
+
});
|
|
96
|
+
writeFileSync(file, `AUTH0_ADMIN_USER_EMAIL=${email}\nAUTH0_ADMIN_USER_PASSWORD=${password}\n`, {
|
|
97
|
+
mode: 0o600
|
|
98
|
+
});
|
|
99
|
+
chmodSync(file, 0o600);
|
|
100
|
+
console.log(`\n No key store found, so the Auth0 admin password is in a private file: ${file}`);
|
|
101
|
+
if (isInContainer()) {
|
|
102
|
+
console.log(" That's inside this container, so copy it if the container may be rebuilt.");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function printNextSteps(targetDir) {
|
|
106
|
+
const open = isInContainer() ? ` cd ${targetDir}\n pnpm install\n pnpm dev\n` : ` code ${targetDir}\n\n Then reopen it in its Dev Container.\n`;
|
|
107
|
+
console.log(`\n Scaffold complete!\n\n Open your project:\n\n${open}`);
|
|
108
|
+
}
|
|
109
|
+
/** Run the scaffolder in this container, without starting another one. */ function runHere(plan) {
|
|
110
|
+
const { targetDir, containerArgv, projectName, kind } = plan;
|
|
111
|
+
const creating = kind === "create" && writesProject(plan);
|
|
112
|
+
if (creating) {
|
|
113
|
+
refuseFolderWithFiles(targetDir);
|
|
114
|
+
mkdirSync(targetDir, {
|
|
115
|
+
recursive: true
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const adminPassword = generateAdminPassword();
|
|
119
|
+
const result = spawnSync("npx", [
|
|
120
|
+
"--yes",
|
|
121
|
+
SCAFFOLD_PACKAGE,
|
|
122
|
+
...containerArgv
|
|
123
|
+
], {
|
|
124
|
+
cwd: targetDir,
|
|
125
|
+
stdio: "inherit",
|
|
126
|
+
env: {
|
|
127
|
+
...process.env,
|
|
128
|
+
DECOHERE_BUILDER: "1",
|
|
129
|
+
DECOHERE_PROJECT_NAME: projectName,
|
|
130
|
+
[ADMIN_PASSWORD_ENV]: adminPassword
|
|
131
|
+
}
|
|
15
132
|
});
|
|
133
|
+
if (result.error != null) {
|
|
134
|
+
console.error(`\n Could not start the scaffolder: ${result.error.message}\n`);
|
|
135
|
+
process.exit(1);
|
|
136
|
+
}
|
|
137
|
+
if (result.status === 0 && creating) {
|
|
138
|
+
saveAdminPassword(targetDir, projectName, adminPassword);
|
|
139
|
+
printNextSteps(targetDir);
|
|
140
|
+
}
|
|
141
|
+
process.exit(result.status ?? 1);
|
|
142
|
+
}
|
|
143
|
+
function assertDockerAvailable() {
|
|
16
144
|
try {
|
|
17
145
|
execFileSync("docker", [
|
|
18
146
|
"info"
|
|
@@ -20,74 +148,140 @@ const BUILDER_IMAGE = `${CREATE_IMAGE_REPOSITORY}:${CREATE_VERSION}`;
|
|
|
20
148
|
stdio: "pipe"
|
|
21
149
|
});
|
|
22
150
|
} catch {
|
|
23
|
-
console.error("\n Docker is required to scaffold a Decohere project.\n" + " Install Docker Desktop: https://www.docker.com/products/docker-desktop\n");
|
|
151
|
+
console.error(isInContainer() ? "\n Docker is required to scaffold a Decohere project, and this container can't reach it.\n" + " Run this on your own machine, or give this container access to Docker.\n" : "\n Docker is required to scaffold a Decohere project.\n" + " Install Docker Desktop: https://www.docker.com/products/docker-desktop\n");
|
|
24
152
|
process.exit(1);
|
|
25
153
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
154
|
+
}
|
|
155
|
+
/** The container holds generated files and any Auth0 login until this runs. */ function removeContainer(name) {
|
|
156
|
+
const removed = spawnSync("docker", [
|
|
157
|
+
"rm",
|
|
158
|
+
"-f",
|
|
159
|
+
name
|
|
160
|
+
], {
|
|
161
|
+
stdio: "ignore"
|
|
32
162
|
});
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"-v",
|
|
36
|
-
`${targetDir}:/workspace`,
|
|
37
|
-
"-v",
|
|
38
|
-
`${npmCacheDir}:/npm-cache`
|
|
39
|
-
];
|
|
40
|
-
const envArgs = [
|
|
41
|
-
"-e",
|
|
42
|
-
"DECOHERE_BUILDER=1",
|
|
43
|
-
"-e",
|
|
44
|
-
`DECOHERE_PROJECT_NAME=${projectName}`,
|
|
45
|
-
"-e",
|
|
46
|
-
"NPM_CONFIG_CACHE=/npm-cache",
|
|
47
|
-
"-e",
|
|
48
|
-
"NPM_CONFIG_PREFER_OFFLINE=true",
|
|
49
|
-
"-e",
|
|
50
|
-
`HOME=${builderHome}`
|
|
51
|
-
];
|
|
52
|
-
const npmrcPath = resolve(homedir(), ".npmrc");
|
|
53
|
-
if (existsSync(npmrcPath)) {
|
|
54
|
-
volumes.push("-v", `${npmrcPath}:/tmp/host-npmrc:ro`);
|
|
55
|
-
envArgs.push("-e", "NPM_CONFIG_USERCONFIG=/tmp/host-npmrc");
|
|
163
|
+
if (removed.status !== 0) {
|
|
164
|
+
console.error(`\n Could not remove container ${name}. Remove it with: docker rm -f ${name}\n`);
|
|
56
165
|
}
|
|
57
|
-
|
|
58
|
-
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Copy the finished project out of the container. It lands in a temporary folder first, so a
|
|
169
|
+
* copy that fails partway leaves the target as it was.
|
|
170
|
+
*/ function copyProjectOut(name, targetDir) {
|
|
171
|
+
const createdTarget = !existsSync(targetDir);
|
|
172
|
+
mkdirSync(targetDir, {
|
|
59
173
|
recursive: true
|
|
60
174
|
});
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
175
|
+
const temp = mkdtempSync(join(targetDir, ".decohere-create-"));
|
|
176
|
+
try {
|
|
177
|
+
const copy = spawnSync("docker", [
|
|
178
|
+
"cp",
|
|
179
|
+
`${name}:${WORKSPACE}/.`,
|
|
180
|
+
temp
|
|
181
|
+
], {
|
|
182
|
+
stdio: [
|
|
183
|
+
"ignore",
|
|
184
|
+
"ignore",
|
|
185
|
+
"inherit"
|
|
186
|
+
]
|
|
187
|
+
});
|
|
188
|
+
if (copy.status !== 0) {
|
|
189
|
+
throw new Error(`docker cp failed with exit code ${copy.status}`);
|
|
190
|
+
}
|
|
191
|
+
for (const entry of readdirSync(temp)){
|
|
192
|
+
renameSync(join(temp, entry), join(targetDir, entry));
|
|
193
|
+
}
|
|
194
|
+
rmSync(temp, {
|
|
195
|
+
recursive: true,
|
|
196
|
+
force: true
|
|
197
|
+
});
|
|
198
|
+
} catch (reason) {
|
|
199
|
+
rmSync(createdTarget ? targetDir : temp, {
|
|
200
|
+
recursive: true,
|
|
201
|
+
force: true
|
|
202
|
+
});
|
|
203
|
+
throw reason;
|
|
68
204
|
}
|
|
69
|
-
|
|
205
|
+
}
|
|
206
|
+
function attach(name) {
|
|
207
|
+
return new Promise((resolve, reject)=>{
|
|
208
|
+
const child = spawn("docker", [
|
|
209
|
+
"start",
|
|
210
|
+
"--attach",
|
|
211
|
+
"--interactive",
|
|
212
|
+
name
|
|
213
|
+
], {
|
|
214
|
+
stdio: "inherit"
|
|
215
|
+
});
|
|
216
|
+
child.on("error", reject);
|
|
217
|
+
child.on("close", (code)=>resolve(code ?? 1));
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Run the scaffolder image with nothing shared from this machine, then copy the project out.
|
|
222
|
+
* Docker finds shared folders on the machine running Docker, which may not be this one, so
|
|
223
|
+
* this works the same from a host, a container, or against a remote Docker.
|
|
224
|
+
*/ export async function runInBuilder(argv) {
|
|
225
|
+
const plan = resolveLaunch(argv);
|
|
226
|
+
const { targetDir, containerArgv, projectName } = plan;
|
|
227
|
+
const copyOut = writesProject(plan);
|
|
228
|
+
if (copyOut) refuseFolderWithFiles(targetDir);
|
|
229
|
+
assertDockerAvailable();
|
|
230
|
+
const name = `decohere-create-${randomBytes(6).toString("hex")}`;
|
|
231
|
+
const adminPassword = generateAdminPassword();
|
|
70
232
|
const ttyFlags = process.stdin.isTTY ? [
|
|
71
|
-
"
|
|
233
|
+
"--interactive",
|
|
234
|
+
"--tty"
|
|
72
235
|
] : [
|
|
73
|
-
"
|
|
236
|
+
"--interactive"
|
|
74
237
|
];
|
|
75
|
-
const
|
|
76
|
-
"
|
|
77
|
-
"--
|
|
238
|
+
const created = spawnSync("docker", [
|
|
239
|
+
"create",
|
|
240
|
+
"--name",
|
|
241
|
+
name,
|
|
78
242
|
...ttyFlags,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
"
|
|
243
|
+
"--env",
|
|
244
|
+
"DECOHERE_BUILDER=1",
|
|
245
|
+
"--env",
|
|
246
|
+
`DECOHERE_PROJECT_NAME=${projectName}`,
|
|
247
|
+
"--env",
|
|
248
|
+
ADMIN_PASSWORD_ENV,
|
|
249
|
+
"--workdir",
|
|
250
|
+
WORKSPACE,
|
|
84
251
|
BUILDER_IMAGE,
|
|
85
252
|
...containerArgv
|
|
86
253
|
], {
|
|
87
|
-
stdio:
|
|
254
|
+
stdio: [
|
|
255
|
+
"ignore",
|
|
256
|
+
"ignore",
|
|
257
|
+
"inherit"
|
|
258
|
+
],
|
|
259
|
+
env: {
|
|
260
|
+
...process.env,
|
|
261
|
+
[ADMIN_PASSWORD_ENV]: adminPassword
|
|
262
|
+
}
|
|
88
263
|
});
|
|
89
|
-
if (
|
|
90
|
-
|
|
264
|
+
if (created.status !== 0) process.exit(created.status ?? 1);
|
|
265
|
+
for (const [signal, code] of Object.entries(SIGNAL_EXIT_CODES)){
|
|
266
|
+
process.once(signal, ()=>{
|
|
267
|
+
removeContainer(name);
|
|
268
|
+
process.exit(code);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
console.log(" Running scaffolding in a sandboxed container...\n");
|
|
272
|
+
let exitCode = 1;
|
|
273
|
+
try {
|
|
274
|
+
exitCode = await attach(name);
|
|
275
|
+
if (exitCode === 0 && copyOut) {
|
|
276
|
+
copyProjectOut(name, targetDir);
|
|
277
|
+
saveAdminPassword(targetDir, projectName, adminPassword);
|
|
278
|
+
printNextSteps(targetDir);
|
|
279
|
+
}
|
|
280
|
+
} catch (reason) {
|
|
281
|
+
exitCode = 1;
|
|
282
|
+
console.error(`\n Could not finish the scaffold: ${reason instanceof Error ? reason.message : String(reason)}\n`);
|
|
283
|
+
} finally{
|
|
284
|
+
removeContainer(name);
|
|
91
285
|
}
|
|
92
|
-
process.exit(
|
|
286
|
+
process.exit(exitCode);
|
|
93
287
|
}
|
package/es6/src/tools.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
/** Set by Decohere's Dev Container image, and by the images built from it. */ const TOOLS_ENV = "DECOHERE_TOOLS";
|
|
3
|
+
/** Whether this process runs somewhere that already has Decohere's scaffolding tools. */ export function hasDecohereTools(env = process.env) {
|
|
4
|
+
return env[TOOLS_ENV] === "1";
|
|
5
|
+
}
|
|
6
|
+
/** Whether this process runs in any container, where a project's own Dev Container can't open. */ export function isInContainer(env = process.env) {
|
|
7
|
+
return env.REMOTE_CONTAINERS === "true" || env.CODESPACES === "true" || existsSync("/.dockerenv") || existsSync("/run/.containerenv");
|
|
8
|
+
}
|
|
9
|
+
export const OPEN_DEV_CONTAINER_MESSAGE = "\n This command runs inside the project's Dev Container, which has the tools it needs.\n" + " Open the project in its Dev Container, then run it there, for example:\n\n" + " pnpm decohere add auth0\n";
|
package/package.json
CHANGED
package/types/src/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export type { LaunchKind, LaunchPlan } from "./resolve-launch.js";
|
|
2
2
|
export { containerArgvFromHost, resolveLaunch, toProductArgv } from "./resolve-launch.js";
|
|
3
|
-
export { runInBuilder } from "./runner.js";
|
|
3
|
+
export { launch, runInBuilder } from "./runner.js";
|
|
4
|
+
export { hasDecohereTools, OPEN_DEV_CONTAINER_MESSAGE } from "./tools.js";
|
package/types/src/runner.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* the
|
|
2
|
+
* Where a command runs: right here if this container has Decohere's tools, otherwise in
|
|
3
|
+
* the scaffolder image. Commands on an existing project need that project's Dev Container.
|
|
4
4
|
*/
|
|
5
|
-
export declare function
|
|
5
|
+
export declare function launch(argv: string[]): Promise<never>;
|
|
6
|
+
/**
|
|
7
|
+
* Run the scaffolder image with nothing shared from this machine, then copy the project out.
|
|
8
|
+
* Docker finds shared folders on the machine running Docker, which may not be this one, so
|
|
9
|
+
* this works the same from a host, a container, or against a remote Docker.
|
|
10
|
+
*/
|
|
11
|
+
export declare function runInBuilder(argv: string[]): Promise<never>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Whether this process runs somewhere that already has Decohere's scaffolding tools. */
|
|
2
|
+
export declare function hasDecohereTools(env?: NodeJS.ProcessEnv): boolean;
|
|
3
|
+
/** Whether this process runs in any container, where a project's own Dev Container can't open. */
|
|
4
|
+
export declare function isInContainer(env?: NodeJS.ProcessEnv): boolean;
|
|
5
|
+
export declare const OPEN_DEV_CONTAINER_MESSAGE: string;
|