@progressive-development/pd-provider-firebase-functions 0.3.5 → 0.4.0

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.
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Reusable Mail Templates Import
3
+ *
4
+ * Imports mail templates from a templates directory into Firestore.
5
+ * Each template consists of three files:
6
+ * - {name}.json — metadata (name, subject)
7
+ * - {name}.html.hbs — HTML template
8
+ * - {name}.txt.hbs — Text template
9
+ */
10
+ export interface ImportTemplatesConfig {
11
+ /** Path to the mail-templates/ directory */
12
+ templatesDir: string;
13
+ /** Path to the serviceAccountKey.json */
14
+ serviceAccountPath: string;
15
+ /** Optional Firestore named database */
16
+ databaseId?: string;
17
+ /** Optional collection name (default: "emailTemplates") */
18
+ collection?: string;
19
+ /** Optional single template name to import */
20
+ templateFilter?: string;
21
+ /** If true, no writes are performed (default: false) */
22
+ dryRun?: boolean;
23
+ }
24
+ /**
25
+ * Import mail templates into Firestore.
26
+ */
27
+ export declare function runImportTemplates(config: ImportTemplatesConfig): Promise<void>;
28
+ //# sourceMappingURL=import-templates.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"import-templates.d.ts","sourceRoot":"","sources":["../src/import-templates.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,MAAM,WAAW,qBAAqB;IACpC,4CAA4C;IAC5C,YAAY,EAAE,MAAM,CAAC;IACrB,yCAAyC;IACzC,kBAAkB,EAAE,MAAM,CAAC;IAC3B,wCAAwC;IACxC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,2DAA2D;IAC3D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8CAA8C;IAC9C,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,wDAAwD;IACxD,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AA6CD;;GAEG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CA2FrF"}
@@ -0,0 +1,109 @@
1
+ import { existsSync, readdirSync, readFileSync } from 'fs';
2
+ import { scriptLog, createScriptContext } from './script-helpers.js';
3
+
4
+ function findTemplates(templatesDir) {
5
+ const files = readdirSync(templatesDir);
6
+ const jsonFiles = files.filter((f) => f.endsWith(".json") && f !== "serviceAccountKey.json");
7
+ return jsonFiles.map((f) => f.replace(".json", ""));
8
+ }
9
+ function loadTemplate(templatesDir, name) {
10
+ const jsonPath = `${templatesDir}/${name}.json`;
11
+ const htmlPath = `${templatesDir}/${name}.html.hbs`;
12
+ const textPath = `${templatesDir}/${name}.txt.hbs`;
13
+ if (!existsSync(jsonPath)) {
14
+ scriptLog(`Metadata not found: ${jsonPath}`, "error");
15
+ return null;
16
+ }
17
+ if (!existsSync(htmlPath)) {
18
+ scriptLog(`HTML template not found: ${htmlPath}`, "error");
19
+ return null;
20
+ }
21
+ if (!existsSync(textPath)) {
22
+ scriptLog(`Text template not found: ${textPath}`, "error");
23
+ return null;
24
+ }
25
+ const metadata = JSON.parse(readFileSync(jsonPath, "utf-8"));
26
+ const html = readFileSync(htmlPath, "utf-8");
27
+ const text = readFileSync(textPath, "utf-8");
28
+ return { ...metadata, html, text };
29
+ }
30
+ async function runImportTemplates(config) {
31
+ const {
32
+ templatesDir,
33
+ serviceAccountPath,
34
+ databaseId,
35
+ collection = "emailTemplates",
36
+ templateFilter,
37
+ dryRun = false
38
+ } = config;
39
+ if (!existsSync(templatesDir)) {
40
+ scriptLog(`Templates directory not found: ${templatesDir}`, "error");
41
+ process.exit(1);
42
+ }
43
+ const { db } = createScriptContext({
44
+ title: "Mail Templates Import",
45
+ serviceAccountPath,
46
+ databaseId,
47
+ dryRun
48
+ });
49
+ scriptLog(`Templates dir: ${templatesDir}`);
50
+ scriptLog(`Target collection: ${collection}`);
51
+ let templates = findTemplates(templatesDir);
52
+ if (templateFilter) {
53
+ templates = templates.filter((t) => t === templateFilter);
54
+ if (templates.length === 0) {
55
+ scriptLog(`Template not found: ${templateFilter}`, "error");
56
+ process.exit(1);
57
+ }
58
+ }
59
+ scriptLog(`Found ${templates.length} template(s): ${templates.join(", ")}`);
60
+ console.log("");
61
+ let successCount = 0;
62
+ let errorCount = 0;
63
+ for (const templateName of templates) {
64
+ const template = loadTemplate(templatesDir, templateName);
65
+ if (!template) {
66
+ errorCount++;
67
+ continue;
68
+ }
69
+ const docId = `${templateName}Id`;
70
+ console.log(`
71
+ ${templateName}`);
72
+ console.log(` Document ID: ${docId}`);
73
+ console.log(` Subject: ${template.subject}`);
74
+ console.log(` HTML: ${template.html.length} chars`);
75
+ console.log(` Text: ${template.text.length} chars`);
76
+ if (!dryRun) {
77
+ try {
78
+ await db.collection(collection).doc(docId).set({
79
+ name: template.name,
80
+ subject: template.subject,
81
+ html: template.html,
82
+ text: template.text,
83
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
84
+ });
85
+ scriptLog(`Imported: ${templateName} -> ${docId}`, "success");
86
+ successCount++;
87
+ } catch (error) {
88
+ scriptLog(`Failed to import ${templateName}: ${error}`, "error");
89
+ errorCount++;
90
+ }
91
+ } else {
92
+ scriptLog(`Would import: ${templateName} -> ${docId}`, "info");
93
+ successCount++;
94
+ }
95
+ }
96
+ console.log("\n" + "-".repeat(50));
97
+ console.log(`
98
+ Summary:`);
99
+ console.log(` Success: ${successCount}`);
100
+ console.log(` Errors: ${errorCount}`);
101
+ if (dryRun) {
102
+ console.log(`
103
+ Run without --dry-run to apply changes.
104
+ `);
105
+ }
106
+ process.exit(errorCount > 0 ? 1 : 0);
107
+ }
108
+
109
+ export { runImportTemplates };
package/dist/index.d.ts CHANGED
@@ -10,4 +10,8 @@ export { moveToTrash, restoreFromTrash, permanentDelete, listTrash, getTrashItem
10
10
  export type { AuthTrashOptions } from './auth-trash';
11
11
  export { moveToTrashWithAuth, restoreFromTrashWithAuth, permanentDeleteWithAuth } from './auth-trash';
12
12
  export { logger } from './logger';
13
+ export type { LogType, ScriptContextConfig, ScriptContext } from './script-helpers';
14
+ export { scriptLog, initFirebaseAdmin, createScriptContext } from './script-helpers';
15
+ export type { ImportTemplatesConfig } from './import-templates';
16
+ export { runImportTemplates } from './import-templates';
13
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGrC,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,iBAAiB,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGpK,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAGtE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG3D,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGpC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C,YAAY,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,eAAe,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAGlG,YAAY,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAGtG,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGrC,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,iBAAiB,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGpK,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAGtE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG3D,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGpC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C,YAAY,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,eAAe,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAGlG,YAAY,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAGtG,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGlC,YAAY,EAAE,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACpF,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAGrF,YAAY,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/index.js CHANGED
@@ -7,3 +7,5 @@ export { getNextCounter } from './counter.js';
7
7
  export { getTrashItem, listTrash, moveToTrash, permanentDelete, restoreFromTrash } from './trash.js';
8
8
  export { moveToTrashWithAuth, permanentDeleteWithAuth, restoreFromTrashWithAuth } from './auth-trash.js';
9
9
  export { logger } from './logger.js';
10
+ export { createScriptContext, initFirebaseAdmin, scriptLog } from './script-helpers.js';
11
+ export { runImportTemplates } from './import-templates.js';
@@ -0,0 +1,35 @@
1
+ import { Firestore } from 'firebase-admin/firestore';
2
+ export type LogType = "info" | "success" | "error" | "warn";
3
+ /**
4
+ * Print a prefixed log message to console.
5
+ */
6
+ export declare function scriptLog(message: string, type?: LogType): void;
7
+ /**
8
+ * Initialize Firebase Admin SDK if not already initialized.
9
+ */
10
+ export declare function initFirebaseAdmin(serviceAccountPath: string): void;
11
+ export interface ScriptContextConfig {
12
+ /** Script title shown in console header */
13
+ title: string;
14
+ /** Path to serviceAccountKey.json */
15
+ serviceAccountPath: string;
16
+ /** Optional Firestore named database */
17
+ databaseId?: string;
18
+ /** Dry-run mode (default: false) */
19
+ dryRun?: boolean;
20
+ }
21
+ export interface ScriptContext {
22
+ /** Firestore instance (cached via getDb) */
23
+ db: Firestore;
24
+ /** Whether dry-run mode is active */
25
+ dryRun: boolean;
26
+ }
27
+ /**
28
+ * Set up a script context: validates paths, initializes Firebase,
29
+ * returns a ready-to-use Firestore instance.
30
+ *
31
+ * Prints title, connection info, and dry-run banner.
32
+ * Calls `process.exit(1)` if the service account key is missing.
33
+ */
34
+ export declare function createScriptContext(config: ScriptContextConfig): ScriptContext;
35
+ //# sourceMappingURL=script-helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-helpers.d.ts","sourceRoot":"","sources":["../src/script-helpers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAQ1D,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,SAAS,GAAG,OAAO,GAAG,MAAM,CAAC;AAS5D;;GAEG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,GAAE,OAAgB,GAAG,IAAI,CAEvE;AAMD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,kBAAkB,EAAE,MAAM,GAAG,IAAI,CASlE;AAMD,MAAM,WAAW,mBAAmB;IAClC,2CAA2C;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,qCAAqC;IACrC,kBAAkB,EAAE,MAAM,CAAC;IAC3B,wCAAwC;IACxC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oCAAoC;IACpC,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,aAAa;IAC5B,4CAA4C;IAC5C,EAAE,EAAE,SAAS,CAAC;IACd,qCAAqC;IACrC,MAAM,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,mBAAmB,GAAG,aAAa,CA6B9E"}
@@ -0,0 +1,48 @@
1
+ import { getApps, initializeApp, cert } from 'firebase-admin/app';
2
+ import { readFileSync, existsSync } from 'fs';
3
+ import { getDb } from './firestore.js';
4
+
5
+ const LOG_PREFIX = {
6
+ info: "i ",
7
+ success: "OK",
8
+ error: "X ",
9
+ warn: "! "
10
+ };
11
+ function scriptLog(message, type = "info") {
12
+ console.log(`${LOG_PREFIX[type]} ${message}`);
13
+ }
14
+ function initFirebaseAdmin(serviceAccountPath) {
15
+ if (getApps().length > 0) return;
16
+ const serviceAccount = JSON.parse(readFileSync(serviceAccountPath, "utf-8"));
17
+ initializeApp({
18
+ credential: cert(serviceAccount),
19
+ databaseURL: `https://${serviceAccount.projectId}.firebaseio.com`
20
+ });
21
+ }
22
+ function createScriptContext(config) {
23
+ const { title, serviceAccountPath, databaseId, dryRun = false } = config;
24
+ console.log(`
25
+ ${title}
26
+ `);
27
+ console.log("-".repeat(50));
28
+ if (!existsSync(serviceAccountPath)) {
29
+ scriptLog("Service Account Key not found!", "error");
30
+ console.log(`
31
+ Expected: ${serviceAccountPath}`);
32
+ console.log(`
33
+ Download from Firebase Console:`);
34
+ console.log(` Project Settings > Service Accounts > Generate new private key
35
+ `);
36
+ process.exit(1);
37
+ }
38
+ initFirebaseAdmin(serviceAccountPath);
39
+ const db = getDb(databaseId);
40
+ scriptLog(`Firestore: ${databaseId ?? "(default)"}`);
41
+ if (dryRun) {
42
+ scriptLog("DRY RUN - No changes will be made", "warn");
43
+ }
44
+ console.log("-".repeat(50));
45
+ return { db, dryRun };
46
+ }
47
+
48
+ export { createScriptContext, initFirebaseAdmin, scriptLog };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@progressive-development/pd-provider-firebase-functions",
3
- "version": "0.3.5",
3
+ "version": "0.4.0",
4
4
  "description": "Firebase Functions v2 utilities for pd-spa-helper backend",
5
5
  "author": "PD Progressive Development",
6
6
  "license": "SEE LICENSE IN LICENSE",