nurev 0.0.6 → 0.0.7

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/bun.lock CHANGED
@@ -8,9 +8,6 @@
8
8
  "fs-extra": "^11.3.4",
9
9
  "prompts": "^2.4.2",
10
10
  },
11
- "peerDependencies": {
12
- "typescript": "^5",
13
- },
14
11
  },
15
12
  },
16
13
  "packages": {
@@ -26,8 +23,6 @@
26
23
 
27
24
  "sisteransi": ["sisteransi@1.0.5", "", {}, "sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg=="],
28
25
 
29
- "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
30
-
31
26
  "universalify": ["universalify@2.0.1", "", {}, "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw=="],
32
27
  }
33
28
  }
package/index.js CHANGED
@@ -1,15 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ import kleur from "kleur";
3
4
  import { default as prompts } from "prompts";
4
5
  import { default as fs } from "fs-extra";
5
6
  import { fileURLToPath } from "url";
6
- import kleur from "kleur";
7
- import { spawn } from "child_process";
7
+ import { spawnPromise } from "./utils/cmd.js";
8
+ import { writeEnvFile } from "./utils/file.js";
8
9
 
9
10
  console.log(`${kleur.bgGreen().bold("[Nurev]")}`);
10
11
  console.log("Template generator 'on demanding revalidation' with Nuxt");
11
12
 
12
- const questions = [
13
+ const questionsBase = [
13
14
  {
14
15
  type: "select",
15
16
  name: "manager",
@@ -48,49 +49,88 @@ const questions = [
48
49
  },
49
50
  ];
50
51
 
52
+ const questionsPocketbase = [
53
+ {
54
+ type: "text",
55
+ name: "admin",
56
+ message: "Email address to assign to the Pocketbase superadmin",
57
+ },
58
+ {
59
+ type: "text",
60
+ style: "password",
61
+ name: "adminpassword",
62
+ message: "Password for superadmin",
63
+ },
64
+ {
65
+ type: "text",
66
+ name: "user",
67
+ message: "Email for the API user",
68
+ },
69
+ {
70
+ type: "text",
71
+ style: "password",
72
+ name: "userpassword",
73
+ message: "Password for API user",
74
+ },
75
+ ];
76
+
51
77
  (async () => {
52
- const response = await prompts(questions);
78
+ const responseBase = await prompts(questionsBase);
53
79
 
54
- if (Object.keys(questions).length !== Object.keys(response).length) {
80
+ if (Object.keys(questionsBase).length !== Object.keys(responseBase).length) {
55
81
  console.log("Script finished");
56
82
  return;
57
83
  }
58
84
 
59
- const templateBase = fileURLToPath(
60
- new URL("./templates/base", import.meta.url),
61
- );
62
- const templateBackend = fileURLToPath(
63
- new URL(`./templates/${response.backend}`, import.meta.url),
64
- );
65
- const templatePackageManager = fileURLToPath(
66
- new URL(
67
- `./templates/${response.backend}-${response.manager}`,
68
- import.meta.url,
85
+ const paths = {
86
+ base: fileURLToPath(new URL("./templates/base", import.meta.url)),
87
+ backend: fileURLToPath(
88
+ new URL(`./templates/${responseBase.backend}`, import.meta.url),
69
89
  ),
70
- );
71
- const templateDest = process.cwd();
72
-
73
- try {
74
- await fs.copy(templateBase, templateDest);
75
- await fs.copy(templateBackend, templateDest);
76
- await fs.copy(templatePackageManager, templateDest);
90
+ packageManager: fileURLToPath(
91
+ new URL(
92
+ `./templates/${responseBase.backend}-${responseBase.manager}`,
93
+ import.meta.url,
94
+ ),
95
+ ),
96
+ destination: process.cwd(),
97
+ destinationEnvExample: process.cwd() + "/.env.example",
98
+ destinationEnv: process.cwd() + "/.env",
99
+ };
77
100
 
78
- const processInstall = spawn("/usr/bin/make", ["install"]);
101
+ if (responseBase.backend === "pocketbase") {
102
+ const responsePocketbase = await prompts(questionsPocketbase);
79
103
 
80
- processInstall.stdout.pipe(process.stdout);
81
- processInstall.stderr.pipe(process.stderr);
104
+ if (
105
+ Object.keys(questionsPocketbase).length !==
106
+ Object.keys(responsePocketbase).length
107
+ ) {
108
+ console.log("Script finished");
109
+ return;
110
+ }
82
111
 
83
- processInstall.on("close", (_code) => {
84
- console.log(
85
- kleur
86
- .bgGreen()
87
- .bold(`Template ${response.backend}-${response.manager} ready`),
88
- );
89
- });
112
+ for (const key in responsePocketbase) {
113
+ responseBase[key] = responsePocketbase[key];
114
+ }
115
+ }
90
116
 
91
- processInstall.on("error", (err) => {
92
- console.error(`Error: `, err);
93
- });
117
+ try {
118
+ await fs.copy(paths.base, paths.destination);
119
+ await fs.copy(paths.backend, paths.destination);
120
+ await fs.copy(paths.packageManager, paths.destination);
121
+ await spawnPromise("/usr/bin/make", ["install"]);
122
+ await fs.copy(paths.destinationEnvExample, paths.destinationEnv);
123
+ switch (responseBase.backend) {
124
+ case "pocketbase":
125
+ await writeEnvFile(paths.destinationEnv, {
126
+ NUXT_POCKETBASE_USER: responseBase.user,
127
+ NUXT_POCKETBASE_PASSWORD: responseBase.userpassword,
128
+ NUXT_JWT_SECRET: Buffer.from(
129
+ crypto.getRandomValues(new Uint8Array(32)),
130
+ ).toString("hex"),
131
+ });
132
+ break;
133
+ }
94
134
  } catch (error) {
95
135
  console.log(error);
96
136
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nurev",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "module": "index.js",
5
5
  "dependencies": {
6
6
  "fs-extra": "^11.3.4",
@@ -38,10 +38,8 @@ export const defineAuthResponseHandler = <T extends EventHandlerRequest, D>(
38
38
  }
39
39
 
40
40
  const response = await handler(event);
41
- // do something after the route handler
42
41
  return { response };
43
42
  } catch (err) {
44
- // Error handling
45
43
  return { err };
46
44
  }
47
45
  });
@@ -1,5 +1,5 @@
1
- TABLES_TRIGGER=
2
- JWT_EXPIRES_MINUTES=
1
+ TABLES_TRIGGER=posts
2
+ JWT_EXPIRES_MINUTES=1
3
3
  FRONTEND_URL=http://localhost:3000/api/private/reloadcache
4
4
  NUXT_POCKETBASE_URL=https://localhost:8090
5
5
  NUXT_POCKETBASE_USER=
@@ -9,7 +9,7 @@ Tech stack:
9
9
 
10
10
  ## Setup
11
11
 
12
- 1. Create a `.env` file, follow the `.env.example` file
12
+ 1. Check if your `.env` file is correct
13
13
 
14
14
  - `TABLES_TRIGGER`: collections that trigger the Nuxt cache to be cleared when collections are updated, a new row is added or a row is removed
15
15
  - **Example**
@@ -31,17 +31,17 @@ Tech stack:
31
31
  ```bash
32
32
  NUXT_POCKETBASE_URL=http://localhost:8090
33
33
  ```
34
- - `NUXT_POCKETBASE_USER`: PocketBase user created in `user` collection for login to the API
34
+ - `NUXT_POCKETBASE_USER`: PocketBase user created in `user` collection for login to the API (requested)
35
35
  - **Example**
36
36
  ```bash
37
37
  NUXT_POCKETBASE_USER=paco@paco.me
38
38
  ```
39
- - `NUXT_POCKETBASE_PASSWORD`: PocketBase password user created in `user` collection for login to the API
39
+ - `NUXT_POCKETBASE_PASSWORD`: PocketBase password user created in `user` collection for login to the API (requested)
40
40
  - **Example**
41
41
  ```bash
42
42
  NUXT_POCKETBASE_PASSWORD=secretpassword
43
43
  ```
44
- - `NUXT_JWT_SECRET`: JWT secret for signing and validating tokens
44
+ - `NUXT_JWT_SECRET`: JWT secret for signing and validating tokens (autogenerated)
45
45
  - **Example**
46
46
  ```bash
47
47
  NUXT_JWT_SECRET=secretjwt
@@ -63,9 +63,11 @@ make backend-dev
63
63
 
64
64
  5. Create a user inside `users` collection with the same values inside `NUXT_POCKETBASE_USER` and `NUXT_POCKETBASE_PASSWORD`
65
65
 
66
- 6. Create a collection named `posts` and create a new post
66
+ 6. Create a collection named `posts` and change API Rules for give permissions with `@request.auth.id != ""`
67
67
 
68
- 7. Start frontend dev mode
68
+ 7. Create a new post
69
+
70
+ 8. Start frontend dev mode
69
71
 
70
72
  ```bash
71
73
  make frontend-dev
package/utils/cmd.js ADDED
@@ -0,0 +1,23 @@
1
+ import { spawn } from "child_process";
2
+
3
+ export function spawnPromise(command, args) {
4
+ return new Promise((resolve, reject) => {
5
+ const processChild = spawn(command, args);
6
+
7
+ processChild.stdout.pipe(process.stdout);
8
+ processChild.stderr.pipe(process.stderr);
9
+
10
+ processChild.on("close", (code) => {
11
+ if (code === 0) {
12
+ resolve({ code });
13
+ } else {
14
+ reject(new Error(`Process exited with code ${code}`));
15
+ }
16
+ });
17
+
18
+ processChild.on("error", (err) => {
19
+ console.error(`Error: `, err);
20
+ reject(err);
21
+ });
22
+ });
23
+ }
package/utils/file.js ADDED
@@ -0,0 +1,25 @@
1
+ import { default as fs } from "fs-extra";
2
+
3
+ export function writeEnvFile(filePath, params) {
4
+ return new Promise(async (resolve, reject) => {
5
+ try {
6
+ let content = await fs.readFile(filePath, "utf8");
7
+
8
+ for (const key in params) {
9
+ const value = params[key];
10
+ const regex = new RegExp(`^${key}=.*`, "m");
11
+
12
+ if (regex.test(content) === true) {
13
+ content = content.replace(regex, `${key}=${value}`);
14
+ } else {
15
+ reject(new Error(`Param ${key} not found inside .env`));
16
+ }
17
+ }
18
+
19
+ await fs.writeFile(filePath, content, `utf8`);
20
+ resolve();
21
+ } catch (error) {
22
+ console.error(error);
23
+ }
24
+ });
25
+ }