astro-blog-kit 0.1.1 → 0.1.3

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.
Files changed (3) hide show
  1. package/bin/cli.js +165 -8
  2. package/cli.ts +1 -1
  3. package/package.json +7 -2
package/bin/cli.js CHANGED
@@ -1,8 +1,165 @@
1
- #!/usr/bin/env node
2
- import { fileURLToPath, pathToFileURL } from 'url';
3
- import { dirname, join } from 'path';
4
-
5
- const __dirname = dirname(fileURLToPath(import.meta.url));
6
- const cliPath = pathToFileURL(join(__dirname, '..', 'cli.ts')).href;
7
-
8
- import(cliPath);
1
+ #!/usr/bin/env node
2
+
3
+ // cli.ts
4
+ import * as p from "@clack/prompts";
5
+ import fs from "fs";
6
+ import path from "path";
7
+ import { fileURLToPath } from "url";
8
+ var __dirname = path.dirname(fileURLToPath(import.meta.url));
9
+ function copyTemplate(templateName, destPath, replacements) {
10
+ const templatePath = path.join(__dirname, "..", "templates", templateName);
11
+ let content = fs.readFileSync(templatePath, "utf-8");
12
+ for (const [key, value] of Object.entries(replacements)) {
13
+ content = content.replaceAll(`__${key}__`, value);
14
+ }
15
+ const dir = path.dirname(destPath);
16
+ if (!fs.existsSync(dir)) {
17
+ fs.mkdirSync(dir, { recursive: true });
18
+ }
19
+ if (fs.existsSync(destPath)) {
20
+ return false;
21
+ }
22
+ fs.writeFileSync(destPath, content, "utf-8");
23
+ return true;
24
+ }
25
+ function log(symbol, message) {
26
+ console.log(`${symbol} ${message}`);
27
+ }
28
+ async function main() {
29
+ console.log("");
30
+ p.intro("astro-blog-kit \u2014 Blog setup wizard");
31
+ const answers = await p.group(
32
+ {
33
+ wpUrl: () => p.text({
34
+ message: "WordPress URL",
35
+ placeholder: "https://cms.tudominio.com",
36
+ validate: (v) => {
37
+ if (!v) return "WordPress URL is required";
38
+ try {
39
+ new URL(v);
40
+ } catch {
41
+ return "Please enter a valid URL";
42
+ }
43
+ }
44
+ }),
45
+ postsPerPage: () => p.text({
46
+ message: "Posts per page",
47
+ placeholder: "5",
48
+ initialValue: "5",
49
+ validate: (v) => {
50
+ const n = Number(v);
51
+ if (isNaN(n) || n < 1) return "Must be a number greater than 0";
52
+ }
53
+ }),
54
+ defaultLayout: () => p.select({
55
+ message: "Default layout",
56
+ options: [
57
+ { value: "magazine", label: "Magazine \u2014 featured post + grid" },
58
+ { value: "grid", label: "Grid \u2014 3 column card grid" },
59
+ { value: "list", label: "List \u2014 horizontal rows" }
60
+ ]
61
+ }),
62
+ locale: () => p.text({
63
+ message: "Default locale",
64
+ placeholder: "en",
65
+ initialValue: "en"
66
+ }),
67
+ i18n: () => p.confirm({
68
+ message: "Enable i18n (multiple languages)?",
69
+ initialValue: false
70
+ })
71
+ },
72
+ {
73
+ onCancel: () => {
74
+ p.cancel("Setup cancelled.");
75
+ process.exit(0);
76
+ }
77
+ }
78
+ );
79
+ const replacements = {
80
+ WP_URL: answers.wpUrl,
81
+ POSTS_PER_PAGE: answers.postsPerPage,
82
+ DEFAULT_LAYOUT: answers.defaultLayout,
83
+ LOCALE: answers.locale
84
+ };
85
+ const cwd = process.cwd();
86
+ const spinner2 = p.spinner();
87
+ spinner2.start("Creating blog files...");
88
+ const files = [
89
+ {
90
+ template: "blog-config.ts.template",
91
+ dest: path.join(cwd, "blog.config.ts")
92
+ },
93
+ {
94
+ template: "blog-index.astro.template",
95
+ dest: path.join(cwd, "src", "pages", "blog", "index.astro")
96
+ },
97
+ {
98
+ template: "blog-slug.astro.template",
99
+ dest: path.join(cwd, "src", "pages", "blog", "[...slug].astro")
100
+ },
101
+ {
102
+ template: "blog-page.astro.template",
103
+ dest: path.join(cwd, "src", "pages", "blog", "page", "[page].astro")
104
+ },
105
+ {
106
+ template: "api-comments.ts.template",
107
+ dest: path.join(cwd, "src", "pages", "api", "comments.ts")
108
+ }
109
+ ];
110
+ const created = [];
111
+ const skipped = [];
112
+ for (const file of files) {
113
+ const ok = copyTemplate(file.template, file.dest, replacements);
114
+ const relativePath = path.relative(cwd, file.dest);
115
+ if (ok) {
116
+ created.push(relativePath);
117
+ } else {
118
+ skipped.push(relativePath);
119
+ }
120
+ }
121
+ spinner2.stop("Blog files ready!");
122
+ const envExample = path.join(cwd, ".env.example");
123
+ if (!fs.existsSync(envExample)) {
124
+ fs.writeFileSync(
125
+ envExample,
126
+ [
127
+ `WP_API_URL=${answers.wpUrl}`,
128
+ `WP_APP_USER=your-wordpress-username`,
129
+ `WP_APP_PASSWORD=xxxx xxxx xxxx xxxx xxxx xxxx`
130
+ ].join("\n"),
131
+ "utf-8"
132
+ );
133
+ created.push(".env.example");
134
+ }
135
+ console.log("");
136
+ if (created.length > 0) {
137
+ log("\u2705", "Created:");
138
+ created.forEach((f) => log(" ", f));
139
+ }
140
+ if (skipped.length > 0) {
141
+ console.log("");
142
+ log("\u26A0\uFE0F ", "Skipped (already exist):");
143
+ skipped.forEach((f) => log(" ", f));
144
+ }
145
+ console.log("");
146
+ p.note(
147
+ [
148
+ "1. Add your WordPress credentials to .env:",
149
+ " WP_APP_USER=your-username",
150
+ " WP_APP_PASSWORD=xxxx xxxx xxxx xxxx",
151
+ "",
152
+ "2. Add blogKit() to your astro.config.mjs:",
153
+ " import { blogKit } from 'astro-blog-kit/integration';",
154
+ " integrations: [blogKit()]",
155
+ "",
156
+ "3. Run: npm run dev"
157
+ ].join("\n"),
158
+ "Next steps"
159
+ );
160
+ p.outro("Your blog is ready! \u{1F680}");
161
+ }
162
+ main().catch((err) => {
163
+ console.error(err);
164
+ process.exit(1);
165
+ });
package/cli.ts CHANGED
@@ -18,7 +18,7 @@ function copyTemplate(
18
18
  destPath: string,
19
19
  replacements: Record<string, string>
20
20
  ) {
21
- const templatePath = path.join(__dirname, "templates", templateName);
21
+ const templatePath = path.join(__dirname, "..", "templates", templateName);
22
22
  let content = fs.readFileSync(templatePath, "utf-8");
23
23
 
24
24
  for (const [key, value] of Object.entries(replacements)) {
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "astro-blog-kit",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "A ready-to-use blog system for Astro with WordPress headless support, optional i18n, multiple layouts, and a comment system.",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "bin": {
8
- "astro-blog-kit": "./bin/cli.js"
8
+ "astro-blog-kit": "bin/cli.js"
9
+ },
10
+ "scripts": {
11
+ "build:cli": "esbuild cli.ts --bundle --platform=node --format=esm --outfile=bin/cli.js --external:@clack/prompts",
12
+ "prepublishOnly": "npm run build:cli"
9
13
  },
10
14
  "exports": {
11
15
  ".": "./index.ts",
@@ -43,6 +47,7 @@
43
47
  "devDependencies": {
44
48
  "@types/node": "latest",
45
49
  "astro": "^6.3.1",
50
+ "esbuild": "^0.28.0",
46
51
  "tsx": "latest"
47
52
  }
48
53
  }