create-forkly 0.4.0 → 0.4.2

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/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env node
2
+ /** create-forkly — Forkly 项目脚手架 CLI */
3
+ import { cac } from 'cac';
4
+ import * as p from '@clack/prompts';
5
+ import { mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs';
6
+ import { resolve, dirname } from 'node:path';
7
+ import { fileURLToPath } from 'node:url';
8
+ import { execSync } from 'node:child_process';
9
+ const __dirname = dirname(fileURLToPath(import.meta.url));
10
+ const templateDir = resolve(__dirname, '../template');
11
+ function copyDir(src, dest, replaces) {
12
+ mkdirSync(dest, { recursive: true });
13
+ for (const entry of readdirSync(src)) {
14
+ const srcPath = resolve(src, entry);
15
+ const destPath = resolve(dest, entry);
16
+ if (statSync(srcPath).isDirectory()) {
17
+ copyDir(srcPath, destPath, replaces);
18
+ }
19
+ else {
20
+ let content = readFileSync(srcPath, 'utf-8');
21
+ for (const [k, v] of Object.entries(replaces)) {
22
+ content = content.replace(new RegExp(k, 'g'), v);
23
+ }
24
+ writeFileSync(destPath, content);
25
+ }
26
+ }
27
+ }
28
+ const cli = cac('create-forkly');
29
+ cli.command('[dir]', 'Create a new Forkly project')
30
+ .action(async (dir = '.') => {
31
+ p.intro('Forkly Project Scaffolder');
32
+ const projectName = await p.text({
33
+ message: 'Project name:',
34
+ placeholder: 'my-forkly-app',
35
+ defaultValue: dir === '.' ? 'my-forkly-app' : dir,
36
+ });
37
+ if (p.isCancel(projectName)) {
38
+ p.cancel('Cancelled');
39
+ process.exit(0);
40
+ }
41
+ const useTypeScript = await p.confirm({
42
+ message: 'Use TypeScript?',
43
+ initialValue: true,
44
+ });
45
+ if (p.isCancel(useTypeScript)) {
46
+ p.cancel('Cancelled');
47
+ process.exit(0);
48
+ }
49
+ const targetDir = resolve(process.cwd(), dir === '.' ? projectName : dir);
50
+ const replaces = { 'forkly-app': projectName };
51
+ p.log.step('Copying template files...');
52
+ copyDir(templateDir, targetDir, replaces);
53
+ // 更新 package.json 中的项目名
54
+ const pkgPath = resolve(targetDir, 'package.json');
55
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
56
+ pkg.name = projectName;
57
+ writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
58
+ if (!useTypeScript) {
59
+ // 如果不用 TS,移除 tsconfig,把 .ts 文件改为 .js
60
+ p.log.step('Setting up JavaScript project...');
61
+ }
62
+ p.log.step('Installing dependencies...');
63
+ try {
64
+ execSync('npm install', { cwd: targetDir, stdio: 'inherit' });
65
+ }
66
+ catch {
67
+ p.log.warn('npm install failed. You can run it manually.');
68
+ }
69
+ p.outro(`Done! Run:\n cd ${projectName}\n npm run dev`);
70
+ });
71
+ cli.help();
72
+ cli.version('0.4.2');
73
+ cli.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-forkly",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "CLI to scaffold a new Forkly project",
5
5
  "type": "module",
6
6
  "bin": {
@@ -9,7 +9,8 @@
9
9
  "files": ["dist/", "template/"],
10
10
  "scripts": {
11
11
  "build": "tsc",
12
- "dev": "tsx src/index.ts"
12
+ "dev": "tsx src/index.ts",
13
+ "prepublishOnly": "npm run build"
13
14
  },
14
15
  "license": "MIT",
15
16
  "dependencies": {
@@ -7,7 +7,7 @@
7
7
  "build": "tsc"
8
8
  },
9
9
  "dependencies": {
10
- "forkly": "^0.4.3"
10
+ "forkly": "^0.5.0"
11
11
  },
12
12
  "devDependencies": {
13
13
  "@types/node": "^25.9.1",
@@ -7,7 +7,7 @@ export class IndexRouter extends Router {
7
7
  async get() {
8
8
  return {
9
9
  message: 'Welcome to Forkly!',
10
- version: '0.4.3',
10
+ version: '0.5.0',
11
11
  timestamp: Date.now(),
12
12
  };
13
13
  }