create-esmx 3.0.0-rc.104
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/LICENSE +21 -0
- package/README.md +52 -0
- package/README.zh-CN.md +52 -0
- package/dist/cli.d.ts +5 -0
- package/dist/cli.integration.test.d.ts +1 -0
- package/dist/cli.integration.test.mjs +238 -0
- package/dist/cli.mjs +166 -0
- package/dist/create.d.ts +2 -0
- package/dist/create.mjs +6 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.mjs +2 -0
- package/dist/project.d.ts +5 -0
- package/dist/project.mjs +46 -0
- package/dist/project.test.d.ts +1 -0
- package/dist/project.test.mjs +155 -0
- package/dist/template.d.ts +17 -0
- package/dist/template.mjs +76 -0
- package/dist/template.test.d.ts +1 -0
- package/dist/template.test.mjs +106 -0
- package/dist/types.d.ts +30 -0
- package/dist/types.mjs +0 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.mjs +7 -0
- package/dist/utils/package-manager.d.ts +10 -0
- package/dist/utils/package-manager.mjs +49 -0
- package/dist/utils/package-manager.test.d.ts +4 -0
- package/dist/utils/package-manager.test.mjs +275 -0
- package/dist/utils/project-name.d.ts +48 -0
- package/dist/utils/project-name.mjs +42 -0
- package/dist/utils/project-name.test.d.ts +1 -0
- package/dist/utils/project-name.test.mjs +332 -0
- package/dist/utils/template.d.ts +19 -0
- package/dist/utils/template.mjs +8 -0
- package/dist/utils/template.test.d.ts +4 -0
- package/dist/utils/template.test.mjs +150 -0
- package/package.json +75 -0
- package/src/cli.integration.test.ts +289 -0
- package/src/cli.ts +214 -0
- package/src/create.ts +8 -0
- package/src/index.ts +3 -0
- package/src/project.test.ts +200 -0
- package/src/project.ts +75 -0
- package/src/template.test.ts +135 -0
- package/src/template.ts +117 -0
- package/src/types.ts +32 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/package-manager.test.ts +540 -0
- package/src/utils/package-manager.ts +92 -0
- package/src/utils/project-name.test.ts +441 -0
- package/src/utils/project-name.ts +101 -0
- package/src/utils/template.test.ts +234 -0
- package/src/utils/template.ts +34 -0
- package/template/react-csr/README.md +81 -0
- package/template/react-csr/package.json +29 -0
- package/template/react-csr/src/app.css +98 -0
- package/template/react-csr/src/app.tsx +26 -0
- package/template/react-csr/src/components/hello-world.css +48 -0
- package/template/react-csr/src/components/hello-world.tsx +29 -0
- package/template/react-csr/src/create-app.tsx +9 -0
- package/template/react-csr/src/entry.client.ts +13 -0
- package/template/react-csr/src/entry.node.ts +35 -0
- package/template/react-csr/src/entry.server.tsx +27 -0
- package/template/react-csr/tsconfig.json +27 -0
- package/template/react-ssr/README.md +81 -0
- package/template/react-ssr/package.json +29 -0
- package/template/react-ssr/src/app.css +98 -0
- package/template/react-ssr/src/app.tsx +26 -0
- package/template/react-ssr/src/components/hello-world.css +48 -0
- package/template/react-ssr/src/components/hello-world.tsx +29 -0
- package/template/react-ssr/src/create-app.tsx +9 -0
- package/template/react-ssr/src/entry.client.ts +13 -0
- package/template/react-ssr/src/entry.node.ts +32 -0
- package/template/react-ssr/src/entry.server.tsx +36 -0
- package/template/react-ssr/tsconfig.json +27 -0
- package/template/shared-modules/README.md +85 -0
- package/template/shared-modules/package.json +28 -0
- package/template/shared-modules/src/entry.client.ts +50 -0
- package/template/shared-modules/src/entry.node.ts +67 -0
- package/template/shared-modules/src/entry.server.ts +299 -0
- package/template/shared-modules/src/index.ts +3 -0
- package/template/shared-modules/src/vue/index.ts +1 -0
- package/template/shared-modules/src/vue2/index.ts +1 -0
- package/template/shared-modules/tsconfig.json +26 -0
- package/template/vue-csr/README.md +80 -0
- package/template/vue-csr/package.json +26 -0
- package/template/vue-csr/src/app.vue +127 -0
- package/template/vue-csr/src/components/hello-world.vue +77 -0
- package/template/vue-csr/src/create-app.ts +9 -0
- package/template/vue-csr/src/entry.client.ts +5 -0
- package/template/vue-csr/src/entry.node.ts +35 -0
- package/template/vue-csr/src/entry.server.ts +26 -0
- package/template/vue-csr/tsconfig.json +26 -0
- package/template/vue-ssr/README.md +80 -0
- package/template/vue-ssr/package.json +27 -0
- package/template/vue-ssr/src/app.vue +127 -0
- package/template/vue-ssr/src/components/hello-world.vue +77 -0
- package/template/vue-ssr/src/create-app.ts +9 -0
- package/template/vue-ssr/src/entry.client.ts +5 -0
- package/template/vue-ssr/src/entry.node.ts +37 -0
- package/template/vue-ssr/src/entry.server.ts +30 -0
- package/template/vue-ssr/tsconfig.json +26 -0
- package/template/vue2-csr/README.md +80 -0
- package/template/vue2-csr/package.json +26 -0
- package/template/vue2-csr/src/app.vue +127 -0
- package/template/vue2-csr/src/components/hello-world.vue +77 -0
- package/template/vue2-csr/src/create-app.ts +11 -0
- package/template/vue2-csr/src/entry.client.ts +5 -0
- package/template/vue2-csr/src/entry.node.ts +35 -0
- package/template/vue2-csr/src/entry.server.ts +26 -0
- package/template/vue2-csr/tsconfig.json +26 -0
- package/template/vue2-ssr/README.md +80 -0
- package/template/vue2-ssr/package.json +27 -0
- package/template/vue2-ssr/src/app.vue +127 -0
- package/template/vue2-ssr/src/components/hello-world.vue +77 -0
- package/template/vue2-ssr/src/create-app.ts +11 -0
- package/template/vue2-ssr/src/entry.client.ts +5 -0
- package/template/vue2-ssr/src/entry.node.ts +32 -0
- package/template/vue2-ssr/src/entry.server.ts +37 -0
- package/template/vue2-ssr/tsconfig.json +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Esmx Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://esmx.dev/logo.svg?t=2025" width="120" alt="Esmx Logo" />
|
|
3
|
+
<h1>create-esmx</h1>
|
|
4
|
+
|
|
5
|
+
<div>
|
|
6
|
+
<a href="https://www.npmjs.com/package/create-esmx">
|
|
7
|
+
<img src="https://img.shields.io/npm/v/create-esmx.svg" alt="npm version" />
|
|
8
|
+
</a>
|
|
9
|
+
<a href="https://github.com/esmnext/esmx/actions/workflows/build.yml">
|
|
10
|
+
<img src="https://github.com/esmnext/esmx/actions/workflows/build.yml/badge.svg" alt="Build" />
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://esmx.dev/coverage/">
|
|
13
|
+
<img src="https://img.shields.io/badge/coverage-live%20report-brightgreen" alt="Coverage Report" />
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://nodejs.org/">
|
|
16
|
+
<img src="https://img.shields.io/node/v/@esmx/core.svg" alt="node version" />
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://bundlephobia.com/package/create-esmx">
|
|
19
|
+
<img src="https://img.shields.io/bundlephobia/minzip/create-esmx" alt="size" />
|
|
20
|
+
</a>
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
<p>A scaffold tool for creating Esmx projects</p>
|
|
24
|
+
|
|
25
|
+
<p>
|
|
26
|
+
English | <a href="https://github.com/esmnext/esmx/blob/master/packages/create-esmx/README.zh-CN.md">中文</a>
|
|
27
|
+
</p>
|
|
28
|
+
</div>
|
|
29
|
+
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
- Interactive command-line interface
|
|
33
|
+
- Support for multiple project templates
|
|
34
|
+
- Complete TypeScript support
|
|
35
|
+
- Automatic project structure configuration
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# Using npm
|
|
41
|
+
npm create esmx@latest my-project
|
|
42
|
+
|
|
43
|
+
# Using yarn
|
|
44
|
+
yarn create esmx my-project
|
|
45
|
+
|
|
46
|
+
# Using pnpm
|
|
47
|
+
pnpm create esmx my-project
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
MIT © Esmx Team
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://esmx.dev/logo.svg?t=2025" width="120" alt="Esmx Logo" />
|
|
3
|
+
<h1>create-esmx</h1>
|
|
4
|
+
|
|
5
|
+
<div>
|
|
6
|
+
<a href="https://www.npmjs.com/package/create-esmx">
|
|
7
|
+
<img src="https://img.shields.io/npm/v/create-esmx.svg" alt="npm version" />
|
|
8
|
+
</a>
|
|
9
|
+
<a href="https://github.com/esmnext/esmx/actions/workflows/build.yml">
|
|
10
|
+
<img src="https://github.com/esmnext/esmx/actions/workflows/build.yml/badge.svg" alt="Build" />
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://esmx.dev/coverage/">
|
|
13
|
+
<img src="https://img.shields.io/badge/coverage-live%20report-brightgreen" alt="Coverage Report" />
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://nodejs.org/">
|
|
16
|
+
<img src="https://img.shields.io/node/v/@esmx/core.svg" alt="node version" />
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://bundlephobia.com/package/create-esmx">
|
|
19
|
+
<img src="https://img.shields.io/bundlephobia/minzip/create-esmx" alt="size" />
|
|
20
|
+
</a>
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
<p>用于创建 Esmx 项目的脚手架工具</p>
|
|
24
|
+
|
|
25
|
+
<p>
|
|
26
|
+
<a href="https://github.com/esmnext/esmx/blob/master/packages/create-esmx/README.md">English</a> | 中文
|
|
27
|
+
</p>
|
|
28
|
+
</div>
|
|
29
|
+
|
|
30
|
+
## 功能特性
|
|
31
|
+
|
|
32
|
+
- 交互式命令行界面
|
|
33
|
+
- 支持多种项目模板
|
|
34
|
+
- 完整的 TypeScript 支持
|
|
35
|
+
- 自动配置项目结构
|
|
36
|
+
|
|
37
|
+
## 安装使用
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# 使用 npm
|
|
41
|
+
npm create esmx@latest my-project
|
|
42
|
+
|
|
43
|
+
# 使用 yarn
|
|
44
|
+
yarn create esmx my-project
|
|
45
|
+
|
|
46
|
+
# 使用 pnpm
|
|
47
|
+
pnpm create esmx my-project
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## 许可证
|
|
51
|
+
|
|
52
|
+
MIT © Esmx Team
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
6
|
+
import { cli } from "./cli.mjs";
|
|
7
|
+
import { getAvailableTemplates } from "./template.mjs";
|
|
8
|
+
async function createTempDir(prefix = "esmx-test-") {
|
|
9
|
+
return mkdtemp(join(tmpdir(), prefix));
|
|
10
|
+
}
|
|
11
|
+
async function cleanupTempDir(tempDir) {
|
|
12
|
+
try {
|
|
13
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.warn(`Failed to cleanup temp directory: ${tempDir}`, error);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function verifyProjectStructure(projectPath, projectName) {
|
|
19
|
+
expect(existsSync(projectPath)).toBe(true);
|
|
20
|
+
expect(existsSync(join(projectPath, "src"))).toBe(true);
|
|
21
|
+
const requiredFiles = [
|
|
22
|
+
"package.json",
|
|
23
|
+
"tsconfig.json",
|
|
24
|
+
"README.md",
|
|
25
|
+
"src/entry.node.ts"
|
|
26
|
+
];
|
|
27
|
+
for (const file of requiredFiles) {
|
|
28
|
+
expect(existsSync(join(projectPath, file))).toBe(true);
|
|
29
|
+
if (!existsSync(join(projectPath, file))) {
|
|
30
|
+
throw new Error(`Missing required file: ${file}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const entryClientTs = join(projectPath, "src/entry.client.ts");
|
|
34
|
+
expect(existsSync(entryClientTs)).toBe(true);
|
|
35
|
+
if (!existsSync(entryClientTs)) {
|
|
36
|
+
throw new Error("Missing required file: src/entry.client.ts");
|
|
37
|
+
}
|
|
38
|
+
const packageJson = JSON.parse(
|
|
39
|
+
readFileSync(join(projectPath, "package.json"), "utf-8")
|
|
40
|
+
);
|
|
41
|
+
const typeCheckCommands = ["vue-tsc --noEmit", "tsc --noEmit"];
|
|
42
|
+
const typeGenCommands = [
|
|
43
|
+
"vue-tsc --declaration --emitDeclarationOnly --noEmit false --outDir dist/src && tsc-alias -p tsconfig.json --outDir dist/src",
|
|
44
|
+
"tsc --declaration --emitDeclarationOnly --outDir dist/src && tsc-alias -p tsconfig.json --outDir dist/src"
|
|
45
|
+
];
|
|
46
|
+
expect(packageJson).toMatchObject({
|
|
47
|
+
name: projectName,
|
|
48
|
+
type: "module",
|
|
49
|
+
private: true,
|
|
50
|
+
scripts: {
|
|
51
|
+
dev: "esmx dev",
|
|
52
|
+
build: "esmx build",
|
|
53
|
+
preview: "esmx preview",
|
|
54
|
+
start: "esmx start"
|
|
55
|
+
},
|
|
56
|
+
dependencies: {
|
|
57
|
+
"@esmx/core": expect.any(String)
|
|
58
|
+
},
|
|
59
|
+
devDependencies: {
|
|
60
|
+
typescript: expect.any(String),
|
|
61
|
+
"@types/node": expect.any(String),
|
|
62
|
+
"tsc-alias": expect.any(String)
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
expect(packageJson.scripts["lint:type"]).toBeOneOf(typeCheckCommands);
|
|
66
|
+
expect(packageJson.scripts["build:type"]).toBeOneOf(typeGenCommands);
|
|
67
|
+
const tsconfig = JSON.parse(
|
|
68
|
+
readFileSync(join(projectPath, "tsconfig.json"), "utf-8")
|
|
69
|
+
);
|
|
70
|
+
expect(tsconfig).toMatchObject({
|
|
71
|
+
compilerOptions: {
|
|
72
|
+
module: "ESNext",
|
|
73
|
+
moduleResolution: "node",
|
|
74
|
+
target: "ESNext",
|
|
75
|
+
strict: true,
|
|
76
|
+
baseUrl: ".",
|
|
77
|
+
paths: {
|
|
78
|
+
[`${projectName}/src/*`]: ["./src/*"],
|
|
79
|
+
[`${projectName}/*`]: ["./*"]
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
include: ["src"],
|
|
83
|
+
exclude: ["dist", "node_modules"]
|
|
84
|
+
});
|
|
85
|
+
const readmeContent = readFileSync(join(projectPath, "README.md"), "utf-8");
|
|
86
|
+
expect(readmeContent.length).toBeGreaterThan(0);
|
|
87
|
+
expect(readmeContent).toContain(projectName);
|
|
88
|
+
}
|
|
89
|
+
describe("create-esmx CLI integration tests", () => {
|
|
90
|
+
let tmpDir;
|
|
91
|
+
beforeEach(async () => {
|
|
92
|
+
tmpDir = await createTempDir();
|
|
93
|
+
});
|
|
94
|
+
afterEach(async () => {
|
|
95
|
+
await cleanupTempDir(tmpDir);
|
|
96
|
+
});
|
|
97
|
+
it("should create project with all available templates", async () => {
|
|
98
|
+
const templates = getAvailableTemplates();
|
|
99
|
+
expect(templates.length).toBeGreaterThan(0);
|
|
100
|
+
for (const template of templates) {
|
|
101
|
+
const projectName = `test-${template.folder}`;
|
|
102
|
+
const projectPath = join(tmpDir, projectName);
|
|
103
|
+
await cli({
|
|
104
|
+
argv: [projectName, "--template", template.folder],
|
|
105
|
+
cwd: tmpDir,
|
|
106
|
+
userAgent: "npm/test"
|
|
107
|
+
});
|
|
108
|
+
await verifyProjectStructure(projectPath, projectName);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
it("should handle --force parameter correctly", async () => {
|
|
112
|
+
const projectPath = join(tmpDir, "test-project");
|
|
113
|
+
await cli({
|
|
114
|
+
argv: ["test-project", "--template", "vue2-csr"],
|
|
115
|
+
cwd: tmpDir,
|
|
116
|
+
userAgent: "npm/test"
|
|
117
|
+
});
|
|
118
|
+
expect(existsSync(join(projectPath, "package.json"))).toBe(true);
|
|
119
|
+
await cli({
|
|
120
|
+
argv: ["test-project", "--template", "vue2-csr", "--force"],
|
|
121
|
+
cwd: tmpDir,
|
|
122
|
+
userAgent: "npm/test"
|
|
123
|
+
});
|
|
124
|
+
expect(existsSync(join(projectPath, "package.json"))).toBe(true);
|
|
125
|
+
expect(existsSync(join(projectPath, "src"))).toBe(true);
|
|
126
|
+
expect(existsSync(join(projectPath, "src/entry.client.ts"))).toBe(true);
|
|
127
|
+
expect(existsSync(join(projectPath, "src/entry.node.ts"))).toBe(true);
|
|
128
|
+
expect(existsSync(join(projectPath, "src/entry.server.ts"))).toBe(true);
|
|
129
|
+
expect(existsSync(join(projectPath, "src/create-app.ts"))).toBe(true);
|
|
130
|
+
});
|
|
131
|
+
it("should show help information", async () => {
|
|
132
|
+
const originalLog = console.log;
|
|
133
|
+
const logOutput = [];
|
|
134
|
+
console.log = (...args) => {
|
|
135
|
+
logOutput.push(args.join(" "));
|
|
136
|
+
};
|
|
137
|
+
try {
|
|
138
|
+
await cli({
|
|
139
|
+
argv: ["--help"],
|
|
140
|
+
cwd: tmpDir,
|
|
141
|
+
userAgent: "npm/test"
|
|
142
|
+
});
|
|
143
|
+
const output = logOutput.join("\n");
|
|
144
|
+
expect(output).toContain("Usage");
|
|
145
|
+
expect(output).toContain("Options");
|
|
146
|
+
expect(output).toContain("Examples");
|
|
147
|
+
} finally {
|
|
148
|
+
console.log = originalLog;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
it("should show version information", async () => {
|
|
152
|
+
const originalLog = console.log;
|
|
153
|
+
const logOutput = [];
|
|
154
|
+
console.log = (...args) => {
|
|
155
|
+
logOutput.push(args.join(" "));
|
|
156
|
+
};
|
|
157
|
+
try {
|
|
158
|
+
await cli({
|
|
159
|
+
argv: ["--version"],
|
|
160
|
+
cwd: tmpDir,
|
|
161
|
+
userAgent: "npm/test"
|
|
162
|
+
});
|
|
163
|
+
const output = logOutput.join("\n");
|
|
164
|
+
expect(output).toMatch(/^\d+\.\d+\.\d+/);
|
|
165
|
+
} finally {
|
|
166
|
+
console.log = originalLog;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
it("should handle creating directory when target directory does not exist", async () => {
|
|
170
|
+
const projectPath = join(tmpDir, "non-existent-parent", "test-project");
|
|
171
|
+
await cli({
|
|
172
|
+
argv: [
|
|
173
|
+
"non-existent-parent/test-project",
|
|
174
|
+
"--template",
|
|
175
|
+
"vue2-csr"
|
|
176
|
+
],
|
|
177
|
+
cwd: tmpDir,
|
|
178
|
+
userAgent: "npm/test"
|
|
179
|
+
});
|
|
180
|
+
expect(existsSync(projectPath)).toBe(true);
|
|
181
|
+
expect(existsSync(join(projectPath, "package.json"))).toBe(true);
|
|
182
|
+
expect(existsSync(join(projectPath, "src"))).toBe(true);
|
|
183
|
+
});
|
|
184
|
+
it("should handle force overwrite for non-empty directory", async () => {
|
|
185
|
+
const projectPath = join(tmpDir, "test-project");
|
|
186
|
+
await mkdir(projectPath, { recursive: true });
|
|
187
|
+
await writeFile(
|
|
188
|
+
join(projectPath, "existing-file.txt"),
|
|
189
|
+
"existing content"
|
|
190
|
+
);
|
|
191
|
+
await cli({
|
|
192
|
+
argv: ["test-project", "--template", "vue2-csr", "--force"],
|
|
193
|
+
cwd: tmpDir,
|
|
194
|
+
userAgent: "npm/test"
|
|
195
|
+
});
|
|
196
|
+
expect(existsSync(join(projectPath, "package.json"))).toBe(true);
|
|
197
|
+
expect(existsSync(join(projectPath, "src"))).toBe(true);
|
|
198
|
+
});
|
|
199
|
+
it("should handle force overwrite in current directory", async () => {
|
|
200
|
+
const testFile = join(tmpDir, "existing-file.txt");
|
|
201
|
+
await writeFile(testFile, "existing content");
|
|
202
|
+
await cli({
|
|
203
|
+
argv: [".", "--template", "vue2-csr", "--force"],
|
|
204
|
+
cwd: tmpDir,
|
|
205
|
+
userAgent: "npm/test"
|
|
206
|
+
});
|
|
207
|
+
expect(existsSync(join(tmpDir, "package.json"))).toBe(true);
|
|
208
|
+
expect(existsSync(join(tmpDir, "src"))).toBe(true);
|
|
209
|
+
expect(existsSync(join(tmpDir, "src/entry.client.ts"))).toBe(true);
|
|
210
|
+
});
|
|
211
|
+
it('should create project in current directory when target is "."', async () => {
|
|
212
|
+
await cli({
|
|
213
|
+
argv: [".", "--template", "vue2-csr"],
|
|
214
|
+
cwd: tmpDir,
|
|
215
|
+
userAgent: "npm/test"
|
|
216
|
+
});
|
|
217
|
+
expect(existsSync(join(tmpDir, "package.json"))).toBe(true);
|
|
218
|
+
expect(existsSync(join(tmpDir, "src"))).toBe(true);
|
|
219
|
+
expect(existsSync(join(tmpDir, "src/entry.client.ts"))).toBe(true);
|
|
220
|
+
});
|
|
221
|
+
it("should handle various project name formats", async () => {
|
|
222
|
+
const testCases = [
|
|
223
|
+
"simple-name",
|
|
224
|
+
"nested/project-name",
|
|
225
|
+
"deep/nested/project-name"
|
|
226
|
+
];
|
|
227
|
+
for (const projectName of testCases) {
|
|
228
|
+
const projectPath = join(tmpDir, projectName);
|
|
229
|
+
await cli({
|
|
230
|
+
argv: [projectName, "--template", "vue2-csr"],
|
|
231
|
+
cwd: tmpDir,
|
|
232
|
+
userAgent: "npm/test"
|
|
233
|
+
});
|
|
234
|
+
expect(existsSync(projectPath)).toBe(true);
|
|
235
|
+
expect(existsSync(join(projectPath, "package.json"))).toBe(true);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
});
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cancel,
|
|
3
|
+
intro,
|
|
4
|
+
isCancel,
|
|
5
|
+
note,
|
|
6
|
+
outro,
|
|
7
|
+
select,
|
|
8
|
+
text
|
|
9
|
+
} from "@clack/prompts";
|
|
10
|
+
import minimist from "minimist";
|
|
11
|
+
import color from "picocolors";
|
|
12
|
+
import { createProjectFromTemplate } from "./project.mjs";
|
|
13
|
+
import { getAvailableTemplates, getEsmxVersion } from "./template.mjs";
|
|
14
|
+
import { formatProjectName, getCommand } from "./utils/index.mjs";
|
|
15
|
+
function showHelp(userAgent) {
|
|
16
|
+
const createCmd = getCommand("create", userAgent);
|
|
17
|
+
console.log(`
|
|
18
|
+
${color.reset(color.bold(color.blue("\u{1F680} Create Esmx Project")))}
|
|
19
|
+
|
|
20
|
+
${color.bold("Usage:")}
|
|
21
|
+
${createCmd} [project-name]
|
|
22
|
+
${createCmd} [project-name] [options]
|
|
23
|
+
|
|
24
|
+
${color.bold("Options:")}
|
|
25
|
+
-t, --template <template> Template to use (default: vue2-csr)
|
|
26
|
+
-n, --name <name> Project name or path
|
|
27
|
+
-f, --force Force overwrite existing directory
|
|
28
|
+
-h, --help Show help information
|
|
29
|
+
-v, --version Show version number
|
|
30
|
+
|
|
31
|
+
${color.bold("Examples:")}
|
|
32
|
+
${createCmd} my-project
|
|
33
|
+
${createCmd} my-project -t vue2-csr
|
|
34
|
+
${createCmd} my-project --force
|
|
35
|
+
${createCmd} . -f -t vue2-csr
|
|
36
|
+
|
|
37
|
+
${color.bold("Available Templates:")}
|
|
38
|
+
${getAvailableTemplates().map((t) => ` ${t.folder.padEnd(25)} ${t.description}`).join("\n")}
|
|
39
|
+
|
|
40
|
+
For more information, visit: ${color.cyan("https://esmx.dev")}
|
|
41
|
+
`);
|
|
42
|
+
}
|
|
43
|
+
async function getProjectName(argName, positionalName) {
|
|
44
|
+
const providedName = argName || positionalName;
|
|
45
|
+
if (providedName) {
|
|
46
|
+
return providedName;
|
|
47
|
+
}
|
|
48
|
+
const projectName = await text({
|
|
49
|
+
message: "Project name or path:",
|
|
50
|
+
placeholder: "my-esmx-project",
|
|
51
|
+
validate: (value) => {
|
|
52
|
+
if (!value.trim()) {
|
|
53
|
+
return "Project name or path is required";
|
|
54
|
+
}
|
|
55
|
+
if (!/^[a-zA-Z0-9_./@-]+$/.test(value.trim())) {
|
|
56
|
+
return "Project name or path should only contain letters, numbers, hyphens, underscores, dots, and slashes";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return String(projectName).trim();
|
|
61
|
+
}
|
|
62
|
+
async function getTemplateType(argTemplate) {
|
|
63
|
+
const availableTemplates = getAvailableTemplates();
|
|
64
|
+
if (argTemplate && availableTemplates.some((t) => t.folder === argTemplate)) {
|
|
65
|
+
return argTemplate;
|
|
66
|
+
}
|
|
67
|
+
const options = availableTemplates.map((t) => ({
|
|
68
|
+
label: color.reset(color.gray(`${t.folder} - `) + color.bold(t.name)),
|
|
69
|
+
value: t.folder,
|
|
70
|
+
hint: t.description
|
|
71
|
+
}));
|
|
72
|
+
const template = await select({
|
|
73
|
+
message: "Select a template:",
|
|
74
|
+
options
|
|
75
|
+
});
|
|
76
|
+
return String(template);
|
|
77
|
+
}
|
|
78
|
+
export async function cli(options = {}) {
|
|
79
|
+
const { argv, cwd, userAgent, version } = options;
|
|
80
|
+
const commandLineArgs = argv || process.argv.slice(2);
|
|
81
|
+
const workingDir = cwd || process.cwd();
|
|
82
|
+
const parsedArgs = minimist(commandLineArgs, {
|
|
83
|
+
string: ["template", "name"],
|
|
84
|
+
boolean: ["help", "version", "force"],
|
|
85
|
+
alias: {
|
|
86
|
+
t: "template",
|
|
87
|
+
n: "name",
|
|
88
|
+
f: "force",
|
|
89
|
+
h: "help",
|
|
90
|
+
v: "version"
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
if (parsedArgs.help) {
|
|
94
|
+
showHelp(userAgent);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (parsedArgs.version) {
|
|
98
|
+
console.log(getEsmxVersion());
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
console.log();
|
|
102
|
+
intro(
|
|
103
|
+
color.reset(
|
|
104
|
+
color.bold(color.blue("\u{1F680} Welcome to Esmx Project Creator!"))
|
|
105
|
+
)
|
|
106
|
+
);
|
|
107
|
+
const projectNameInput = await getProjectName(
|
|
108
|
+
parsedArgs.name,
|
|
109
|
+
parsedArgs._[0]
|
|
110
|
+
);
|
|
111
|
+
if (isCancel(projectNameInput)) {
|
|
112
|
+
cancel("Operation cancelled");
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
let name;
|
|
116
|
+
let root;
|
|
117
|
+
if (parsedArgs.name && parsedArgs._[0]) {
|
|
118
|
+
name = parsedArgs.name;
|
|
119
|
+
root = formatProjectName(parsedArgs._[0], workingDir).root;
|
|
120
|
+
} else {
|
|
121
|
+
const result = formatProjectName(projectNameInput, workingDir);
|
|
122
|
+
name = result.name;
|
|
123
|
+
root = result.root;
|
|
124
|
+
}
|
|
125
|
+
const templateType = await getTemplateType(parsedArgs.template);
|
|
126
|
+
if (isCancel(templateType)) {
|
|
127
|
+
cancel("Operation cancelled");
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const installCommand = getCommand("install", userAgent);
|
|
131
|
+
const devCommand = getCommand("dev", userAgent);
|
|
132
|
+
const buildCommand = getCommand("build", userAgent);
|
|
133
|
+
const startCommand = getCommand("start", userAgent);
|
|
134
|
+
const buildTypeCommand = getCommand("build:type", userAgent);
|
|
135
|
+
const lintTypeCommand = getCommand("lint:type", userAgent);
|
|
136
|
+
await createProjectFromTemplate(
|
|
137
|
+
root,
|
|
138
|
+
templateType,
|
|
139
|
+
workingDir,
|
|
140
|
+
parsedArgs.force,
|
|
141
|
+
{
|
|
142
|
+
projectName: name,
|
|
143
|
+
esmxVersion: version || getEsmxVersion(),
|
|
144
|
+
installCommand,
|
|
145
|
+
devCommand,
|
|
146
|
+
buildCommand,
|
|
147
|
+
startCommand,
|
|
148
|
+
buildTypeCommand,
|
|
149
|
+
lintTypeCommand
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
const installCmd = installCommand;
|
|
153
|
+
const devCmd = devCommand;
|
|
154
|
+
const targetDirForDisplay = projectNameInput === "." ? "." : projectNameInput;
|
|
155
|
+
const steps = [
|
|
156
|
+
projectNameInput !== "." ? `cd ${targetDirForDisplay}` : null,
|
|
157
|
+
installCmd,
|
|
158
|
+
`git init ${color.gray("(optional)")}`,
|
|
159
|
+
devCmd
|
|
160
|
+
].filter(Boolean);
|
|
161
|
+
const nextSteps = steps.map((step, index) => {
|
|
162
|
+
return color.reset(`${index + 1}. ${color.cyan(step)}`);
|
|
163
|
+
});
|
|
164
|
+
note(nextSteps.join("\n"), "Next steps");
|
|
165
|
+
outro(color.reset(color.green("Happy coding! \u{1F389}")));
|
|
166
|
+
}
|
package/dist/create.d.ts
ADDED
package/dist/create.mjs
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
package/dist/project.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { existsSync, mkdirSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { cancel, confirm, isCancel } from "@clack/prompts";
|
|
5
|
+
import { copyTemplateFiles, isDirectoryEmpty } from "./template.mjs";
|
|
6
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
export async function createProjectFromTemplate(targetDir, templateType, workingDir, force, variables) {
|
|
8
|
+
const templatePath = resolve(__dirname, "../template", templateType);
|
|
9
|
+
const targetPath = isAbsolute(targetDir) ? targetDir : targetDir === "." ? workingDir : resolve(workingDir, targetDir);
|
|
10
|
+
if (!existsSync(templatePath)) {
|
|
11
|
+
throw new Error(`Template "${templateType}" not found`);
|
|
12
|
+
}
|
|
13
|
+
if (targetDir !== "." && existsSync(targetPath)) {
|
|
14
|
+
if (!isDirectoryEmpty(targetPath)) {
|
|
15
|
+
if (!force) {
|
|
16
|
+
const shouldOverwrite = await confirm({
|
|
17
|
+
message: `Directory "${targetDir}" is not empty. Do you want to overwrite it?`
|
|
18
|
+
});
|
|
19
|
+
if (isCancel(shouldOverwrite)) {
|
|
20
|
+
cancel("Operation cancelled");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!shouldOverwrite) {
|
|
24
|
+
throw new Error("Operation cancelled by user");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
} else if (targetDir !== ".") {
|
|
29
|
+
mkdirSync(targetPath, { recursive: true });
|
|
30
|
+
}
|
|
31
|
+
if (targetDir === "." && !isDirectoryEmpty(targetPath)) {
|
|
32
|
+
if (!force) {
|
|
33
|
+
const shouldOverwrite = await confirm({
|
|
34
|
+
message: "Current directory is not empty. Do you want to overwrite existing files?"
|
|
35
|
+
});
|
|
36
|
+
if (isCancel(shouldOverwrite)) {
|
|
37
|
+
cancel("Operation cancelled");
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!shouldOverwrite) {
|
|
41
|
+
throw new Error("Operation cancelled by user");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
copyTemplateFiles(templatePath, targetPath, variables);
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|