@component-hook/create-app 0.2.4-alpha.1 → 0.2.4-alpha.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.d.ts +2 -0
- package/dist/index.js +234 -0
- package/package.json +4 -5
- package/dist/index.mjs +0 -17
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import path2 from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
5
|
+
import minimist from "minimist";
|
|
6
|
+
import colors2 from "picocolors";
|
|
7
|
+
import { copy, ensureDirSync } from "fs-extra/esm";
|
|
8
|
+
import artTemplate from "art-template";
|
|
9
|
+
import ora from "ora";
|
|
10
|
+
|
|
11
|
+
// src/prompts.ts
|
|
12
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
13
|
+
import prompts from "prompts";
|
|
14
|
+
import colors from "picocolors";
|
|
15
|
+
|
|
16
|
+
// src/utils.ts
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { existsSync, readdirSync } from "node:fs";
|
|
19
|
+
import { remove } from "fs-extra/esm";
|
|
20
|
+
|
|
21
|
+
// src/config.ts
|
|
22
|
+
var DEFAULT_PROJECT_NAME = "basic-project";
|
|
23
|
+
var TEMPLATES = ["vue"];
|
|
24
|
+
|
|
25
|
+
// src/utils.ts
|
|
26
|
+
function formatTargetDir(targetDir) {
|
|
27
|
+
if (!targetDir) return targetDir;
|
|
28
|
+
targetDir = targetDir.trim();
|
|
29
|
+
if (targetDir[0] === "/") {
|
|
30
|
+
targetDir = targetDir.slice(1);
|
|
31
|
+
}
|
|
32
|
+
if (targetDir.at(-1) === "/") {
|
|
33
|
+
targetDir = targetDir.slice(0, -1);
|
|
34
|
+
}
|
|
35
|
+
return targetDir;
|
|
36
|
+
}
|
|
37
|
+
function getProjectName(targetDir) {
|
|
38
|
+
if (!targetDir) {
|
|
39
|
+
return DEFAULT_PROJECT_NAME;
|
|
40
|
+
}
|
|
41
|
+
return path.basename(path.resolve(targetDir));
|
|
42
|
+
}
|
|
43
|
+
function isValidPackageName(projectName) {
|
|
44
|
+
return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(projectName);
|
|
45
|
+
}
|
|
46
|
+
function toValidPackageName(projectName) {
|
|
47
|
+
return projectName.trim().toLowerCase().replaceAll(/\s+/g, "-").replace(/^[._]/, "").replaceAll(/[^a-z\d\-~]+/g, "-");
|
|
48
|
+
}
|
|
49
|
+
function getPkgManagerInfo() {
|
|
50
|
+
const userAgent = process.env.npm_config_user_agent;
|
|
51
|
+
if (!userAgent) return;
|
|
52
|
+
const [pkgManager] = userAgent.split(" ");
|
|
53
|
+
const [name, version] = pkgManager.split("/");
|
|
54
|
+
return { name, version };
|
|
55
|
+
}
|
|
56
|
+
function isEmptyFolder(dir) {
|
|
57
|
+
const files = readdirSync(dir);
|
|
58
|
+
return files.length === 0 || files.length === 1 && files[0] === ".git";
|
|
59
|
+
}
|
|
60
|
+
function clearFolder(dir) {
|
|
61
|
+
if (!existsSync(dir)) return;
|
|
62
|
+
const files = readdirSync(dir).filter((file) => file !== ".git");
|
|
63
|
+
return Promise.all(files.map((file) => remove(path.resolve(dir, file))));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/prompts.ts
|
|
67
|
+
function builtinTemplate(template) {
|
|
68
|
+
return template && TEMPLATES.includes(template);
|
|
69
|
+
}
|
|
70
|
+
function onCancel() {
|
|
71
|
+
throw new Error(`${colors.red("\u2716")} Operation cancelled`);
|
|
72
|
+
}
|
|
73
|
+
function styleTitle(title) {
|
|
74
|
+
return colors.bold(colors.magenta(title));
|
|
75
|
+
}
|
|
76
|
+
function operationPrompts({ targetDir, template }) {
|
|
77
|
+
const projectName = {
|
|
78
|
+
type: targetDir ? null : "text",
|
|
79
|
+
name: "projectName",
|
|
80
|
+
message: styleTitle("Project name:"),
|
|
81
|
+
initial: DEFAULT_PROJECT_NAME
|
|
82
|
+
};
|
|
83
|
+
const overwrite = {
|
|
84
|
+
type: (nameAnswer) => {
|
|
85
|
+
return existsSync2(nameAnswer) && !isEmptyFolder(nameAnswer) ? "select" : null;
|
|
86
|
+
},
|
|
87
|
+
name: "overwrite",
|
|
88
|
+
message: (nameAnswer) => {
|
|
89
|
+
const isCurrentDir = nameAnswer === ".";
|
|
90
|
+
const dir = isCurrentDir ? "current directory" : `Target directory "${targetDir}"`;
|
|
91
|
+
return styleTitle(`${dir} is not empty. Please choose how to proceed:`);
|
|
92
|
+
},
|
|
93
|
+
initial: 0,
|
|
94
|
+
choices: [
|
|
95
|
+
{ title: "Cancel operation", value: "cancel" },
|
|
96
|
+
{ title: "Remove existing files and continue", value: "overwrite" },
|
|
97
|
+
{ title: "Ignore files and continue", value: "ignore" }
|
|
98
|
+
]
|
|
99
|
+
};
|
|
100
|
+
const overwriteChecker = {
|
|
101
|
+
type: (overwriteAnswer) => {
|
|
102
|
+
if (overwriteAnswer === "cancel") onCancel();
|
|
103
|
+
return null;
|
|
104
|
+
},
|
|
105
|
+
name: "overwriteChecker"
|
|
106
|
+
};
|
|
107
|
+
const packageName = {
|
|
108
|
+
type: (_, { projectName: nameAnswer }) => {
|
|
109
|
+
return isValidPackageName(getProjectName(nameAnswer)) ? null : "text";
|
|
110
|
+
},
|
|
111
|
+
name: "projectName",
|
|
112
|
+
message: styleTitle("Package name:"),
|
|
113
|
+
initial: (_, { projectName: nameAnswer }) => toValidPackageName(getProjectName(nameAnswer)),
|
|
114
|
+
validate: (name) => {
|
|
115
|
+
return isValidPackageName(name) || "Invalid package.json name";
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const framework = {
|
|
119
|
+
type: builtinTemplate(template) ? null : "select",
|
|
120
|
+
name: "framework",
|
|
121
|
+
message: template ? styleTitle(`${colors.red(template)} isn't a valid template. Please choose from below:`) : styleTitle("Select a framework:"),
|
|
122
|
+
initial: 0,
|
|
123
|
+
choices: [
|
|
124
|
+
{ title: colors.green("Vue"), value: "vue" }
|
|
125
|
+
// { title: 'colors.blue('React'), value: 'react' },
|
|
126
|
+
]
|
|
127
|
+
};
|
|
128
|
+
return prompts([projectName, overwrite, overwriteChecker, packageName, framework], { onCancel });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/index.ts
|
|
132
|
+
var cwd = process.cwd();
|
|
133
|
+
var argv = minimist(process.argv.slice(2), {
|
|
134
|
+
default: { gitlab: false, help: false },
|
|
135
|
+
alias: { t: "template", l: "gitlab", h: "help" },
|
|
136
|
+
string: ["_"]
|
|
137
|
+
});
|
|
138
|
+
var renameFiles = {
|
|
139
|
+
_gitignore: ".gitignore",
|
|
140
|
+
"_gitlab-ci.yml": ".gitlab-ci.yml"
|
|
141
|
+
};
|
|
142
|
+
var helpMessage = `
|
|
143
|
+
Usage: create-component-hook [project name] [options]
|
|
144
|
+
|
|
145
|
+
Create a new project in TypeScript.
|
|
146
|
+
With no arguments, start the CLI in interactive mode.
|
|
147
|
+
|
|
148
|
+
Options:
|
|
149
|
+
-t, --template [name] Choose a framework template
|
|
150
|
+
-l, --gitlab Use Gitlab CI/CD, default is Github CI/CD
|
|
151
|
+
-h, --help Display this help message
|
|
152
|
+
|
|
153
|
+
Available templates:
|
|
154
|
+
${colors2.green("vue")}
|
|
155
|
+
`;
|
|
156
|
+
async function copyTemplateFolder(root, templateDir, packageName) {
|
|
157
|
+
const isGitlab = argv.lab || argv.gitlab;
|
|
158
|
+
const [files, pkgJson] = await Promise.all([
|
|
159
|
+
readdir(templateDir),
|
|
160
|
+
readFile(path2.join(templateDir, "package.json"), "utf-8")
|
|
161
|
+
]);
|
|
162
|
+
const filterFiles = files.filter((file) => {
|
|
163
|
+
const filterCi = isGitlab ? ".github" : ".gitlab";
|
|
164
|
+
const filterCiYml = isGitlab ? "" : "_gitlab-ci.yml";
|
|
165
|
+
return file !== "package.json" && file !== filterCi && file !== filterCiYml;
|
|
166
|
+
});
|
|
167
|
+
const pkg = JSON.parse(pkgJson);
|
|
168
|
+
const rewriteOrCopyFile = (file, content) => {
|
|
169
|
+
const targetPath = path2.join(root, renameFiles?.[file] ?? file);
|
|
170
|
+
if (content) {
|
|
171
|
+
return writeFile(targetPath, content);
|
|
172
|
+
}
|
|
173
|
+
const templatePath = path2.join(templateDir, file);
|
|
174
|
+
if (file.endsWith(".art")) {
|
|
175
|
+
const name = packageName.split("-").map((str) => `${str[0].toUpperCase()}${str.slice(1)}`).join(" ");
|
|
176
|
+
const renderedResult = artTemplate(templatePath, { projectName: name });
|
|
177
|
+
return writeFile(targetPath.replace(".art", ""), renderedResult);
|
|
178
|
+
}
|
|
179
|
+
return copy(templatePath, targetPath);
|
|
180
|
+
};
|
|
181
|
+
pkg.name = packageName;
|
|
182
|
+
return Promise.all([
|
|
183
|
+
...filterFiles.map((file) => rewriteOrCopyFile(file)),
|
|
184
|
+
rewriteOrCopyFile("package.json", `${JSON.stringify(pkg, null, 2)}
|
|
185
|
+
`),
|
|
186
|
+
new Promise((resolve) => setTimeout(resolve, 300))
|
|
187
|
+
]);
|
|
188
|
+
}
|
|
189
|
+
async function createApp() {
|
|
190
|
+
const help = argv.h || argv.help;
|
|
191
|
+
if (help) {
|
|
192
|
+
console.log(helpMessage);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const targetDir = formatTargetDir(argv._[0]);
|
|
196
|
+
const template = argv.t || argv.template;
|
|
197
|
+
const result = await operationPrompts({ targetDir, template });
|
|
198
|
+
const packageName = result.packageName || getProjectName(result.projectName);
|
|
199
|
+
const framework = result.framework || template;
|
|
200
|
+
const root = path2.join(cwd, result.projectName);
|
|
201
|
+
const pkgManagerInfo = getPkgManagerInfo();
|
|
202
|
+
const pkgManager = pkgManagerInfo?.name ?? "npm";
|
|
203
|
+
const spinner = ora();
|
|
204
|
+
console.log();
|
|
205
|
+
spinner.start(colors2.yellow(`Scaffolding project...`));
|
|
206
|
+
if (result.overwrite === "overwrite") {
|
|
207
|
+
await clearFolder(root);
|
|
208
|
+
} else {
|
|
209
|
+
ensureDirSync(root);
|
|
210
|
+
}
|
|
211
|
+
const templateDir = path2.resolve(fileURLToPath(import.meta.url), "../..", `template-${framework}`);
|
|
212
|
+
copyTemplateFolder(root, templateDir, packageName).then(() => {
|
|
213
|
+
const cdProjectName = path2.relative(cwd, root);
|
|
214
|
+
const formattedCdProjectName = cdProjectName.includes(" ") ? `"${cdProjectName}"` : cdProjectName;
|
|
215
|
+
spinner.succeed(`${colors2.green("Scaffolded project in")} ${root}`);
|
|
216
|
+
console.log(`
|
|
217
|
+
${colors2.green("Done. Now run:")}
|
|
218
|
+
`);
|
|
219
|
+
console.log(colors2.cyan(` cd ${formattedCdProjectName}`));
|
|
220
|
+
if (pkgManager === "yarn") {
|
|
221
|
+
console.log(colors2.cyan(` yarn`));
|
|
222
|
+
console.log(colors2.cyan(` yarn dev`));
|
|
223
|
+
} else {
|
|
224
|
+
console.log(colors2.cyan(` ${pkgManager} install`));
|
|
225
|
+
console.log(colors2.cyan(` ${pkgManager} run dev`));
|
|
226
|
+
}
|
|
227
|
+
console.log();
|
|
228
|
+
}).catch(() => {
|
|
229
|
+
spinner.fail(colors2.red("Failed to scaffold project"));
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
createApp().catch((error) => {
|
|
233
|
+
console.error(colors2.red(error.message));
|
|
234
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@component-hook/create-app",
|
|
3
|
-
"version": "0.2.4-alpha.
|
|
3
|
+
"version": "0.2.4-alpha.2",
|
|
4
4
|
"description": "Scaffolding a basic typescript project",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -36,12 +36,11 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/fs-extra": "^11.0.4",
|
|
38
38
|
"@types/minimist": "^1.2.5",
|
|
39
|
-
"@types/prompts": "^2.4.9"
|
|
40
|
-
"unbuild": "^3.2.0"
|
|
39
|
+
"@types/prompts": "^2.4.9"
|
|
41
40
|
},
|
|
42
41
|
"scripts": {
|
|
43
|
-
"dev": "
|
|
44
|
-
"build": "
|
|
42
|
+
"dev": "tsup --watch",
|
|
43
|
+
"build": "tsup",
|
|
45
44
|
"typecheck": "tsc --noEmit"
|
|
46
45
|
}
|
|
47
46
|
}
|
package/dist/index.mjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import c from"node:path";import{fileURLToPath as S}from"node:url";import D from"minimist";import a from"picocolors";import{existsSync as j,readdirSync as w,remove as T,ensureDirSync as A,readdir as E,readFile as F,writeFile as b,copy as I}from"fs-extra";import O from"art-template";import U from"ora";import z from"prompts";const $="basic-project",L=["vue"];function x(e){return e&&(e=e.trim(),e[0]==="/"&&(e=e.slice(1)),e.at(-1)==="/"&&(e=e.slice(0,-1)),e)}function y(e){return e?c.basename(c.resolve(e)):$}function P(e){return/^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(e)}function M(e){return e.trim().toLowerCase().replaceAll(/\s+/g,"-").replace(/^[._]/,"").replaceAll(/[^a-z\d\-~]+/g,"-")}function G(){const e=process.env.npm_config_user_agent;if(!e)return;const[t]=e.split(" "),[n,s]=t.split("/");return{name:n,version:s}}function J(e){const t=w(e);return t.length===0||t.length===1&&t[0]===".git"}function R(e){if(!j(e))return;const t=w(e).filter(n=>n!==".git");return Promise.all(t.map(n=>T(c.resolve(e,n))))}function V(e){return e&&L.includes(e)}function k(){throw new Error(`${a.red("\u2716")} Operation cancelled`)}function f(e){return a.bold(a.magenta(e))}function W({targetDir:e,template:t}){const n={type:e?null:"text",name:"projectName",message:f("Project name:"),initial:$},s={type:o=>j(o)&&!J(o)?"select":null,name:"overwrite",message:o=>{const i=o==="."?"current directory":`Target directory "${e}"`;return f(`${i} is not empty. Please choose how to proceed:`)},initial:0,choices:[{title:"Cancel operation",value:"cancel"},{title:"Remove existing files and continue",value:"overwrite"},{title:"Ignore files and continue",value:"ignore"}]},g={type:o=>(o==="cancel"&&k(),null),name:"overwriteChecker"},l={type:(o,{projectName:i})=>P(y(i))?null:"text",name:"projectName",message:f("Package name:"),initial:(o,{projectName:i})=>M(y(i)),validate:o=>P(o)||"Invalid package.json name"},m={type:V(t)?null:"select",name:"framework",message:f(t?`${a.red(t)} isn't a valid template. Please choose from below:`:"Select a framework:"),initial:0,choices:[{title:a.green("Vue"),value:"vue"}]};return z([n,s,g,l,m],{onCancel:k})}const N=process.cwd(),p=D(process.argv.slice(2),{default:{gitlab:!1,help:!1},alias:{t:"template",l:"gitlab",h:"help"},string:["_"]}),q={_gitignore:".gitignore","_gitlab-ci.yml":".gitlab-ci.yml"},B=`
|
|
2
|
-
Usage: create-component-hook [project name] [options]
|
|
3
|
-
|
|
4
|
-
Create a new project in TypeScript.
|
|
5
|
-
With no arguments, start the CLI in interactive mode.
|
|
6
|
-
|
|
7
|
-
Options:
|
|
8
|
-
-t, --template [name] Choose a framework template
|
|
9
|
-
-l, --gitlab Use Gitlab CI/CD, default is Github CI/CD
|
|
10
|
-
-h, --help Display this help message
|
|
11
|
-
|
|
12
|
-
Available templates:
|
|
13
|
-
${a.green("vue")}
|
|
14
|
-
`;async function H(e,t,n){const s=p.lab||p.gitlab,[g,l]=await Promise.all([E(t),F(c.join(t,"package.json"),"utf-8")]),m=g.filter(r=>r!=="package.json"&&r!==(s?".github":".gitlab")&&r!==(s?"":"_gitlab-ci.yml")),o=JSON.parse(l),i=(r,u)=>{const d=c.join(e,q?.[r]??r);if(u)return b(d,u);const h=c.join(t,r);if(r.endsWith(".art")){const C=n.split("-").map(v=>`${v[0].toUpperCase()}${v.slice(1)}`).join(" "),_=O(h,{projectName:C});return b(d.replace(".art",""),_)}return I(h,d)};return o.name=n,Promise.all([...m.map(r=>i(r)),i("package.json",`${JSON.stringify(o,null,2)}
|
|
15
|
-
`),new Promise(r=>setTimeout(r,300))])}async function K(){if(p.h||p.help){console.log(B);return}const e=x(p._[0]),t=p.t||p.template,n=await W({targetDir:e,template:t}),s=n.packageName||y(n.projectName),g=n.framework||t,l=c.join(N,n.projectName),m=G()?.name??"npm",o=U();console.log(),o.start(a.yellow("Scaffolding project...")),n.overwrite==="overwrite"?await R(l):A(l);const i=c.resolve(S(import.meta.url),"../..",`template-${g}`);H(l,i,s).then(()=>{const r=c.relative(N,l),u=r.includes(" ")?`"${r}"`:r;o.succeed(`${a.green("Scaffolded project in")} ${l}`),console.log(`
|
|
16
|
-
${a.green("Done. Now run:")}
|
|
17
|
-
`),console.log(a.cyan(` cd ${u}`)),m==="yarn"?(console.log(a.cyan(" yarn")),console.log(a.cyan(" yarn dev"))):(console.log(a.cyan(` ${m} install`)),console.log(a.cyan(` ${m} run dev`))),console.log()}).catch(()=>{o.fail(a.red("Failed to scaffold project"))})}K().catch(e=>{console.error(a.red(e.message))});
|