nurev 0.0.6 β†’ 0.0.8

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
- console.log(`${kleur.bgGreen().bold("[Nurev]")}`);
10
- console.log("Template generator 'on demanding revalidation' with Nuxt");
10
+ console.log(`\n${kleur.bgGreen().bold("πŸš€NUREVπŸš€")}`);
11
+ console.log("Template generator 'on demanding revalidation' with Nuxt\n");
11
12
 
12
- const questions = [
13
+ const questionsBase = [
13
14
  {
14
15
  type: "select",
15
16
  name: "manager",
@@ -48,49 +49,103 @@ 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
+ validate: (password) =>
64
+ password.length > 7 ? true : `8 characters needed or more`,
65
+ },
66
+ {
67
+ type: "text",
68
+ name: "user",
69
+ message: "Email for the API user",
70
+ },
71
+ {
72
+ type: "text",
73
+ style: "password",
74
+ name: "userpassword",
75
+ message: "Password for API user",
76
+ validate: (userpassword) =>
77
+ userpassword.length > 7 ? true : `8 characters needed or more`,
78
+ },
79
+ ];
80
+
51
81
  (async () => {
52
- const response = await prompts(questions);
82
+ const responseBase = await prompts(questionsBase);
53
83
 
54
- if (Object.keys(questions).length !== Object.keys(response).length) {
84
+ if (Object.keys(questionsBase).length !== Object.keys(responseBase).length) {
55
85
  console.log("Script finished");
56
86
  return;
57
87
  }
58
88
 
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,
89
+ const paths = {
90
+ base: fileURLToPath(new URL("./templates/base", import.meta.url)),
91
+ backend: fileURLToPath(
92
+ new URL(`./templates/${responseBase.backend}`, import.meta.url),
69
93
  ),
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);
94
+ packageManager: fileURLToPath(
95
+ new URL(
96
+ `./templates/${responseBase.backend}-${responseBase.manager}`,
97
+ import.meta.url,
98
+ ),
99
+ ),
100
+ destination: {
101
+ base: process.cwd(),
102
+ env: `${process.cwd()}/.env`,
103
+ envExample: `${process.cwd()}/.env.example`,
104
+ },
105
+ };
77
106
 
78
- const processInstall = spawn("/usr/bin/make", ["install"]);
107
+ if (responseBase.backend === "pocketbase") {
108
+ const responsePocketbase = await prompts(questionsPocketbase);
79
109
 
80
- processInstall.stdout.pipe(process.stdout);
81
- processInstall.stderr.pipe(process.stderr);
110
+ if (
111
+ Object.keys(questionsPocketbase).length !==
112
+ Object.keys(responsePocketbase).length
113
+ ) {
114
+ console.log("Script finished");
115
+ return;
116
+ }
82
117
 
83
- processInstall.on("close", (_code) => {
84
- console.log(
85
- kleur
86
- .bgGreen()
87
- .bold(`Template ${response.backend}-${response.manager} ready`),
88
- );
89
- });
118
+ for (const key in responsePocketbase) {
119
+ responseBase[key] = responsePocketbase[key];
120
+ }
121
+ }
90
122
 
91
- processInstall.on("error", (err) => {
92
- console.error(`Error: `, err);
93
- });
123
+ try {
124
+ await fs.copy(paths.base, paths.destination.base);
125
+ await fs.copy(paths.backend, paths.destination.base);
126
+ await fs.copy(paths.packageManager, paths.destination.base);
127
+ await spawnPromise("make", ["install"]);
128
+ await fs.copy(paths.destination.envExample, paths.destination.env);
129
+ await spawnPromise("make", ["setup"]);
130
+ switch (responseBase.backend) {
131
+ case "pocketbase":
132
+ await writeEnvFile(paths.destination.env, {
133
+ NUXT_POCKETBASE_USER: responseBase.user,
134
+ NUXT_POCKETBASE_PASSWORD: responseBase.userpassword,
135
+ NUXT_JWT_SECRET: Buffer.from(
136
+ crypto.getRandomValues(new Uint8Array(32)),
137
+ ).toString("hex"),
138
+ });
139
+ await spawnPromise("make", [
140
+ "-s",
141
+ "backend-superuser",
142
+ `email=${responseBase.admin}`,
143
+ `password=${responseBase.adminpassword}`,
144
+ ]);
145
+ //TODO: ADD USERAPI AND POSTS TABLE
146
+ break;
147
+ }
148
+ console.log("\nπŸŽ‰ Your template is ready! πŸŽ‰\n");
94
149
  } catch (error) {
95
150
  console.log(error);
96
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nurev",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
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,47 +31,41 @@ 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
48
48
  ```
49
49
 
50
- 2. Create symbolic links for the environment variables with
51
-
52
- ```bash
53
- make setup
54
- ```
55
-
56
- 3. Start backend dev mode
50
+ 2. Start backend dev mode
57
51
 
58
52
  ```bash
59
53
  make backend-dev
60
54
  ```
61
55
 
62
- 4. Open [http://localhost:8090](`http://localhost:8090/_/) and create a superuser
56
+ 3. Create a user inside `users` collection with the same values inside `NUXT_POCKETBASE_USER` and `NUXT_POCKETBASE_PASSWORD`
63
57
 
64
- 5. Create a user inside `users` collection with the same values inside `NUXT_POCKETBASE_USER` and `NUXT_POCKETBASE_PASSWORD`
58
+ 4. Create a collection named `posts` and change API Rules for give permissions with `@request.auth.id != ""`
65
59
 
66
- 6. Create a collection named `posts` and create a new post
60
+ 5. Create a new post
67
61
 
68
- 7. Start frontend dev mode
62
+ 6. Start frontend dev mode
69
63
 
70
64
  ```bash
71
65
  make frontend-dev
72
66
  ```
73
67
 
74
- 8. Open `http://localhost:3000/posts/[id]` and replace `[id]` with the post id created in the 6. step
68
+ 7. Open `http://localhost:3000/posts/[id]` and replace `[id]` with the post id created in the 6. step
75
69
 
76
70
  ## Build
77
71
 
@@ -1,4 +1,4 @@
1
- .PHONY: all backend-build backend-update backend-dev backend-run backend-setup \
1
+ .PHONY: all backend-build backend-update backend-dev backend-run backend-setup backend-superuser \
2
2
  frontend-install frontend-dev frontend-build frontend-preview frontend-setup\
3
3
  help setup build install
4
4
 
@@ -26,6 +26,10 @@ backend-dev: backend-update
26
26
  backend-run: backend-build
27
27
  @echo "Running backend..."
28
28
  cd backend && ./backend serve
29
+
30
+ backend-superuser: backend-update
31
+ @echo "Adding superuser..."
32
+ cd backend && go run . superuser create $(email) $(password)
29
33
 
30
34
  # Frontend targets
31
35
  frontend-setup:
@@ -64,6 +68,7 @@ help:
64
68
  @echo " make backend-dev - Run the backend development only"
65
69
  @echo " make backend-run - Run the backend only"
66
70
  @echo " make backend-update - Update backend dependencies"
71
+ @echo " make backend-superuser email=test@test.com password=123 - Add a new superuser"
67
72
  @echo " make frontend-setup - Create a symbolic link for frontend"
68
73
  @echo " make frontend-install - Install frontend dependencies"
69
74
  @echo " make frontend-dev - Start frontend development server"
@@ -1,4 +1,4 @@
1
- .PHONY: all backend-build backend-update backend-dev backend-run backend-setup \
1
+ .PHONY: all backend-build backend-update backend-dev backend-run backend-setup backend-superuser \
2
2
  frontend-install frontend-dev frontend-build frontend-preview frontend-setup\
3
3
  help setup build install
4
4
 
@@ -27,6 +27,9 @@ backend-run: backend-build
27
27
  @echo "Running backend..."
28
28
  cd backend && ./backend serve
29
29
 
30
+ backend-superuser: backend-update
31
+ @echo "Adding superuser..."
32
+ cd backend && go run . superuser create $(email) $(password)
30
33
  # Frontend targets
31
34
  frontend-setup:
32
35
  @echo "Preparing frontend..."
@@ -64,6 +67,7 @@ help:
64
67
  @echo " make backend-dev - Run the backend development only"
65
68
  @echo " make backend-run - Run the backend only"
66
69
  @echo " make backend-update - Update backend dependencies"
70
+ @echo " make backend-superuser email=test@test.com password=123 - Add a new superuser"
67
71
  @echo " make frontend-setup - Create a symbolic link for frontend"
68
72
  @echo " make frontend-install - Install frontend dependencies"
69
73
  @echo " make frontend-dev - Start frontend development server"
@@ -1,4 +1,4 @@
1
- .PHONY: all backend-build backend-update backend-dev backend-run backend-setup \
1
+ .PHONY: all backend-build backend-update backend-dev backend-run backend-setup backend-superuser \
2
2
  frontend-install frontend-dev frontend-build frontend-preview frontend-setup\
3
3
  help setup build install
4
4
 
@@ -27,6 +27,10 @@ backend-run: backend-build
27
27
  @echo "Running backend..."
28
28
  cd backend && ./backend serve
29
29
 
30
+ backend-superuser: backend-update
31
+ @echo "Adding superuser..."
32
+ cd backend && go run . superuser create $(email) $(password)
33
+
30
34
  # Frontend targets
31
35
  frontend-setup:
32
36
  @echo "Preparing frontend..."
@@ -64,6 +68,7 @@ help:
64
68
  @echo " make backend-dev - Run the backend development only"
65
69
  @echo " make backend-run - Run the backend only"
66
70
  @echo " make backend-update - Update backend dependencies"
71
+ @echo " make backend-superuser email=test@test.com password=123 - Add a new superuser"
67
72
  @echo " make frontend-setup - Create a symbolic link for frontend"
68
73
  @echo " make frontend-install - Install frontend dependencies"
69
74
  @echo " make frontend-dev - Start frontend development server"
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
+ }