htmv 0.0.10 → 0.0.12

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/README.md CHANGED
@@ -69,5 +69,13 @@ export function POST(routeParams: RouteParams) {
69
69
  ```
70
70
  Note how the `default` keyword was removed, that keyword is instead reserved for when you want to hit all endpoints (`ALL` method).
71
71
 
72
+ Supported methods currently are:
73
+ - GET
74
+ - POST
75
+ - PUT
76
+ - PATCH
77
+ - DELETE
78
+ - ALL (add `default` keyword)
79
+
72
80
  # Hot reloading
73
81
  Having to restart the server every time you make a change can be quite tedious. HTMV takes care of this thanks to Bun. Just develop with `bun dev` and it should work out of the box! Note that this does not include hot reloading in the browser. As of now, you have to refresh the page to see new changes. It doesn't update in real time.
@@ -57,6 +57,13 @@ export default async (_params: RouteParams) => {
57
57
  </body>
58
58
  </html>`;
59
59
  await fs.writeFile(path.join(fullPath, "views", "example.html"), viewContent);
60
+ console.log("5. Creating run scripts...");
61
+ await runCommand(`npm pkg set scripts.dev="bun --watch ."`, {
62
+ cwd: fullPath,
63
+ });
64
+ await runCommand(`npm pkg set scripts.start="bun index.ts"`, {
65
+ cwd: fullPath,
66
+ });
60
67
  console.log(`All done! Project ${name} created.`);
61
68
  console.log(`Now run cd ${name} and start building your next big project!`);
62
69
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "htmv",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.10",
5
+ "version": "0.0.12",
6
6
  "devDependencies": {
7
7
  "@biomejs/biome": "2.3.3",
8
8
  "@types/bun": "latest"
package/src/cli/cli.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import gen from "./commands/gen.js";
2
3
  import help from "./commands/help.js";
3
4
  import newCommand from "./commands/new.js";
4
5
  import { AVAILABLE_COMMANDS } from "./consts.js";
@@ -15,6 +16,7 @@ const commandArgs = args.slice(1);
15
16
  const commands = {
16
17
  help,
17
18
  new: () => newCommand(commandArgs),
19
+ gen: () => gen(commandArgs),
18
20
  };
19
21
 
20
22
  if (command in commands) {
@@ -0,0 +1,79 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { exists } from "../utils.js";
4
+
5
+ export default async (args: string[]) => {
6
+ const [type, name, ...options] = args;
7
+ if (type === undefined || name === undefined)
8
+ return console.error(
9
+ "You must specify both a type and name of file to generate.\nCorrect usage: htmv gen {TYPE} {NAME} {OPTIONS}",
10
+ );
11
+ if (!["view", "route"].includes(type.toLowerCase()))
12
+ return console.error("Invalid type inputted. Valid types are: view, route");
13
+ if (type.toLowerCase() === "view") {
14
+ const viewsFolderPath = await validateOptions("views", ...options);
15
+ const viewsContents = `<!DOCTYPE html>
16
+ <html lang="en">
17
+
18
+ <head>
19
+ <title>${name} view</title>
20
+ </head>
21
+
22
+ <body>
23
+ <h1>This view was quickly generated with htmv gen.</h1>
24
+ </body>
25
+ </html>`;
26
+ await generateFile(path.join(viewsFolderPath, `${name}.ts`), viewsContents);
27
+ }
28
+ if (type.toLowerCase() === "route") {
29
+ const routesFolderPath = await validateOptions("route", ...options);
30
+ const routeContents = `import { type RouteParams } from "htmv";
31
+
32
+ export default (_params: RouteParams) => {
33
+ return "Just a simple ALL method.";
34
+ };
35
+
36
+ export function POST (_params: RouteParams) => {
37
+ return "Searching for something more specific? How about a POST method route?"
38
+ };
39
+ `;
40
+ await generateFile(
41
+ path.join(routesFolderPath, `${name}.ts`),
42
+ routeContents,
43
+ );
44
+ }
45
+ console.log(`${type} ${name} generated succesfully.`);
46
+ };
47
+
48
+ async function generateFile(_path: string, contents: string) {
49
+ await fs.writeFile(_path, contents);
50
+ }
51
+
52
+ async function validateOptions(
53
+ folderName: string,
54
+ optionName?: string,
55
+ optionValue?: string,
56
+ ) {
57
+ let folderPath = path.resolve(folderName);
58
+ if (optionName) {
59
+ if (optionName !== "--path") {
60
+ console.error("Invalid option provided. Valid options are: --path.");
61
+ process.exit(1);
62
+ }
63
+
64
+ if (optionValue === undefined) {
65
+ console.error("No option value provided.");
66
+ process.exit(1);
67
+ }
68
+
69
+ folderPath = path.resolve(optionValue);
70
+ }
71
+ const viewsFolderExists = await exists(folderPath);
72
+ if (!viewsFolderExists) {
73
+ console.error(
74
+ `Unable to find ${folderName} folder. Did you change your ${folderName} folder's name? If so, did you pass the option --path with the corresponding path to it?`,
75
+ );
76
+ process.exit(1);
77
+ }
78
+ return folderPath;
79
+ }
@@ -1,6 +1,7 @@
1
1
  import childProcess from "node:child_process";
2
2
  import fs from "node:fs/promises";
3
3
  import path from "node:path";
4
+ import { exists } from "../utils.js";
4
5
 
5
6
  export default async (args: string[]) => {
6
7
  const name = args[0];
@@ -59,8 +60,12 @@ export default async (_params: RouteParams) => {
59
60
  </html>`;
60
61
  await fs.writeFile(path.join(fullPath, "views", "example.html"), viewContent);
61
62
  console.log("5. Creating run scripts...");
62
- await runCommand(`npm pkg set scripts.dev="bun --watch ."`);
63
- await runCommand(`npm pkg set scripts.start="bun index.ts"`);
63
+ await runCommand(`npm pkg set scripts.dev="bun --watch ."`, {
64
+ cwd: fullPath,
65
+ });
66
+ await runCommand(`npm pkg set scripts.start="bun index.ts"`, {
67
+ cwd: fullPath,
68
+ });
64
69
  console.log(`All done! Project ${name} created.`);
65
70
  console.log(`Now run cd ${name} and start building your next big project!`);
66
71
  };
@@ -76,12 +81,3 @@ async function runCommand(
76
81
  });
77
82
  });
78
83
  }
79
-
80
- async function exists(path: string) {
81
- try {
82
- await fs.access(path);
83
- return true;
84
- } catch {
85
- return false;
86
- }
87
- }
@@ -0,0 +1,10 @@
1
+ import fs from "node:fs/promises";
2
+
3
+ export async function exists(path: string) {
4
+ try {
5
+ await fs.access(path);
6
+ return true;
7
+ } catch {
8
+ return false;
9
+ }
10
+ }