@lightfish/cli 0.0.2 → 0.0.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.
@@ -4,9 +4,9 @@
4
4
  import cac from "cac";
5
5
 
6
6
  // src/commands/create.ts
7
- import path3 from "path";
7
+ import path4 from "path";
8
8
  import { execa as execa2 } from "execa";
9
- import fs3 from "fs-extra";
9
+ import fs4 from "fs-extra";
10
10
  import prompts3 from "prompts";
11
11
 
12
12
  // src/utils/download-template.ts
@@ -147,6 +147,63 @@ function toPackageName(projectName) {
147
147
  return projectName.trim().toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
148
148
  }
149
149
 
150
+ // src/utils/replace-token.ts
151
+ import path3 from "path";
152
+ import fs3 from "fs-extra";
153
+ var SKIP_DIRS = /* @__PURE__ */ new Set([
154
+ ".git",
155
+ "node_modules",
156
+ "dist",
157
+ ".cache",
158
+ "coverage"
159
+ ]);
160
+ var SKIP_EXTS = /* @__PURE__ */ new Set([
161
+ ".png",
162
+ ".jpg",
163
+ ".jpeg",
164
+ ".gif",
165
+ ".svg",
166
+ ".ico",
167
+ ".webp",
168
+ ".woff",
169
+ ".woff2",
170
+ ".ttf",
171
+ ".eot",
172
+ ".otf",
173
+ ".mp4",
174
+ ".webm",
175
+ ".mp3",
176
+ ".wav",
177
+ ".zip",
178
+ ".gz",
179
+ ".tar",
180
+ ".7z",
181
+ ".lock"
182
+ ]);
183
+ async function replaceToken(targetDir, token, value) {
184
+ const entries = await fs3.readdir(targetDir);
185
+ for (const entry of entries) {
186
+ const fullPath = path3.join(targetDir, entry);
187
+ const stat = await fs3.stat(fullPath);
188
+ if (stat.isDirectory()) {
189
+ if (SKIP_DIRS.has(entry)) continue;
190
+ await replaceToken(fullPath, token, value);
191
+ continue;
192
+ }
193
+ const ext = path3.extname(entry).toLowerCase();
194
+ if (SKIP_EXTS.has(ext)) continue;
195
+ try {
196
+ const content = await fs3.readFile(fullPath, "utf8");
197
+ if (content.includes(token)) {
198
+ const replaced = content.replaceAll(token, value);
199
+ await fs3.writeFile(fullPath, replaced, "utf8");
200
+ console.log(` [replace] ${entry}`);
201
+ }
202
+ } catch {
203
+ }
204
+ }
205
+ }
206
+
150
207
  // src/utils/resolve-template.ts
151
208
  import prompts2 from "prompts";
152
209
 
@@ -217,13 +274,13 @@ async function resolveTemplateRepo(options) {
217
274
 
218
275
  // src/commands/create.ts
219
276
  async function patchPackageJsonName(targetDir, projectName) {
220
- const packageJsonPath = path3.join(targetDir, "package.json");
221
- if (!await fs3.pathExists(packageJsonPath)) {
277
+ const packageJsonPath = path4.join(targetDir, "package.json");
278
+ if (!await fs4.pathExists(packageJsonPath)) {
222
279
  throw new Error("\u6A21\u677F\u7F3A\u5C11 package.json");
223
280
  }
224
- const packageJson = await fs3.readJson(packageJsonPath);
281
+ const packageJson = await fs4.readJson(packageJsonPath);
225
282
  packageJson.name = toPackageName(projectName);
226
- await fs3.writeJson(packageJsonPath, packageJson, { spaces: 2 });
283
+ await fs4.writeJson(packageJsonPath, packageJson, { spaces: 2 });
227
284
  }
228
285
  async function resolveCreateOptions(options) {
229
286
  const cwd = options.cwd ?? process.cwd();
@@ -310,6 +367,9 @@ async function createProject(options = {}) {
310
367
  console.log("[create] \u5F00\u59CB\u4FEE\u6539 package.json...");
311
368
  await patchPackageJsonName(resolvedOptions.targetDir, resolvedOptions.projectName);
312
369
  console.log("[create] package.json \u4FEE\u6539\u5B8C\u6210");
370
+ console.log("[create] \u5F00\u59CB\u66FF\u6362\u9879\u76EE\u540D\u5360\u4F4D\u7B26...");
371
+ await replaceToken(resolvedOptions.targetDir, "__PROJECT_NAME__", resolvedOptions.projectName);
372
+ console.log("[create] \u5360\u4F4D\u7B26\u66FF\u6362\u5B8C\u6210");
313
373
  console.log("[create] \u5F00\u59CB\u5B89\u88C5\u4F9D\u8D56...");
314
374
  await maybeInstall(resolvedOptions);
315
375
  console.log("[create] \u4F9D\u8D56\u5B89\u88C5\u5B8C\u6210");
@@ -320,22 +380,22 @@ async function createProject(options = {}) {
320
380
  }
321
381
 
322
382
  // src/utils/package-info.ts
323
- import fs4 from "fs";
324
- import path4 from "path";
383
+ import fs5 from "fs";
384
+ import path5 from "path";
325
385
  import { fileURLToPath } from "url";
326
- var dirname = path4.dirname(fileURLToPath(import.meta.url));
386
+ var dirname = path5.dirname(fileURLToPath(import.meta.url));
327
387
  function readPackageVersion() {
328
388
  const packageJsonPathCandidates = [
329
389
  // 源码模式:src/utils/package-info.ts -> packages/cli/package.json
330
- path4.resolve(dirname, "../../package.json"),
390
+ path5.resolve(dirname, "../../package.json"),
331
391
  // 构建产物模式:dist/lightfish.js -> packages/cli/package.json
332
- path4.resolve(dirname, "../package.json")
392
+ path5.resolve(dirname, "../package.json")
333
393
  ];
334
- const packageJsonPath = packageJsonPathCandidates.find((candidate) => fs4.existsSync(candidate));
394
+ const packageJsonPath = packageJsonPathCandidates.find((candidate) => fs5.existsSync(candidate));
335
395
  if (!packageJsonPath) {
336
396
  return "0.0.0";
337
397
  }
338
- const packageJson = JSON.parse(fs4.readFileSync(packageJsonPath, "utf8"));
398
+ const packageJson = JSON.parse(fs5.readFileSync(packageJsonPath, "utf8"));
339
399
  return packageJson.version ?? "0.0.0";
340
400
  }
341
401
 
package/dist/lightfish.js CHANGED
@@ -5,9 +5,9 @@ import cac from "cac";
5
5
  import { execa as execa3 } from "execa";
6
6
 
7
7
  // src/commands/create.ts
8
- import path3 from "path";
8
+ import path4 from "path";
9
9
  import { execa as execa2 } from "execa";
10
- import fs3 from "fs-extra";
10
+ import fs4 from "fs-extra";
11
11
  import prompts3 from "prompts";
12
12
 
13
13
  // src/utils/download-template.ts
@@ -148,6 +148,63 @@ function toPackageName(projectName) {
148
148
  return projectName.trim().toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
149
149
  }
150
150
 
151
+ // src/utils/replace-token.ts
152
+ import path3 from "path";
153
+ import fs3 from "fs-extra";
154
+ var SKIP_DIRS = /* @__PURE__ */ new Set([
155
+ ".git",
156
+ "node_modules",
157
+ "dist",
158
+ ".cache",
159
+ "coverage"
160
+ ]);
161
+ var SKIP_EXTS = /* @__PURE__ */ new Set([
162
+ ".png",
163
+ ".jpg",
164
+ ".jpeg",
165
+ ".gif",
166
+ ".svg",
167
+ ".ico",
168
+ ".webp",
169
+ ".woff",
170
+ ".woff2",
171
+ ".ttf",
172
+ ".eot",
173
+ ".otf",
174
+ ".mp4",
175
+ ".webm",
176
+ ".mp3",
177
+ ".wav",
178
+ ".zip",
179
+ ".gz",
180
+ ".tar",
181
+ ".7z",
182
+ ".lock"
183
+ ]);
184
+ async function replaceToken(targetDir, token, value) {
185
+ const entries = await fs3.readdir(targetDir);
186
+ for (const entry of entries) {
187
+ const fullPath = path3.join(targetDir, entry);
188
+ const stat = await fs3.stat(fullPath);
189
+ if (stat.isDirectory()) {
190
+ if (SKIP_DIRS.has(entry)) continue;
191
+ await replaceToken(fullPath, token, value);
192
+ continue;
193
+ }
194
+ const ext = path3.extname(entry).toLowerCase();
195
+ if (SKIP_EXTS.has(ext)) continue;
196
+ try {
197
+ const content = await fs3.readFile(fullPath, "utf8");
198
+ if (content.includes(token)) {
199
+ const replaced = content.replaceAll(token, value);
200
+ await fs3.writeFile(fullPath, replaced, "utf8");
201
+ console.log(` [replace] ${entry}`);
202
+ }
203
+ } catch {
204
+ }
205
+ }
206
+ }
207
+
151
208
  // src/utils/resolve-template.ts
152
209
  import prompts2 from "prompts";
153
210
 
@@ -218,13 +275,13 @@ async function resolveTemplateRepo(options) {
218
275
 
219
276
  // src/commands/create.ts
220
277
  async function patchPackageJsonName(targetDir, projectName) {
221
- const packageJsonPath = path3.join(targetDir, "package.json");
222
- if (!await fs3.pathExists(packageJsonPath)) {
278
+ const packageJsonPath = path4.join(targetDir, "package.json");
279
+ if (!await fs4.pathExists(packageJsonPath)) {
223
280
  throw new Error("\u6A21\u677F\u7F3A\u5C11 package.json");
224
281
  }
225
- const packageJson = await fs3.readJson(packageJsonPath);
282
+ const packageJson = await fs4.readJson(packageJsonPath);
226
283
  packageJson.name = toPackageName(projectName);
227
- await fs3.writeJson(packageJsonPath, packageJson, { spaces: 2 });
284
+ await fs4.writeJson(packageJsonPath, packageJson, { spaces: 2 });
228
285
  }
229
286
  async function resolveCreateOptions(options) {
230
287
  const cwd = options.cwd ?? process.cwd();
@@ -311,6 +368,9 @@ async function createProject(options = {}) {
311
368
  console.log("[create] \u5F00\u59CB\u4FEE\u6539 package.json...");
312
369
  await patchPackageJsonName(resolvedOptions.targetDir, resolvedOptions.projectName);
313
370
  console.log("[create] package.json \u4FEE\u6539\u5B8C\u6210");
371
+ console.log("[create] \u5F00\u59CB\u66FF\u6362\u9879\u76EE\u540D\u5360\u4F4D\u7B26...");
372
+ await replaceToken(resolvedOptions.targetDir, "__PROJECT_NAME__", resolvedOptions.projectName);
373
+ console.log("[create] \u5360\u4F4D\u7B26\u66FF\u6362\u5B8C\u6210");
314
374
  console.log("[create] \u5F00\u59CB\u5B89\u88C5\u4F9D\u8D56...");
315
375
  await maybeInstall(resolvedOptions);
316
376
  console.log("[create] \u4F9D\u8D56\u5B89\u88C5\u5B8C\u6210");
@@ -321,22 +381,22 @@ async function createProject(options = {}) {
321
381
  }
322
382
 
323
383
  // src/utils/package-info.ts
324
- import fs4 from "fs";
325
- import path4 from "path";
384
+ import fs5 from "fs";
385
+ import path5 from "path";
326
386
  import { fileURLToPath } from "url";
327
- var dirname = path4.dirname(fileURLToPath(import.meta.url));
387
+ var dirname = path5.dirname(fileURLToPath(import.meta.url));
328
388
  function readPackageVersion() {
329
389
  const packageJsonPathCandidates = [
330
390
  // 源码模式:src/utils/package-info.ts -> packages/cli/package.json
331
- path4.resolve(dirname, "../../package.json"),
391
+ path5.resolve(dirname, "../../package.json"),
332
392
  // 构建产物模式:dist/lightfish.js -> packages/cli/package.json
333
- path4.resolve(dirname, "../package.json")
393
+ path5.resolve(dirname, "../package.json")
334
394
  ];
335
- const packageJsonPath = packageJsonPathCandidates.find((candidate) => fs4.existsSync(candidate));
395
+ const packageJsonPath = packageJsonPathCandidates.find((candidate) => fs5.existsSync(candidate));
336
396
  if (!packageJsonPath) {
337
397
  return "0.0.0";
338
398
  }
339
- const packageJson = JSON.parse(fs4.readFileSync(packageJsonPath, "utf8"));
399
+ const packageJson = JSON.parse(fs5.readFileSync(packageJsonPath, "utf8"));
340
400
  return packageJson.version ?? "0.0.0";
341
401
  }
342
402
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightfish/cli",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",