create-cabloy 3.0.2 → 5.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.
@@ -0,0 +1,5 @@
1
+ const version = "5.0.0";
2
+ const _package = {
3
+ version: version};
4
+
5
+ export { _package as default, version };
package/dist/index.mjs ADDED
@@ -0,0 +1,192 @@
1
+ #!/usr/bin/env node
2
+ import { createWriteStream, existsSync } from 'node:fs';
3
+ import { unlink, mkdir } from 'node:fs/promises';
4
+ import { join, dirname, resolve, basename } from 'node:path';
5
+ import mri from 'mri';
6
+ import * as p from '@clack/prompts';
7
+ import pc from 'picocolors';
8
+ import { pipeline } from 'node:stream/promises';
9
+ import { tmpdir } from 'node:os';
10
+ import spawn from 'cross-spawn';
11
+
12
+ const NPM_REGISTRY = "https://registry.npmjs.org";
13
+ const PACKAGE_NAME = "cabloy";
14
+
15
+ const validNameRegex = /^(?:@[a-z0-9][-a-z0-9]*[a-z0-9]\/)?[a-z0-9][-a-z0-9]*[a-z0-9]$/;
16
+ function isValidPackageName(name) {
17
+ return validNameRegex.test(name);
18
+ }
19
+ function toValidPackageName(name) {
20
+ return name.trim().toLowerCase().replace(/\s+/g, "-").replace(/^[._]/, "").replace(/[^a-z0-9-~]+/g, "-");
21
+ }
22
+ async function fetchLatestTarballUrl() {
23
+ const url = `${NPM_REGISTRY}/${PACKAGE_NAME}/latest`;
24
+ const res = await fetch(url);
25
+ if (!res.ok) {
26
+ throw new Error(`Failed to fetch package info: ${res.status} ${res.statusText}`);
27
+ }
28
+ const data = await res.json();
29
+ return data.dist.tarball;
30
+ }
31
+ async function downloadTarball(tarballUrl) {
32
+ const tmpFile = join(tmpdir(), `create-cabloy-${Date.now()}.tgz`);
33
+ const res = await fetch(tarballUrl);
34
+ if (!res.ok) {
35
+ throw new Error(`Failed to download tarball: ${res.status} ${res.statusText}`);
36
+ }
37
+ if (!res.body) {
38
+ throw new Error("Failed to download tarball: empty response body");
39
+ }
40
+ await mkdir(dirname(tmpFile), { recursive: true });
41
+ const fileStream = createWriteStream(tmpFile);
42
+ await pipeline(res.body, fileStream);
43
+ return tmpFile;
44
+ }
45
+ async function extractTarball(tarballPath, targetDir) {
46
+ await mkdir(targetDir, { recursive: true });
47
+ const exitCode = await spawnAsync("tar", ["--strip-components=1", "-xzf", tarballPath, "-C", targetDir]);
48
+ if (exitCode !== 0) {
49
+ throw new Error("Failed to extract tarball");
50
+ }
51
+ }
52
+ async function downloadAndExtract(targetDir) {
53
+ const tarballUrl = await fetchLatestTarballUrl();
54
+ const tarballPath = await downloadTarball(tarballUrl);
55
+ try {
56
+ await extractTarball(tarballPath, targetDir);
57
+ } finally {
58
+ await unlink(tarballPath).catch(() => {
59
+ });
60
+ }
61
+ }
62
+ function spawnAsync(command, args, options) {
63
+ return new Promise((resolve) => {
64
+ const child = spawn(command, args, { stdio: "inherit", ...options });
65
+ child.on("close", (code) => {
66
+ resolve(code);
67
+ });
68
+ child.on("error", () => {
69
+ resolve(1);
70
+ });
71
+ });
72
+ }
73
+
74
+ async function runPrompts(options) {
75
+ p.intro(pc.bgCyan(pc.black(" Create Cabloy ")));
76
+ let projectName = null;
77
+ if (options.projectName) {
78
+ projectName = isValidPackageName(options.projectName) ? options.projectName : toValidPackageName(options.projectName);
79
+ } else {
80
+ const name = await p.text({
81
+ message: "Project name (leave empty to use current directory)",
82
+ placeholder: "cabloy-app",
83
+ validate: (v) => {
84
+ if (v.trim() && !isValidPackageName(v) && !isValidPackageName(toValidPackageName(v))) {
85
+ return "Invalid package name";
86
+ }
87
+ return void 0;
88
+ }
89
+ });
90
+ if (p.isCancel(name)) {
91
+ p.cancel("Operation cancelled");
92
+ return null;
93
+ }
94
+ if (name.trim()) {
95
+ projectName = isValidPackageName(name) ? name : toValidPackageName(name);
96
+ } else {
97
+ projectName = null;
98
+ }
99
+ }
100
+ return {
101
+ projectName,
102
+ force: options.force ?? false
103
+ };
104
+ }
105
+
106
+ function printHelp() {
107
+ console.log(`
108
+ ${pc.bold("create-cabloy")} - Create a new Cabloy project
109
+
110
+ ${pc.bold("Usage:")}
111
+ ${pc.cyan("npm create cabloy")} ${pc.dim("[project-name]")}
112
+ ${pc.cyan("pnpm create cabloy")} ${pc.dim("[project-name]")}
113
+ ${pc.cyan("yarn create cabloy")} ${pc.dim("[project-name]")}
114
+
115
+ ${pc.bold("Options:")}
116
+ --force Overwrite existing directory
117
+ -h, --help Show this help message
118
+ -v, --version Show version number
119
+ `);
120
+ }
121
+ async function main() {
122
+ const argv = process.argv.slice(2);
123
+ const args = mri(argv, {
124
+ boolean: ["force", "help", "version"],
125
+ alias: { h: "help", v: "version" }
126
+ });
127
+ if (args.help) {
128
+ printHelp();
129
+ process.exit(0);
130
+ }
131
+ if (args.version) {
132
+ const { default: pkg } = await import('./chunks/package.mjs');
133
+ console.log(pkg.version);
134
+ process.exit(0);
135
+ }
136
+ const result = await runPrompts({
137
+ projectName: args._[0],
138
+ force: args.force
139
+ });
140
+ if (!result) {
141
+ process.exit(0);
142
+ }
143
+ let targetDir;
144
+ if (result.projectName) {
145
+ targetDir = resolve(process.cwd(), result.projectName);
146
+ result.projectName;
147
+ } else {
148
+ targetDir = process.cwd();
149
+ basename(targetDir);
150
+ }
151
+ if (existsSync(targetDir) && result.projectName) {
152
+ const files = await import('node:fs/promises').then((fs) => fs.readdir(targetDir));
153
+ if (files.length > 0 && !result.force) {
154
+ p.log.error(
155
+ pc.red(`Directory "${result.projectName}" already exists and is not empty. Use --force to overwrite.`)
156
+ );
157
+ process.exit(1);
158
+ }
159
+ }
160
+ if (result.projectName) {
161
+ await mkdir(targetDir, { recursive: true });
162
+ }
163
+ const s = p.spinner();
164
+ s.start("Downloading cabloy from npm registry...");
165
+ try {
166
+ await downloadAndExtract(targetDir);
167
+ s.stop("Downloaded and extracted successfully!");
168
+ } catch (err) {
169
+ s.stop("Download failed", 1);
170
+ p.log.error(pc.red(err instanceof Error ? err.message : "Failed to download cabloy"));
171
+ process.exit(1);
172
+ }
173
+ const s2 = p.spinner();
174
+ s2.start("Running npm run init...");
175
+ const exitCode = await spawnAsync("npm", ["run", "init"], {
176
+ cwd: targetDir
177
+ });
178
+ if (exitCode !== 0) {
179
+ s2.stop("Init failed", 1);
180
+ p.log.error(pc.red("npm run init failed. You can try running it manually."));
181
+ } else {
182
+ s2.stop("Project initialized successfully!");
183
+ }
184
+ p.outro(
185
+ pc.green(" Done! ") + "Next steps:\n" + (result.projectName ? pc.dim(` cd ${result.projectName}
186
+ `) : "") + pc.dim(" npm run dev\n")
187
+ );
188
+ }
189
+ main().catch((err) => {
190
+ p.log.error(pc.red(err.message));
191
+ process.exit(1);
192
+ });
package/package.json CHANGED
@@ -1,20 +1,45 @@
1
1
  {
2
2
  "name": "create-cabloy",
3
- "version": "3.0.2",
4
- "description": "so you could use `npm init cabloy showcase` to init cabloy project",
5
- "bin": "./bin/create-cabloy.js",
3
+ "version": "5.0.0",
4
+ "description": "Create a new Cabloy project - npm create cabloy",
5
+ "keywords": [
6
+ "cabloy",
7
+ "create-cabloy",
8
+ "fullstack",
9
+ "scaffold"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "zhennann",
6
13
  "repository": {
7
14
  "type": "git",
8
- "url": "git+https://github.com/zhennann/create-cabloy.git"
15
+ "url": "git+https://github.com/cabloy/cabloy.git"
9
16
  },
10
- "author": "zhennann",
11
- "license": "MIT",
12
- "bugs": {
13
- "url": "https://github.com/zhennann/create-cabloy/issues"
17
+ "bin": {
18
+ "create-cabloy": "dist/index.mjs"
14
19
  },
15
- "homepage": "https://github.com/zhennann/create-cabloy#readme",
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "type": "module",
16
24
  "dependencies": {
17
- "egg-born": "^3.0.2"
25
+ "@clack/prompts": "^0.9.1",
26
+ "cross-spawn": "^7.0.6",
27
+ "mri": "^1.2.0",
28
+ "picocolors": "^1.1.1"
29
+ },
30
+ "devDependencies": {
31
+ "@types/cross-spawn": "^6.0.6",
32
+ "@types/node": "^22.19.17",
33
+ "typescript": "^5.9.3",
34
+ "unbuild": "^3.5.0"
35
+ },
36
+ "engines": {
37
+ "node": ">=20.19.0"
18
38
  },
19
- "gitHead": "a675a7fd4292be9d072aa44fd0469e11082c1d35"
20
- }
39
+ "scripts": {
40
+ "dev": "unbuild --stub",
41
+ "build": "unbuild",
42
+ "release-patch": "npm version patch && npm publish && git push --follow-tags",
43
+ "release-minor": "npm version minor && npm publish && git push --follow-tags"
44
+ }
45
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2016-present zhennann
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 DELETED
@@ -1,20 +0,0 @@
1
- # create-cabloy
2
-
3
- [![NPM version][npm-image]][npm-url]
4
- [![NPM download][download-image]][download-url]
5
-
6
- [npm-image]: https://img.shields.io/npm/v/create-cabloy.svg?style=flat-square
7
- [npm-url]: https://npmjs.org/package/create-cabloy
8
- [download-image]: https://img.shields.io/npm/dm/create-cabloy.svg?style=flat-square
9
- [download-url]: https://npmjs.org/package/create-cabloy
10
-
11
- Alias for [egg-born](https://github.com/zhennann/egg-born), so you could use `npm init cabloy showcase`.
12
-
13
- Thanks to `npm init` feature introduced at npm@6, see [npm-init](https://docs.npmjs.com/cli/init) for more details.
14
-
15
- ## Usage
16
-
17
- ```bash
18
- $ npm init cabloy showcase
19
- $ npm init cabloy showcase --type=cabloy
20
- ```
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- require('egg-born/bin/egg-born');