create-soybean 0.6.3 → 0.6.4
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.mjs +182 -0
- package/package.json +11 -2
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { green, blue, cyan, lightBlue, reset, red } from 'kolorist';
|
|
6
|
+
import minimist from 'minimist';
|
|
7
|
+
import prompts from 'prompts';
|
|
8
|
+
import { consola } from 'consola';
|
|
9
|
+
|
|
10
|
+
function formatTargetDir(targetDir) {
|
|
11
|
+
return targetDir?.trim()?.replace(/\/+$/g, "");
|
|
12
|
+
}
|
|
13
|
+
function isPathEmpty($path) {
|
|
14
|
+
const files = fs.readdirSync($path);
|
|
15
|
+
return files.length === 0 || files.length === 1 && files[0] === ".git";
|
|
16
|
+
}
|
|
17
|
+
function isValidPackageName(projectName) {
|
|
18
|
+
return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(projectName);
|
|
19
|
+
}
|
|
20
|
+
function toValidPackageName(projectName) {
|
|
21
|
+
return projectName.trim().toLowerCase().replace(/\s+/g, "-").replace(/^[._]/, "").replace(/[^a-z\d\-~]+/g, "-");
|
|
22
|
+
}
|
|
23
|
+
function emptyDir(dir) {
|
|
24
|
+
const isExist = fs.existsSync(dir);
|
|
25
|
+
if (!isExist) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const files = fs.readdirSync(dir);
|
|
29
|
+
for (const file of files) {
|
|
30
|
+
if (file !== ".git") {
|
|
31
|
+
const filePath = path.resolve(dir, file);
|
|
32
|
+
fs.rmSync(filePath, { recursive: true, force: true });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function copyDir(srcDir, destDir) {
|
|
37
|
+
fs.mkdirSync(destDir, { recursive: true });
|
|
38
|
+
const files = fs.readdirSync(srcDir);
|
|
39
|
+
for (const file of files) {
|
|
40
|
+
const srcFile = path.resolve(srcDir, file);
|
|
41
|
+
const destFile = path.resolve(destDir, file);
|
|
42
|
+
copy(srcFile, destFile);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function copy(src, dest) {
|
|
46
|
+
const stat = fs.statSync(src);
|
|
47
|
+
if (stat.isDirectory()) {
|
|
48
|
+
copyDir(src, dest);
|
|
49
|
+
} else {
|
|
50
|
+
fs.copyFileSync(src, dest);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const templates = [
|
|
55
|
+
{
|
|
56
|
+
type: "vue",
|
|
57
|
+
name: "Vue 3",
|
|
58
|
+
color: green
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
type: "ts-lib",
|
|
62
|
+
name: "TypeScript library",
|
|
63
|
+
color: blue
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
type: "react",
|
|
67
|
+
name: "React",
|
|
68
|
+
color: cyan
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
type: "react-native",
|
|
72
|
+
name: "React Native",
|
|
73
|
+
color: cyan
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
type: "solid",
|
|
77
|
+
name: "Solid",
|
|
78
|
+
color: lightBlue
|
|
79
|
+
}
|
|
80
|
+
];
|
|
81
|
+
const TEMPLATES = templates.map((t) => t.type);
|
|
82
|
+
const renameFiles = {
|
|
83
|
+
_gitignore: ".gitignore",
|
|
84
|
+
_eslintrc: ".eslintrc",
|
|
85
|
+
_npmrc: ".npmrc"
|
|
86
|
+
};
|
|
87
|
+
const defaultTargetDir = "create-soybean-project";
|
|
88
|
+
async function setupCli() {
|
|
89
|
+
const cwd = process.cwd();
|
|
90
|
+
const argv = minimist(process.argv.slice(2), { string: ["_"] });
|
|
91
|
+
const argTargetDir = formatTargetDir(argv._[0]);
|
|
92
|
+
const argTemplate = argv.template || argv.t;
|
|
93
|
+
let targetDir = argTargetDir || defaultTargetDir;
|
|
94
|
+
function getProjectName() {
|
|
95
|
+
return targetDir === "." ? path.basename(path.resolve()) : targetDir;
|
|
96
|
+
}
|
|
97
|
+
let result = null;
|
|
98
|
+
try {
|
|
99
|
+
result = await prompts([
|
|
100
|
+
{
|
|
101
|
+
type: argTargetDir ? null : "text",
|
|
102
|
+
name: "projectName",
|
|
103
|
+
message: reset("Project name:"),
|
|
104
|
+
initial: defaultTargetDir,
|
|
105
|
+
onState: (state) => {
|
|
106
|
+
targetDir = formatTargetDir(state.value) || defaultTargetDir;
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
type: () => !fs.existsSync(targetDir) || isPathEmpty(targetDir) ? null : "confirm",
|
|
111
|
+
name: "overwrite",
|
|
112
|
+
message: () => `${targetDir === "." ? "Current directory" : `Target directory "${targetDir}"`} is not empty. Remove existing files and continue?`
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
type: (_, { overwrite: overwrite2 }) => {
|
|
116
|
+
if (overwrite2 === false) {
|
|
117
|
+
throw new Error(`${red("\u2716")} Operation cancelled`);
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
},
|
|
121
|
+
name: "overwriteChecker"
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
type: () => isValidPackageName(getProjectName()) ? null : "text",
|
|
125
|
+
name: "packageName",
|
|
126
|
+
message: reset("Package name:"),
|
|
127
|
+
initial: () => toValidPackageName(getProjectName()),
|
|
128
|
+
validate: (dir) => isValidPackageName(dir) || "Invalid package.json name"
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
type: argTemplate && TEMPLATES.includes(argTemplate) ? null : "select",
|
|
132
|
+
name: "template",
|
|
133
|
+
message: typeof argTemplate === "string" && !TEMPLATES.includes(argTemplate) ? reset(`"${argTemplate}" isn't a valid template. Please choose from below: `) : reset("Select a template:"),
|
|
134
|
+
initial: 0,
|
|
135
|
+
choices: templates.map(({ type, name, color }) => ({
|
|
136
|
+
title: color(name),
|
|
137
|
+
value: type
|
|
138
|
+
}))
|
|
139
|
+
}
|
|
140
|
+
]);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
consola.error(error);
|
|
143
|
+
}
|
|
144
|
+
if (!result) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const { template, overwrite, packageName } = result;
|
|
148
|
+
const root = path.join(cwd, targetDir);
|
|
149
|
+
if (overwrite) {
|
|
150
|
+
emptyDir(root);
|
|
151
|
+
} else if (!fs.existsSync(root)) {
|
|
152
|
+
fs.mkdirSync(root, { recursive: true });
|
|
153
|
+
}
|
|
154
|
+
const $template = template || argTemplate;
|
|
155
|
+
consola.info(`
|
|
156
|
+
Scaffolding project in ${root}...`);
|
|
157
|
+
const templateDir = path.resolve(fileURLToPath(import.meta.url), "../..", `template-${$template}`);
|
|
158
|
+
const write = (file, content) => {
|
|
159
|
+
const targetPath = path.join(root, renameFiles[file] ?? file);
|
|
160
|
+
if (content) {
|
|
161
|
+
fs.writeFileSync(targetPath, content);
|
|
162
|
+
} else {
|
|
163
|
+
copy(path.join(templateDir, file), targetPath);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const files = fs.readdirSync(templateDir);
|
|
167
|
+
for (const file of files.filter((f) => f !== "package.json")) {
|
|
168
|
+
write(file);
|
|
169
|
+
}
|
|
170
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(templateDir, `package.json`), "utf-8"));
|
|
171
|
+
pkg.name = packageName || getProjectName();
|
|
172
|
+
write("package.json", `${JSON.stringify(pkg, null, 2)}
|
|
173
|
+
`);
|
|
174
|
+
const cdProjectName = path.relative(cwd, root);
|
|
175
|
+
consola.info(`
|
|
176
|
+
Done. Now run:
|
|
177
|
+
`);
|
|
178
|
+
if (root !== cwd) {
|
|
179
|
+
consola.info(` cd ${cdProjectName.includes(" ") ? `"${cdProjectName}"` : cdProjectName}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
setupCli();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-soybean",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.4",
|
|
4
4
|
"description": "SoybeanJS's command line to create different project templates",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Soybean",
|
|
@@ -12,12 +12,21 @@
|
|
|
12
12
|
"registry": "https://registry.npmjs.org/"
|
|
13
13
|
},
|
|
14
14
|
"bin": {
|
|
15
|
-
"create-soybean": "dist/index.mjs"
|
|
15
|
+
"create-soybean": "dist/index.mjs",
|
|
16
|
+
"create-soy": "dist/index.mjs"
|
|
16
17
|
},
|
|
17
18
|
"files": [
|
|
18
19
|
"dist"
|
|
19
20
|
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"consola": "^3.2.3",
|
|
23
|
+
"kolorist": "1.8.0",
|
|
24
|
+
"minimist": "1.2.8",
|
|
25
|
+
"prompts": "^2.4.2"
|
|
26
|
+
},
|
|
20
27
|
"devDependencies": {
|
|
28
|
+
"@types/minimist": "^1.2.2",
|
|
29
|
+
"@types/prompts": "^2.4.4",
|
|
21
30
|
"typescript": "5.1.6",
|
|
22
31
|
"unbuild": "1.2.1"
|
|
23
32
|
},
|