create-waku 0.4.9 → 0.5.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/package.json +32 -3
- package/src/cli.ts +174 -0
- package/cli.js +0 -46
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Daishi Kato
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-waku",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"author": "Daishi Kato",
|
|
5
|
+
"contributors": [
|
|
6
|
+
"Vasu Singh"
|
|
7
|
+
],
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/dai-shi/waku.git"
|
|
11
|
+
},
|
|
12
|
+
"bin": "./dist/cli.js",
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"dist",
|
|
16
|
+
"template"
|
|
17
|
+
],
|
|
18
|
+
"type": "commonjs",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"fs-extra": "^11.1.1",
|
|
21
|
+
"kolorist": "^1.8.0",
|
|
22
|
+
"prompts": "^2.4.2"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/fs-extra": "^11.0.1",
|
|
26
|
+
"@types/prompts": "^2.4.4",
|
|
27
|
+
"esbuild": "0.19.2"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"start": "node dist/cli.js",
|
|
31
|
+
"template": "cp -r ../../examples template/",
|
|
32
|
+
"build": "esbuild src/* --bundle --platform=node --outdir=dist"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
// FIXME what's the proper fix?
|
|
6
|
+
// eslint-disable-next-line import/no-named-as-default
|
|
7
|
+
import prompts from "prompts";
|
|
8
|
+
import { red, green, bold } from "kolorist";
|
|
9
|
+
// FIXME why @types/fs-extra doesn't work?
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
import fse from "fs-extra/esm";
|
|
13
|
+
|
|
14
|
+
function isValidPackageName(projectName: string) {
|
|
15
|
+
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
|
|
16
|
+
projectName,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function toValidPackageName(projectName: string) {
|
|
21
|
+
return projectName
|
|
22
|
+
.trim()
|
|
23
|
+
.toLowerCase()
|
|
24
|
+
.replace(/\s+/g, "-")
|
|
25
|
+
.replace(/^[._]/, "")
|
|
26
|
+
.replace(/[^a-z0-9-~]+/g, "-");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// if the dir is empty or not exist
|
|
30
|
+
function canSafelyOverwrite(dir: string) {
|
|
31
|
+
return !fs.existsSync(dir) || fs.readdirSync(dir).length === 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function init() {
|
|
35
|
+
let targetDir = "";
|
|
36
|
+
const defaultProjectName = "waku-project";
|
|
37
|
+
|
|
38
|
+
const CHOICES = fs.readdirSync("template");
|
|
39
|
+
let result: {
|
|
40
|
+
packageName: string;
|
|
41
|
+
shouldOverwrite: string;
|
|
42
|
+
chooseProject: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
result = await prompts(
|
|
47
|
+
[
|
|
48
|
+
{
|
|
49
|
+
name: "projectName",
|
|
50
|
+
type: "text",
|
|
51
|
+
message: "Project Name",
|
|
52
|
+
initial: defaultProjectName,
|
|
53
|
+
onState: (state: any) =>
|
|
54
|
+
(targetDir = String(state.value).trim() || defaultProjectName),
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: "shouldOverwrite",
|
|
58
|
+
type: () => (canSafelyOverwrite(targetDir) ? null : "confirm"),
|
|
59
|
+
message: `${targetDir} is not empty. Remove existing files and continue?`,
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: "overwriteChecker",
|
|
63
|
+
type: (values: any) => {
|
|
64
|
+
if (values === false) {
|
|
65
|
+
throw new Error(red("✖") + " Operation cancelled");
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "packageName",
|
|
72
|
+
type: () => (isValidPackageName(targetDir) ? null : "text"),
|
|
73
|
+
message: "Package name",
|
|
74
|
+
initial: () => toValidPackageName(targetDir),
|
|
75
|
+
validate: (dir: string) =>
|
|
76
|
+
isValidPackageName(dir) || "Invalid package.json name",
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: "chooseProject",
|
|
80
|
+
type: "select",
|
|
81
|
+
message: "Choose a starter template",
|
|
82
|
+
choices: [
|
|
83
|
+
{ title: "basic-template", value: CHOICES[0] },
|
|
84
|
+
{ title: "async-template", value: CHOICES[1] },
|
|
85
|
+
{ title: "promise-template", value: CHOICES[2] },
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
{
|
|
90
|
+
onCancel: () => {
|
|
91
|
+
throw new Error(red("✖") + " Operation cancelled");
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
} catch (cancelled) {
|
|
96
|
+
if (cancelled instanceof Error) {
|
|
97
|
+
console.log(cancelled.message);
|
|
98
|
+
}
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const { packageName, shouldOverwrite, chooseProject } = result;
|
|
103
|
+
|
|
104
|
+
const root = path.resolve(targetDir);
|
|
105
|
+
|
|
106
|
+
if (shouldOverwrite) {
|
|
107
|
+
fse.emptyDirSync(root);
|
|
108
|
+
} else if (!fs.existsSync(root)) {
|
|
109
|
+
fs.mkdirSync(root, { recursive: true });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const pkg = {
|
|
113
|
+
name: packageName ?? toValidPackageName(targetDir),
|
|
114
|
+
version: "0.0.0",
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
console.log("Setting up project...");
|
|
118
|
+
|
|
119
|
+
const templateRoot = path.join(__dirname, "../template");
|
|
120
|
+
const templateDir = path.resolve(templateRoot, chooseProject);
|
|
121
|
+
|
|
122
|
+
// Read existing package.json from the root directory
|
|
123
|
+
const packageJsonPath = path.join(root, "package.json");
|
|
124
|
+
|
|
125
|
+
// Read new package.json from the template directory
|
|
126
|
+
const newPackageJsonPath = path.join(templateDir, "package.json");
|
|
127
|
+
const newPackageJson = JSON.parse(
|
|
128
|
+
fs.readFileSync(newPackageJsonPath, "utf-8"),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
fse.copySync(templateDir, root);
|
|
132
|
+
|
|
133
|
+
fs.writeFileSync(
|
|
134
|
+
packageJsonPath,
|
|
135
|
+
JSON.stringify(
|
|
136
|
+
{
|
|
137
|
+
...newPackageJson,
|
|
138
|
+
...pkg,
|
|
139
|
+
},
|
|
140
|
+
null,
|
|
141
|
+
2,
|
|
142
|
+
),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const manager = process.env.npm_config_user_agent ?? "";
|
|
146
|
+
const packageManager = /pnpm/.test(manager)
|
|
147
|
+
? "pnpm"
|
|
148
|
+
: /yarn/.test(manager)
|
|
149
|
+
? "yarn"
|
|
150
|
+
: "npm";
|
|
151
|
+
|
|
152
|
+
const commandsMap = {
|
|
153
|
+
install: {
|
|
154
|
+
pnpm: "pnpm install",
|
|
155
|
+
yarn: "yarn",
|
|
156
|
+
npm: "npm install",
|
|
157
|
+
},
|
|
158
|
+
dev: {
|
|
159
|
+
pnpm: "pnpm dev",
|
|
160
|
+
yarn: "yarn dev",
|
|
161
|
+
npm: "npm run dev",
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
console.log(`\nDone. Now run:\n`);
|
|
166
|
+
console.log(`${bold(green(`cd ${targetDir}`))}`);
|
|
167
|
+
console.log(`${bold(green(commandsMap.install[packageManager]))}`);
|
|
168
|
+
console.log(`${bold(green(commandsMap.dev[packageManager]))}`);
|
|
169
|
+
console.log();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
init().catch((e) => {
|
|
173
|
+
console.error(e);
|
|
174
|
+
});
|
package/cli.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const path = require("node:path");
|
|
4
|
-
const fs = require("node:fs");
|
|
5
|
-
const https = require("node:https");
|
|
6
|
-
|
|
7
|
-
const dirName = "waku-example";
|
|
8
|
-
|
|
9
|
-
if (fs.existsSync(dirName)) {
|
|
10
|
-
console.error(`Directory "${dirName}" already exists!`);
|
|
11
|
-
process.exit(1);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const baseUrl =
|
|
15
|
-
"https://raw.githubusercontent.com/dai-shi/waku/v0.13.0/examples/01_counter/";
|
|
16
|
-
|
|
17
|
-
const files = `
|
|
18
|
-
package.json
|
|
19
|
-
tsconfig.json
|
|
20
|
-
src/main.tsx
|
|
21
|
-
src/entries.ts
|
|
22
|
-
src/index.html
|
|
23
|
-
src/components/App.tsx
|
|
24
|
-
src/components/Counter.tsx
|
|
25
|
-
`
|
|
26
|
-
.split(/\s/)
|
|
27
|
-
.filter((file) => file);
|
|
28
|
-
|
|
29
|
-
const getFiles = (index = 0) => {
|
|
30
|
-
const file = files[index];
|
|
31
|
-
if (!file) return;
|
|
32
|
-
const destFile = path.join(dirName, file.replace("/", path.sep));
|
|
33
|
-
fs.mkdirSync(path.dirname(destFile), { recursive: true });
|
|
34
|
-
https.get(baseUrl + file, (res) => {
|
|
35
|
-
res.pipe(fs.createWriteStream(destFile));
|
|
36
|
-
res.on("end", () => getFiles(index + 1));
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
getFiles();
|
|
41
|
-
|
|
42
|
-
process.on("exit", (code) => {
|
|
43
|
-
if (!code) {
|
|
44
|
-
console.info(`Done! Change directory "${dirName}"`);
|
|
45
|
-
}
|
|
46
|
-
});
|