@knotx/cli 0.0.1 → 0.0.3
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.cjs +237 -22
- package/dist/index.mjs +237 -22
- package/package.json +4 -4
- package/templates/.gitignore.template +45 -0
- package/templates/README.md.template +1 -1
- package/templates/eslint.config.js.template +1 -1
- package/templates/index.ts.template +1 -1
- package/templates/playground/index.html.template +41 -0
- package/templates/playground/main.jsx.template +11 -0
- package/templates/playground/main.react.template +39 -0
- package/templates/playground/tsconfig.json.template +8 -0
- package/templates/plugin.jsx.template +1 -1
- package/templates/vite.config.ts.template +19 -0
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
+
const node_child_process = require('node:child_process');
|
|
4
5
|
const path = require('node:path');
|
|
5
6
|
const process = require('node:process');
|
|
6
7
|
const chalk = require('chalk');
|
|
@@ -18,6 +19,25 @@ const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
|
18
19
|
const inquirer__default = /*#__PURE__*/_interopDefaultCompat(inquirer);
|
|
19
20
|
const ora__default = /*#__PURE__*/_interopDefaultCompat(ora);
|
|
20
21
|
|
|
22
|
+
var __defProp = Object.defineProperty;
|
|
23
|
+
var __defProps = Object.defineProperties;
|
|
24
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
25
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
26
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
27
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
28
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
29
|
+
var __spreadValues = (a, b) => {
|
|
30
|
+
for (var prop in b || (b = {}))
|
|
31
|
+
if (__hasOwnProp.call(b, prop))
|
|
32
|
+
__defNormalProp(a, prop, b[prop]);
|
|
33
|
+
if (__getOwnPropSymbols)
|
|
34
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
35
|
+
if (__propIsEnum.call(b, prop))
|
|
36
|
+
__defNormalProp(a, prop, b[prop]);
|
|
37
|
+
}
|
|
38
|
+
return a;
|
|
39
|
+
};
|
|
40
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
41
|
var __async = (__this, __arguments, generator) => {
|
|
22
42
|
return new Promise((resolve, reject) => {
|
|
23
43
|
var fulfilled = (value) => {
|
|
@@ -41,7 +61,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
41
61
|
const templatesDir = path__default.resolve(__dirname, "..", "templates");
|
|
42
62
|
const program = new commander.Command();
|
|
43
63
|
program.name("knotx").description("Knotx CLI - \u521B\u5EFA\u548C\u7BA1\u7406Knotx\u63D2\u4EF6").version("0.0.1");
|
|
44
|
-
program.command("create-plugin").description("\u521B\u5EFA\u4E00\u4E2A\u65B0\u7684Knotx\u63D2\u4EF6").action(() => __async(undefined, null, function* () {
|
|
64
|
+
program.command("create-plugin").description("\u521B\u5EFA\u4E00\u4E2A\u65B0\u7684Knotx\u63D2\u4EF6\uFF0C\u5305\u542B\u5F00\u53D1\u73AF\u5883\u548C\u6F14\u793A playground").action(() => __async(undefined, null, function* () {
|
|
45
65
|
console.log(chalk__default.blue("\u{1F539} \u6B22\u8FCE\u4F7F\u7528 Knotx \u63D2\u4EF6\u521B\u5EFA\u5DE5\u5177\uFF01"));
|
|
46
66
|
const answers = yield inquirer__default.prompt([
|
|
47
67
|
{
|
|
@@ -70,43 +90,98 @@ program.command("create-plugin").description("\u521B\u5EFA\u4E00\u4E2A\u65B0\u76
|
|
|
70
90
|
console.log(chalk__default.red(`\u274C \u9519\u8BEF: \u76EE\u5F55 ${fullPluginName} \u5DF2\u5B58\u5728`));
|
|
71
91
|
return;
|
|
72
92
|
}
|
|
73
|
-
|
|
93
|
+
let spinner = ora__default("\u{1F4C2} \u521B\u5EFA\u63D2\u4EF6\u76EE\u5F55...").start();
|
|
74
94
|
try {
|
|
75
95
|
yield fs__default.ensureDir(targetDir);
|
|
76
96
|
yield fs__default.ensureDir(path__default.join(targetDir, "src"));
|
|
77
97
|
spinner.succeed("\u{1F4C2} \u63D2\u4EF6\u76EE\u5F55\u521B\u5EFA\u6210\u529F");
|
|
78
|
-
spinner
|
|
79
|
-
|
|
80
|
-
|
|
98
|
+
spinner = ora__default("\u{1F50D} \u83B7\u53D6 @knotx \u5305\u7684\u6700\u65B0\u7248\u672C...").start();
|
|
99
|
+
const knotxVersions = yield getKnotxPackageVersions();
|
|
100
|
+
spinner.succeed("\u{1F50D} @knotx \u5305\u7248\u672C\u4FE1\u606F\u83B7\u53D6\u6210\u529F");
|
|
101
|
+
spinner = ora__default("\u{1F4C4} \u521B\u5EFA package.json...").start();
|
|
102
|
+
yield createPackageJson(targetDir, pluginName, environment, knotxVersions);
|
|
81
103
|
spinner.succeed("\u{1F4C4} package.json \u521B\u5EFA\u6210\u529F");
|
|
82
|
-
spinner
|
|
83
|
-
spinner.start();
|
|
104
|
+
spinner = ora__default("\u{1F4C4} \u521B\u5EFA\u914D\u7F6E\u6587\u4EF6...").start();
|
|
84
105
|
yield createConfigFiles(targetDir);
|
|
85
106
|
spinner.succeed("\u{1F4C4} \u914D\u7F6E\u6587\u4EF6\u521B\u5EFA\u6210\u529F");
|
|
86
|
-
spinner
|
|
87
|
-
spinner.start();
|
|
107
|
+
spinner = ora__default("\u{1F4C4} \u521B\u5EFA\u6E90\u4EE3\u7801\u6587\u4EF6...").start();
|
|
88
108
|
yield createSourceFiles(targetDir, pluginName, environment);
|
|
89
109
|
spinner.succeed("\u{1F4C4} \u6E90\u4EE3\u7801\u6587\u4EF6\u521B\u5EFA\u6210\u529F");
|
|
90
|
-
spinner
|
|
91
|
-
spinner.start();
|
|
110
|
+
spinner = ora__default("\u{1F4C4} \u521B\u5EFA README.md...").start();
|
|
92
111
|
yield createReadme(targetDir, pluginName);
|
|
93
112
|
spinner.succeed("\u{1F4C4} README.md \u521B\u5EFA\u6210\u529F");
|
|
113
|
+
spinner = ora__default("\u{1F4C4} \u521B\u5EFA .gitignore \u6587\u4EF6...").start();
|
|
114
|
+
yield createGitignore(targetDir);
|
|
115
|
+
spinner.succeed("\u{1F4C4} .gitignore \u521B\u5EFA\u6210\u529F");
|
|
116
|
+
spinner = ora__default("\u{1F527} \u521D\u59CB\u5316 Git \u4ED3\u5E93...").start();
|
|
117
|
+
try {
|
|
118
|
+
initGitRepo(targetDir);
|
|
119
|
+
spinner.succeed("\u{1F527} Git \u4ED3\u5E93\u521D\u59CB\u5316\u6210\u529F");
|
|
120
|
+
} catch (error) {
|
|
121
|
+
spinner.warn("\u26A0\uFE0F Git \u4ED3\u5E93\u521D\u59CB\u5316\u5931\u8D25\uFF0C\u8BF7\u624B\u52A8\u521D\u59CB\u5316");
|
|
122
|
+
console.warn(chalk__default.yellow(` \u53EF\u80FD\u7684\u539F\u56E0: ${error.message}`));
|
|
123
|
+
}
|
|
94
124
|
console.log(chalk__default.green(`
|
|
95
125
|
\u2705 \u63D2\u4EF6 ${chalk__default.bold(fullPluginName)} \u521B\u5EFA\u6210\u529F\uFF01`));
|
|
96
126
|
console.log(`
|
|
97
127
|
\u{1F4C1} \u63D2\u4EF6\u76EE\u5F55: ${chalk__default.cyan(targetDir)}`);
|
|
98
128
|
console.log(`
|
|
99
|
-
\u8FD0\u884C\u4EE5\u4E0B\u547D\u4EE4\u4EE5\u5B89\u88C5\u4F9D\u8D56\u5E76\
|
|
129
|
+
\u8FD0\u884C\u4EE5\u4E0B\u547D\u4EE4\u4EE5\u5B89\u88C5\u4F9D\u8D56\u5E76\u5F00\u59CB\u5F00\u53D1:
|
|
100
130
|
`);
|
|
101
131
|
console.log(chalk__default.cyan(` cd ${path__default.relative(process__default.cwd(), targetDir)}`));
|
|
102
132
|
console.log(chalk__default.cyan(" pnpm install"));
|
|
103
133
|
console.log(chalk__default.cyan(" pnpm build"));
|
|
134
|
+
console.log(chalk__default.cyan(" pnpm dev # \u542F\u52A8\u5F00\u53D1\u670D\u52A1\u5668\uFF0C\u8FD0\u884C playground"));
|
|
135
|
+
console.log(`
|
|
136
|
+
\u{1F680} \u63D2\u4EF6\u5F00\u53D1\u8BF4\u660E:`);
|
|
137
|
+
console.log(` 1. \u4E3B\u8981\u4EE3\u7801\u4F4D\u4E8E ${chalk__default.yellow("src/")} \u76EE\u5F55`);
|
|
138
|
+
console.log(` 2. \u5F00\u53D1\u73AF\u5883\uFF08Playground\uFF09\u4F4D\u4E8E ${chalk__default.yellow("playground/")} \u76EE\u5F55`);
|
|
139
|
+
console.log(` 3. \u8FD0\u884C ${chalk__default.yellow("pnpm dev")} \u542F\u52A8 Vite \u5F00\u53D1\u670D\u52A1\u5668\uFF0C\u8BBF\u95EE ${chalk__default.blue("http://localhost:5173")} \u67E5\u770B\u6F14\u793A\u9875\u9762`);
|
|
140
|
+
console.log(` 4. \u5BF9\u4EE3\u7801\u7684\u4FEE\u6539\u4F1A\u81EA\u52A8\u70ED\u91CD\u8F7D\uFF0C\u5B9E\u65F6\u67E5\u770B\u6548\u679C`);
|
|
104
141
|
} catch (error) {
|
|
105
142
|
spinner.fail("\u274C \u521B\u5EFA\u63D2\u4EF6\u5931\u8D25");
|
|
106
143
|
console.error(chalk__default.red(error));
|
|
107
144
|
}
|
|
108
145
|
}));
|
|
109
|
-
function
|
|
146
|
+
function getKnotxPackageVersions() {
|
|
147
|
+
return __async(this, null, function* () {
|
|
148
|
+
const knotxVersions = {};
|
|
149
|
+
try {
|
|
150
|
+
const packages = [
|
|
151
|
+
"@knotx/core",
|
|
152
|
+
"@knotx/decorators",
|
|
153
|
+
"@knotx/jsx",
|
|
154
|
+
"@knotx/build-config",
|
|
155
|
+
"@knotx/eslint-config",
|
|
156
|
+
"@knotx/typescript-config",
|
|
157
|
+
"@knotx/react",
|
|
158
|
+
"@knotx/plugins-base-render"
|
|
159
|
+
];
|
|
160
|
+
const npmRegistry = "https://registry.npmjs.org";
|
|
161
|
+
for (const pkg of packages) {
|
|
162
|
+
try {
|
|
163
|
+
const version = node_child_process.execSync(`npm view ${pkg} version --registry=${npmRegistry}`, { encoding: "utf8" }).trim();
|
|
164
|
+
knotxVersions[pkg] = `^${version}`;
|
|
165
|
+
} catch (e) {
|
|
166
|
+
console.warn(chalk__default.yellow(`\u26A0\uFE0F \u65E0\u6CD5\u83B7\u53D6 ${pkg} \u7684\u6700\u65B0\u7248\u672C\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u7248\u672C`));
|
|
167
|
+
knotxVersions[pkg] = "latest";
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return knotxVersions;
|
|
171
|
+
} catch (e) {
|
|
172
|
+
console.warn(chalk__default.yellow("\u26A0\uFE0F \u83B7\u53D6 @knotx \u5305\u7248\u672C\u5931\u8D25\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u7248\u672C"));
|
|
173
|
+
return {
|
|
174
|
+
"@knotx/core": "latest",
|
|
175
|
+
"@knotx/decorators": "latest",
|
|
176
|
+
"@knotx/jsx": "latest",
|
|
177
|
+
"@knotx/build-config": "latest",
|
|
178
|
+
"@knotx/eslint-config": "latest",
|
|
179
|
+
"@knotx/typescript-config": "latest"
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function createPackageJson(targetDir, pluginName, environment, knotxVersions) {
|
|
110
185
|
return __async(this, null, function* () {
|
|
111
186
|
const packageJson = {
|
|
112
187
|
name: `@knotx/plugins-${pluginName}`,
|
|
@@ -133,20 +208,21 @@ function createPackageJson(targetDir, pluginName, environment) {
|
|
|
133
208
|
],
|
|
134
209
|
scripts: {
|
|
135
210
|
build: "unbuild --failOnWarn=false",
|
|
136
|
-
dev: "
|
|
211
|
+
dev: "cd ./playground && vite",
|
|
137
212
|
lint: "eslint .",
|
|
138
213
|
typecheck: "tsc --noEmit"
|
|
139
214
|
},
|
|
140
215
|
peerDependencies: {},
|
|
141
216
|
dependencies: {
|
|
142
|
-
"@knotx/core": "
|
|
143
|
-
"@knotx/decorators": "
|
|
144
|
-
"@knotx/jsx": "
|
|
217
|
+
"@knotx/core": knotxVersions["@knotx/core"],
|
|
218
|
+
"@knotx/decorators": knotxVersions["@knotx/decorators"],
|
|
219
|
+
"@knotx/jsx": knotxVersions["@knotx/jsx"]
|
|
145
220
|
},
|
|
146
221
|
devDependencies: {
|
|
147
|
-
"@
|
|
148
|
-
"@knotx/
|
|
149
|
-
"@knotx/
|
|
222
|
+
"@babel/plugin-proposal-decorators": "^7.25.9",
|
|
223
|
+
"@knotx/build-config": knotxVersions["@knotx/build-config"],
|
|
224
|
+
"@knotx/eslint-config": knotxVersions["@knotx/eslint-config"],
|
|
225
|
+
"@knotx/typescript-config": knotxVersions["@knotx/typescript-config"],
|
|
150
226
|
"@types/node": "^22.13.5",
|
|
151
227
|
"eslint": "^9.23.0",
|
|
152
228
|
"typescript": "^5.7.3",
|
|
@@ -158,18 +234,69 @@ function createPackageJson(targetDir, pluginName, environment) {
|
|
|
158
234
|
if (environment === "jsx") ; else if (environment === "react") {
|
|
159
235
|
packageJson.peerDependencies.react = "^17";
|
|
160
236
|
packageJson.peerDependencies["react-dom"] = "^17";
|
|
237
|
+
packageJson.devDependencies["@knotx/react"] = knotxVersions["@knotx/react"];
|
|
238
|
+
packageJson.devDependencies["@knotx/plugins-base-render"] = knotxVersions["@knotx/plugins-base-render"];
|
|
161
239
|
packageJson.devDependencies.react = "^17";
|
|
162
240
|
packageJson.devDependencies["@vitejs/plugin-react"] = "^4.3.4";
|
|
163
241
|
packageJson.devDependencies["react-dom"] = "^17";
|
|
164
242
|
packageJson.devDependencies["@types/react"] = "^17";
|
|
165
243
|
packageJson.devDependencies["@types/react-dom"] = "^17";
|
|
166
244
|
}
|
|
245
|
+
const sortedPackageJson = sortObjectKeys(packageJson);
|
|
167
246
|
yield fs__default.writeFile(
|
|
168
247
|
path__default.join(targetDir, "package.json"),
|
|
169
|
-
JSON.stringify(
|
|
248
|
+
`${JSON.stringify(sortedPackageJson, null, 2)}
|
|
249
|
+
`
|
|
170
250
|
);
|
|
171
251
|
});
|
|
172
252
|
}
|
|
253
|
+
function sortObjectKeys(obj) {
|
|
254
|
+
const keyOrder = [
|
|
255
|
+
"name",
|
|
256
|
+
"type",
|
|
257
|
+
"version",
|
|
258
|
+
"description",
|
|
259
|
+
"license",
|
|
260
|
+
"publishConfig",
|
|
261
|
+
"sideEffects",
|
|
262
|
+
"exports",
|
|
263
|
+
"main",
|
|
264
|
+
"module",
|
|
265
|
+
"types",
|
|
266
|
+
"files",
|
|
267
|
+
"scripts",
|
|
268
|
+
"peerDependencies",
|
|
269
|
+
"dependencies",
|
|
270
|
+
"devDependencies"
|
|
271
|
+
];
|
|
272
|
+
const sortedObj = {};
|
|
273
|
+
for (const key of keyOrder) {
|
|
274
|
+
if (key in obj)
|
|
275
|
+
sortedObj[key] = obj[key];
|
|
276
|
+
}
|
|
277
|
+
const remainingKeys = Object.keys(obj).filter((key) => !keyOrder.includes(key)).sort();
|
|
278
|
+
for (const key of remainingKeys) {
|
|
279
|
+
sortedObj[key] = obj[key];
|
|
280
|
+
}
|
|
281
|
+
for (const key of Object.keys(sortedObj)) {
|
|
282
|
+
if (typeof sortedObj[key] === "object" && sortedObj[key] !== null && !Array.isArray(sortedObj[key])) {
|
|
283
|
+
if (["dependencies", "devDependencies", "peerDependencies"].includes(key)) {
|
|
284
|
+
sortedObj[key] = sortObjectAlphabetically(sortedObj[key]);
|
|
285
|
+
} else {
|
|
286
|
+
sortedObj[key] = sortObjectKeys(sortedObj[key]);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return sortedObj;
|
|
291
|
+
}
|
|
292
|
+
function sortObjectAlphabetically(obj) {
|
|
293
|
+
const sortedObj = {};
|
|
294
|
+
const sortedKeys = Object.keys(obj).sort();
|
|
295
|
+
for (const key of sortedKeys) {
|
|
296
|
+
sortedObj[key] = obj[key];
|
|
297
|
+
}
|
|
298
|
+
return sortedObj;
|
|
299
|
+
}
|
|
173
300
|
function createConfigFiles(targetDir) {
|
|
174
301
|
return __async(this, null, function* () {
|
|
175
302
|
try {
|
|
@@ -221,17 +348,44 @@ function createSourceFiles(targetDir, pluginName, environment) {
|
|
|
221
348
|
const pluginContent = pluginTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{capitalizedPluginName\}\}/g, capitalize(pluginName));
|
|
222
349
|
const fileName = `${pluginName}.tsx`;
|
|
223
350
|
yield fs__default.writeFile(path__default.join(targetDir, "src", fileName), pluginContent);
|
|
351
|
+
yield createPlayground(targetDir, pluginName, environment);
|
|
224
352
|
} catch (error) {
|
|
225
353
|
console.error("\u65E0\u6CD5\u8BFB\u53D6\u6A21\u677F\u6587\u4EF6:", error);
|
|
226
354
|
}
|
|
227
355
|
});
|
|
228
356
|
}
|
|
357
|
+
function createPlayground(targetDir, pluginName, environment) {
|
|
358
|
+
return __async(this, null, function* () {
|
|
359
|
+
const playgroundDir = path__default.join(targetDir, "playground");
|
|
360
|
+
yield fs__default.ensureDir(playgroundDir);
|
|
361
|
+
try {
|
|
362
|
+
const viteConfigTemplate = yield fs__default.readFile(path__default.join(templatesDir, "vite.config.ts.template"), "utf-8");
|
|
363
|
+
const htmlTemplate = yield fs__default.readFile(path__default.join(templatesDir, "playground/index.html.template"), "utf-8");
|
|
364
|
+
const tsConfigTemplate = yield fs__default.readFile(path__default.join(templatesDir, "playground/tsconfig.json.template"), "utf-8");
|
|
365
|
+
const isReact = environment === "react";
|
|
366
|
+
const mainTemplate = yield fs__default.readFile(
|
|
367
|
+
path__default.join(templatesDir, `playground/main.${isReact ? "react" : "jsx"}.template`),
|
|
368
|
+
"utf-8"
|
|
369
|
+
);
|
|
370
|
+
const fileExtension = isReact ? "tsx" : "ts";
|
|
371
|
+
const viteConfigContent = viteConfigTemplate.replace(new RegExp("\\{\\{#if isReact\\}\\}(.*?)\\{\\{\\/if\\}\\}", "gs"), isReact ? "$1" : "");
|
|
372
|
+
const htmlContent = htmlTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{capitalizedPluginName\}\}/g, capitalize(pluginName)).replace(/\{\{fileExtension\}\}/g, fileExtension);
|
|
373
|
+
const mainContent = mainTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{capitalizedPluginName\}\}/g, capitalize(pluginName));
|
|
374
|
+
yield fs__default.writeFile(path__default.join(playgroundDir, "vite.config.ts"), viteConfigContent);
|
|
375
|
+
yield fs__default.writeFile(path__default.join(playgroundDir, "index.html"), htmlContent);
|
|
376
|
+
yield fs__default.writeFile(path__default.join(playgroundDir, `main.${fileExtension}`), mainContent);
|
|
377
|
+
yield fs__default.writeFile(path__default.join(playgroundDir, "tsconfig.json"), tsConfigTemplate);
|
|
378
|
+
} catch (error) {
|
|
379
|
+
console.error("\u8BFB\u53D6 playground \u6A21\u677F\u6587\u4EF6\u5931\u8D25:", error);
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
}
|
|
229
383
|
function createReadme(targetDir, pluginName) {
|
|
230
384
|
return __async(this, null, function* () {
|
|
231
385
|
try {
|
|
232
386
|
const readmeTemplate = yield fs__default.readFile(path__default.join(templatesDir, "README.md.template"), "utf-8");
|
|
233
387
|
const description = `${capitalize(pluginName)} Plugin for Knotx`;
|
|
234
|
-
const readmeContent = readmeTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{description\}\}/g, description);
|
|
388
|
+
const readmeContent = readmeTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{capitalizedPluginName\}\}/g, capitalize(pluginName)).replace(/\{\{description\}\}/g, description);
|
|
235
389
|
yield fs__default.writeFile(path__default.join(targetDir, "README.md"), readmeContent);
|
|
236
390
|
} catch (error) {
|
|
237
391
|
console.error("\u65E0\u6CD5\u8BFB\u53D6 README \u6A21\u677F\u6587\u4EF6:", error);
|
|
@@ -268,6 +422,67 @@ MIT`;
|
|
|
268
422
|
function capitalize(str) {
|
|
269
423
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
270
424
|
}
|
|
425
|
+
function createGitignore(targetDir) {
|
|
426
|
+
return __async(this, null, function* () {
|
|
427
|
+
try {
|
|
428
|
+
const gitignoreTemplate = yield fs__default.readFile(path__default.join(templatesDir, ".gitignore.template"), "utf-8");
|
|
429
|
+
yield fs__default.writeFile(path__default.join(targetDir, ".gitignore"), gitignoreTemplate);
|
|
430
|
+
} catch (error) {
|
|
431
|
+
console.error("\u65E0\u6CD5\u8BFB\u53D6 .gitignore \u6A21\u677F\u6587\u4EF6:", error);
|
|
432
|
+
const gitignoreContent = `# \u4F9D\u8D56\u76EE\u5F55
|
|
433
|
+
node_modules
|
|
434
|
+
|
|
435
|
+
# \u6784\u5EFA\u4EA7\u7269
|
|
436
|
+
dist
|
|
437
|
+
build
|
|
438
|
+
|
|
439
|
+
# \u65E5\u5FD7\u6587\u4EF6
|
|
440
|
+
logs
|
|
441
|
+
*.log
|
|
442
|
+
npm-debug.log*
|
|
443
|
+
yarn-debug.log*
|
|
444
|
+
yarn-error.log*
|
|
445
|
+
pnpm-debug.log*
|
|
446
|
+
|
|
447
|
+
# \u73AF\u5883\u53D8\u91CF\u6587\u4EF6
|
|
448
|
+
.env
|
|
449
|
+
.env.local
|
|
450
|
+
|
|
451
|
+
# \u7F16\u8F91\u5668\u76EE\u5F55\u548C\u6587\u4EF6
|
|
452
|
+
.vscode
|
|
453
|
+
.idea
|
|
454
|
+
.DS_Store
|
|
455
|
+
|
|
456
|
+
# \u6D4B\u8BD5\u8986\u76D6\u7387
|
|
457
|
+
coverage
|
|
458
|
+
`;
|
|
459
|
+
yield fs__default.writeFile(path__default.join(targetDir, ".gitignore"), gitignoreContent);
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
function initGitRepo(targetDir) {
|
|
464
|
+
try {
|
|
465
|
+
node_child_process.execSync("git init", { cwd: targetDir, stdio: "ignore" });
|
|
466
|
+
node_child_process.execSync("git add .", { cwd: targetDir, stdio: "ignore" });
|
|
467
|
+
node_child_process.execSync('git commit -m "Initial commit with Knotx plugin template"', {
|
|
468
|
+
cwd: targetDir,
|
|
469
|
+
stdio: "ignore",
|
|
470
|
+
env: __spreadProps(__spreadValues({}, process__default.env), {
|
|
471
|
+
GIT_AUTHOR_NAME: "Knotx CLI",
|
|
472
|
+
GIT_AUTHOR_EMAIL: "knotx-cli@noreply.github.com",
|
|
473
|
+
GIT_COMMITTER_NAME: "Knotx CLI",
|
|
474
|
+
GIT_COMMITTER_EMAIL: "knotx-cli@noreply.github.com"
|
|
475
|
+
})
|
|
476
|
+
});
|
|
477
|
+
return true;
|
|
478
|
+
} catch (error) {
|
|
479
|
+
if (error instanceof Error) {
|
|
480
|
+
throw new TypeError(`Git \u521D\u59CB\u5316\u5931\u8D25: ${error.message}`);
|
|
481
|
+
} else {
|
|
482
|
+
throw new TypeError(`Git \u521D\u59CB\u5316\u5931\u8D25: ${String(error)}`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
271
486
|
program.parse();
|
|
272
487
|
if (!process__default.argv.slice(2).length) {
|
|
273
488
|
program.outputHelp();
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { execSync } from 'node:child_process';
|
|
2
3
|
import path from 'node:path';
|
|
3
4
|
import process from 'node:process';
|
|
4
5
|
import chalk from 'chalk';
|
|
@@ -7,6 +8,25 @@ import fs from 'fs-extra';
|
|
|
7
8
|
import inquirer from 'inquirer';
|
|
8
9
|
import ora from 'ora';
|
|
9
10
|
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __defProps = Object.defineProperties;
|
|
13
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
14
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
17
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
18
|
+
var __spreadValues = (a, b) => {
|
|
19
|
+
for (var prop in b || (b = {}))
|
|
20
|
+
if (__hasOwnProp.call(b, prop))
|
|
21
|
+
__defNormalProp(a, prop, b[prop]);
|
|
22
|
+
if (__getOwnPropSymbols)
|
|
23
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
24
|
+
if (__propIsEnum.call(b, prop))
|
|
25
|
+
__defNormalProp(a, prop, b[prop]);
|
|
26
|
+
}
|
|
27
|
+
return a;
|
|
28
|
+
};
|
|
29
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
10
30
|
var __async = (__this, __arguments, generator) => {
|
|
11
31
|
return new Promise((resolve, reject) => {
|
|
12
32
|
var fulfilled = (value) => {
|
|
@@ -30,7 +50,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
30
50
|
const templatesDir = path.resolve(__dirname, "..", "templates");
|
|
31
51
|
const program = new Command();
|
|
32
52
|
program.name("knotx").description("Knotx CLI - \u521B\u5EFA\u548C\u7BA1\u7406Knotx\u63D2\u4EF6").version("0.0.1");
|
|
33
|
-
program.command("create-plugin").description("\u521B\u5EFA\u4E00\u4E2A\u65B0\u7684Knotx\u63D2\u4EF6").action(() => __async(undefined, null, function* () {
|
|
53
|
+
program.command("create-plugin").description("\u521B\u5EFA\u4E00\u4E2A\u65B0\u7684Knotx\u63D2\u4EF6\uFF0C\u5305\u542B\u5F00\u53D1\u73AF\u5883\u548C\u6F14\u793A playground").action(() => __async(undefined, null, function* () {
|
|
34
54
|
console.log(chalk.blue("\u{1F539} \u6B22\u8FCE\u4F7F\u7528 Knotx \u63D2\u4EF6\u521B\u5EFA\u5DE5\u5177\uFF01"));
|
|
35
55
|
const answers = yield inquirer.prompt([
|
|
36
56
|
{
|
|
@@ -59,43 +79,98 @@ program.command("create-plugin").description("\u521B\u5EFA\u4E00\u4E2A\u65B0\u76
|
|
|
59
79
|
console.log(chalk.red(`\u274C \u9519\u8BEF: \u76EE\u5F55 ${fullPluginName} \u5DF2\u5B58\u5728`));
|
|
60
80
|
return;
|
|
61
81
|
}
|
|
62
|
-
|
|
82
|
+
let spinner = ora("\u{1F4C2} \u521B\u5EFA\u63D2\u4EF6\u76EE\u5F55...").start();
|
|
63
83
|
try {
|
|
64
84
|
yield fs.ensureDir(targetDir);
|
|
65
85
|
yield fs.ensureDir(path.join(targetDir, "src"));
|
|
66
86
|
spinner.succeed("\u{1F4C2} \u63D2\u4EF6\u76EE\u5F55\u521B\u5EFA\u6210\u529F");
|
|
67
|
-
spinner
|
|
68
|
-
|
|
69
|
-
|
|
87
|
+
spinner = ora("\u{1F50D} \u83B7\u53D6 @knotx \u5305\u7684\u6700\u65B0\u7248\u672C...").start();
|
|
88
|
+
const knotxVersions = yield getKnotxPackageVersions();
|
|
89
|
+
spinner.succeed("\u{1F50D} @knotx \u5305\u7248\u672C\u4FE1\u606F\u83B7\u53D6\u6210\u529F");
|
|
90
|
+
spinner = ora("\u{1F4C4} \u521B\u5EFA package.json...").start();
|
|
91
|
+
yield createPackageJson(targetDir, pluginName, environment, knotxVersions);
|
|
70
92
|
spinner.succeed("\u{1F4C4} package.json \u521B\u5EFA\u6210\u529F");
|
|
71
|
-
spinner
|
|
72
|
-
spinner.start();
|
|
93
|
+
spinner = ora("\u{1F4C4} \u521B\u5EFA\u914D\u7F6E\u6587\u4EF6...").start();
|
|
73
94
|
yield createConfigFiles(targetDir);
|
|
74
95
|
spinner.succeed("\u{1F4C4} \u914D\u7F6E\u6587\u4EF6\u521B\u5EFA\u6210\u529F");
|
|
75
|
-
spinner
|
|
76
|
-
spinner.start();
|
|
96
|
+
spinner = ora("\u{1F4C4} \u521B\u5EFA\u6E90\u4EE3\u7801\u6587\u4EF6...").start();
|
|
77
97
|
yield createSourceFiles(targetDir, pluginName, environment);
|
|
78
98
|
spinner.succeed("\u{1F4C4} \u6E90\u4EE3\u7801\u6587\u4EF6\u521B\u5EFA\u6210\u529F");
|
|
79
|
-
spinner
|
|
80
|
-
spinner.start();
|
|
99
|
+
spinner = ora("\u{1F4C4} \u521B\u5EFA README.md...").start();
|
|
81
100
|
yield createReadme(targetDir, pluginName);
|
|
82
101
|
spinner.succeed("\u{1F4C4} README.md \u521B\u5EFA\u6210\u529F");
|
|
102
|
+
spinner = ora("\u{1F4C4} \u521B\u5EFA .gitignore \u6587\u4EF6...").start();
|
|
103
|
+
yield createGitignore(targetDir);
|
|
104
|
+
spinner.succeed("\u{1F4C4} .gitignore \u521B\u5EFA\u6210\u529F");
|
|
105
|
+
spinner = ora("\u{1F527} \u521D\u59CB\u5316 Git \u4ED3\u5E93...").start();
|
|
106
|
+
try {
|
|
107
|
+
initGitRepo(targetDir);
|
|
108
|
+
spinner.succeed("\u{1F527} Git \u4ED3\u5E93\u521D\u59CB\u5316\u6210\u529F");
|
|
109
|
+
} catch (error) {
|
|
110
|
+
spinner.warn("\u26A0\uFE0F Git \u4ED3\u5E93\u521D\u59CB\u5316\u5931\u8D25\uFF0C\u8BF7\u624B\u52A8\u521D\u59CB\u5316");
|
|
111
|
+
console.warn(chalk.yellow(` \u53EF\u80FD\u7684\u539F\u56E0: ${error.message}`));
|
|
112
|
+
}
|
|
83
113
|
console.log(chalk.green(`
|
|
84
114
|
\u2705 \u63D2\u4EF6 ${chalk.bold(fullPluginName)} \u521B\u5EFA\u6210\u529F\uFF01`));
|
|
85
115
|
console.log(`
|
|
86
116
|
\u{1F4C1} \u63D2\u4EF6\u76EE\u5F55: ${chalk.cyan(targetDir)}`);
|
|
87
117
|
console.log(`
|
|
88
|
-
\u8FD0\u884C\u4EE5\u4E0B\u547D\u4EE4\u4EE5\u5B89\u88C5\u4F9D\u8D56\u5E76\
|
|
118
|
+
\u8FD0\u884C\u4EE5\u4E0B\u547D\u4EE4\u4EE5\u5B89\u88C5\u4F9D\u8D56\u5E76\u5F00\u59CB\u5F00\u53D1:
|
|
89
119
|
`);
|
|
90
120
|
console.log(chalk.cyan(` cd ${path.relative(process.cwd(), targetDir)}`));
|
|
91
121
|
console.log(chalk.cyan(" pnpm install"));
|
|
92
122
|
console.log(chalk.cyan(" pnpm build"));
|
|
123
|
+
console.log(chalk.cyan(" pnpm dev # \u542F\u52A8\u5F00\u53D1\u670D\u52A1\u5668\uFF0C\u8FD0\u884C playground"));
|
|
124
|
+
console.log(`
|
|
125
|
+
\u{1F680} \u63D2\u4EF6\u5F00\u53D1\u8BF4\u660E:`);
|
|
126
|
+
console.log(` 1. \u4E3B\u8981\u4EE3\u7801\u4F4D\u4E8E ${chalk.yellow("src/")} \u76EE\u5F55`);
|
|
127
|
+
console.log(` 2. \u5F00\u53D1\u73AF\u5883\uFF08Playground\uFF09\u4F4D\u4E8E ${chalk.yellow("playground/")} \u76EE\u5F55`);
|
|
128
|
+
console.log(` 3. \u8FD0\u884C ${chalk.yellow("pnpm dev")} \u542F\u52A8 Vite \u5F00\u53D1\u670D\u52A1\u5668\uFF0C\u8BBF\u95EE ${chalk.blue("http://localhost:5173")} \u67E5\u770B\u6F14\u793A\u9875\u9762`);
|
|
129
|
+
console.log(` 4. \u5BF9\u4EE3\u7801\u7684\u4FEE\u6539\u4F1A\u81EA\u52A8\u70ED\u91CD\u8F7D\uFF0C\u5B9E\u65F6\u67E5\u770B\u6548\u679C`);
|
|
93
130
|
} catch (error) {
|
|
94
131
|
spinner.fail("\u274C \u521B\u5EFA\u63D2\u4EF6\u5931\u8D25");
|
|
95
132
|
console.error(chalk.red(error));
|
|
96
133
|
}
|
|
97
134
|
}));
|
|
98
|
-
function
|
|
135
|
+
function getKnotxPackageVersions() {
|
|
136
|
+
return __async(this, null, function* () {
|
|
137
|
+
const knotxVersions = {};
|
|
138
|
+
try {
|
|
139
|
+
const packages = [
|
|
140
|
+
"@knotx/core",
|
|
141
|
+
"@knotx/decorators",
|
|
142
|
+
"@knotx/jsx",
|
|
143
|
+
"@knotx/build-config",
|
|
144
|
+
"@knotx/eslint-config",
|
|
145
|
+
"@knotx/typescript-config",
|
|
146
|
+
"@knotx/react",
|
|
147
|
+
"@knotx/plugins-base-render"
|
|
148
|
+
];
|
|
149
|
+
const npmRegistry = "https://registry.npmjs.org";
|
|
150
|
+
for (const pkg of packages) {
|
|
151
|
+
try {
|
|
152
|
+
const version = execSync(`npm view ${pkg} version --registry=${npmRegistry}`, { encoding: "utf8" }).trim();
|
|
153
|
+
knotxVersions[pkg] = `^${version}`;
|
|
154
|
+
} catch (e) {
|
|
155
|
+
console.warn(chalk.yellow(`\u26A0\uFE0F \u65E0\u6CD5\u83B7\u53D6 ${pkg} \u7684\u6700\u65B0\u7248\u672C\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u7248\u672C`));
|
|
156
|
+
knotxVersions[pkg] = "latest";
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return knotxVersions;
|
|
160
|
+
} catch (e) {
|
|
161
|
+
console.warn(chalk.yellow("\u26A0\uFE0F \u83B7\u53D6 @knotx \u5305\u7248\u672C\u5931\u8D25\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u7248\u672C"));
|
|
162
|
+
return {
|
|
163
|
+
"@knotx/core": "latest",
|
|
164
|
+
"@knotx/decorators": "latest",
|
|
165
|
+
"@knotx/jsx": "latest",
|
|
166
|
+
"@knotx/build-config": "latest",
|
|
167
|
+
"@knotx/eslint-config": "latest",
|
|
168
|
+
"@knotx/typescript-config": "latest"
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function createPackageJson(targetDir, pluginName, environment, knotxVersions) {
|
|
99
174
|
return __async(this, null, function* () {
|
|
100
175
|
const packageJson = {
|
|
101
176
|
name: `@knotx/plugins-${pluginName}`,
|
|
@@ -122,20 +197,21 @@ function createPackageJson(targetDir, pluginName, environment) {
|
|
|
122
197
|
],
|
|
123
198
|
scripts: {
|
|
124
199
|
build: "unbuild --failOnWarn=false",
|
|
125
|
-
dev: "
|
|
200
|
+
dev: "cd ./playground && vite",
|
|
126
201
|
lint: "eslint .",
|
|
127
202
|
typecheck: "tsc --noEmit"
|
|
128
203
|
},
|
|
129
204
|
peerDependencies: {},
|
|
130
205
|
dependencies: {
|
|
131
|
-
"@knotx/core": "
|
|
132
|
-
"@knotx/decorators": "
|
|
133
|
-
"@knotx/jsx": "
|
|
206
|
+
"@knotx/core": knotxVersions["@knotx/core"],
|
|
207
|
+
"@knotx/decorators": knotxVersions["@knotx/decorators"],
|
|
208
|
+
"@knotx/jsx": knotxVersions["@knotx/jsx"]
|
|
134
209
|
},
|
|
135
210
|
devDependencies: {
|
|
136
|
-
"@
|
|
137
|
-
"@knotx/
|
|
138
|
-
"@knotx/
|
|
211
|
+
"@babel/plugin-proposal-decorators": "^7.25.9",
|
|
212
|
+
"@knotx/build-config": knotxVersions["@knotx/build-config"],
|
|
213
|
+
"@knotx/eslint-config": knotxVersions["@knotx/eslint-config"],
|
|
214
|
+
"@knotx/typescript-config": knotxVersions["@knotx/typescript-config"],
|
|
139
215
|
"@types/node": "^22.13.5",
|
|
140
216
|
"eslint": "^9.23.0",
|
|
141
217
|
"typescript": "^5.7.3",
|
|
@@ -147,18 +223,69 @@ function createPackageJson(targetDir, pluginName, environment) {
|
|
|
147
223
|
if (environment === "jsx") ; else if (environment === "react") {
|
|
148
224
|
packageJson.peerDependencies.react = "^17";
|
|
149
225
|
packageJson.peerDependencies["react-dom"] = "^17";
|
|
226
|
+
packageJson.devDependencies["@knotx/react"] = knotxVersions["@knotx/react"];
|
|
227
|
+
packageJson.devDependencies["@knotx/plugins-base-render"] = knotxVersions["@knotx/plugins-base-render"];
|
|
150
228
|
packageJson.devDependencies.react = "^17";
|
|
151
229
|
packageJson.devDependencies["@vitejs/plugin-react"] = "^4.3.4";
|
|
152
230
|
packageJson.devDependencies["react-dom"] = "^17";
|
|
153
231
|
packageJson.devDependencies["@types/react"] = "^17";
|
|
154
232
|
packageJson.devDependencies["@types/react-dom"] = "^17";
|
|
155
233
|
}
|
|
234
|
+
const sortedPackageJson = sortObjectKeys(packageJson);
|
|
156
235
|
yield fs.writeFile(
|
|
157
236
|
path.join(targetDir, "package.json"),
|
|
158
|
-
JSON.stringify(
|
|
237
|
+
`${JSON.stringify(sortedPackageJson, null, 2)}
|
|
238
|
+
`
|
|
159
239
|
);
|
|
160
240
|
});
|
|
161
241
|
}
|
|
242
|
+
function sortObjectKeys(obj) {
|
|
243
|
+
const keyOrder = [
|
|
244
|
+
"name",
|
|
245
|
+
"type",
|
|
246
|
+
"version",
|
|
247
|
+
"description",
|
|
248
|
+
"license",
|
|
249
|
+
"publishConfig",
|
|
250
|
+
"sideEffects",
|
|
251
|
+
"exports",
|
|
252
|
+
"main",
|
|
253
|
+
"module",
|
|
254
|
+
"types",
|
|
255
|
+
"files",
|
|
256
|
+
"scripts",
|
|
257
|
+
"peerDependencies",
|
|
258
|
+
"dependencies",
|
|
259
|
+
"devDependencies"
|
|
260
|
+
];
|
|
261
|
+
const sortedObj = {};
|
|
262
|
+
for (const key of keyOrder) {
|
|
263
|
+
if (key in obj)
|
|
264
|
+
sortedObj[key] = obj[key];
|
|
265
|
+
}
|
|
266
|
+
const remainingKeys = Object.keys(obj).filter((key) => !keyOrder.includes(key)).sort();
|
|
267
|
+
for (const key of remainingKeys) {
|
|
268
|
+
sortedObj[key] = obj[key];
|
|
269
|
+
}
|
|
270
|
+
for (const key of Object.keys(sortedObj)) {
|
|
271
|
+
if (typeof sortedObj[key] === "object" && sortedObj[key] !== null && !Array.isArray(sortedObj[key])) {
|
|
272
|
+
if (["dependencies", "devDependencies", "peerDependencies"].includes(key)) {
|
|
273
|
+
sortedObj[key] = sortObjectAlphabetically(sortedObj[key]);
|
|
274
|
+
} else {
|
|
275
|
+
sortedObj[key] = sortObjectKeys(sortedObj[key]);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return sortedObj;
|
|
280
|
+
}
|
|
281
|
+
function sortObjectAlphabetically(obj) {
|
|
282
|
+
const sortedObj = {};
|
|
283
|
+
const sortedKeys = Object.keys(obj).sort();
|
|
284
|
+
for (const key of sortedKeys) {
|
|
285
|
+
sortedObj[key] = obj[key];
|
|
286
|
+
}
|
|
287
|
+
return sortedObj;
|
|
288
|
+
}
|
|
162
289
|
function createConfigFiles(targetDir) {
|
|
163
290
|
return __async(this, null, function* () {
|
|
164
291
|
try {
|
|
@@ -210,17 +337,44 @@ function createSourceFiles(targetDir, pluginName, environment) {
|
|
|
210
337
|
const pluginContent = pluginTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{capitalizedPluginName\}\}/g, capitalize(pluginName));
|
|
211
338
|
const fileName = `${pluginName}.tsx`;
|
|
212
339
|
yield fs.writeFile(path.join(targetDir, "src", fileName), pluginContent);
|
|
340
|
+
yield createPlayground(targetDir, pluginName, environment);
|
|
213
341
|
} catch (error) {
|
|
214
342
|
console.error("\u65E0\u6CD5\u8BFB\u53D6\u6A21\u677F\u6587\u4EF6:", error);
|
|
215
343
|
}
|
|
216
344
|
});
|
|
217
345
|
}
|
|
346
|
+
function createPlayground(targetDir, pluginName, environment) {
|
|
347
|
+
return __async(this, null, function* () {
|
|
348
|
+
const playgroundDir = path.join(targetDir, "playground");
|
|
349
|
+
yield fs.ensureDir(playgroundDir);
|
|
350
|
+
try {
|
|
351
|
+
const viteConfigTemplate = yield fs.readFile(path.join(templatesDir, "vite.config.ts.template"), "utf-8");
|
|
352
|
+
const htmlTemplate = yield fs.readFile(path.join(templatesDir, "playground/index.html.template"), "utf-8");
|
|
353
|
+
const tsConfigTemplate = yield fs.readFile(path.join(templatesDir, "playground/tsconfig.json.template"), "utf-8");
|
|
354
|
+
const isReact = environment === "react";
|
|
355
|
+
const mainTemplate = yield fs.readFile(
|
|
356
|
+
path.join(templatesDir, `playground/main.${isReact ? "react" : "jsx"}.template`),
|
|
357
|
+
"utf-8"
|
|
358
|
+
);
|
|
359
|
+
const fileExtension = isReact ? "tsx" : "ts";
|
|
360
|
+
const viteConfigContent = viteConfigTemplate.replace(new RegExp("\\{\\{#if isReact\\}\\}(.*?)\\{\\{\\/if\\}\\}", "gs"), isReact ? "$1" : "");
|
|
361
|
+
const htmlContent = htmlTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{capitalizedPluginName\}\}/g, capitalize(pluginName)).replace(/\{\{fileExtension\}\}/g, fileExtension);
|
|
362
|
+
const mainContent = mainTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{capitalizedPluginName\}\}/g, capitalize(pluginName));
|
|
363
|
+
yield fs.writeFile(path.join(playgroundDir, "vite.config.ts"), viteConfigContent);
|
|
364
|
+
yield fs.writeFile(path.join(playgroundDir, "index.html"), htmlContent);
|
|
365
|
+
yield fs.writeFile(path.join(playgroundDir, `main.${fileExtension}`), mainContent);
|
|
366
|
+
yield fs.writeFile(path.join(playgroundDir, "tsconfig.json"), tsConfigTemplate);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
console.error("\u8BFB\u53D6 playground \u6A21\u677F\u6587\u4EF6\u5931\u8D25:", error);
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
218
372
|
function createReadme(targetDir, pluginName) {
|
|
219
373
|
return __async(this, null, function* () {
|
|
220
374
|
try {
|
|
221
375
|
const readmeTemplate = yield fs.readFile(path.join(templatesDir, "README.md.template"), "utf-8");
|
|
222
376
|
const description = `${capitalize(pluginName)} Plugin for Knotx`;
|
|
223
|
-
const readmeContent = readmeTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{description\}\}/g, description);
|
|
377
|
+
const readmeContent = readmeTemplate.replace(/\{\{pluginName\}\}/g, pluginName).replace(/\{\{capitalizedPluginName\}\}/g, capitalize(pluginName)).replace(/\{\{description\}\}/g, description);
|
|
224
378
|
yield fs.writeFile(path.join(targetDir, "README.md"), readmeContent);
|
|
225
379
|
} catch (error) {
|
|
226
380
|
console.error("\u65E0\u6CD5\u8BFB\u53D6 README \u6A21\u677F\u6587\u4EF6:", error);
|
|
@@ -257,6 +411,67 @@ MIT`;
|
|
|
257
411
|
function capitalize(str) {
|
|
258
412
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
259
413
|
}
|
|
414
|
+
function createGitignore(targetDir) {
|
|
415
|
+
return __async(this, null, function* () {
|
|
416
|
+
try {
|
|
417
|
+
const gitignoreTemplate = yield fs.readFile(path.join(templatesDir, ".gitignore.template"), "utf-8");
|
|
418
|
+
yield fs.writeFile(path.join(targetDir, ".gitignore"), gitignoreTemplate);
|
|
419
|
+
} catch (error) {
|
|
420
|
+
console.error("\u65E0\u6CD5\u8BFB\u53D6 .gitignore \u6A21\u677F\u6587\u4EF6:", error);
|
|
421
|
+
const gitignoreContent = `# \u4F9D\u8D56\u76EE\u5F55
|
|
422
|
+
node_modules
|
|
423
|
+
|
|
424
|
+
# \u6784\u5EFA\u4EA7\u7269
|
|
425
|
+
dist
|
|
426
|
+
build
|
|
427
|
+
|
|
428
|
+
# \u65E5\u5FD7\u6587\u4EF6
|
|
429
|
+
logs
|
|
430
|
+
*.log
|
|
431
|
+
npm-debug.log*
|
|
432
|
+
yarn-debug.log*
|
|
433
|
+
yarn-error.log*
|
|
434
|
+
pnpm-debug.log*
|
|
435
|
+
|
|
436
|
+
# \u73AF\u5883\u53D8\u91CF\u6587\u4EF6
|
|
437
|
+
.env
|
|
438
|
+
.env.local
|
|
439
|
+
|
|
440
|
+
# \u7F16\u8F91\u5668\u76EE\u5F55\u548C\u6587\u4EF6
|
|
441
|
+
.vscode
|
|
442
|
+
.idea
|
|
443
|
+
.DS_Store
|
|
444
|
+
|
|
445
|
+
# \u6D4B\u8BD5\u8986\u76D6\u7387
|
|
446
|
+
coverage
|
|
447
|
+
`;
|
|
448
|
+
yield fs.writeFile(path.join(targetDir, ".gitignore"), gitignoreContent);
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
function initGitRepo(targetDir) {
|
|
453
|
+
try {
|
|
454
|
+
execSync("git init", { cwd: targetDir, stdio: "ignore" });
|
|
455
|
+
execSync("git add .", { cwd: targetDir, stdio: "ignore" });
|
|
456
|
+
execSync('git commit -m "Initial commit with Knotx plugin template"', {
|
|
457
|
+
cwd: targetDir,
|
|
458
|
+
stdio: "ignore",
|
|
459
|
+
env: __spreadProps(__spreadValues({}, process.env), {
|
|
460
|
+
GIT_AUTHOR_NAME: "Knotx CLI",
|
|
461
|
+
GIT_AUTHOR_EMAIL: "knotx-cli@noreply.github.com",
|
|
462
|
+
GIT_COMMITTER_NAME: "Knotx CLI",
|
|
463
|
+
GIT_COMMITTER_EMAIL: "knotx-cli@noreply.github.com"
|
|
464
|
+
})
|
|
465
|
+
});
|
|
466
|
+
return true;
|
|
467
|
+
} catch (error) {
|
|
468
|
+
if (error instanceof Error) {
|
|
469
|
+
throw new TypeError(`Git \u521D\u59CB\u5316\u5931\u8D25: ${error.message}`);
|
|
470
|
+
} else {
|
|
471
|
+
throw new TypeError(`Git \u521D\u59CB\u5316\u5931\u8D25: ${String(error)}`);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
260
475
|
program.parse();
|
|
261
476
|
if (!process.argv.slice(2).length) {
|
|
262
477
|
program.outputHelp();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knotx/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "CLI tool for Knotx",
|
|
5
5
|
"author": "boenfu",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/fs-extra": "^11.0.4",
|
|
42
42
|
"@types/inquirer": "^9.0.7",
|
|
43
|
-
"@knotx/build-config": "0.0.
|
|
44
|
-
"@knotx/eslint-config": "0.0.
|
|
45
|
-
"@knotx/typescript-config": "0.0.
|
|
43
|
+
"@knotx/build-config": "0.0.2",
|
|
44
|
+
"@knotx/eslint-config": "0.0.2",
|
|
45
|
+
"@knotx/typescript-config": "0.0.2"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "unbuild --failOnWarn=false",
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# 依赖目录
|
|
2
|
+
node_modules
|
|
3
|
+
.pnp
|
|
4
|
+
.pnp.js
|
|
5
|
+
|
|
6
|
+
# 构建产物
|
|
7
|
+
dist
|
|
8
|
+
build
|
|
9
|
+
*.tsbuildinfo
|
|
10
|
+
|
|
11
|
+
# 日志文件
|
|
12
|
+
logs
|
|
13
|
+
*.log
|
|
14
|
+
npm-debug.log*
|
|
15
|
+
yarn-debug.log*
|
|
16
|
+
yarn-error.log*
|
|
17
|
+
pnpm-debug.log*
|
|
18
|
+
|
|
19
|
+
# 环境变量文件
|
|
20
|
+
.env
|
|
21
|
+
.env.local
|
|
22
|
+
.env.development.local
|
|
23
|
+
.env.test.local
|
|
24
|
+
.env.production.local
|
|
25
|
+
|
|
26
|
+
# 编辑器目录和文件
|
|
27
|
+
.vscode/*
|
|
28
|
+
!.vscode/extensions.json
|
|
29
|
+
!.vscode/settings.json
|
|
30
|
+
.idea
|
|
31
|
+
.DS_Store
|
|
32
|
+
*.suo
|
|
33
|
+
*.ntvs*
|
|
34
|
+
*.njsproj
|
|
35
|
+
*.sln
|
|
36
|
+
*.sw?
|
|
37
|
+
|
|
38
|
+
# 测试覆盖率
|
|
39
|
+
coverage
|
|
40
|
+
|
|
41
|
+
# 缓存
|
|
42
|
+
.eslintcache
|
|
43
|
+
.stylelintcache
|
|
44
|
+
.npm
|
|
45
|
+
.cache
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default } from '@knotx/eslint-config'
|
|
1
|
+
export { default } from '@knotx/eslint-config'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './{{pluginName}}'
|
|
1
|
+
export * from './{{pluginName}}'
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>{{capitalizedPluginName}} Plugin Playground</title>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
|
10
|
+
margin: 0;
|
|
11
|
+
padding: 20px;
|
|
12
|
+
background-color: #f7f7f7;
|
|
13
|
+
}
|
|
14
|
+
.container {
|
|
15
|
+
max-width: 800px;
|
|
16
|
+
margin: 0 auto;
|
|
17
|
+
background-color: white;
|
|
18
|
+
padding: 20px;
|
|
19
|
+
border-radius: 5px;
|
|
20
|
+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
|
21
|
+
}
|
|
22
|
+
h1 {
|
|
23
|
+
color: #333;
|
|
24
|
+
border-bottom: 1px solid #eee;
|
|
25
|
+
padding-bottom: 10px;
|
|
26
|
+
}
|
|
27
|
+
</style>
|
|
28
|
+
</head>
|
|
29
|
+
<body>
|
|
30
|
+
<div class="container">
|
|
31
|
+
<h1>{{capitalizedPluginName}} Plugin Demo</h1>
|
|
32
|
+
<p>这是 {{pluginName}} 插件的演示页面。您可以在这里测试插件的功能。</p>
|
|
33
|
+
|
|
34
|
+
<div id="app">
|
|
35
|
+
<!-- 插件内容将在这里展示 -->
|
|
36
|
+
</div>
|
|
37
|
+
</div>
|
|
38
|
+
|
|
39
|
+
<script type="module" src="./main.{{fileExtension}}"></script>
|
|
40
|
+
</body>
|
|
41
|
+
</html>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { {{capitalizedPluginName}} } from '../src'
|
|
2
|
+
|
|
3
|
+
// 在这里初始化和使用您的插件
|
|
4
|
+
const plugin = new {{capitalizedPluginName}}()
|
|
5
|
+
plugin.init()
|
|
6
|
+
|
|
7
|
+
// 更新演示区域
|
|
8
|
+
const appElement = document.getElementById('app')
|
|
9
|
+
if (appElement) {
|
|
10
|
+
appElement.innerHTML += '<p>插件已初始化,请查看控制台输出</p>'
|
|
11
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { BaseRender } from '@knotx/plugins-base-render'
|
|
2
|
+
import { Knotx } from '@knotx/react'
|
|
3
|
+
import { StrictMode } from 'react'
|
|
4
|
+
import ReactDOM from 'react-dom'
|
|
5
|
+
|
|
6
|
+
import { {{capitalizedPluginName}} } from '../src'
|
|
7
|
+
|
|
8
|
+
function App() {
|
|
9
|
+
const nodes = [
|
|
10
|
+
{
|
|
11
|
+
id: '1',
|
|
12
|
+
position: { x: 0, y: 0 },
|
|
13
|
+
data: {},
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
id: '2',
|
|
17
|
+
position: { x: 100, y: 100 },
|
|
18
|
+
data: {},
|
|
19
|
+
},
|
|
20
|
+
]
|
|
21
|
+
const edges = [
|
|
22
|
+
{
|
|
23
|
+
id: '1',
|
|
24
|
+
source: '1',
|
|
25
|
+
target: '2',
|
|
26
|
+
},
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<Knotx
|
|
31
|
+
style={{ height: 480, boxShadow: '0 0 2px #333', borderRadius: 4 }}
|
|
32
|
+
nodes={nodes}
|
|
33
|
+
edges={edges}
|
|
34
|
+
plugins={[BaseRender, {{capitalizedPluginName}}]}
|
|
35
|
+
/>
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
ReactDOM.render(<StrictMode><App /></StrictMode>, document.getElementById('app'))
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { NodeData } from '@knotx/core'
|
|
2
2
|
|
|
3
|
-
import { inject, OnInit } from '@knotx/decorators'
|
|
4
3
|
import { BasePlugin } from '@knotx/core'
|
|
4
|
+
import { inject, OnInit } from '@knotx/decorators'
|
|
5
5
|
|
|
6
6
|
export class {{capitalizedPluginName}} extends BasePlugin<'{{pluginName}}'> {
|
|
7
7
|
name = '{{pluginName}}' as const
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{{#if isReact}}import react from '@vitejs/plugin-react'{{/if}}
|
|
2
|
+
import { defineConfig } from 'vite'
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
plugins: [{{#if isReact}}react({
|
|
6
|
+
babel: {
|
|
7
|
+
plugins: [[
|
|
8
|
+
'@babel/plugin-proposal-decorators',
|
|
9
|
+
{
|
|
10
|
+
version: '2023-11',
|
|
11
|
+
decoratorsBeforeExport: true,
|
|
12
|
+
},
|
|
13
|
+
]],
|
|
14
|
+
},
|
|
15
|
+
}){{/if}}],
|
|
16
|
+
server: {
|
|
17
|
+
port: 5173,
|
|
18
|
+
},
|
|
19
|
+
})
|