clefbase 1.2.1 → 1.2.3

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.
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.runInit = runInit;
7
+ exports.scaffoldLib = scaffoldLib;
7
8
  const path_1 = __importDefault(require("path"));
8
9
  const fs_1 = __importDefault(require("fs"));
9
10
  const chalk_1 = __importDefault(require("chalk"));
@@ -93,25 +94,130 @@ async function runInit(cwd = process.cwd()) {
93
94
  if (cfg.services.hosting) {
94
95
  await setupHosting(cfg, cwd);
95
96
  }
96
- // ── Step 5: Write config files ────────────────────────────────────────────
97
+ // ── Step 5: Write root config files ──────────────────────────────────────
97
98
  const configPath = (0, config_1.saveConfig)(cfg, cwd);
98
99
  (0, config_1.ensureGitignore)(cwd);
99
100
  (0, config_1.writeEnvExample)(cfg, cwd);
101
+ // ── Step 6: Scaffold src/lib ──────────────────────────────────────────────
102
+ const libResult = scaffoldLib(cfg, cwd);
103
+ // ── Done ──────────────────────────────────────────────────────────────────
100
104
  console.log();
101
105
  console.log(chalk_1.default.green.bold(" ✓ Project initialised!"));
102
106
  console.log();
103
- console.log(chalk_1.default.dim(` Config: ${path_1.default.relative(cwd, configPath)}`));
104
- console.log(chalk_1.default.dim(" Secrets: clefbase.json has been added to .gitignore"));
105
- console.log(chalk_1.default.dim(" Env example: .env.example created"));
107
+ console.log(chalk_1.default.dim(` Config: ${path_1.default.relative(cwd, configPath)}`));
108
+ console.log(chalk_1.default.dim(" Secrets: clefbase.json has been added to .gitignore"));
109
+ console.log(chalk_1.default.dim(" Env example: .env.example created"));
110
+ if (libResult) {
111
+ console.log(chalk_1.default.dim(` Lib config: ${libResult.configCopy}`));
112
+ console.log(chalk_1.default.dim(` Lib entry: ${libResult.libFile}`));
113
+ }
106
114
  console.log();
107
115
  printUsageHint(cfg);
108
116
  }
117
+ /**
118
+ * Writes two files into `<cwd>/src/lib/`:
119
+ * • clefbase.json — a copy of the project config (secrets stripped for safety)
120
+ * • clefBase.ts — ready-to-import service exports
121
+ *
122
+ * Safe to call multiple times — the TS file is regenerated on every call so it
123
+ * stays in sync with the enabled services.
124
+ */
125
+ function scaffoldLib(cfg, cwd = process.cwd()) {
126
+ const libDir = path_1.default.join(cwd, "src", "lib");
127
+ fs_1.default.mkdirSync(libDir, { recursive: true });
128
+ // ── 1. Config copy (strip adminSecret for safety) ─────────────────────────
129
+ const publicCfg = {
130
+ serverUrl: cfg.serverUrl,
131
+ projectId: cfg.projectId,
132
+ apiKey: cfg.apiKey,
133
+ // adminSecret intentionally omitted — not needed in client code
134
+ services: cfg.services,
135
+ ...(cfg.hosting ? { hosting: cfg.hosting } : {}),
136
+ };
137
+ const configCopyAbs = path_1.default.join(libDir, "clefbase.json");
138
+ fs_1.default.writeFileSync(configCopyAbs, JSON.stringify(publicCfg, null, 2) + "\n");
139
+ // ── 2. clefBase.ts ────────────────────────────────────────────────────────
140
+ const libFileAbs = path_1.default.join(libDir, "clefBase.ts");
141
+ fs_1.default.writeFileSync(libFileAbs, buildLibTs(cfg));
142
+ return {
143
+ configCopy: path_1.default.relative(cwd, configCopyAbs),
144
+ libFile: path_1.default.relative(cwd, libFileAbs),
145
+ };
146
+ }
147
+ /** Build the content of src/lib/clefBase.ts based on enabled services. */
148
+ function buildLibTs(cfg) {
149
+ const { database, auth, storage } = cfg.services;
150
+ // Collect SDK imports
151
+ const sdkImports = ["initClefbase"];
152
+ if (database)
153
+ sdkImports.push("getDatabase");
154
+ if (auth)
155
+ sdkImports.push("getAuth");
156
+ if (storage)
157
+ sdkImports.push("getStorage");
158
+ // Collect type imports
159
+ const typeImports = [];
160
+ if (database)
161
+ typeImports.push("Database");
162
+ if (auth)
163
+ typeImports.push("Auth");
164
+ if (storage)
165
+ typeImports.push("ClefbaseStorage");
166
+ const lines = [
167
+ `/**`,
168
+ ` * Clefbase — pre-initialised service exports`,
169
+ ` *`,
170
+ ` * Usage:`,
171
+ ` * import { db, auth, storage } from "@lib/clefBase";`,
172
+ ` * import db from "@lib/clefBase";`,
173
+ ` */`,
174
+ ``,
175
+ `import { ${sdkImports.join(", ")} } from "clefbase";`,
176
+ ...(typeImports.length > 0
177
+ ? [`import type { ${typeImports.join(", ")} } from "clefbase";`]
178
+ : []),
179
+ `import config from "./clefbase.json";`,
180
+ ``,
181
+ `// ─── App ─────────────────────────────────────────────────────────────────────`,
182
+ ``,
183
+ `const app = initClefbase({`,
184
+ ` serverUrl: config.serverUrl,`,
185
+ ` projectId: config.projectId,`,
186
+ ` apiKey: config.apiKey,`,
187
+ ` adminSecret: "",`,
188
+ `});`,
189
+ ``,
190
+ ];
191
+ // ── Service instances ──────────────────────────────────────────────────────
192
+ if (database || auth || storage) {
193
+ lines.push("// ─── Services ───────────────────────────────────────────────────────────────");
194
+ lines.push("");
195
+ }
196
+ if (database) {
197
+ lines.push(`/** Clefbase Database — query and mutate documents. */`);
198
+ lines.push(`export const db: Database = getDatabase(app);`);
199
+ lines.push("");
200
+ }
201
+ if (auth) {
202
+ lines.push(`/** Clefbase Auth — sign up, sign in, manage sessions. */`);
203
+ lines.push(`export const auth: Auth = getAuth(app);`);
204
+ lines.push("");
205
+ }
206
+ if (storage) {
207
+ lines.push(`/** Clefbase Storage — upload and manage files. */`);
208
+ lines.push(`export const storage: ClefbaseStorage = getStorage(app);`);
209
+ lines.push("");
210
+ }
211
+ // ── Re-export app for advanced use ────────────────────────────────────────
212
+ lines.push(`// ─── Advanced ────────────────────────────────────────────────────────────────`, ``, `/** The underlying ClefbaseApp instance (for advanced use). */`, `export { app };`, ``);
213
+ lines.push("");
214
+ return lines.join("\n");
215
+ }
109
216
  // ─── Hosting sub-flow ─────────────────────────────────────────────────────────
110
217
  async function setupHosting(cfg, cwd) {
111
218
  console.log();
112
219
  console.log(chalk_1.default.bold(" Hosting"));
113
220
  console.log();
114
- // Fetch existing sites
115
221
  let existing = [];
116
222
  const siteSpinner = (0, ora_1.default)("Fetching existing sites…").start();
117
223
  try {
@@ -191,35 +297,37 @@ function guessDistDir(cwd) {
191
297
  return "dist";
192
298
  }
193
299
  function printUsageHint(cfg) {
194
- const imports = ["initClefbase"];
300
+ const namedImports = [];
195
301
  if (cfg.services.database)
196
- imports.push("getDatabase");
302
+ namedImports.push("db");
197
303
  if (cfg.services.auth)
198
- imports.push("getAuth");
304
+ namedImports.push("auth");
199
305
  if (cfg.services.storage)
200
- imports.push("getStorage");
201
- if (cfg.services.hosting)
202
- imports.push("getHosting");
306
+ namedImports.push("storage");
203
307
  console.log(chalk_1.default.bold(" Quick start:"));
204
308
  console.log();
205
- console.log(chalk_1.default.cyan(` import { ${imports.join(", ")} } from "clefbase";`));
206
- console.log(chalk_1.default.cyan(` import config from "./clefbase.json";`));
207
- console.log();
208
- console.log(chalk_1.default.cyan(` const app = initClefbase(config);`));
309
+ if (namedImports.length > 0) {
310
+ console.log(chalk_1.default.cyan(` import { ${namedImports.join(", ")} } from "@lib/clefBase";`));
311
+ }
312
+ else {
313
+ console.log(chalk_1.default.cyan(` import app from "@lib/clefBase";`));
314
+ }
209
315
  if (cfg.services.database) {
210
316
  console.log();
211
- console.log(chalk_1.default.cyan(` const db = getDatabase(app);`));
212
317
  console.log(chalk_1.default.cyan(` const docs = await db.collection("items").getDocs();`));
213
318
  }
214
319
  if (cfg.services.auth) {
215
320
  console.log();
216
- console.log(chalk_1.default.cyan(` const auth = getAuth(app);`));
217
321
  console.log(chalk_1.default.cyan(` const { user } = await auth.signIn("email", "pass");`));
218
322
  }
323
+ if (cfg.services.storage) {
324
+ console.log();
325
+ console.log(chalk_1.default.cyan(` await storage.ref("uploads/photo.jpg").upload(file);`));
326
+ }
219
327
  if (cfg.services.hosting && cfg.hosting) {
220
328
  console.log();
221
329
  console.log(chalk_1.default.bold(" Deploy:"));
222
- console.log(chalk_1.default.cyan(` $ npm run build && clefbase deploy`));
330
+ console.log(chalk_1.default.cyan(` $ npm run build && npx clefbase deploy`));
223
331
  console.log(chalk_1.default.dim(` Site: ${cfg.serverUrl}/hosted/${cfg.projectId}/${cfg.hosting.siteId}`));
224
332
  }
225
333
  console.log();
package/dist/cli.js CHANGED
@@ -33903,15 +33903,102 @@ async function runInit(cwd = process.cwd()) {
33903
33903
  const configPath = saveConfig(cfg, cwd);
33904
33904
  ensureGitignore(cwd);
33905
33905
  writeEnvExample(cfg, cwd);
33906
+ const libResult = scaffoldLib(cfg, cwd);
33906
33907
  console.log();
33907
33908
  console.log(source_default.green.bold(" \u2713 Project initialised!"));
33908
33909
  console.log();
33909
- console.log(source_default.dim(` Config: ${import_path2.default.relative(cwd, configPath)}`));
33910
- console.log(source_default.dim(" Secrets: clefbase.json has been added to .gitignore"));
33911
- console.log(source_default.dim(" Env example: .env.example created"));
33910
+ console.log(source_default.dim(` Config: ${import_path2.default.relative(cwd, configPath)}`));
33911
+ console.log(source_default.dim(" Secrets: clefbase.json has been added to .gitignore"));
33912
+ console.log(source_default.dim(" Env example: .env.example created"));
33913
+ if (libResult) {
33914
+ console.log(source_default.dim(` Lib config: ${libResult.configCopy}`));
33915
+ console.log(source_default.dim(` Lib entry: ${libResult.libFile}`));
33916
+ }
33912
33917
  console.log();
33913
33918
  printUsageHint(cfg);
33914
33919
  }
33920
+ function scaffoldLib(cfg, cwd = process.cwd()) {
33921
+ const libDir = import_path2.default.join(cwd, "src", "lib");
33922
+ import_fs3.default.mkdirSync(libDir, { recursive: true });
33923
+ const publicCfg = {
33924
+ serverUrl: cfg.serverUrl,
33925
+ projectId: cfg.projectId,
33926
+ apiKey: cfg.apiKey,
33927
+ // adminSecret intentionally omitted — not needed in client code
33928
+ services: cfg.services,
33929
+ ...cfg.hosting ? { hosting: cfg.hosting } : {}
33930
+ };
33931
+ const configCopyAbs = import_path2.default.join(libDir, "clefbase.json");
33932
+ import_fs3.default.writeFileSync(configCopyAbs, JSON.stringify(publicCfg, null, 2) + "\n");
33933
+ const libFileAbs = import_path2.default.join(libDir, "clefBase.ts");
33934
+ import_fs3.default.writeFileSync(libFileAbs, buildLibTs(cfg));
33935
+ return {
33936
+ configCopy: import_path2.default.relative(cwd, configCopyAbs),
33937
+ libFile: import_path2.default.relative(cwd, libFileAbs)
33938
+ };
33939
+ }
33940
+ function buildLibTs(cfg) {
33941
+ const { database, auth, storage } = cfg.services;
33942
+ const sdkImports = ["initClefbase"];
33943
+ if (database) sdkImports.push("getDatabase");
33944
+ if (auth) sdkImports.push("getAuth");
33945
+ if (storage) sdkImports.push("getStorage");
33946
+ const typeImports = [];
33947
+ if (database) typeImports.push("Database");
33948
+ if (auth) typeImports.push("Auth");
33949
+ if (storage) typeImports.push("ClefbaseStorage");
33950
+ const lines = [
33951
+ `/**`,
33952
+ ` * Clefbase \u2014 pre-initialised service exports`,
33953
+ ` *`,
33954
+ ` * Usage:`,
33955
+ ` * import { db, auth, storage } from "@lib/clefBase";`,
33956
+ ` * import db from "@lib/clefBase";`,
33957
+ ` */`,
33958
+ ``,
33959
+ `import { ${sdkImports.join(", ")} } from "clefbase";`,
33960
+ ...typeImports.length > 0 ? [`import type { ${typeImports.join(", ")} } from "clefbase";`] : [],
33961
+ `import config from "./clefbase.json";`,
33962
+ ``,
33963
+ `// \u2500\u2500\u2500 App \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`,
33964
+ ``,
33965
+ `const app = initClefbase({`,
33966
+ ` serverUrl: config.serverUrl,`,
33967
+ ` projectId: config.projectId,`,
33968
+ ` apiKey: config.apiKey,`,
33969
+ ` adminSecret: "",`,
33970
+ `});`,
33971
+ ``
33972
+ ];
33973
+ if (database || auth || storage) {
33974
+ lines.push("// \u2500\u2500\u2500 Services \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500");
33975
+ lines.push("");
33976
+ }
33977
+ if (database) {
33978
+ lines.push(`/** Clefbase Database \u2014 query and mutate documents. */`);
33979
+ lines.push(`export const db: Database = getDatabase(app);`);
33980
+ lines.push("");
33981
+ }
33982
+ if (auth) {
33983
+ lines.push(`/** Clefbase Auth \u2014 sign up, sign in, manage sessions. */`);
33984
+ lines.push(`export const auth: Auth = getAuth(app);`);
33985
+ lines.push("");
33986
+ }
33987
+ if (storage) {
33988
+ lines.push(`/** Clefbase Storage \u2014 upload and manage files. */`);
33989
+ lines.push(`export const storage: ClefbaseStorage = getStorage(app);`);
33990
+ lines.push("");
33991
+ }
33992
+ lines.push(
33993
+ `// \u2500\u2500\u2500 Advanced \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`,
33994
+ ``,
33995
+ `/** The underlying ClefbaseApp instance (for advanced use). */`,
33996
+ `export { app };`,
33997
+ ``
33998
+ );
33999
+ lines.push("");
34000
+ return lines.join("\n");
34001
+ }
33915
34002
  async function setupHosting(cfg, cwd) {
33916
34003
  var _a;
33917
34004
  console.log();
@@ -33992,31 +34079,35 @@ function guessDistDir(cwd) {
33992
34079
  return "dist";
33993
34080
  }
33994
34081
  function printUsageHint(cfg) {
33995
- const imports = ["initClefbase"];
33996
- if (cfg.services.database) imports.push("getDatabase");
33997
- if (cfg.services.auth) imports.push("getAuth");
33998
- if (cfg.services.storage) imports.push("getStorage");
33999
- if (cfg.services.hosting) imports.push("getHosting");
34082
+ const namedImports = [];
34083
+ if (cfg.services.database) namedImports.push("db");
34084
+ if (cfg.services.auth) namedImports.push("auth");
34085
+ if (cfg.services.storage) namedImports.push("storage");
34000
34086
  console.log(source_default.bold(" Quick start:"));
34001
34087
  console.log();
34002
- console.log(source_default.cyan(` import { ${imports.join(", ")} } from "clefbase";`));
34003
- console.log(source_default.cyan(` import config from "./clefbase.json";`));
34004
- console.log();
34005
- console.log(source_default.cyan(` const app = initClefbase(config);`));
34088
+ if (namedImports.length > 0) {
34089
+ console.log(
34090
+ source_default.cyan(` import { ${namedImports.join(", ")} } from "@lib/clefBase";`)
34091
+ );
34092
+ } else {
34093
+ console.log(source_default.cyan(` import app from "@lib/clefBase";`));
34094
+ }
34006
34095
  if (cfg.services.database) {
34007
34096
  console.log();
34008
- console.log(source_default.cyan(` const db = getDatabase(app);`));
34009
34097
  console.log(source_default.cyan(` const docs = await db.collection("items").getDocs();`));
34010
34098
  }
34011
34099
  if (cfg.services.auth) {
34012
34100
  console.log();
34013
- console.log(source_default.cyan(` const auth = getAuth(app);`));
34014
34101
  console.log(source_default.cyan(` const { user } = await auth.signIn("email", "pass");`));
34015
34102
  }
34103
+ if (cfg.services.storage) {
34104
+ console.log();
34105
+ console.log(source_default.cyan(` await storage.ref("uploads/photo.jpg").upload(file);`));
34106
+ }
34016
34107
  if (cfg.services.hosting && cfg.hosting) {
34017
34108
  console.log();
34018
34109
  console.log(source_default.bold(" Deploy:"));
34019
- console.log(source_default.cyan(` $ npm run build && clefbase deploy`));
34110
+ console.log(source_default.cyan(` $ npm run build && npx clefbase deploy`));
34020
34111
  console.log(source_default.dim(` Site: ${cfg.serverUrl}/hosted/${cfg.projectId}/${cfg.hosting.siteId}`));
34021
34112
  }
34022
34113
  console.log();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clefbase",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "Firebase-style SDK and CLI for Clefbase — database, auth, storage, and hosting",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",