create-protobase 0.46.11 → 0.46.13
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/dist/ask-dir.js +9 -0
- package/dist/ask-preset.js +3 -9
- package/dist/create-project.js +17 -5
- package/dist/env-file.js +22 -0
- package/dist/free-port.js +18 -0
- package/dist/index.js +44 -7
- package/dist/local-database.js +41 -0
- package/dist/next-steps.js +16 -11
- package/dist/templates/erp/package.json +5 -5
- package/dist/templates/real-estate/package.json +5 -5
- package/dist/templates/scratch/package.json +5 -5
- package/dist/tools.js +10 -0
- package/package.json +1 -1
package/dist/ask-dir.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/ask-dir.ts
|
|
2
|
+
/** The folder a new project goes in when nobody names one. */
|
|
3
|
+
const defaultDir = "my-app";
|
|
4
|
+
/** The folder for an answer: the answer trimmed, the default when it is empty. */
|
|
5
|
+
const dirForAnswer = (answer) => answer.trim() || "my-app";
|
|
6
|
+
/** Asks with `question`, a terminal prompt's, which folder to write the new project in. */
|
|
7
|
+
const askDir = async (question) => dirForAnswer(await question(`Project folder [${defaultDir}]: `));
|
|
8
|
+
//#endregion
|
|
9
|
+
export { askDir, defaultDir, dirForAnswer };
|
package/dist/ask-preset.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createInterface } from "node:readline/promises";
|
|
2
1
|
//#region src/ask-preset.ts
|
|
3
2
|
/** The preset for an answer: its number in `names` or its name, the first one when the answer is empty. */
|
|
4
3
|
const presetForAnswer = (names, answer) => {
|
|
@@ -9,16 +8,11 @@ const presetForAnswer = (names, answer) => {
|
|
|
9
8
|
return names.find((name) => name === text);
|
|
10
9
|
};
|
|
11
10
|
const menu = (names) => `Which preset do you want to start from?\n${names.map((name, i) => ` ${i + 1}. ${name}${i === 0 ? " (default)" : ""}\n`).join("")}`;
|
|
12
|
-
/** Asks
|
|
13
|
-
const askPreset = async (names) => {
|
|
14
|
-
const prompt = createInterface({
|
|
15
|
-
input: process.stdin,
|
|
16
|
-
output: process.stdout
|
|
17
|
-
});
|
|
11
|
+
/** Asks with `question`, a terminal prompt's, which of `names` to start from, again until the answer is one of them. */
|
|
12
|
+
const askPreset = async (names, question) => {
|
|
18
13
|
process.stdout.write(menu(names));
|
|
19
14
|
let picked;
|
|
20
|
-
while (!picked) picked = presetForAnswer(names, await
|
|
21
|
-
prompt.close();
|
|
15
|
+
while (!picked) picked = presetForAnswer(names, await question(`Preset [1-${names.length}]: `));
|
|
22
16
|
return picked;
|
|
23
17
|
};
|
|
24
18
|
//#endregion
|
package/dist/create-project.js
CHANGED
|
@@ -1,20 +1,32 @@
|
|
|
1
|
+
import { databaseUrl, envFile, withDatabasePort } from "./env-file.js";
|
|
2
|
+
import { composeFile, databaseScripts } from "./local-database.js";
|
|
1
3
|
import path from "node:path";
|
|
2
4
|
import { cpSync, existsSync, readFileSync, readdirSync, renameSync, writeFileSync } from "node:fs";
|
|
3
5
|
//#region src/create-project.ts
|
|
4
6
|
/**
|
|
5
|
-
* Writes a new project to `targetDir` from `templateDir`, which the build writes from a preset,
|
|
6
|
-
* named after the folder,
|
|
7
|
+
* Writes a new project to `targetDir` from `templateDir`, which the build writes from a preset, and returns its
|
|
8
|
+
* package.json: named after the folder, with db:up and db:down for the docker-compose.yml it adds, whose Postgres takes
|
|
9
|
+
* `port` in it, .env.example and a .env with `secret`. The folder may exist only when it is empty.
|
|
7
10
|
*/
|
|
8
|
-
const createProject = (templateDir, targetDir) => {
|
|
11
|
+
const createProject = (templateDir, targetDir, { port, secret }) => {
|
|
9
12
|
if (existsSync(targetDir) && readdirSync(targetDir).length > 0) throw new Error(`${targetDir} is not empty`);
|
|
10
13
|
cpSync(templateDir, targetDir, { recursive: true });
|
|
11
14
|
for (const file of readdirSync(targetDir).filter((file) => file.startsWith("_"))) renameSync(path.join(targetDir, file), path.join(targetDir, file.slice(1)));
|
|
12
15
|
const packageFile = path.join(targetDir, "package.json");
|
|
16
|
+
const source = JSON.parse(readFileSync(packageFile, "utf8"));
|
|
13
17
|
const manifest = {
|
|
14
|
-
...
|
|
15
|
-
name: path.basename(targetDir)
|
|
18
|
+
...source,
|
|
19
|
+
name: path.basename(targetDir),
|
|
20
|
+
scripts: {
|
|
21
|
+
...databaseScripts,
|
|
22
|
+
...source.scripts
|
|
23
|
+
}
|
|
16
24
|
};
|
|
17
25
|
writeFileSync(packageFile, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
26
|
+
const example = withDatabasePort(readFileSync(path.join(targetDir, ".env.example"), "utf8"), port);
|
|
27
|
+
writeFileSync(path.join(targetDir, ".env.example"), example);
|
|
28
|
+
writeFileSync(path.join(targetDir, ".env"), envFile(example, secret));
|
|
29
|
+
writeFileSync(path.join(targetDir, "docker-compose.yml"), composeFile(databaseUrl(example)));
|
|
18
30
|
return manifest;
|
|
19
31
|
};
|
|
20
32
|
//#endregion
|
package/dist/env-file.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/env-file.ts
|
|
2
|
+
const setting = (text, name) => {
|
|
3
|
+
const line = text.split("\n").find((line) => line.startsWith(`${name}=`));
|
|
4
|
+
if (line === void 0) throw new Error(`.env.example has no ${name}`);
|
|
5
|
+
return line.slice(name.length + 1).trim();
|
|
6
|
+
};
|
|
7
|
+
const replaceSetting = (text, name, value) => text.split("\n").map((line) => line.startsWith(`${name}=`) ? `${name}=${value}` : line).join("\n");
|
|
8
|
+
/** The DATABASE_URL of a preset's .env.example. */
|
|
9
|
+
const databaseUrl = (example) => new URL(setting(example, "DATABASE_URL"));
|
|
10
|
+
/** `example`, a preset's .env.example, with its DATABASE_URL on `port`. */
|
|
11
|
+
const withDatabasePort = (example, port) => {
|
|
12
|
+
const url = databaseUrl(example);
|
|
13
|
+
url.port = String(port);
|
|
14
|
+
return replaceSetting(example, "DATABASE_URL", url.toString());
|
|
15
|
+
};
|
|
16
|
+
/** A new project's .env: its .env.example with `secret` as BETTER_AUTH_SECRET in place of the placeholder. */
|
|
17
|
+
const envFile = (example, secret) => {
|
|
18
|
+
setting(example, "BETTER_AUTH_SECRET");
|
|
19
|
+
return replaceSetting(example, "BETTER_AUTH_SECRET", secret);
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
22
|
+
export { databaseUrl, envFile, withDatabasePort };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createServer } from "node:net";
|
|
2
|
+
//#region src/free-port.ts
|
|
3
|
+
const canListen = (port, host) => new Promise((resolve, reject) => {
|
|
4
|
+
const server = createServer();
|
|
5
|
+
server.once("error", (error) => {
|
|
6
|
+
if (error.code === "EADDRINUSE" || error.code === "EACCES") return resolve(false);
|
|
7
|
+
reject(error);
|
|
8
|
+
});
|
|
9
|
+
server.listen(port, host, () => server.close(() => resolve(true)));
|
|
10
|
+
});
|
|
11
|
+
const isFree = async (port) => await canListen(port, void 0) && await canListen(port, "127.0.0.1");
|
|
12
|
+
/** The first port from `start` on, within 100, that nothing listens on. */
|
|
13
|
+
const freePort = async (start) => {
|
|
14
|
+
for (let port = start; port < start + 100; port++) if (await isFree(port)) return port;
|
|
15
|
+
throw new Error(`no free port from ${start} to ${start + 99} for the project's Postgres`);
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { freePort };
|
package/dist/index.js
CHANGED
|
@@ -1,29 +1,66 @@
|
|
|
1
|
+
import { askDir } from "./ask-dir.js";
|
|
1
2
|
import { askPreset } from "./ask-preset.js";
|
|
3
|
+
import { defaultPort } from "./local-database.js";
|
|
2
4
|
import { createProject } from "./create-project.js";
|
|
5
|
+
import { freePort } from "./free-port.js";
|
|
3
6
|
import { nextSteps } from "./next-steps.js";
|
|
4
7
|
import { checkPreset, defaultPreset, presetNames } from "./presets.js";
|
|
8
|
+
import { findTools } from "./tools.js";
|
|
9
|
+
import { randomBytes } from "node:crypto";
|
|
5
10
|
import path from "node:path";
|
|
11
|
+
import { createInterface } from "node:readline/promises";
|
|
6
12
|
import { parseArgs } from "node:util";
|
|
7
13
|
//#region src/index.ts
|
|
8
14
|
const templatesDir = path.join(import.meta.dirname, "templates");
|
|
9
15
|
const usage = "usage: npm create protobase <dir> [-- --preset <name>]";
|
|
10
|
-
const
|
|
16
|
+
const checkNpmPreset = (flag) => {
|
|
17
|
+
if (flag !== void 0 || process.env.npm_config_preset === void 0) return;
|
|
18
|
+
throw new Error("npm took --preset for itself: put -- before it, as in npm create protobase my-app -- --preset erp");
|
|
19
|
+
};
|
|
20
|
+
const pickDir = async (positionals, question) => {
|
|
21
|
+
const [dir, ...extra] = positionals;
|
|
22
|
+
if (extra.length > 0) throw new Error(usage);
|
|
23
|
+
if (dir) return dir;
|
|
24
|
+
if (question) return askDir(question);
|
|
25
|
+
throw new Error(usage);
|
|
26
|
+
};
|
|
27
|
+
const pickPreset = async (flag, question) => {
|
|
11
28
|
const names = presetNames(templatesDir);
|
|
12
29
|
if (flag !== void 0) return checkPreset(names, flag);
|
|
13
|
-
if (
|
|
30
|
+
if (question) return askPreset(names, question);
|
|
14
31
|
return defaultPreset;
|
|
15
32
|
};
|
|
33
|
+
const pick = async (positionals, flag) => {
|
|
34
|
+
const prompt = process.stdin.isTTY && process.stdout.isTTY ? createInterface({
|
|
35
|
+
input: process.stdin,
|
|
36
|
+
output: process.stdout
|
|
37
|
+
}) : void 0;
|
|
38
|
+
const question = prompt && ((text) => prompt.question(text));
|
|
39
|
+
try {
|
|
40
|
+
return {
|
|
41
|
+
dir: await pickDir(positionals, question),
|
|
42
|
+
preset: await pickPreset(flag, question)
|
|
43
|
+
};
|
|
44
|
+
} finally {
|
|
45
|
+
prompt?.close();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
16
48
|
const create = async (argv) => {
|
|
17
49
|
const { values, positionals } = parseArgs({
|
|
18
50
|
args: argv.slice(2),
|
|
19
51
|
allowPositionals: true,
|
|
20
52
|
options: { preset: { type: "string" } }
|
|
21
53
|
});
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
|
|
54
|
+
checkNpmPreset(values.preset);
|
|
55
|
+
const { dir, preset } = await pick(positionals, values.preset);
|
|
56
|
+
const port = await freePort(defaultPort);
|
|
57
|
+
const secret = randomBytes(32).toString("base64");
|
|
58
|
+
const { scripts } = createProject(path.join(templatesDir, preset), path.resolve(dir), {
|
|
59
|
+
port,
|
|
60
|
+
secret
|
|
61
|
+
});
|
|
62
|
+
const steps = nextSteps(dir, scripts, port, findTools());
|
|
63
|
+
process.stdout.write(`Created a Protobase project in ${dir} from the ${preset} preset. Next:\n\n${steps}`);
|
|
27
64
|
};
|
|
28
65
|
const run = async (argv) => {
|
|
29
66
|
try {
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region src/local-database.ts
|
|
2
|
+
/** The port a new project's Postgres takes when it is free, as in the presets' .env.example: one beside a local 5432. */
|
|
3
|
+
const defaultPort = 55432;
|
|
4
|
+
/** The scripts a new project starts and stops its Postgres with, from the docker-compose.yml that composeFile writes. */
|
|
5
|
+
const databaseScripts = {
|
|
6
|
+
"db:up": "docker compose up -d --wait",
|
|
7
|
+
"db:down": "docker compose down"
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* A docker-compose.yml with one Postgres 18 for the database, role and password of `databaseUrl`, published on its port
|
|
11
|
+
* and kept in a volume. Compose names the project, and so the volume, after the folder.
|
|
12
|
+
*/
|
|
13
|
+
const composeFile = (databaseUrl) => {
|
|
14
|
+
const user = decodeURIComponent(databaseUrl.username);
|
|
15
|
+
const database = databaseUrl.pathname.slice(1);
|
|
16
|
+
return `# The app's Postgres for development: \`bun run db:up\` starts it and \`bun run db:down\` stops it. The pgdata volume
|
|
17
|
+
# keeps the data; \`docker compose down -v\` removes it.
|
|
18
|
+
services:
|
|
19
|
+
postgres:
|
|
20
|
+
image: postgres:18
|
|
21
|
+
ports:
|
|
22
|
+
- '${databaseUrl.port}:5432'
|
|
23
|
+
environment:
|
|
24
|
+
POSTGRES_USER: ${user}
|
|
25
|
+
POSTGRES_PASSWORD: ${decodeURIComponent(databaseUrl.password)}
|
|
26
|
+
POSTGRES_DB: ${database}
|
|
27
|
+
shm_size: 1gb
|
|
28
|
+
volumes:
|
|
29
|
+
- pgdata:/var/lib/postgresql
|
|
30
|
+
healthcheck:
|
|
31
|
+
test: ['CMD-SHELL', 'pg_isready -U ${user} -d ${database}']
|
|
32
|
+
interval: 2s
|
|
33
|
+
timeout: 3s
|
|
34
|
+
retries: 30
|
|
35
|
+
|
|
36
|
+
volumes:
|
|
37
|
+
pgdata:
|
|
38
|
+
`;
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
41
|
+
export { composeFile, databaseScripts, defaultPort };
|
package/dist/next-steps.js
CHANGED
|
@@ -2,17 +2,22 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* What to run in a new project, from `cwd`'s point of view, until its admin app serves. `scripts` are those of the
|
|
4
4
|
* project's package.json: a preset with a base seed loads it, and one with sample data offers it, before the first user,
|
|
5
|
-
* who then owns the organizations the seeds wrote.
|
|
5
|
+
* who then owns the organizations the seeds wrote. Without Docker, the database is one of the user's own; without Bun,
|
|
6
|
+
* which runs every script, installing it comes first.
|
|
6
7
|
*/
|
|
7
|
-
const nextSteps = (dir, scripts) =>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
const nextSteps = (dir, scripts, port, tools) => {
|
|
9
|
+
const lines = [
|
|
10
|
+
`cd ${dir}`,
|
|
11
|
+
"bun install",
|
|
12
|
+
tools.docker ? `bun run db:up # Postgres 18 in Docker on port ${port}` : "# no Docker found: set DATABASE_URL in .env to a Postgres database of yours",
|
|
13
|
+
"bun run db:migrate",
|
|
14
|
+
..."db:seed:base" in scripts ? ["bun run db:seed:base"] : [],
|
|
15
|
+
..."db:seed" in scripts ? ["bun run db:seed --scale small # optional: sample data, replacing the app tables"] : [],
|
|
16
|
+
"bun run protobase users create you@example.com --generate-password",
|
|
17
|
+
"bun run dev # http://localhost:5173"
|
|
18
|
+
].map((line) => ` ${line}\n`).join("");
|
|
19
|
+
if (tools.bun) return lines;
|
|
20
|
+
return `Bun 1.4 runs the project's scripts: install it first (https://bun.sh).\n\n${lines}`;
|
|
21
|
+
};
|
|
17
22
|
//#endregion
|
|
18
23
|
export { nextSteps };
|
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
"dev": "protobase dev"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@protobase/cli": ">=0.46.
|
|
20
|
-
"@protobase/layout": ">=0.46.
|
|
21
|
-
"@protobase/schema": ">=0.46.
|
|
22
|
-
"@protobase/server": ">=0.46.
|
|
23
|
-
"@protobase/ui": ">=0.46.
|
|
19
|
+
"@protobase/cli": ">=0.46.13 <1",
|
|
20
|
+
"@protobase/layout": ">=0.46.13 <1",
|
|
21
|
+
"@protobase/schema": ">=0.46.13 <1",
|
|
22
|
+
"@protobase/server": ">=0.46.13 <1",
|
|
23
|
+
"@protobase/ui": ">=0.46.13 <1",
|
|
24
24
|
"better-auth": "^1.7.12",
|
|
25
25
|
"jose": "^6.2.12",
|
|
26
26
|
"kysely": "^0.29.6",
|
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
"dev": "protobase dev"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@protobase/cli": ">=0.46.
|
|
20
|
-
"@protobase/layout": ">=0.46.
|
|
21
|
-
"@protobase/schema": ">=0.46.
|
|
22
|
-
"@protobase/server": ">=0.46.
|
|
23
|
-
"@protobase/ui": ">=0.46.
|
|
19
|
+
"@protobase/cli": ">=0.46.13 <1",
|
|
20
|
+
"@protobase/layout": ">=0.46.13 <1",
|
|
21
|
+
"@protobase/schema": ">=0.46.13 <1",
|
|
22
|
+
"@protobase/server": ">=0.46.13 <1",
|
|
23
|
+
"@protobase/ui": ">=0.46.13 <1",
|
|
24
24
|
"better-auth": "^1.7.12",
|
|
25
25
|
"jose": "^6.2.12",
|
|
26
26
|
"kysely": "^0.29.6",
|
|
@@ -13,11 +13,11 @@
|
|
|
13
13
|
"dev": "protobase dev"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@protobase/cli": ">=0.46.
|
|
17
|
-
"@protobase/layout": ">=0.46.
|
|
18
|
-
"@protobase/schema": ">=0.46.
|
|
19
|
-
"@protobase/server": ">=0.46.
|
|
20
|
-
"@protobase/ui": ">=0.46.
|
|
16
|
+
"@protobase/cli": ">=0.46.13 <1",
|
|
17
|
+
"@protobase/layout": ">=0.46.13 <1",
|
|
18
|
+
"@protobase/schema": ">=0.46.13 <1",
|
|
19
|
+
"@protobase/server": ">=0.46.13 <1",
|
|
20
|
+
"@protobase/ui": ">=0.46.13 <1",
|
|
21
21
|
"better-auth": "^1.7.12",
|
|
22
22
|
"jose": "^6.2.12",
|
|
23
23
|
"kysely": "^0.29.6",
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
//#region src/tools.ts
|
|
3
|
+
const runs = (command) => spawnSync(command, ["--version"], { stdio: "ignore" }).error === void 0;
|
|
4
|
+
/** Which of the commands the next steps use are on PATH. */
|
|
5
|
+
const findTools = () => ({
|
|
6
|
+
bun: runs("bun"),
|
|
7
|
+
docker: runs("docker")
|
|
8
|
+
});
|
|
9
|
+
//#endregion
|
|
10
|
+
export { findTools };
|