@component-hook/create-app 0.2.4 → 1.0.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/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # @component-hook/create-app
2
+
3
+ Scaffolding a basic `typescript` project.
4
+
5
+ > **Compatibility Note:**
6
+ > Requires [Node.js](https://nodejs.org/en/) version 18+, 20+. However, some templates require a higher Node.js version to work, please upgrade if your package manager warns about it.
7
+
8
+ <p>
9
+ <a href="https://npm-stat.com/charts.html?package=@component-hook/create-app">
10
+ <img src="https://img.shields.io/npm/dm/@component-hook/create-app.svg" alt="npm"/>
11
+ </a>
12
+ <a href="https://www.npmjs.com/package/@component-hook/create-app">
13
+ <img src="https://img.shields.io/npm/v/@component-hook/create-app.svg" alt="npm"/>
14
+ </a>
15
+ </p>
16
+
17
+ ## Create Project
18
+
19
+ ```bash
20
+ $ npm create @component-hook/app@latest
21
+
22
+ # or use yarn
23
+ $ yarn create @component-hook/app
24
+
25
+ # or use pnpm
26
+ $ pnpm create @component-hook/app
27
+ ```
28
+
29
+ Then follow the prompts!
30
+
31
+ You can also directly specify the project name and the template you want to use via additional command line options. For example, to scaffold a project, run:
32
+
33
+ ```bash
34
+ $ npm create @component-hook/app@latest basic-app -- --template vue
35
+
36
+ # yarn
37
+ $ yarn create @component-hook/app basic-app --template vue
38
+
39
+ # pnpm
40
+ $ pnpm create @component-hook/app basic-app --template vue
41
+ ```
42
+
43
+ Currently supported template presets include:
44
+
45
+ - `vue`
46
+
47
+ ## Project Default Setup
48
+
49
+ - [Vite](https://cn.vite.dev/)
50
+ - [TypeScript](https://www.typescriptlang.org/)
51
+ - [Axios](https://github.com/axios/axios)
52
+ - [TailwindcSS](https://tailwindcss.com/)
53
+ - [PostCSS](https://postcss.org/) w/ `postcss-nesting` plugin
54
+ - [Cssnano](https://cssnano.github.io/cssnano/) for minimizing production CSS
55
+ - [Postcss-pxtorem](https://www.npmjs.com/package/postcss-pxtorem) generates rem units from pixel units
56
+ - [Vite-plugin-svg-icons](https://github.com/vbenjs/vite-plugin-svg-icons) generate svg sprite map
57
+ - [Eslint](https://eslint.org/)
58
+ - [@component-hook/eslint-plugin](https://www.npmjs.com/package/@component-hook/eslint-plugin?activeTab=readme)
59
+ - [Prettier](https://prettier.io/)
60
+ - [Husky](https://www.npmjs.com/package/husky) to improves commits
61
+ - if encounter npx: command not found, can execute `ln -s $(which npx) /usr/local/bin/npx` in zsh
62
+ - [commitlint](https://commitlint.js.org/#/) checks if your commit messages meet the conventional commit format
63
+ - [lint-staged](https://github.com/okonet/lint-staged) filter files checked by pre commit lint
64
+ - Alias `@` to `<project_root>/src`
65
+ - Mock use [msw](https://mswjs.io/)
66
+ - `npx msw init ./public`
67
+ - Unit test use [Vitest](https://vitest.dev/) + [Testing Library](https://testing-library.com/)
68
+ - [jsdom](https://www.npmjs.com/package/jsdom)
69
+ - [@vitest/coverage-v8](https://vitest.dev/guide/coverage.html)
70
+ - [@testing-library/jest-dom](https://testing-library.com/docs/ecosystem-jest-dom/)
71
+ - [@testing-library/user-event](https://testing-library.com/docs/ecosystem-user-event/)
72
+ - E2E test use [playwright](https://playwright.dev/)
73
+ - [Rollup-plugin-visualizer](https://github.com/btd/rollup-plugin-visualizer) visualize and analyze your Rollup bundle to see which modules are taking up space
74
+ - Predefined and fully typed global variables:
75
+ - `VITE_APP_VERSION` is read from `package.json` version at build time
76
+ - `VITE_APP_BUILD_EPOCH` is populated as `new Date().getTime()` at build time
77
+ - Github workflows (default)
78
+ - ci - on workflow call and dispatch
79
+ - deploy - on push main branch
80
+ - preview - on pull request
81
+ - Gitlab CI (use `--gitlab` command create project)
82
+ - run tests - on branches, merge request and manual
package/bin/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import '../dist/index.mjs';
3
+ import '../dist/index.js';
@@ -0,0 +1,2 @@
1
+
2
+ export { }
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",
3
+ "version": "1.0.0",
4
4
  "description": "Scaffolding a basic typescript project",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -25,22 +25,22 @@
25
25
  "url": "https://github.com/tzuyi0817/component-hook/issues"
26
26
  },
27
27
  "homepage": "https://github.com/tzuyi0817/component-hook/tree/master/packages/create-app#readme",
28
- "devDependencies": {
29
- "@types/fs-extra": "^11.0.4",
30
- "@types/minimist": "^1.2.5",
31
- "@types/prompts": "^2.4.9",
28
+ "dependencies": {
32
29
  "art-template": "^4.13.2",
33
- "cross-spawn": "^7.0.6",
34
- "fs-extra": "^11.2.0",
30
+ "fs-extra": "^11.3.0",
35
31
  "minimist": "^1.2.8",
36
32
  "ora": "^8.1.1",
37
33
  "picocolors": "^1.1.1",
38
- "prompts": "^2.4.2",
39
- "unbuild": "^3.2.0"
34
+ "prompts": "^2.4.2"
35
+ },
36
+ "devDependencies": {
37
+ "@types/fs-extra": "^11.0.4",
38
+ "@types/minimist": "^1.2.5",
39
+ "@types/prompts": "^2.4.9"
40
40
  },
41
41
  "scripts": {
42
- "dev": "unbuild --stub",
43
- "build": "unbuild",
42
+ "dev": "tsup --watch",
43
+ "build": "tsup",
44
44
  "typecheck": "tsc --noEmit"
45
45
  }
46
46
  }
@@ -3,7 +3,7 @@
3
3
  "version": "0.0.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
- "packageManager": "pnpm@9.15.3",
6
+ "packageManager": "pnpm@9.15.4",
7
7
  "scripts": {
8
8
  "dev": "vite",
9
9
  "dev:mock": "MOCK=true vite",
@@ -19,30 +19,30 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "axios": "^1.7.9",
22
- "pinia": "^2.3.0",
22
+ "pinia": "^2.3.1",
23
23
  "pinia-plugin-persistedstate": "^4.2.0",
24
24
  "vue": "^3.5.13",
25
25
  "vue-i18n": "^11.0.1",
26
26
  "vue-router": "^4.5.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@component-hook/eslint-plugin": "0.2.4",
29
+ "@component-hook/eslint-plugin": "0.2.5",
30
30
  "@intlify/unplugin-vue-i18n": "^6.0.3",
31
31
  "@playwright/test": "^1.49.1",
32
32
  "@testing-library/jest-dom": "^6.6.3",
33
- "@testing-library/user-event": "^14.5.2",
33
+ "@testing-library/user-event": "^14.6.1",
34
34
  "@testing-library/vue": "^8.1.0",
35
35
  "@tsconfig/node22": "22.0.0",
36
- "@types/node": "^22.10.5",
36
+ "@types/node": "^22.10.7",
37
37
  "@vitejs/plugin-vue": "^5.2.1",
38
38
  "@vitest/coverage-v8": "^2.1.8",
39
39
  "@vue/tsconfig": "^0.7.0",
40
40
  "autoprefixer": "^10.4.20",
41
41
  "cssnano": "^7.0.6",
42
- "eslint": "^9.17.0",
42
+ "eslint": "^9.18.0",
43
43
  "jsdom": "^25.0.1",
44
44
  "msw": "^2.7.0",
45
- "postcss": "^8.4.49",
45
+ "postcss": "^8.5.1",
46
46
  "postcss-import": "^16.1.0",
47
47
  "postcss-nesting": "^13.0.1",
48
48
  "postcss-pxtorem": "^6.1.0",
@@ -51,7 +51,7 @@
51
51
  "tailwindcss": "^3.4.17",
52
52
  "typescript": "^5.7.3",
53
53
  "unplugin-vue-components": "^0.28.0",
54
- "vite": "^5.4.11",
54
+ "vite": "^5.4.14",
55
55
  "vite-plugin-svg-icons": "^2.0.1",
56
56
  "vitest": "^2.1.8",
57
57
  "vue-tsc": "^2.2.0"
@@ -68,6 +68,6 @@
68
68
  "@commitlint/cli": "^19.6.1",
69
69
  "@commitlint/config-conventional": "^19.6.0",
70
70
  "husky": "^9.1.7",
71
- "lint-staged": "^15.3.0"
71
+ "lint-staged": "^15.4.1"
72
72
  }
73
73
  }