@widget-js/cli 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/.idea/cli.iml ADDED
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/temp" />
6
+ <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
+ <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ </component>
12
+ </module>
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptLibraryMappings">
4
+ <includedPredefinedLibrary name="Node.js Core" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/cli.iml" filepath="$PROJECT_DIR$/.idea/cli.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
5
+ </component>
6
+ </project>
package/bin/widget.js ADDED
@@ -0,0 +1,217 @@
1
+ import inquirer from "inquirer";
2
+ import {chalk} from "@vue/cli-shared-utils";
3
+ import fs from "fs";
4
+ import path from "path";
5
+ import {snakeCase, paramCase} from "change-case";
6
+ import {fileURLToPath} from 'url';
7
+ import shell from 'shelljs';
8
+ import promptChecker from "../lib/promts/promptChecker.js";
9
+ import {exit, exitProcess} from "@vue/cli-shared-utils/lib/exit.js";
10
+ import ejs from "ejs";
11
+ import consola from "consola";
12
+
13
+
14
+ let widgetJson = path.join(process.cwd(), "widget.json");
15
+ if (!fs.existsSync(widgetJson)) {
16
+ consola.error("没有在根目录找到widget.json文件")
17
+ exit();
18
+ }
19
+
20
+ let widgetPackage = JSON.parse(fs.readFileSync(widgetJson));
21
+
22
+ let widgetFolder = path.join(process.cwd(), "./src/widgets");
23
+ let devOptions = widgetPackage["devOptions"] ?? {};
24
+ if (devOptions["folder"]) {
25
+ widgetFolder = devOptions["folder"];
26
+ consola.info(`组件路径:${widgetFolder}`);
27
+ } else {
28
+ consola.info(`没有配置devOptions.folder,使用默认路径${widgetFolder}`);
29
+ }
30
+
31
+ if (!fs.existsSync(widgetFolder)) {
32
+ fs.mkdirSync(widgetFolder, {recursive: true});
33
+ }
34
+
35
+ const __filename = fileURLToPath(import.meta.url);
36
+ const __dirname = path.dirname(__filename);
37
+
38
+ Array.prototype.max = function () {
39
+ return Math.max.apply(null, this);
40
+ };
41
+
42
+ Array.prototype.min = function () {
43
+ return Math.min.apply(null, this);
44
+ };
45
+
46
+ const getMiddleValue = (arr) => {
47
+ if (arr.length === 1) {
48
+ return arr[0];
49
+ } else if (arr.length === 2) {
50
+ return arr.max();
51
+ } else {
52
+ const max = arr.max();
53
+ const min = arr.min();
54
+ const sum = arr[0] + arr[1] + arr[2];
55
+ return sum - max - min;
56
+ }
57
+ }
58
+
59
+ let name = await promptChecker({
60
+ type: "input",
61
+ name: 'name',
62
+ message: chalk.blue("请输入组件名(大驼峰式),如:CountdownClock")
63
+ }, (answer) => {
64
+ const name = answer.name;
65
+ if (name == null || name === '') {
66
+ consola.log(chalk.red('组件名不能为空'));
67
+ return false;
68
+ }
69
+ return true;
70
+ })
71
+
72
+ consola.log(chalk.green(name))
73
+
74
+ let title = await promptChecker({
75
+ type: "input",
76
+ name: 'title',
77
+ message: chalk.blue("请输入组件标题,如:倒计时")
78
+ })
79
+ consola.log(chalk.green(title))
80
+
81
+ let answerW = await promptChecker({
82
+ type: "checkbox",
83
+ name: 'w',
84
+ message: chalk.blue("请选择组件宽度,最多选3个,例如选中2,4,6,代表组件最小宽为2,默认宽为4,最大宽为6,单选代表不可拉伸"),
85
+ choices: [1, 2, 3, 4, 5, 6]
86
+ }, (answer) => {
87
+ if (answer.w.length === 0) {
88
+ consola.log(chalk.red('宽度必须选择'));
89
+ return false;
90
+ }
91
+
92
+ if (answer.w.length > 3) {
93
+ consola.log(chalk.red('宽度最多选择3个'));
94
+ return false;
95
+ }
96
+ return true;
97
+ })
98
+ consola.log(chalk.green(answerW))
99
+
100
+ let answerH = await promptChecker({
101
+ type: "checkbox",
102
+ name: 'h',
103
+ message: chalk.blue("请选择组件高度,最多选3个,例如选中1,2,代表组件最小高为1,默认高为2,最大高为2,单选代表不可拉伸"),
104
+ choices: [1, 2, 3, 4, 5, 6]
105
+ }, (answer) => {
106
+ if (answer.h.length === 0) {
107
+ consola.log(chalk.red('高度必须选择'));
108
+ return false;
109
+ }
110
+
111
+ if (answer.h.length > 3) {
112
+ consola.log(chalk.red('高度最多选择3个'));
113
+ return false;
114
+ }
115
+ return true;
116
+ })
117
+
118
+ consola.log(chalk.green(answerH))
119
+
120
+ let configurable = await promptChecker({
121
+ type: "confirm",
122
+ name: 'configurable',
123
+ message: chalk.blue("组件是否可配置,例如修改背景颜色,字体大小等")
124
+ })
125
+
126
+ consola.log(chalk.green(configurable))
127
+
128
+ const width = getMiddleValue(answerW);
129
+ const height = getMiddleValue(answerH);
130
+ const minWidth = answerW.min();
131
+ const maxWidth = answerW.max();
132
+ const minHeight = answerH.min();
133
+ const maxHeight = answerH.max();
134
+ const snakeCaseName = snakeCase(name);
135
+ const paramCaseName = paramCase(name);
136
+ const packageName = "com.wisdom.widgets." + snakeCaseName;
137
+
138
+ const widgetDir = path.join(widgetFolder, paramCaseName)
139
+ if (!fs.existsSync(widgetDir)) {
140
+ fs.mkdirSync(widgetDir);
141
+ } else {
142
+ let answer = await inquirer.prompt([{
143
+ type: 'confirm',
144
+ name: 'override',
145
+ message: chalk.red('组件名已存在,是否继续?')
146
+ }])
147
+ if (!answer.override) {
148
+ exitProcess();
149
+ }
150
+ }
151
+
152
+ const renderOptions = {
153
+ name: name,
154
+ snakeCaseName: snakeCaseName,
155
+ paramCaseName: paramCaseName,
156
+ packageName: packageName,
157
+ title: title,
158
+ configurable: configurable,
159
+ width: width,
160
+ height: height,
161
+ maxWidth: maxWidth,
162
+ minHeight: minHeight,
163
+ maxHeight: maxHeight,
164
+ minWidth: minWidth
165
+ }
166
+
167
+ function renderToFile(templateFile, outputFile, renderOptions) {
168
+ const defineTemplatePath = path.join(__dirname, '../template', templateFile)
169
+ let defineTemplate = fs.readFileSync(defineTemplatePath, 'utf8');
170
+ fs.writeFileSync(outputFile, ejs.render(defineTemplate, renderOptions))
171
+ }
172
+
173
+ const widgetDefineFile = path.resolve(widgetDir, `${name}.widget.ts`)
174
+ const widgetFile = path.resolve(widgetDir, `${name}Widget.vue`);
175
+ const widgetViewFile = path.resolve(widgetDir, `${name}WidgetView.vue`)
176
+ const widgetRoutesFile = path.resolve(widgetDir, `${name}WidgetRoutes.ts`)
177
+
178
+ renderToFile('WidgetDefine.ejs', widgetDefineFile, renderOptions);
179
+ renderToFile('Widget.ejs', widgetFile, renderOptions);
180
+ renderToFile('WidgetView.ejs', widgetViewFile, renderOptions);
181
+ renderToFile('WidgetRoutes.ejs', widgetRoutesFile, renderOptions);
182
+ if (configurable) {
183
+ const configFile = path.resolve(widgetDir, `${name}ConfigView.vue`)
184
+ renderToFile('WidgetConfig.ejs', configFile, renderOptions);
185
+ }
186
+ if (devOptions["useStorybook"]) {
187
+ const storiesFile = path.resolve(widgetDir, `${name}Widget.stories.ts`)
188
+ renderToFile('stories.ejs', storiesFile, renderOptions);
189
+ }
190
+
191
+ // 注册路由
192
+ const routeFile = path.join(widgetFolder, 'widget-router.ts');
193
+ let routeContent;
194
+ if (fs.existsSync(routeFile)) {
195
+ routeContent = fs.readFileSync(routeFile, 'utf8');
196
+ } else {
197
+ routeContent = fs.readFileSync(path.join(__dirname, "../template/widget-router.ts"), 'utf8');
198
+ }
199
+ const importRouteStr = `import ${name}WidgetRoutes from "./${paramCaseName}/${name}WidgetRoutes";`
200
+ const routeStr = `...${name}WidgetRoutes,`
201
+ if (!routeContent.includes(importRouteStr)) {
202
+ routeContent = routeContent.replaceAll("//FBI WANING! IMPORT PLACE", `${importRouteStr}\n//FBI WANING! IMPORT PLACE`)
203
+ }
204
+ if (!routeContent.includes(routeStr)) {
205
+ routeContent = routeContent.replaceAll("//FBI WANING! ROUTE PLACE", `${routeStr}\n //FBI WANING! ROUTE PLACE`)
206
+ }
207
+
208
+ fs.writeFileSync(routeFile, routeContent)
209
+
210
+ //添加到版本控制
211
+ let gitAdd = `git add ${widgetDir}`;
212
+ consola.info(chalk.grey(gitAdd))
213
+ shell.exec(gitAdd);
214
+ consola.log("=================")
215
+ consola.info(`已创建组件:${widgetDir}`)
216
+ consola.success("Happy coding!")
217
+
@@ -0,0 +1,15 @@
1
+ import inquirer from "inquirer";
2
+
3
+ const promptChecker = async (prompt, checker) => {
4
+ let answer = await inquirer.prompt([prompt]);
5
+ if (checker) {
6
+ if (checker(answer)) {
7
+ return answer[prompt.name];
8
+ } else {
9
+ return promptChecker(prompt, checker);
10
+ }
11
+ }
12
+ return answer[prompt.name];
13
+ }
14
+
15
+ export default promptChecker;
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@widget-js/cli",
3
+ "version": "1.0.0",
4
+ "main": "bin/widget.js",
5
+ "author": "Neo Fu",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "private": false,
9
+ "bin": {
10
+ "widget": "bin/widget.js"
11
+ },
12
+ "scripts": {
13
+ "widget": "node ./bin/widget.js"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "engines": {
19
+ "node": "^12.0.0 || >= 14.0.0"
20
+ },
21
+ "dependencies": {
22
+ },
23
+ "devDependencies": {
24
+ "@vue/cli-shared-utils": "^5.0.8",
25
+ "change-case": "^4.1.2",
26
+ "consola": "^2.15.3",
27
+ "ejs": "^3.1.8",
28
+ "inquirer": "^9.1.4",
29
+ "shelljs": "^0.8.5"
30
+ }
31
+ }
@@ -0,0 +1,14 @@
1
+ <template>
2
+ <!-- TODO 这里只写组件内容,不做自定义数据、业务处理 -->
3
+ </template>
4
+
5
+ <script lang="ts">
6
+
7
+ export default {
8
+ name: "<%= name %>Widget",
9
+ }
10
+ </script>
11
+
12
+ <style scoped>
13
+
14
+ </style>
@@ -0,0 +1,52 @@
1
+ <template>
2
+
3
+ <widget-edit-dialog :widget-params="widgetParams" :option="widgetConfigOption"
4
+ :widget-data="widgetData"
5
+ @confirm="onSaveClick()">
6
+ <template v-slot:widget>
7
+ <!-- 组件配置内容 -->
8
+ <<%= paramCaseName %>-widget :style="{
9
+ width:`${widgetParams.widthPx}px`,
10
+ height:`${widgetParams.heightPx}px`
11
+ }" :background-color="widgetData.backgroundColor"></<%= paramCaseName %>-widget>
12
+ </template>
13
+ <template v-slot:form>
14
+ <!-- TODO 这里写自定义表单内容 -->
15
+ </template>
16
+ </widget-edit-dialog>
17
+ </template>
18
+
19
+ <script lang="ts">
20
+
21
+ import <%= name %>Widget from "@/widgets/clock/ClockWidget.vue";
22
+ import {useWidget, WidgetConfigOption, WidgetEditDialog} from "@widget-js/vue3";
23
+ import {WidgetData, WidgetDataRepository} from "@widget-js/core";
24
+ import {reactive} from "vue";
25
+
26
+ export default {
27
+ name: "",
28
+ components: {<%= name %>Widget, WidgetEditDialog},
29
+ setup() {
30
+ const {widgetData, widgetParams} = useWidget(WidgetData)
31
+
32
+ //修改成需要设置组件参数配置
33
+ const widgetConfigOption = reactive(new WidgetConfigOption({
34
+ custom: false,
35
+ backgroundColor: true,
36
+ borderRadius: true
37
+ }));
38
+
39
+ return {widgetData, widgetParams, widgetConfigOption}
40
+ },
41
+ methods: {
42
+ async onSaveClick() {
43
+ await WidgetDataRepository.save(this.widgetData);
44
+ window.close();
45
+ }
46
+ }
47
+ }
48
+ </script>
49
+
50
+ <style scoped>
51
+
52
+ </style>
@@ -0,0 +1,31 @@
1
+ import {Widget, WidgetKeyword} from "@widget-js/core";
2
+ //TODO 修改组件信息,标题,描述,关键词
3
+ const name = "cn.widgetjs.widgets.<%= snakeCaseName%>";
4
+ //组件标题
5
+ const title = {"zh": "<%= title%>"};
6
+ //组件描述
7
+ const description = {"zh": ""};
8
+ //组件关键词
9
+ const keywords = [WidgetKeyword.RECOMMEND];
10
+ //组件路由地址
11
+ const url = "/widget/<%= snakeCaseName%>";
12
+ //配置页路由地址
13
+ const configUrl = <% if (configurable) { %>"/widget/config/<%= snakeCaseName%>"<% } else { %>undefined<% } %>;
14
+ //组件关键词
15
+ const <%= name %>WidgetDefine = new Widget({
16
+ name: name,
17
+ title: title,
18
+ description: description,
19
+ keywords: keywords,
20
+ lang: "zh",
21
+ url: url,
22
+ configUrl: configUrl,
23
+ width: <%= width %>,
24
+ height: <%= height %>,
25
+ minWidth: <%= minWidth %>,
26
+ maxWidth: <%= maxWidth %>,
27
+ minHeight: <%= minHeight %>,
28
+ maxHeight: <%= maxHeight %>
29
+ })
30
+
31
+ export default <%= name %>WidgetDefine;
@@ -0,0 +1,29 @@
1
+ import <%= name %>WidgetDefine from "./<%= name %>.widget";
2
+
3
+ const url = <%= name %>WidgetDefine.url;
4
+ const name = <%= name %>WidgetDefine.name;
5
+ <% if (configurable) { %>
6
+ const configUrl = <%= name %>WidgetDefine.configUrl;
7
+
8
+ const <%= name %>WidgetRoutes = [
9
+ {
10
+ path: url,
11
+ name: `${name}`,
12
+ component: () => import(/* webpackChunkName: "<%= packageName %>" */ './<%= name %>WidgetView.vue')
13
+ },
14
+ {
15
+ path: configUrl,
16
+ name: `${name}.config`,
17
+ component: () => import(/* webpackChunkName: "<%= packageName %>.config" */ './<%= name %>ConfigView.vue')
18
+ }
19
+ ]
20
+ <% } else { %>
21
+ const <%= name %>WidgetRoutes = [
22
+ {
23
+ path: url,
24
+ name: `${name}`,
25
+ component: () => import(/* webpackChunkName: "<%= packageName %>" */ './<%= name %>WidgetView.vue')
26
+ }
27
+ ]
28
+ <% } %>
29
+ export default <%= name %>WidgetRoutes;
@@ -0,0 +1,23 @@
1
+ <template>
2
+ <!-- TODO:组件页面,这里编写组件业务逻辑-->
3
+ <<%= paramCaseName %>-widget :background-color="widgetData.backgroundColor"></<%= paramCaseName %>-widget>
4
+ </template>
5
+
6
+ <script lang="ts">
7
+ import {WidgetData} from "@widget-js/core";
8
+ import <%= name %>Widget from "./<%= name %>Widget.vue"
9
+ import {useWidget} from "@widget-js/vue3";
10
+
11
+ export default {
12
+ name:"<%= name %>WidgetView",
13
+ components: {<%= name %>Widget},
14
+ setup(){
15
+ const {widgetData, widgetParams} = useWidget(WidgetData);
16
+ return {widgetData, widgetParams};
17
+ }
18
+ }
19
+ </script>
20
+
21
+ <style scoped>
22
+
23
+ </style>
@@ -0,0 +1,15 @@
1
+ import <%= name %>Widget from './<%= name %>Widget.vue';
2
+
3
+ export default {
4
+ title: 'Widget/<%= name %>',
5
+ component: <%= name %>Widget,
6
+ argTypes: {},
7
+ };
8
+
9
+ export const Widget = (args:any) => ({
10
+ components: {<%= name %>Widget},
11
+ setup() {
12
+ return {args};
13
+ },
14
+ template: '<<%= paramCaseName %>-widget></<%= paramCaseName %>-widget>',
15
+ });
@@ -0,0 +1,6 @@
1
+ //FBI WANING! IMPORT PLACE, DONT DELETE THIS LINE
2
+
3
+ const WidgetRouter = [
4
+ //FBI WANING! ROUTE PLACE, DONT DELETE THIS LINE
5
+ ];
6
+ export default WidgetRouter
package/widget.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "cn.widgetjs.widgets",
3
+ "author": "Widget JS",
4
+ "homepage": "https://widgetjs.cn",
5
+ "description": {
6
+ "zh": "内置基础组件"
7
+ },
8
+ "entry": "index.html#",
9
+ "version": "0.0.1",
10
+ "devOptions": {
11
+ "useStorybook": true,
12
+ "folder": "./test/"
13
+ }
14
+ }