create-cloud-db 1.0.1
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/.env +6 -0
- package/CHANGELOG.md +5 -0
- package/create-cloud-db.js +150 -0
- package/package.json +22 -0
package/.env
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
|
|
2
|
+
TURSO_DATABASE_URL=You are not logged in, please login with turso auth login before running other commands.
|
|
3
|
+
TURSO_AUTH_TOKEN=You are not logged in, please login with turso auth login before running other commands.
|
|
4
|
+
|
|
5
|
+
TURSO_DATABASE_URL=libsql://lol-xfreestar10.aws-us-west-2.turso.io
|
|
6
|
+
TURSO_AUTH_TOKEN=eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3NjYzNzYwNjEsImlkIjoiNDUyY2M2NzUtODk2Ny00Njc5LTk4MTItNTA2MGY1YmEyMDljIiwicmlkIjoiZjdkMWFhZTMtYzEzZC00OTEyLWFjYTAtNWRkY2Y5MTZkOWI5In0.loooOtvg-8fYzl_bi032Y32KGdDjOZhbKXdGFQLfrzt7Jbu9vDLY9I8KrUtOPprRFRrCHExXkfQtIg6xmt_XCg
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
### [1.0.1](https://github.com/vtempest/Svelte-Starter-DOCS/compare/v0.9.2...v1.0.1) (2025-12-22)
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { execSync } from "child_process";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import readline from "readline";
|
|
5
|
+
|
|
6
|
+
function ask(question) {
|
|
7
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
8
|
+
return new Promise((resolve) => rl.question(question, (answer) => {
|
|
9
|
+
rl.close();
|
|
10
|
+
resolve(answer.trim());
|
|
11
|
+
}));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function run(cmd, { ignoreError = false } = {}) {
|
|
15
|
+
try {
|
|
16
|
+
console.log(`\n$ ${cmd}`);
|
|
17
|
+
return execSync(cmd, { encoding: "utf8", stdio: ["inherit", "pipe", "pipe"] }).trim();
|
|
18
|
+
} catch (e) {
|
|
19
|
+
if (ignoreError) return (e.stdout || e.stderr || "").toString();
|
|
20
|
+
throw e;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Simple login check via `turso auth token`
|
|
25
|
+
function isLoggedIn() {
|
|
26
|
+
const out = run("turso auth token", { ignoreError: true }); // errors if not logged in [web:72][web:74]
|
|
27
|
+
const text = String(out || "");
|
|
28
|
+
if (!text) return false;
|
|
29
|
+
if (text.includes("You are not logged in, please login with turso auth login")) return false;
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function ensureLogin() {
|
|
34
|
+
if (isLoggedIn()) return;
|
|
35
|
+
console.error(
|
|
36
|
+
"\nYou are not logged in. Please run `turso auth login` (or `turso auth login --headless`) and try again.\n"
|
|
37
|
+
);
|
|
38
|
+
const answer = await ask("Run `turso auth login` now? (y/N): ");
|
|
39
|
+
if (!/^y(es)?$/i.test(answer)) process.exit(1);
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
run("turso auth login");
|
|
43
|
+
} catch {
|
|
44
|
+
console.error("Login failed. Please complete `turso auth login` manually and rerun this command.");
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!isLoggedIn()) {
|
|
49
|
+
console.error("Still not logged in after `turso auth login`. Exiting.");
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Parse env file into plain object
|
|
55
|
+
function readEnvFile(filePath) {
|
|
56
|
+
if (!fs.existsSync(filePath)) return {};
|
|
57
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
58
|
+
const lines = content.split(/\r?\n/);
|
|
59
|
+
|
|
60
|
+
const env = {};
|
|
61
|
+
for (const line of lines) {
|
|
62
|
+
const trimmed = line.trim();
|
|
63
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
64
|
+
const idx = trimmed.indexOf("=");
|
|
65
|
+
if (idx === -1) continue;
|
|
66
|
+
const key = trimmed.slice(0, idx).trim();
|
|
67
|
+
let value = trimmed.slice(idx + 1).trim();
|
|
68
|
+
if (
|
|
69
|
+
(value.startsWith('"') && value.endsWith('"')) ||
|
|
70
|
+
(value.startsWith("'") && value.endsWith("'"))
|
|
71
|
+
) {
|
|
72
|
+
value = value.slice(1, -1);
|
|
73
|
+
}
|
|
74
|
+
env[key] = value;
|
|
75
|
+
}
|
|
76
|
+
return env;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Write env object back to file, overwriting file completely
|
|
80
|
+
function writeEnvFile(filePath, envObj) {
|
|
81
|
+
const lines = [];
|
|
82
|
+
for (const [key, value] of Object.entries(envObj)) {
|
|
83
|
+
lines.push(`${key}=${value}`);
|
|
84
|
+
}
|
|
85
|
+
fs.writeFileSync(filePath, lines.join("\n") + "\n");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function main() {
|
|
89
|
+
await ensureLogin();
|
|
90
|
+
|
|
91
|
+
// DB name from argv or prompt
|
|
92
|
+
let dbName = process.argv[2];
|
|
93
|
+
if (!dbName) {
|
|
94
|
+
dbName = await ask("Enter Turso database name: ");
|
|
95
|
+
}
|
|
96
|
+
if (!dbName) {
|
|
97
|
+
console.error("Database name is required. Exiting.");
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Load existing .env into object
|
|
102
|
+
const envPath = path.join(process.cwd(), ".env");
|
|
103
|
+
const envObj = readEnvFile(envPath);
|
|
104
|
+
|
|
105
|
+
const existingUrl = envObj.TURSO_DATABASE_URL || process.env.TURSO_DATABASE_URL;
|
|
106
|
+
const existingToken = envObj.TURSO_AUTH_TOKEN || process.env.TURSO_AUTH_TOKEN;
|
|
107
|
+
|
|
108
|
+
if (existingUrl || existingToken) {
|
|
109
|
+
console.log("\nExisting Turso env values detected in .env or process env:");
|
|
110
|
+
if (existingUrl) console.log(`- TURSO_DATABASE_URL: ${existingUrl}`);
|
|
111
|
+
if (existingToken) console.log(`- TURSO_AUTH_TOKEN: ${existingToken}`);
|
|
112
|
+
const answer = await ask(
|
|
113
|
+
"Overwrite existing TURSO_DATABASE_URL and TURSO_AUTH_TOKEN? (y/N): "
|
|
114
|
+
);
|
|
115
|
+
if (!/^y(es)?$/i.test(answer)) {
|
|
116
|
+
console.log("Keeping existing Turso env values. Exiting.");
|
|
117
|
+
process.exit(0);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Ensure DB exists
|
|
122
|
+
try {
|
|
123
|
+
run(`turso db create ${dbName}`);
|
|
124
|
+
} catch (e) {
|
|
125
|
+
console.warn(`turso db create ${dbName} failed (may already exist): ${e.message}`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Get URL & token
|
|
129
|
+
const dbUrl = run(`turso db show ${dbName} --url`); // libsql URL [web:24]
|
|
130
|
+
const authToken = run(`turso db tokens create ${dbName}`); // token as text [web:1][web:45]
|
|
131
|
+
|
|
132
|
+
// Put into env object and process.env
|
|
133
|
+
envObj.TURSO_DATABASE_URL = dbUrl;
|
|
134
|
+
envObj.TURSO_AUTH_TOKEN = authToken;
|
|
135
|
+
process.env.TURSO_DATABASE_URL = dbUrl;
|
|
136
|
+
process.env.TURSO_AUTH_TOKEN = authToken;
|
|
137
|
+
|
|
138
|
+
// Overwrite .env with updated values (no duplicates, no stale “not logged in” values)
|
|
139
|
+
writeEnvFile(envPath, envObj);
|
|
140
|
+
|
|
141
|
+
console.log("\nUpdated Turso environment variables (overwritten in .env):");
|
|
142
|
+
console.log(`TURSO_DATABASE_URL=${process.env.TURSO_DATABASE_URL}`);
|
|
143
|
+
console.log(`TURSO_AUTH_TOKEN=${process.env.TURSO_AUTH_TOKEN}`);
|
|
144
|
+
console.log(`\nWrote updated values to ${envPath}`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
main().catch((err) => {
|
|
148
|
+
console.error("Unexpected error:", err);
|
|
149
|
+
process.exit(1);
|
|
150
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-cloud-db",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"description": "CLI to create a Turso database and manage TURSO_* env vars",
|
|
6
|
+
"main": "create-cloud-db.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"create-cloud-db": "./create-cloud-db.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"ship": "npx standard-version --release-as patch; npm publish",
|
|
12
|
+
"start": "node create-cloud-db.js"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"turso",
|
|
16
|
+
"database",
|
|
17
|
+
"cli",
|
|
18
|
+
"env"
|
|
19
|
+
],
|
|
20
|
+
"author": "",
|
|
21
|
+
"license": "MIT"
|
|
22
|
+
}
|