create-glosc 0.1.0
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 +66 -0
- package/bin/index.js +12 -0
- package/dist/index.js +245 -0
- package/dist/scaffold.js +69 -0
- package/dist/templates.js +123 -0
- package/package.json +45 -0
- package/src/index.ts +271 -0
- package/src/scaffold.ts +44 -0
- package/src/templates.ts +182 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 glosc-ai
|
|
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,66 @@
|
|
|
1
|
+
# Glosc CLI
|
|
2
|
+
|
|
3
|
+
### 使用方法
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm create glosc@latest
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
也支持直接传入项目名:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm create glosc@latest <your-project-name>
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
Project name: <your-project-name>
|
|
17
|
+
Description: A brief description of your project
|
|
18
|
+
Author: Your Name
|
|
19
|
+
Use Language: Python / TypeScript
|
|
20
|
+
Main File Name: main.py / index.ts
|
|
21
|
+
Readme: Y / N
|
|
22
|
+
License: MIT
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Python结构
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
|
|
30
|
+
<your-project-name>/
|
|
31
|
+
├── src/ # 源代码目录
|
|
32
|
+
│ ├── main.py # 主程序文件 (Python)
|
|
33
|
+
│ ├── pyproject.toml # 项目配置
|
|
34
|
+
│ └── requirements.txt # 依赖文件
|
|
35
|
+
├── config.yml # 配置文件
|
|
36
|
+
├── README.md # 项目说明文件
|
|
37
|
+
└── LICENSE # 许可证文件
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### TypeScript结构
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
<your-project-name>/
|
|
45
|
+
├── src/ # 源代码目录
|
|
46
|
+
│ ├── index.ts # 主程序文件 (TypeScript)
|
|
47
|
+
│ └── package.json # 依赖文件
|
|
48
|
+
├── config.yml # 配置文件
|
|
49
|
+
├── README.md # 项目说明文件
|
|
50
|
+
└── LICENSE # 许可证文件
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 本地开发(维护此 CLI)
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
npm install
|
|
57
|
+
npm run build
|
|
58
|
+
node bin/index.js
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
非交互模式(方便 CI/自测,不会卡在交互输入):
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
node bin/index.js my-app --defaults --language python
|
|
65
|
+
node bin/index.js my-app --defaults --language typescript
|
|
66
|
+
```
|
package/bin/index.js
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
const prompts_1 = __importDefault(require("prompts"));
|
|
40
|
+
const path = __importStar(require("node:path"));
|
|
41
|
+
const scaffold_1 = require("./scaffold");
|
|
42
|
+
function parseArgs(argv) {
|
|
43
|
+
const result = {
|
|
44
|
+
defaults: false,
|
|
45
|
+
language: undefined,
|
|
46
|
+
mainFileName: undefined,
|
|
47
|
+
description: undefined,
|
|
48
|
+
author: undefined,
|
|
49
|
+
readme: undefined,
|
|
50
|
+
license: undefined,
|
|
51
|
+
projectName: undefined,
|
|
52
|
+
};
|
|
53
|
+
const args = Array.from(argv);
|
|
54
|
+
for (let i = 0; i < args.length; i++) {
|
|
55
|
+
const a = args[i];
|
|
56
|
+
if (!a)
|
|
57
|
+
continue;
|
|
58
|
+
if (a === "--defaults" || a === "--yes" || a === "-y") {
|
|
59
|
+
result.defaults = true;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (a === "--no-readme") {
|
|
63
|
+
result.readme = false;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (a === "--readme") {
|
|
67
|
+
result.readme = true;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (a === "--no-license") {
|
|
71
|
+
result.license = false;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (a === "--license") {
|
|
75
|
+
result.license = true;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (a === "--language" && i + 1 < args.length) {
|
|
79
|
+
result.language = args[++i];
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (a === "--main" && i + 1 < args.length) {
|
|
83
|
+
result.mainFileName = args[++i];
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (a === "--description" && i + 1 < args.length) {
|
|
87
|
+
result.description = args[++i];
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (a === "--author" && i + 1 < args.length) {
|
|
91
|
+
result.author = args[++i];
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (!a.startsWith("-") && !result.projectName) {
|
|
95
|
+
result.projectName = a;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
function normalizeLanguage(value) {
|
|
102
|
+
const v = String(value || "")
|
|
103
|
+
.trim()
|
|
104
|
+
.toLowerCase();
|
|
105
|
+
if (v === "py" || v === "python")
|
|
106
|
+
return "python";
|
|
107
|
+
if (v === "ts" || v === "typescript")
|
|
108
|
+
return "typescript";
|
|
109
|
+
if (v === "js" || v === "javascript") {
|
|
110
|
+
throw new Error("JavaScript template has been removed. Use --language typescript (or omit --language) instead.");
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
function normalizeMainFileName(language, mainFileNameRaw) {
|
|
115
|
+
const trimmed = String(mainFileNameRaw || "").trim();
|
|
116
|
+
const defaultExt = language === "python" ? ".py" : ".ts";
|
|
117
|
+
const base = trimmed.length > 0
|
|
118
|
+
? trimmed
|
|
119
|
+
: language === "python"
|
|
120
|
+
? "main.py"
|
|
121
|
+
: "index.ts";
|
|
122
|
+
if (path.extname(base))
|
|
123
|
+
return base;
|
|
124
|
+
return `${base}${defaultExt}`;
|
|
125
|
+
}
|
|
126
|
+
async function run() {
|
|
127
|
+
const argv = process.argv.slice(2);
|
|
128
|
+
const args = parseArgs(argv);
|
|
129
|
+
if (args.defaults) {
|
|
130
|
+
if (!args.projectName) {
|
|
131
|
+
throw new Error("Project name is required (e.g. `npm create glosc-cli@latest my-app -- --defaults`)");
|
|
132
|
+
}
|
|
133
|
+
const language = normalizeLanguage(args.language) || "typescript";
|
|
134
|
+
const options = {
|
|
135
|
+
projectName: String(args.projectName).trim(),
|
|
136
|
+
description: String(args.description || "A brief description of your project").trim(),
|
|
137
|
+
author: String(args.author || "Your Name").trim(),
|
|
138
|
+
language,
|
|
139
|
+
mainFileName: normalizeMainFileName(language, args.mainFileName),
|
|
140
|
+
readme: args.readme !== undefined ? Boolean(args.readme) : true,
|
|
141
|
+
license: args.license !== undefined ? Boolean(args.license) : true,
|
|
142
|
+
};
|
|
143
|
+
await (0, scaffold_1.scaffoldProject)(options);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
// allow `npm create ... <name>` to prefill
|
|
147
|
+
prompts_1.default.override({
|
|
148
|
+
projectName: args.projectName,
|
|
149
|
+
});
|
|
150
|
+
let selectedLanguage;
|
|
151
|
+
const response = await (0, prompts_1.default)([
|
|
152
|
+
{
|
|
153
|
+
type: "text",
|
|
154
|
+
name: "projectName",
|
|
155
|
+
message: "Project name:",
|
|
156
|
+
validate: (value) => {
|
|
157
|
+
const name = String(value || "").trim();
|
|
158
|
+
if (!name)
|
|
159
|
+
return "Project name is required";
|
|
160
|
+
if (name.includes("/") || name.includes("\\"))
|
|
161
|
+
return "Project name cannot include path separators";
|
|
162
|
+
return true;
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
type: "text",
|
|
167
|
+
name: "description",
|
|
168
|
+
message: "Description:",
|
|
169
|
+
initial: "A brief description of your project",
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
type: "text",
|
|
173
|
+
name: "author",
|
|
174
|
+
message: "Author:",
|
|
175
|
+
initial: "Your Name",
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
type: "select",
|
|
179
|
+
name: "language",
|
|
180
|
+
message: "Use Language:",
|
|
181
|
+
choices: [
|
|
182
|
+
{ title: "Python", value: "python" },
|
|
183
|
+
{ title: "TypeScript", value: "typescript" },
|
|
184
|
+
],
|
|
185
|
+
onState: (state) => {
|
|
186
|
+
selectedLanguage = state.value;
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
type: (prev) => (prev ? "text" : null),
|
|
191
|
+
name: "mainFileName",
|
|
192
|
+
message: "Main File Name:",
|
|
193
|
+
initial: (prev) => prev === "python" ? "main.py" : "index.ts",
|
|
194
|
+
validate: (value, values) => {
|
|
195
|
+
const name = String(value || "").trim();
|
|
196
|
+
if (!name)
|
|
197
|
+
return "Main file name is required";
|
|
198
|
+
const ext = path.extname(name);
|
|
199
|
+
const currentLanguage = values?.language || selectedLanguage;
|
|
200
|
+
if (!currentLanguage)
|
|
201
|
+
return true;
|
|
202
|
+
const expected = currentLanguage === "python" ? ".py" : ".ts";
|
|
203
|
+
if (ext && ext !== expected)
|
|
204
|
+
return `Main file should end with ${expected}`;
|
|
205
|
+
return true;
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
type: "confirm",
|
|
210
|
+
name: "readme",
|
|
211
|
+
message: "Readme:",
|
|
212
|
+
initial: true,
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
type: "confirm",
|
|
216
|
+
name: "license",
|
|
217
|
+
message: "License (MIT):",
|
|
218
|
+
initial: true,
|
|
219
|
+
},
|
|
220
|
+
], {
|
|
221
|
+
onCancel: () => {
|
|
222
|
+
process.exitCode = 1;
|
|
223
|
+
return false;
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
if (!response || !response.projectName) {
|
|
227
|
+
process.exitCode = 1;
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const options = {
|
|
231
|
+
projectName: String(response.projectName).trim(),
|
|
232
|
+
description: String(response.description || "").trim(),
|
|
233
|
+
author: String(response.author || "").trim(),
|
|
234
|
+
language: response.language,
|
|
235
|
+
mainFileName: normalizeMainFileName(response.language, response.mainFileName),
|
|
236
|
+
readme: Boolean(response.readme),
|
|
237
|
+
license: Boolean(response.license),
|
|
238
|
+
};
|
|
239
|
+
await (0, scaffold_1.scaffoldProject)(options);
|
|
240
|
+
}
|
|
241
|
+
run().catch((err) => {
|
|
242
|
+
// eslint-disable-next-line no-console
|
|
243
|
+
console.error(err);
|
|
244
|
+
process.exitCode = 1;
|
|
245
|
+
});
|
package/dist/scaffold.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.scaffoldProject = scaffoldProject;
|
|
37
|
+
const fs = __importStar(require("node:fs/promises"));
|
|
38
|
+
const path = __importStar(require("node:path"));
|
|
39
|
+
const templates_1 = require("./templates");
|
|
40
|
+
async function pathExists(p) {
|
|
41
|
+
try {
|
|
42
|
+
await fs.access(p);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function ensureDir(dirPath) {
|
|
50
|
+
await fs.mkdir(dirPath, { recursive: true });
|
|
51
|
+
}
|
|
52
|
+
async function writeFileEnsuringDir(filePath, content) {
|
|
53
|
+
await ensureDir(path.dirname(filePath));
|
|
54
|
+
await fs.writeFile(filePath, content, "utf8");
|
|
55
|
+
}
|
|
56
|
+
async function scaffoldProject(options) {
|
|
57
|
+
const targetRoot = path.resolve(process.cwd(), options.projectName);
|
|
58
|
+
if (await pathExists(targetRoot)) {
|
|
59
|
+
throw new Error(`Target directory already exists: ${targetRoot}`);
|
|
60
|
+
}
|
|
61
|
+
const files = (0, templates_1.getProjectFiles)(options);
|
|
62
|
+
await ensureDir(targetRoot);
|
|
63
|
+
for (const file of files) {
|
|
64
|
+
const absPath = path.join(targetRoot, file.relativePath);
|
|
65
|
+
await writeFileEnsuringDir(absPath, file.content);
|
|
66
|
+
}
|
|
67
|
+
// eslint-disable-next-line no-console
|
|
68
|
+
console.log(`\nCreated project at: ${targetRoot}`);
|
|
69
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getProjectFiles = getProjectFiles;
|
|
4
|
+
function escapeYamlString(value) {
|
|
5
|
+
const s = String(value ?? "");
|
|
6
|
+
const escaped = s.replace(/"/g, '\\"');
|
|
7
|
+
return `"${escaped}"`;
|
|
8
|
+
}
|
|
9
|
+
function mitLicenseText({ author }) {
|
|
10
|
+
const year = new Date().getFullYear();
|
|
11
|
+
const owner = String(author || "").trim() || "Copyright Holder";
|
|
12
|
+
return `MIT License\n\nCopyright (c) ${year} ${owner}\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n`;
|
|
13
|
+
}
|
|
14
|
+
function projectReadme(options) {
|
|
15
|
+
const { projectName, description, author, language, mainFileName } = options;
|
|
16
|
+
const langLabel = language === "python" ? "Python" : "TypeScript";
|
|
17
|
+
return `# ${projectName}\n\n${description || ""}\n\n## Author\n\n${author || ""}\n\n## Language\n\n${langLabel}\n\n## Entry\n\n- src/${mainFileName}\n\n## Config\n\n- config.yml\n`;
|
|
18
|
+
}
|
|
19
|
+
function configYml(options) {
|
|
20
|
+
const { projectName, description, author, language, mainFileName } = options;
|
|
21
|
+
return [
|
|
22
|
+
`name: ${escapeYamlString(projectName)}`,
|
|
23
|
+
`description: ${escapeYamlString(description)}`,
|
|
24
|
+
`author: ${escapeYamlString(author)}`,
|
|
25
|
+
`language: ${escapeYamlString(language)}`,
|
|
26
|
+
`entry: ${escapeYamlString(`src/${mainFileName}`)}`,
|
|
27
|
+
"",
|
|
28
|
+
].join("\n");
|
|
29
|
+
}
|
|
30
|
+
function pythonMain({ projectName, description, }) {
|
|
31
|
+
const safeName = String(projectName).replace(/"/g, '\\"');
|
|
32
|
+
const safeDesc = String(description || "").replace(/"/g, '\\"');
|
|
33
|
+
return `def main():\n print("${safeName}")\n ${description ? `print("${safeDesc}")` : "pass"}\n\n\nif __name__ == "__main__":\n main()\n`;
|
|
34
|
+
}
|
|
35
|
+
function pythonRequirements() {
|
|
36
|
+
return `# Add your Python dependencies here\n`;
|
|
37
|
+
}
|
|
38
|
+
function pythonPyproject({ projectName, author, }) {
|
|
39
|
+
const safeName = String(projectName || "glosc-project")
|
|
40
|
+
.trim()
|
|
41
|
+
.replace(/\s+/g, "-");
|
|
42
|
+
const safeAuthor = String(author || "").replace(/"/g, '\\"');
|
|
43
|
+
return `# Minimal pyproject.toml (adjust as needed)\n\n[project]\nname = "${safeName}"\nversion = "0.1.0"\ndescription = ""\nauthors = [{ name = "${safeAuthor}" }]\nrequires-python = ">=3.10"\n\n[build-system]\nrequires = ["setuptools>=61.0"]\nbuild-backend = "setuptools.build_meta"\n`;
|
|
44
|
+
}
|
|
45
|
+
function nodePackageJson(options) {
|
|
46
|
+
const { projectName, description, author, mainFileName } = options;
|
|
47
|
+
const main = mainFileName.replace(/\.ts$/i, ".js");
|
|
48
|
+
const pkg = {
|
|
49
|
+
name: projectName,
|
|
50
|
+
version: "0.1.0",
|
|
51
|
+
description: description || "",
|
|
52
|
+
author: author || "",
|
|
53
|
+
private: true,
|
|
54
|
+
scripts: {
|
|
55
|
+
build: "tsc -p .",
|
|
56
|
+
start: `node ${main}`,
|
|
57
|
+
},
|
|
58
|
+
devDependencies: {
|
|
59
|
+
typescript: "^5.3.3",
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
return JSON.stringify(pkg, null, 2) + "\n";
|
|
63
|
+
}
|
|
64
|
+
function tsConfig() {
|
|
65
|
+
return (JSON.stringify({
|
|
66
|
+
compilerOptions: {
|
|
67
|
+
target: "ES2020",
|
|
68
|
+
module: "CommonJS",
|
|
69
|
+
strict: true,
|
|
70
|
+
outDir: ".",
|
|
71
|
+
rootDir: ".",
|
|
72
|
+
esModuleInterop: true,
|
|
73
|
+
},
|
|
74
|
+
include: ["*.ts"],
|
|
75
|
+
}, null, 2) + "\n");
|
|
76
|
+
}
|
|
77
|
+
function tsMain({ projectName, description, }) {
|
|
78
|
+
const safeName = String(projectName).replace(/`/g, "\\`");
|
|
79
|
+
const safeDesc = String(description || "").replace(/`/g, "\\`");
|
|
80
|
+
return `console.log("${safeName}");\n${description ? `console.log("${safeDesc}");\n` : ""}`;
|
|
81
|
+
}
|
|
82
|
+
function getProjectFiles(options) {
|
|
83
|
+
const files = [];
|
|
84
|
+
files.push({ relativePath: "config.yml", content: configYml(options) });
|
|
85
|
+
if (options.readme) {
|
|
86
|
+
files.push({
|
|
87
|
+
relativePath: "README.md",
|
|
88
|
+
content: projectReadme(options),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (options.license) {
|
|
92
|
+
files.push({
|
|
93
|
+
relativePath: "LICENSE",
|
|
94
|
+
content: mitLicenseText(options),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if (options.language === "python") {
|
|
98
|
+
files.push({
|
|
99
|
+
relativePath: `src/${options.mainFileName}`,
|
|
100
|
+
content: pythonMain(options),
|
|
101
|
+
});
|
|
102
|
+
files.push({
|
|
103
|
+
relativePath: "src/requirements.txt",
|
|
104
|
+
content: pythonRequirements(),
|
|
105
|
+
});
|
|
106
|
+
files.push({
|
|
107
|
+
relativePath: "src/pyproject.toml",
|
|
108
|
+
content: pythonPyproject(options),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
if (options.language === "typescript") {
|
|
112
|
+
files.push({
|
|
113
|
+
relativePath: `src/${options.mainFileName}`,
|
|
114
|
+
content: tsMain(options),
|
|
115
|
+
});
|
|
116
|
+
files.push({
|
|
117
|
+
relativePath: "src/package.json",
|
|
118
|
+
content: nodePackageJson(options),
|
|
119
|
+
});
|
|
120
|
+
files.push({ relativePath: "src/tsconfig.json", content: tsConfig() });
|
|
121
|
+
}
|
|
122
|
+
return files;
|
|
123
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-glosc",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Scaffold Glosc projects (Python/TypeScript/JavaScript) via npm create",
|
|
5
|
+
"author": "glosc-ai",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"bin": {
|
|
8
|
+
"glosc": "bin/index.js"
|
|
9
|
+
},
|
|
10
|
+
"type": "commonjs",
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=18"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -p tsconfig.json",
|
|
16
|
+
"start": "npm run build && node bin/index.js",
|
|
17
|
+
"lint": "npm run build --silent",
|
|
18
|
+
"prepublishOnly": "npm run build"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"prompts": "^2.4.2"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "^22.10.7",
|
|
25
|
+
"@types/prompts": "^2.4.9",
|
|
26
|
+
"typescript": "^5.4.5"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"bin/",
|
|
30
|
+
"dist/",
|
|
31
|
+
"src/",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"keywords": [
|
|
36
|
+
"create",
|
|
37
|
+
"scaffold",
|
|
38
|
+
"cli",
|
|
39
|
+
"glosc"
|
|
40
|
+
],
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/glosc-ai/Glosc-cli"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import prompts from "prompts";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { scaffoldProject } from "./scaffold";
|
|
4
|
+
|
|
5
|
+
type Language = "python" | "typescript";
|
|
6
|
+
|
|
7
|
+
type ProjectOptions = {
|
|
8
|
+
projectName: string;
|
|
9
|
+
description: string;
|
|
10
|
+
author: string;
|
|
11
|
+
language: Language;
|
|
12
|
+
mainFileName: string;
|
|
13
|
+
readme: boolean;
|
|
14
|
+
license: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type ParsedArgs = {
|
|
18
|
+
defaults: boolean;
|
|
19
|
+
language?: string;
|
|
20
|
+
mainFileName?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
author?: string;
|
|
23
|
+
readme?: boolean;
|
|
24
|
+
license?: boolean;
|
|
25
|
+
projectName?: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function parseArgs(argv: string[]): ParsedArgs {
|
|
29
|
+
const result: ParsedArgs = {
|
|
30
|
+
defaults: false,
|
|
31
|
+
language: undefined,
|
|
32
|
+
mainFileName: undefined,
|
|
33
|
+
description: undefined,
|
|
34
|
+
author: undefined,
|
|
35
|
+
readme: undefined,
|
|
36
|
+
license: undefined,
|
|
37
|
+
projectName: undefined,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const args = Array.from(argv);
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < args.length; i++) {
|
|
43
|
+
const a = args[i];
|
|
44
|
+
if (!a) continue;
|
|
45
|
+
|
|
46
|
+
if (a === "--defaults" || a === "--yes" || a === "-y") {
|
|
47
|
+
result.defaults = true;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (a === "--no-readme") {
|
|
52
|
+
result.readme = false;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (a === "--readme") {
|
|
57
|
+
result.readme = true;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (a === "--no-license") {
|
|
62
|
+
result.license = false;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (a === "--license") {
|
|
67
|
+
result.license = true;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (a === "--language" && i + 1 < args.length) {
|
|
72
|
+
result.language = args[++i];
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (a === "--main" && i + 1 < args.length) {
|
|
77
|
+
result.mainFileName = args[++i];
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (a === "--description" && i + 1 < args.length) {
|
|
82
|
+
result.description = args[++i];
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (a === "--author" && i + 1 < args.length) {
|
|
87
|
+
result.author = args[++i];
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!a.startsWith("-") && !result.projectName) {
|
|
92
|
+
result.projectName = a;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function normalizeLanguage(value: unknown): Language | undefined {
|
|
101
|
+
const v = String(value || "")
|
|
102
|
+
.trim()
|
|
103
|
+
.toLowerCase();
|
|
104
|
+
if (v === "py" || v === "python") return "python";
|
|
105
|
+
if (v === "ts" || v === "typescript") return "typescript";
|
|
106
|
+
if (v === "js" || v === "javascript") {
|
|
107
|
+
throw new Error(
|
|
108
|
+
"JavaScript template has been removed. Use --language typescript (or omit --language) instead."
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function normalizeMainFileName(
|
|
115
|
+
language: Language,
|
|
116
|
+
mainFileNameRaw: unknown
|
|
117
|
+
): string {
|
|
118
|
+
const trimmed = String(mainFileNameRaw || "").trim();
|
|
119
|
+
const defaultExt = language === "python" ? ".py" : ".ts";
|
|
120
|
+
const base =
|
|
121
|
+
trimmed.length > 0
|
|
122
|
+
? trimmed
|
|
123
|
+
: language === "python"
|
|
124
|
+
? "main.py"
|
|
125
|
+
: "index.ts";
|
|
126
|
+
|
|
127
|
+
if (path.extname(base)) return base;
|
|
128
|
+
return `${base}${defaultExt}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function run(): Promise<void> {
|
|
132
|
+
const argv = process.argv.slice(2);
|
|
133
|
+
const args = parseArgs(argv);
|
|
134
|
+
|
|
135
|
+
if (args.defaults) {
|
|
136
|
+
if (!args.projectName) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
"Project name is required (e.g. `npm create glosc-cli@latest my-app -- --defaults`)"
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const language = normalizeLanguage(args.language) || "typescript";
|
|
143
|
+
|
|
144
|
+
const options: ProjectOptions = {
|
|
145
|
+
projectName: String(args.projectName).trim(),
|
|
146
|
+
description: String(
|
|
147
|
+
args.description || "A brief description of your project"
|
|
148
|
+
).trim(),
|
|
149
|
+
author: String(args.author || "Your Name").trim(),
|
|
150
|
+
language,
|
|
151
|
+
mainFileName: normalizeMainFileName(language, args.mainFileName),
|
|
152
|
+
readme: args.readme !== undefined ? Boolean(args.readme) : true,
|
|
153
|
+
license: args.license !== undefined ? Boolean(args.license) : true,
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
await scaffoldProject(options);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// allow `npm create ... <name>` to prefill
|
|
161
|
+
prompts.override({
|
|
162
|
+
projectName: args.projectName,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
let selectedLanguage: Language | undefined;
|
|
166
|
+
|
|
167
|
+
const response = await prompts(
|
|
168
|
+
[
|
|
169
|
+
{
|
|
170
|
+
type: "text",
|
|
171
|
+
name: "projectName",
|
|
172
|
+
message: "Project name:",
|
|
173
|
+
validate: (value: string) => {
|
|
174
|
+
const name = String(value || "").trim();
|
|
175
|
+
if (!name) return "Project name is required";
|
|
176
|
+
if (name.includes("/") || name.includes("\\"))
|
|
177
|
+
return "Project name cannot include path separators";
|
|
178
|
+
return true;
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
type: "text",
|
|
183
|
+
name: "description",
|
|
184
|
+
message: "Description:",
|
|
185
|
+
initial: "A brief description of your project",
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
type: "text",
|
|
189
|
+
name: "author",
|
|
190
|
+
message: "Author:",
|
|
191
|
+
initial: "Your Name",
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
type: "select",
|
|
195
|
+
name: "language",
|
|
196
|
+
message: "Use Language:",
|
|
197
|
+
choices: [
|
|
198
|
+
{ title: "Python", value: "python" },
|
|
199
|
+
{ title: "TypeScript", value: "typescript" },
|
|
200
|
+
],
|
|
201
|
+
onState: (state: { value: Language }) => {
|
|
202
|
+
selectedLanguage = state.value;
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
type: (prev: Language) => (prev ? "text" : null),
|
|
207
|
+
name: "mainFileName",
|
|
208
|
+
message: "Main File Name:",
|
|
209
|
+
initial: (prev: Language) =>
|
|
210
|
+
prev === "python" ? "main.py" : "index.ts",
|
|
211
|
+
validate: (value: string, values?: { language?: Language }) => {
|
|
212
|
+
const name = String(value || "").trim();
|
|
213
|
+
if (!name) return "Main file name is required";
|
|
214
|
+
const ext = path.extname(name);
|
|
215
|
+
const currentLanguage =
|
|
216
|
+
values?.language || selectedLanguage;
|
|
217
|
+
if (!currentLanguage) return true;
|
|
218
|
+
const expected =
|
|
219
|
+
currentLanguage === "python" ? ".py" : ".ts";
|
|
220
|
+
if (ext && ext !== expected)
|
|
221
|
+
return `Main file should end with ${expected}`;
|
|
222
|
+
return true;
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
type: "confirm",
|
|
227
|
+
name: "readme",
|
|
228
|
+
message: "Readme:",
|
|
229
|
+
initial: true,
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
type: "confirm",
|
|
233
|
+
name: "license",
|
|
234
|
+
message: "License (MIT):",
|
|
235
|
+
initial: true,
|
|
236
|
+
},
|
|
237
|
+
],
|
|
238
|
+
{
|
|
239
|
+
onCancel: () => {
|
|
240
|
+
process.exitCode = 1;
|
|
241
|
+
return false;
|
|
242
|
+
},
|
|
243
|
+
}
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
if (!response || !response.projectName) {
|
|
247
|
+
process.exitCode = 1;
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const options: ProjectOptions = {
|
|
252
|
+
projectName: String(response.projectName).trim(),
|
|
253
|
+
description: String(response.description || "").trim(),
|
|
254
|
+
author: String(response.author || "").trim(),
|
|
255
|
+
language: response.language as Language,
|
|
256
|
+
mainFileName: normalizeMainFileName(
|
|
257
|
+
response.language as Language,
|
|
258
|
+
response.mainFileName
|
|
259
|
+
),
|
|
260
|
+
readme: Boolean(response.readme),
|
|
261
|
+
license: Boolean(response.license),
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
await scaffoldProject(options);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
run().catch((err) => {
|
|
268
|
+
// eslint-disable-next-line no-console
|
|
269
|
+
console.error(err);
|
|
270
|
+
process.exitCode = 1;
|
|
271
|
+
});
|
package/src/scaffold.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as fs from "node:fs/promises";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { getProjectFiles, type ProjectOptions } from "./templates";
|
|
4
|
+
|
|
5
|
+
async function pathExists(p: string): Promise<boolean> {
|
|
6
|
+
try {
|
|
7
|
+
await fs.access(p);
|
|
8
|
+
return true;
|
|
9
|
+
} catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function ensureDir(dirPath: string): Promise<void> {
|
|
15
|
+
await fs.mkdir(dirPath, { recursive: true });
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function writeFileEnsuringDir(
|
|
19
|
+
filePath: string,
|
|
20
|
+
content: string
|
|
21
|
+
): Promise<void> {
|
|
22
|
+
await ensureDir(path.dirname(filePath));
|
|
23
|
+
await fs.writeFile(filePath, content, "utf8");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function scaffoldProject(options: ProjectOptions): Promise<void> {
|
|
27
|
+
const targetRoot = path.resolve(process.cwd(), options.projectName);
|
|
28
|
+
|
|
29
|
+
if (await pathExists(targetRoot)) {
|
|
30
|
+
throw new Error(`Target directory already exists: ${targetRoot}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const files = getProjectFiles(options);
|
|
34
|
+
|
|
35
|
+
await ensureDir(targetRoot);
|
|
36
|
+
|
|
37
|
+
for (const file of files) {
|
|
38
|
+
const absPath = path.join(targetRoot, file.relativePath);
|
|
39
|
+
await writeFileEnsuringDir(absPath, file.content);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// eslint-disable-next-line no-console
|
|
43
|
+
console.log(`\nCreated project at: ${targetRoot}`);
|
|
44
|
+
}
|
package/src/templates.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
export type Language = "python" | "typescript";
|
|
2
|
+
|
|
3
|
+
export type ProjectOptions = {
|
|
4
|
+
projectName: string;
|
|
5
|
+
description: string;
|
|
6
|
+
author: string;
|
|
7
|
+
language: Language;
|
|
8
|
+
mainFileName: string;
|
|
9
|
+
readme: boolean;
|
|
10
|
+
license: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type ProjectFile = {
|
|
14
|
+
relativePath: string;
|
|
15
|
+
content: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function escapeYamlString(value: unknown): string {
|
|
19
|
+
const s = String(value ?? "");
|
|
20
|
+
const escaped = s.replace(/"/g, '\\"');
|
|
21
|
+
return `"${escaped}"`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function mitLicenseText({ author }: Pick<ProjectOptions, "author">): string {
|
|
25
|
+
const year = new Date().getFullYear();
|
|
26
|
+
const owner = String(author || "").trim() || "Copyright Holder";
|
|
27
|
+
return `MIT License\n\nCopyright (c) ${year} ${owner}\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function projectReadme(options: ProjectOptions): string {
|
|
31
|
+
const { projectName, description, author, language, mainFileName } =
|
|
32
|
+
options;
|
|
33
|
+
const langLabel = language === "python" ? "Python" : "TypeScript";
|
|
34
|
+
|
|
35
|
+
return `# ${projectName}\n\n${description || ""}\n\n## Author\n\n${
|
|
36
|
+
author || ""
|
|
37
|
+
}\n\n## Language\n\n${langLabel}\n\n## Entry\n\n- src/${mainFileName}\n\n## Config\n\n- config.yml\n`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function configYml(options: ProjectOptions): string {
|
|
41
|
+
const { projectName, description, author, language, mainFileName } =
|
|
42
|
+
options;
|
|
43
|
+
return [
|
|
44
|
+
`name: ${escapeYamlString(projectName)}`,
|
|
45
|
+
`description: ${escapeYamlString(description)}`,
|
|
46
|
+
`author: ${escapeYamlString(author)}`,
|
|
47
|
+
`language: ${escapeYamlString(language)}`,
|
|
48
|
+
`entry: ${escapeYamlString(`src/${mainFileName}`)}`,
|
|
49
|
+
"",
|
|
50
|
+
].join("\n");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function pythonMain({
|
|
54
|
+
projectName,
|
|
55
|
+
description,
|
|
56
|
+
}: Pick<ProjectOptions, "projectName" | "description">): string {
|
|
57
|
+
const safeName = String(projectName).replace(/"/g, '\\"');
|
|
58
|
+
const safeDesc = String(description || "").replace(/"/g, '\\"');
|
|
59
|
+
|
|
60
|
+
return `def main():\n print("${safeName}")\n ${
|
|
61
|
+
description ? `print("${safeDesc}")` : "pass"
|
|
62
|
+
}\n\n\nif __name__ == "__main__":\n main()\n`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function pythonRequirements(): string {
|
|
66
|
+
return `# Add your Python dependencies here\n`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function pythonPyproject({
|
|
70
|
+
projectName,
|
|
71
|
+
author,
|
|
72
|
+
}: Pick<ProjectOptions, "projectName" | "author">): string {
|
|
73
|
+
const safeName = String(projectName || "glosc-project")
|
|
74
|
+
.trim()
|
|
75
|
+
.replace(/\s+/g, "-");
|
|
76
|
+
|
|
77
|
+
const safeAuthor = String(author || "").replace(/"/g, '\\"');
|
|
78
|
+
|
|
79
|
+
return `# Minimal pyproject.toml (adjust as needed)\n\n[project]\nname = "${safeName}"\nversion = "0.1.0"\ndescription = ""\nauthors = [{ name = "${safeAuthor}" }]\nrequires-python = ">=3.10"\n\n[build-system]\nrequires = ["setuptools>=61.0"]\nbuild-backend = "setuptools.build_meta"\n`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function nodePackageJson(options: ProjectOptions): string {
|
|
83
|
+
const { projectName, description, author, mainFileName } = options;
|
|
84
|
+
const main = mainFileName.replace(/\.ts$/i, ".js");
|
|
85
|
+
|
|
86
|
+
const pkg: Record<string, unknown> = {
|
|
87
|
+
name: projectName,
|
|
88
|
+
version: "0.1.0",
|
|
89
|
+
description: description || "",
|
|
90
|
+
author: author || "",
|
|
91
|
+
private: true,
|
|
92
|
+
scripts: {
|
|
93
|
+
build: "tsc -p .",
|
|
94
|
+
start: `node ${main}`,
|
|
95
|
+
},
|
|
96
|
+
devDependencies: {
|
|
97
|
+
typescript: "^5.3.3",
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
return JSON.stringify(pkg, null, 2) + "\n";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function tsConfig(): string {
|
|
105
|
+
return (
|
|
106
|
+
JSON.stringify(
|
|
107
|
+
{
|
|
108
|
+
compilerOptions: {
|
|
109
|
+
target: "ES2020",
|
|
110
|
+
module: "CommonJS",
|
|
111
|
+
strict: true,
|
|
112
|
+
outDir: ".",
|
|
113
|
+
rootDir: ".",
|
|
114
|
+
esModuleInterop: true,
|
|
115
|
+
},
|
|
116
|
+
include: ["*.ts"],
|
|
117
|
+
},
|
|
118
|
+
null,
|
|
119
|
+
2
|
|
120
|
+
) + "\n"
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function tsMain({
|
|
125
|
+
projectName,
|
|
126
|
+
description,
|
|
127
|
+
}: Pick<ProjectOptions, "projectName" | "description">): string {
|
|
128
|
+
const safeName = String(projectName).replace(/`/g, "\\`");
|
|
129
|
+
const safeDesc = String(description || "").replace(/`/g, "\\`");
|
|
130
|
+
return `console.log("${safeName}");\n${
|
|
131
|
+
description ? `console.log("${safeDesc}");\n` : ""
|
|
132
|
+
}`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function getProjectFiles(options: ProjectOptions): ProjectFile[] {
|
|
136
|
+
const files: ProjectFile[] = [];
|
|
137
|
+
|
|
138
|
+
files.push({ relativePath: "config.yml", content: configYml(options) });
|
|
139
|
+
|
|
140
|
+
if (options.readme) {
|
|
141
|
+
files.push({
|
|
142
|
+
relativePath: "README.md",
|
|
143
|
+
content: projectReadme(options),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (options.license) {
|
|
148
|
+
files.push({
|
|
149
|
+
relativePath: "LICENSE",
|
|
150
|
+
content: mitLicenseText(options),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (options.language === "python") {
|
|
155
|
+
files.push({
|
|
156
|
+
relativePath: `src/${options.mainFileName}`,
|
|
157
|
+
content: pythonMain(options),
|
|
158
|
+
});
|
|
159
|
+
files.push({
|
|
160
|
+
relativePath: "src/requirements.txt",
|
|
161
|
+
content: pythonRequirements(),
|
|
162
|
+
});
|
|
163
|
+
files.push({
|
|
164
|
+
relativePath: "src/pyproject.toml",
|
|
165
|
+
content: pythonPyproject(options),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (options.language === "typescript") {
|
|
170
|
+
files.push({
|
|
171
|
+
relativePath: `src/${options.mainFileName}`,
|
|
172
|
+
content: tsMain(options),
|
|
173
|
+
});
|
|
174
|
+
files.push({
|
|
175
|
+
relativePath: "src/package.json",
|
|
176
|
+
content: nodePackageJson(options),
|
|
177
|
+
});
|
|
178
|
+
files.push({ relativePath: "src/tsconfig.json", content: tsConfig() });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return files;
|
|
182
|
+
}
|