@repdf/preview 0.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/LICENCE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 repdf
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ STILL IN DEVELOPMENT
2
+
3
+ repdf preview app
package/dist/index.cjs ADDED
@@ -0,0 +1,373 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
18
+ // If the importer is in node compatibility mode or this is not an ESM
19
+ // file that has been converted to a CommonJS file using a Babel-
20
+ // compatible transform (i.e. "__esModule" has not been set), then set
21
+ // "default" to the CommonJS "module.exports" for node compatibility.
22
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
+ mod
24
+ ));
25
+
26
+ // src/cli/index.ts
27
+ var import_commander = require("commander");
28
+
29
+ // package.json
30
+ var package_default = {
31
+ name: "@repdf/preview",
32
+ version: "0.0.1",
33
+ type: "module",
34
+ private: false,
35
+ main: "dist/index.js",
36
+ exports: {
37
+ "./package.json": "./package.json",
38
+ ".": {
39
+ import: "./dist/index.js",
40
+ default: "./dist/index.cjs"
41
+ }
42
+ },
43
+ description: "preview app for repdf. Not ready for production use.",
44
+ keywords: ["pdf", "typescript", "react"],
45
+ homepage: "https://repdf.dev",
46
+ author: "repdf labs",
47
+ files: ["dist"],
48
+ license: "MIT",
49
+ scripts: {
50
+ tsup: "tsup --tsconfig tsconfig.build.json",
51
+ build: "next build",
52
+ dev: "next dev",
53
+ lint: "next lint",
54
+ start: "next start",
55
+ "check-exports": "attw --pack .",
56
+ format: "biome check --write .",
57
+ "local-release": "changeset publish"
58
+ },
59
+ dependencies: {
60
+ "@react-email/render": "1.0.1",
61
+ "@react-email/tailwind": "0.1.0",
62
+ "@tanstack/react-query": "^5.59.0",
63
+ "@tanstack/react-query-devtools": "^5.59.0",
64
+ "@trpc/client": "^11.0.0-rc.553",
65
+ "@trpc/next": "^11.0.0-rc.553",
66
+ "@trpc/react-query": "^11.0.0-rc.553",
67
+ "@trpc/server": "^11.0.0-rc.553",
68
+ chokidar: "4.0.1",
69
+ clsx: "^2.1.1",
70
+ commander: "^12.1.0",
71
+ "dependency-tree": "^11.0.1",
72
+ esbuild: "0.24.0",
73
+ express: "^4.21.0",
74
+ glob: "^11.0.0",
75
+ "js-beautify": "^1.15.1",
76
+ "js-dependency-extractor": "^1.0.3",
77
+ "lucide-react": "^0.445.0",
78
+ next: "^14.2.14",
79
+ nuqs: "^1.19.3",
80
+ ora: "^8.1.0",
81
+ pino: "^9.4.0",
82
+ "pino-pretty": "^11.2.2",
83
+ "prism-react-renderer": "^2.4.0",
84
+ puppeteer: "^23.4.1",
85
+ react: "^18.3.1",
86
+ "react-dom": "^18.3.1",
87
+ "react-use-websocket": "^4.9.0",
88
+ "source-map-js": "^1.2.1",
89
+ "stacktrace-parser": "^0.1.10",
90
+ "string-ts": "^2.2.0",
91
+ superjson: "^2.2.1",
92
+ "tailwind-merge": "^2.5.2",
93
+ tsup: "8.3.0",
94
+ tsx: "^4.19.1",
95
+ webpack: "^5.95.0",
96
+ ws: "^8.18.0",
97
+ zod: "^3.23.8"
98
+ },
99
+ devDependencies: {
100
+ "@changesets/cli": "2.27.9",
101
+ "@arethetypeswrong/cli": "0.16.4",
102
+ "@biomejs/biome": "1.9.3",
103
+ "@types/express": "^5.0.0",
104
+ "@types/js-beautify": "^1.14.3",
105
+ "@types/node": "^20.14.10",
106
+ "@types/react": "^18.3.11",
107
+ "@types/react-dom": "^18.3.0",
108
+ "@types/ws": "^8.5.12",
109
+ postcss: "^8.4.39",
110
+ "prettier-plugin-tailwindcss": "^0.6.8",
111
+ "react-email": "3.0.1",
112
+ tailwindcss: "^3.4.13",
113
+ typescript: "^5.6.3"
114
+ },
115
+ packageManager: "pnpm@9.7.1"
116
+ };
117
+
118
+ // src/cli/commands/dev.ts
119
+ var import_node_fs2 = __toESM(require("fs"), 1);
120
+
121
+ // src/logger.ts
122
+ var import_pino = __toESM(require("pino"), 1);
123
+ var logger = (0, import_pino.default)({
124
+ transport: {
125
+ target: "pino-pretty"
126
+ },
127
+ level: "debug"
128
+ });
129
+ var logger_default = logger;
130
+
131
+ // src/cli/commands/server.ts
132
+ var import_node_url = require("url");
133
+ var import_path = __toESM(require("path"), 1);
134
+ var import_express = __toESM(require("express"), 1);
135
+ var import_next = __toESM(require("next"), 1);
136
+ var import_ws = __toESM(require("ws"), 1);
137
+ var import_ws2 = require("ws");
138
+
139
+ // src/utils/files/pdfTemplateWatcher.ts
140
+ var chokidar = __toESM(require("chokidar"), 1);
141
+
142
+ // src/constants.ts
143
+ var RELATIVE_PATH_TO_PDF_FOLDER = () => {
144
+ if (!process.env.RELATIVE_PATH_TO_PDF_FOLDER) {
145
+ throw new Error("RELATIVE_PATH_TO_PDF_FOLDER is not set");
146
+ }
147
+ return process.env.RELATIVE_PATH_TO_PDF_FOLDER;
148
+ };
149
+ var ABSOLUTE_PATH_TO_PDF_FOLDER = () => {
150
+ if (!process.env.ABSOLUTE_PATH_TO_PDF_FOLDER) {
151
+ throw new Error("ABSOLUTE_PATH_TO_PDF_FOLDER is not set");
152
+ }
153
+ return process.env.ABSOLUTE_PATH_TO_PDF_FOLDER;
154
+ };
155
+
156
+ // src/utils/files/dependencyTree.ts
157
+ var import_dependency_tree = __toESM(require("dependency-tree"), 1);
158
+ function getDependencyTree({
159
+ relViewerPath,
160
+ outputType
161
+ }) {
162
+ const options = {
163
+ filename: relViewerPath,
164
+ //TODO does this work with absolute paths?
165
+ directory: ".",
166
+ // requireConfig: 'path/to/requirejs/config', // optional
167
+ // webpackConfig: 'path/to/webpack/config', // optional
168
+ tsConfig: "tsconfig.json",
169
+ // optional
170
+ // nodeModulesConfig: {
171
+ // entry: 'module'
172
+ // }, // optional
173
+ filter: (path3) => path3.indexOf("node_modules") === -1,
174
+ // remove node_node modules from tree
175
+ // nonExistent: [], // optional
176
+ noTypeDefinitions: false
177
+ // optional
178
+ };
179
+ if (outputType === "list") {
180
+ return import_dependency_tree.default.toList(options);
181
+ }
182
+ return (0, import_dependency_tree.default)(options);
183
+ }
184
+
185
+ // src/utils/files/getPdfTemplates.ts
186
+ var import_node_fs = __toESM(require("fs"), 1);
187
+ var import_node_path = __toESM(require("path"), 1);
188
+ var import_glob = require("glob");
189
+ function isPotentialPdfTemplate(relViewerPath) {
190
+ const stat = import_node_fs.default.statSync(relViewerPath);
191
+ if (stat.isDirectory()) return false;
192
+ const fileName = import_node_path.default.basename(relViewerPath);
193
+ if (fileName.startsWith("_")) {
194
+ return false;
195
+ }
196
+ const extension = import_node_path.default.extname(relViewerPath);
197
+ if (![".tsx", ".ts", ".jsx", ".js"].includes(extension)) {
198
+ return false;
199
+ }
200
+ const absoluteFilePath = import_node_path.default.resolve(relViewerPath);
201
+ if (!import_node_fs.default.existsSync(absoluteFilePath)) {
202
+ return false;
203
+ }
204
+ const fileContents = import_node_fs.default.readFileSync(absoluteFilePath, "utf8");
205
+ const hasDefaultExport = fileContents.includes("export default");
206
+ return hasDefaultExport;
207
+ }
208
+ function getPdfTemplates() {
209
+ const potentialTemplateFilePaths = import_glob.glob.sync(
210
+ `${RELATIVE_PATH_TO_PDF_FOLDER()}/**/*.{tsx,jsx,js}`,
211
+ { nodir: true }
212
+ );
213
+ logger_default.debug({ potentialTemplateFilePaths });
214
+ const filteredTemplateFilePaths = potentialTemplateFilePaths.filter(
215
+ isPotentialPdfTemplate
216
+ );
217
+ return filteredTemplateFilePaths.map((file) => {
218
+ const relTemplatePath = file.replace(
219
+ `${RELATIVE_PATH_TO_PDF_FOLDER()}/`,
220
+ ""
221
+ );
222
+ const pathParts = relTemplatePath.split("/");
223
+ let topParentFolder = null;
224
+ if (pathParts.length > 1 && pathParts[0]) {
225
+ topParentFolder = pathParts[0];
226
+ }
227
+ return {
228
+ fileName: import_node_path.default.basename(file),
229
+ // just the file name
230
+ relTemplatePath,
231
+ relViewerPath: file,
232
+ // to where script is being run
233
+ absolutePath: import_node_path.default.resolve(file),
234
+ // file system
235
+ topParentFolder
236
+ };
237
+ });
238
+ }
239
+
240
+ // src/utils/files/pdfTemplateWatcher.ts
241
+ function pdfTemplateWatcher() {
242
+ const pdfTemplates = getPdfTemplates();
243
+ const allFiles = /* @__PURE__ */ new Set();
244
+ for (const pdfTemplate of pdfTemplates) {
245
+ allFiles.add(pdfTemplate.absolutePath);
246
+ const dependencyTree2 = getDependencyTree({
247
+ relViewerPath: pdfTemplate.relViewerPath,
248
+ outputType: "list"
249
+ });
250
+ for (const absoluteFilePath of dependencyTree2) {
251
+ allFiles.add(absoluteFilePath);
252
+ }
253
+ }
254
+ const watcher2 = chokidar.watch(ABSOLUTE_PATH_TO_PDF_FOLDER(), {
255
+ persistent: true,
256
+ ignoreInitial: true,
257
+ ignored: /(^|[\/\\])\../
258
+ // Ignore dot files
259
+ });
260
+ watcher2.add(Array.from(allFiles));
261
+ watcher2.on("add", (filePath) => logger_default.debug(`File ${filePath} has been added`)).on(
262
+ "change",
263
+ (filePath) => logger_default.debug(`File ${filePath} has been changed`)
264
+ ).on(
265
+ "unlink",
266
+ (filePath) => logger_default.debug(`File ${filePath} has been removed`)
267
+ ).on("error", (error) => logger_default.error(`Watcher error: ${error}`));
268
+ return watcher2;
269
+ }
270
+
271
+ // src/cli/commands/server.ts
272
+ var getEnvVariablesForPreviewApp = (relativePathToPdfsDirectory, cwd) => {
273
+ return {
274
+ RELATIVE_PATH_TO_PDF_FOLDER: relativePathToPdfsDirectory,
275
+ ABSOLUTE_PATH_TO_PDF_FOLDER: import_path.default.resolve(cwd, relativePathToPdfsDirectory),
276
+ USER_PROJECT_LOCATION: cwd
277
+ // TODO what is this used for if anything
278
+ };
279
+ };
280
+ var previewServer;
281
+ var clients = /* @__PURE__ */ new Set();
282
+ var watcher;
283
+ var startPreviewServer = async (pdfsDirRelativePath, port) => {
284
+ previewServer = (0, import_express.default)();
285
+ const server = previewServer.listen(port);
286
+ const wss = new import_ws2.WebSocketServer({ noServer: true });
287
+ process.env = {
288
+ ...process.env,
289
+ NODE_ENV: "development",
290
+ ...getEnvVariablesForPreviewApp(
291
+ // If we don't do normalization here, stuff like https://github.com/resend/react-email/issues/1354 happens.
292
+ import_path.default.normalize(pdfsDirRelativePath),
293
+ process.cwd()
294
+ )
295
+ };
296
+ const nextApp = (0, import_next.default)({
297
+ dev: true,
298
+ //process.env.NODE_ENV !== "production",
299
+ conf: {
300
+ images: {
301
+ // This is to avoid the warning with sharp
302
+ unoptimized: true
303
+ }
304
+ }
305
+ });
306
+ nextApp.prepare().then(() => {
307
+ previewServer.use((req, res, next2) => {
308
+ nextApp.getRequestHandler()(req, res, (0, import_node_url.parse)(req.url, true));
309
+ });
310
+ watcher = pdfTemplateWatcher();
311
+ watcher.on("all", () => {
312
+ for (const client of Array.from(clients)) {
313
+ client.send(`file changed ${(/* @__PURE__ */ new Date()).toISOString()}`);
314
+ }
315
+ watcher = pdfTemplateWatcher();
316
+ });
317
+ wss.on("connection", (ws) => {
318
+ clients.add(ws);
319
+ logger_default.info("New client connected");
320
+ ws.on("message", (message, isBinary) => {
321
+ logger_default.info(`Message received: ${message}`);
322
+ for (const client of Array.from(clients)) {
323
+ if (client.readyState === import_ws.default.OPEN && message.toString() !== `{"event":"ping"}`) {
324
+ client.send(message, { binary: isBinary });
325
+ }
326
+ }
327
+ });
328
+ ws.on("close", () => {
329
+ clients.delete(ws);
330
+ logger_default.info("Client disconnected");
331
+ });
332
+ });
333
+ server.on("upgrade", (req, socket, head) => {
334
+ logger_default.info("upgrading to websockets", req.url);
335
+ const { pathname } = (0, import_node_url.parse)(req.url || "/", true);
336
+ if (pathname === "/_next/webpack-hmr") {
337
+ logger_default.info("inside nextjs hot module reloading path");
338
+ nextApp.getUpgradeHandler()(req, socket, head);
339
+ }
340
+ if (pathname === "/api/ws") {
341
+ wss.handleUpgrade(req, socket, head, (ws) => {
342
+ wss.emit("connection", ws, req);
343
+ });
344
+ }
345
+ });
346
+ });
347
+ return previewServer;
348
+ };
349
+
350
+ // src/cli/commands/dev.ts
351
+ var dev = async ({
352
+ dir: relativePathToPdfFolder,
353
+ port
354
+ }) => {
355
+ try {
356
+ if (!import_node_fs2.default.existsSync(relativePathToPdfFolder)) {
357
+ throw new Error(`Missing ${relativePathToPdfFolder} folder`);
358
+ }
359
+ const devServer = await startPreviewServer(
360
+ relativePathToPdfFolder,
361
+ parseInt(port)
362
+ );
363
+ } catch (error) {
364
+ logger_default.error(error);
365
+ process.exit(1);
366
+ }
367
+ };
368
+
369
+ // src/cli/index.ts
370
+ var PACKAGE_NAME = "repdf";
371
+ import_commander.program.name(PACKAGE_NAME).description("A live preview of your pdf templates").version(package_default.version);
372
+ import_commander.program.command("dev").description("Starts the preview pdf development app").option("-d --dir <path>", "Directory containing pdf templates", "./src/pdfs").option("-p --port <port>", "Port to run dev server on", "3000").action(dev);
373
+ import_commander.program.parse();
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/index.js ADDED
@@ -0,0 +1,350 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli/index.ts
4
+ import { program } from "commander";
5
+
6
+ // package.json
7
+ var package_default = {
8
+ name: "@repdf/preview",
9
+ version: "0.0.1",
10
+ type: "module",
11
+ private: false,
12
+ main: "dist/index.js",
13
+ exports: {
14
+ "./package.json": "./package.json",
15
+ ".": {
16
+ import: "./dist/index.js",
17
+ default: "./dist/index.cjs"
18
+ }
19
+ },
20
+ description: "preview app for repdf. Not ready for production use.",
21
+ keywords: ["pdf", "typescript", "react"],
22
+ homepage: "https://repdf.dev",
23
+ author: "repdf labs",
24
+ files: ["dist"],
25
+ license: "MIT",
26
+ scripts: {
27
+ tsup: "tsup --tsconfig tsconfig.build.json",
28
+ build: "next build",
29
+ dev: "next dev",
30
+ lint: "next lint",
31
+ start: "next start",
32
+ "check-exports": "attw --pack .",
33
+ format: "biome check --write .",
34
+ "local-release": "changeset publish"
35
+ },
36
+ dependencies: {
37
+ "@react-email/render": "1.0.1",
38
+ "@react-email/tailwind": "0.1.0",
39
+ "@tanstack/react-query": "^5.59.0",
40
+ "@tanstack/react-query-devtools": "^5.59.0",
41
+ "@trpc/client": "^11.0.0-rc.553",
42
+ "@trpc/next": "^11.0.0-rc.553",
43
+ "@trpc/react-query": "^11.0.0-rc.553",
44
+ "@trpc/server": "^11.0.0-rc.553",
45
+ chokidar: "4.0.1",
46
+ clsx: "^2.1.1",
47
+ commander: "^12.1.0",
48
+ "dependency-tree": "^11.0.1",
49
+ esbuild: "0.24.0",
50
+ express: "^4.21.0",
51
+ glob: "^11.0.0",
52
+ "js-beautify": "^1.15.1",
53
+ "js-dependency-extractor": "^1.0.3",
54
+ "lucide-react": "^0.445.0",
55
+ next: "^14.2.14",
56
+ nuqs: "^1.19.3",
57
+ ora: "^8.1.0",
58
+ pino: "^9.4.0",
59
+ "pino-pretty": "^11.2.2",
60
+ "prism-react-renderer": "^2.4.0",
61
+ puppeteer: "^23.4.1",
62
+ react: "^18.3.1",
63
+ "react-dom": "^18.3.1",
64
+ "react-use-websocket": "^4.9.0",
65
+ "source-map-js": "^1.2.1",
66
+ "stacktrace-parser": "^0.1.10",
67
+ "string-ts": "^2.2.0",
68
+ superjson: "^2.2.1",
69
+ "tailwind-merge": "^2.5.2",
70
+ tsup: "8.3.0",
71
+ tsx: "^4.19.1",
72
+ webpack: "^5.95.0",
73
+ ws: "^8.18.0",
74
+ zod: "^3.23.8"
75
+ },
76
+ devDependencies: {
77
+ "@changesets/cli": "2.27.9",
78
+ "@arethetypeswrong/cli": "0.16.4",
79
+ "@biomejs/biome": "1.9.3",
80
+ "@types/express": "^5.0.0",
81
+ "@types/js-beautify": "^1.14.3",
82
+ "@types/node": "^20.14.10",
83
+ "@types/react": "^18.3.11",
84
+ "@types/react-dom": "^18.3.0",
85
+ "@types/ws": "^8.5.12",
86
+ postcss: "^8.4.39",
87
+ "prettier-plugin-tailwindcss": "^0.6.8",
88
+ "react-email": "3.0.1",
89
+ tailwindcss: "^3.4.13",
90
+ typescript: "^5.6.3"
91
+ },
92
+ packageManager: "pnpm@9.7.1"
93
+ };
94
+
95
+ // src/cli/commands/dev.ts
96
+ import fs2 from "node:fs";
97
+
98
+ // src/logger.ts
99
+ import pino from "pino";
100
+ var logger = pino({
101
+ transport: {
102
+ target: "pino-pretty"
103
+ },
104
+ level: "debug"
105
+ });
106
+ var logger_default = logger;
107
+
108
+ // src/cli/commands/server.ts
109
+ import { parse } from "node:url";
110
+ import path2 from "path";
111
+ import express from "express";
112
+ import next from "next";
113
+ import WebSocket from "ws";
114
+ import { WebSocketServer } from "ws";
115
+
116
+ // src/utils/files/pdfTemplateWatcher.ts
117
+ import * as chokidar from "chokidar";
118
+
119
+ // src/constants.ts
120
+ var RELATIVE_PATH_TO_PDF_FOLDER = () => {
121
+ if (!process.env.RELATIVE_PATH_TO_PDF_FOLDER) {
122
+ throw new Error("RELATIVE_PATH_TO_PDF_FOLDER is not set");
123
+ }
124
+ return process.env.RELATIVE_PATH_TO_PDF_FOLDER;
125
+ };
126
+ var ABSOLUTE_PATH_TO_PDF_FOLDER = () => {
127
+ if (!process.env.ABSOLUTE_PATH_TO_PDF_FOLDER) {
128
+ throw new Error("ABSOLUTE_PATH_TO_PDF_FOLDER is not set");
129
+ }
130
+ return process.env.ABSOLUTE_PATH_TO_PDF_FOLDER;
131
+ };
132
+
133
+ // src/utils/files/dependencyTree.ts
134
+ import dependencyTree from "dependency-tree";
135
+ function getDependencyTree({
136
+ relViewerPath,
137
+ outputType
138
+ }) {
139
+ const options = {
140
+ filename: relViewerPath,
141
+ //TODO does this work with absolute paths?
142
+ directory: ".",
143
+ // requireConfig: 'path/to/requirejs/config', // optional
144
+ // webpackConfig: 'path/to/webpack/config', // optional
145
+ tsConfig: "tsconfig.json",
146
+ // optional
147
+ // nodeModulesConfig: {
148
+ // entry: 'module'
149
+ // }, // optional
150
+ filter: (path3) => path3.indexOf("node_modules") === -1,
151
+ // remove node_node modules from tree
152
+ // nonExistent: [], // optional
153
+ noTypeDefinitions: false
154
+ // optional
155
+ };
156
+ if (outputType === "list") {
157
+ return dependencyTree.toList(options);
158
+ }
159
+ return dependencyTree(options);
160
+ }
161
+
162
+ // src/utils/files/getPdfTemplates.ts
163
+ import fs from "node:fs";
164
+ import path from "node:path";
165
+ import { glob } from "glob";
166
+ function isPotentialPdfTemplate(relViewerPath) {
167
+ const stat = fs.statSync(relViewerPath);
168
+ if (stat.isDirectory()) return false;
169
+ const fileName = path.basename(relViewerPath);
170
+ if (fileName.startsWith("_")) {
171
+ return false;
172
+ }
173
+ const extension = path.extname(relViewerPath);
174
+ if (![".tsx", ".ts", ".jsx", ".js"].includes(extension)) {
175
+ return false;
176
+ }
177
+ const absoluteFilePath = path.resolve(relViewerPath);
178
+ if (!fs.existsSync(absoluteFilePath)) {
179
+ return false;
180
+ }
181
+ const fileContents = fs.readFileSync(absoluteFilePath, "utf8");
182
+ const hasDefaultExport = fileContents.includes("export default");
183
+ return hasDefaultExport;
184
+ }
185
+ function getPdfTemplates() {
186
+ const potentialTemplateFilePaths = glob.sync(
187
+ `${RELATIVE_PATH_TO_PDF_FOLDER()}/**/*.{tsx,jsx,js}`,
188
+ { nodir: true }
189
+ );
190
+ logger_default.debug({ potentialTemplateFilePaths });
191
+ const filteredTemplateFilePaths = potentialTemplateFilePaths.filter(
192
+ isPotentialPdfTemplate
193
+ );
194
+ return filteredTemplateFilePaths.map((file) => {
195
+ const relTemplatePath = file.replace(
196
+ `${RELATIVE_PATH_TO_PDF_FOLDER()}/`,
197
+ ""
198
+ );
199
+ const pathParts = relTemplatePath.split("/");
200
+ let topParentFolder = null;
201
+ if (pathParts.length > 1 && pathParts[0]) {
202
+ topParentFolder = pathParts[0];
203
+ }
204
+ return {
205
+ fileName: path.basename(file),
206
+ // just the file name
207
+ relTemplatePath,
208
+ relViewerPath: file,
209
+ // to where script is being run
210
+ absolutePath: path.resolve(file),
211
+ // file system
212
+ topParentFolder
213
+ };
214
+ });
215
+ }
216
+
217
+ // src/utils/files/pdfTemplateWatcher.ts
218
+ function pdfTemplateWatcher() {
219
+ const pdfTemplates = getPdfTemplates();
220
+ const allFiles = /* @__PURE__ */ new Set();
221
+ for (const pdfTemplate of pdfTemplates) {
222
+ allFiles.add(pdfTemplate.absolutePath);
223
+ const dependencyTree2 = getDependencyTree({
224
+ relViewerPath: pdfTemplate.relViewerPath,
225
+ outputType: "list"
226
+ });
227
+ for (const absoluteFilePath of dependencyTree2) {
228
+ allFiles.add(absoluteFilePath);
229
+ }
230
+ }
231
+ const watcher2 = chokidar.watch(ABSOLUTE_PATH_TO_PDF_FOLDER(), {
232
+ persistent: true,
233
+ ignoreInitial: true,
234
+ ignored: /(^|[\/\\])\../
235
+ // Ignore dot files
236
+ });
237
+ watcher2.add(Array.from(allFiles));
238
+ watcher2.on("add", (filePath) => logger_default.debug(`File ${filePath} has been added`)).on(
239
+ "change",
240
+ (filePath) => logger_default.debug(`File ${filePath} has been changed`)
241
+ ).on(
242
+ "unlink",
243
+ (filePath) => logger_default.debug(`File ${filePath} has been removed`)
244
+ ).on("error", (error) => logger_default.error(`Watcher error: ${error}`));
245
+ return watcher2;
246
+ }
247
+
248
+ // src/cli/commands/server.ts
249
+ var getEnvVariablesForPreviewApp = (relativePathToPdfsDirectory, cwd) => {
250
+ return {
251
+ RELATIVE_PATH_TO_PDF_FOLDER: relativePathToPdfsDirectory,
252
+ ABSOLUTE_PATH_TO_PDF_FOLDER: path2.resolve(cwd, relativePathToPdfsDirectory),
253
+ USER_PROJECT_LOCATION: cwd
254
+ // TODO what is this used for if anything
255
+ };
256
+ };
257
+ var previewServer;
258
+ var clients = /* @__PURE__ */ new Set();
259
+ var watcher;
260
+ var startPreviewServer = async (pdfsDirRelativePath, port) => {
261
+ previewServer = express();
262
+ const server = previewServer.listen(port);
263
+ const wss = new WebSocketServer({ noServer: true });
264
+ process.env = {
265
+ ...process.env,
266
+ NODE_ENV: "development",
267
+ ...getEnvVariablesForPreviewApp(
268
+ // If we don't do normalization here, stuff like https://github.com/resend/react-email/issues/1354 happens.
269
+ path2.normalize(pdfsDirRelativePath),
270
+ process.cwd()
271
+ )
272
+ };
273
+ const nextApp = next({
274
+ dev: true,
275
+ //process.env.NODE_ENV !== "production",
276
+ conf: {
277
+ images: {
278
+ // This is to avoid the warning with sharp
279
+ unoptimized: true
280
+ }
281
+ }
282
+ });
283
+ nextApp.prepare().then(() => {
284
+ previewServer.use((req, res, next2) => {
285
+ nextApp.getRequestHandler()(req, res, parse(req.url, true));
286
+ });
287
+ watcher = pdfTemplateWatcher();
288
+ watcher.on("all", () => {
289
+ for (const client of Array.from(clients)) {
290
+ client.send(`file changed ${(/* @__PURE__ */ new Date()).toISOString()}`);
291
+ }
292
+ watcher = pdfTemplateWatcher();
293
+ });
294
+ wss.on("connection", (ws) => {
295
+ clients.add(ws);
296
+ logger_default.info("New client connected");
297
+ ws.on("message", (message, isBinary) => {
298
+ logger_default.info(`Message received: ${message}`);
299
+ for (const client of Array.from(clients)) {
300
+ if (client.readyState === WebSocket.OPEN && message.toString() !== `{"event":"ping"}`) {
301
+ client.send(message, { binary: isBinary });
302
+ }
303
+ }
304
+ });
305
+ ws.on("close", () => {
306
+ clients.delete(ws);
307
+ logger_default.info("Client disconnected");
308
+ });
309
+ });
310
+ server.on("upgrade", (req, socket, head) => {
311
+ logger_default.info("upgrading to websockets", req.url);
312
+ const { pathname } = parse(req.url || "/", true);
313
+ if (pathname === "/_next/webpack-hmr") {
314
+ logger_default.info("inside nextjs hot module reloading path");
315
+ nextApp.getUpgradeHandler()(req, socket, head);
316
+ }
317
+ if (pathname === "/api/ws") {
318
+ wss.handleUpgrade(req, socket, head, (ws) => {
319
+ wss.emit("connection", ws, req);
320
+ });
321
+ }
322
+ });
323
+ });
324
+ return previewServer;
325
+ };
326
+
327
+ // src/cli/commands/dev.ts
328
+ var dev = async ({
329
+ dir: relativePathToPdfFolder,
330
+ port
331
+ }) => {
332
+ try {
333
+ if (!fs2.existsSync(relativePathToPdfFolder)) {
334
+ throw new Error(`Missing ${relativePathToPdfFolder} folder`);
335
+ }
336
+ const devServer = await startPreviewServer(
337
+ relativePathToPdfFolder,
338
+ parseInt(port)
339
+ );
340
+ } catch (error) {
341
+ logger_default.error(error);
342
+ process.exit(1);
343
+ }
344
+ };
345
+
346
+ // src/cli/index.ts
347
+ var PACKAGE_NAME = "repdf";
348
+ program.name(PACKAGE_NAME).description("A live preview of your pdf templates").version(package_default.version);
349
+ program.command("dev").description("Starts the preview pdf development app").option("-d --dir <path>", "Directory containing pdf templates", "./src/pdfs").option("-p --port <port>", "Port to run dev server on", "3000").action(dev);
350
+ program.parse();
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "@repdf/preview",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "exports": {
7
+ "./package.json": "./package.json",
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "default": "./dist/index.cjs"
11
+ }
12
+ },
13
+ "description": "preview app for repdf. Not ready for production use.",
14
+ "keywords": ["pdf", "typescript", "react"],
15
+ "homepage": "https://repdf.dev",
16
+ "author": "repdf labs",
17
+ "files": ["dist"],
18
+ "license": "MIT",
19
+ "scripts": {
20
+ "tsup": "tsup --tsconfig tsconfig.build.json",
21
+ "build": "next build",
22
+ "dev": "next dev",
23
+ "lint": "next lint",
24
+ "start": "next start",
25
+ "check-exports": "attw --pack .",
26
+ "format": "biome check --write .",
27
+ "local-release": "changeset publish"
28
+ },
29
+ "dependencies": {
30
+ "@react-email/render": "1.0.1",
31
+ "@react-email/tailwind": "0.1.0",
32
+ "@tanstack/react-query": "^5.59.0",
33
+ "@tanstack/react-query-devtools": "^5.59.0",
34
+ "@trpc/client": "^11.0.0-rc.553",
35
+ "@trpc/next": "^11.0.0-rc.553",
36
+ "@trpc/react-query": "^11.0.0-rc.553",
37
+ "@trpc/server": "^11.0.0-rc.553",
38
+ "chokidar": "4.0.1",
39
+ "clsx": "^2.1.1",
40
+ "commander": "^12.1.0",
41
+ "dependency-tree": "^11.0.1",
42
+ "esbuild": "0.24.0",
43
+ "express": "^4.21.0",
44
+ "glob": "^11.0.0",
45
+ "js-beautify": "^1.15.1",
46
+ "js-dependency-extractor": "^1.0.3",
47
+ "lucide-react": "^0.445.0",
48
+ "next": "^14.2.14",
49
+ "nuqs": "^1.19.3",
50
+ "ora": "^8.1.0",
51
+ "pino": "^9.4.0",
52
+ "pino-pretty": "^11.2.2",
53
+ "prism-react-renderer": "^2.4.0",
54
+ "puppeteer": "^23.4.1",
55
+ "react": "^18.3.1",
56
+ "react-dom": "^18.3.1",
57
+ "react-use-websocket": "^4.9.0",
58
+ "source-map-js": "^1.2.1",
59
+ "stacktrace-parser": "^0.1.10",
60
+ "string-ts": "^2.2.0",
61
+ "superjson": "^2.2.1",
62
+ "tailwind-merge": "^2.5.2",
63
+ "tsup": "8.3.0",
64
+ "tsx": "^4.19.1",
65
+ "webpack": "^5.95.0",
66
+ "ws": "^8.18.0",
67
+ "zod": "^3.23.8"
68
+ },
69
+ "devDependencies": {
70
+ "@changesets/cli": "2.27.9",
71
+ "@arethetypeswrong/cli": "0.16.4",
72
+ "@biomejs/biome": "1.9.3",
73
+ "@types/express": "^5.0.0",
74
+ "@types/js-beautify": "^1.14.3",
75
+ "@types/node": "^20.14.10",
76
+ "@types/react": "^18.3.11",
77
+ "@types/react-dom": "^18.3.0",
78
+ "@types/ws": "^8.5.12",
79
+ "postcss": "^8.4.39",
80
+ "prettier-plugin-tailwindcss": "^0.6.8",
81
+ "react-email": "3.0.1",
82
+ "tailwindcss": "^3.4.13",
83
+ "typescript": "^5.6.3"
84
+ },
85
+ "packageManager": "pnpm@9.7.1"
86
+ }