@widget-js/cli 24.1.1-beta.1 → 24.1.1-beta.15

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.
@@ -1,232 +1,232 @@
1
- import path from 'node:path'
2
- import fs from 'node:fs'
3
- import { fileURLToPath } from 'node:url'
4
- import consola from 'consola'
5
- import { paramCase, snakeCase } from 'change-case'
6
- import inquirer from 'inquirer'
7
- import shell from 'shelljs'
8
- import chalk from 'chalk'
9
- import { scanWidgetPackage } from '@widget-js/utils'
10
- import exit from './utils.js'
11
- import promptChecker from './promts/promptChecker'
12
- import { EJSUtils } from './utils/EJSUtils'
13
- import { WidgetPackageUtils } from './utils/WidgetPackageUtils'
14
-
15
- // import vm from "vm";
16
- const __filename = fileURLToPath(import.meta.url)
17
- const __dirname = path.dirname(__filename)
18
-
19
- interface RenderOptions {
20
- name: string
21
- snakeCaseName: string
22
- paramCaseName: string
23
- packageName: string
24
- widgetName: string
25
- title: string
26
- configurable: string
27
- width: number
28
- height: number
29
- maxWidth: number
30
- minHeight: number
31
- maxHeight: number
32
- minWidth: number
33
- }
34
-
35
- export default async function createWidget() {
36
- const widgetPackage = await scanWidgetPackage()
37
- if (!widgetPackage) {
38
- consola.error('widget.ts or widget.json not found')
39
- return
40
- }
41
- const widgetRootDir = WidgetPackageUtils.getRootDir(widgetPackage)
42
- if (widgetPackage.devOptions && widgetPackage.devOptions.folder) {
43
- consola.info(`组件路径:${widgetRootDir}`)
44
- }
45
- else {
46
- consola.info(`没有配置devOptions.folder,使用默认路径${widgetRootDir}`)
47
- }
48
-
49
- const getMiddleValue = (arr: number[]) => {
50
- if (arr.length === 1) {
51
- return arr[0]
52
- }
53
- else if (arr.length === 2) {
54
- return Math.max(...arr)
55
- }
56
- else {
57
- const max = Math.max(...arr)
58
- const min = Math.min(...arr)
59
- const sum = arr[0] + arr[1] + arr[2]
60
- return sum - max - min
61
- }
62
- }
63
-
64
- const name = await promptChecker(
65
- {
66
- type: 'input',
67
- name: 'name',
68
- message: chalk.blue('请输入组件名(大驼峰式),如:CountdownClock'),
69
- },
70
- (answer) => {
71
- const name = answer.name
72
- if (name == null || name === '') {
73
- consola.log(chalk.red('组件名不能为空'))
74
- return false
75
- }
76
- return true
77
- },
78
- )
79
-
80
- consola.log(chalk.green(name))
81
-
82
- const title = await promptChecker({
83
- type: 'input',
84
- name: 'title',
85
- message: chalk.blue('请输入组件标题,如:倒计时'),
86
- })
87
- consola.log(chalk.green(title))
88
- const answerW = await promptChecker(
89
- {
90
- type: 'checkbox',
91
- name: 'w',
92
- message: chalk.blue(
93
- '请选择组件宽度,最多选3个,例如选中2,4,6,代表组件最小宽为2,默认宽为4,最大宽为6,单选代表不可拉伸',
94
- ),
95
- choices: [1, 2, 3, 4, 5, 6],
96
- },
97
- (answer) => {
98
- if (answer.w.length === 0) {
99
- consola.log(chalk.red('宽度必须选择'))
100
- return false
101
- }
102
-
103
- if (answer.w.length > 3) {
104
- consola.log(chalk.red('宽度最多选择3个'))
105
- return false
106
- }
107
- return true
108
- },
109
- )
110
- consola.log(chalk.green(answerW))
111
-
112
- const answerH = await promptChecker(
113
- {
114
- type: 'checkbox',
115
- name: 'h',
116
- message: chalk.blue(
117
- '请选择组件高度,最多选3个,例如选中1,2,代表组件最小高为1,默认高为2,最大高为2,单选代表不可拉伸',
118
- ),
119
- choices: [1, 2, 3, 4, 5, 6],
120
- },
121
- (answer) => {
122
- if (answer.h.length === 0) {
123
- consola.log(chalk.red('高度必须选择'))
124
- return false
125
- }
126
-
127
- if (answer.h.length > 3) {
128
- consola.log(chalk.red('高度最多选择3个'))
129
- return false
130
- }
131
- return true
132
- },
133
- )
134
-
135
- consola.log(chalk.green(answerH))
136
-
137
- const configurable = await promptChecker({
138
- type: 'confirm',
139
- name: 'configurable',
140
- message: chalk.blue('组件是否可配置,例如修改背景颜色,字体大小等'),
141
- })
142
-
143
- consola.log(chalk.green(configurable))
144
-
145
- const width = getMiddleValue(answerW)
146
- const height = getMiddleValue(answerH)
147
- const minWidth = Math.min(...answerW)
148
- const maxWidth = Math.max(...answerW)
149
- const minHeight = Math.min(...answerH)
150
- const maxHeight = Math.max(...answerH)
151
- const snakeCaseName = snakeCase(name)
152
- const paramCaseName = paramCase(name)
153
-
154
- const widgetName = `${widgetPackage.name}.${snakeCaseName}`
155
-
156
- const widgetDir = path.join(widgetRootDir, paramCaseName)
157
- if (!fs.existsSync(widgetDir)) {
158
- fs.mkdirSync(widgetDir)
159
- }
160
- else {
161
- const answer = await inquirer.prompt([
162
- {
163
- type: 'confirm',
164
- name: 'override',
165
- message: chalk.red('组件名已存在,是否继续?'),
166
- },
167
- ])
168
- if (!answer.override) {
169
- exit()
170
- }
171
- }
172
-
173
- const renderOptions: RenderOptions = {
174
- name,
175
- snakeCaseName,
176
- paramCaseName,
177
- packageName: widgetPackage.name,
178
- widgetName,
179
- title,
180
- configurable,
181
- width,
182
- height,
183
- maxWidth,
184
- minHeight,
185
- maxHeight,
186
- minWidth,
187
- }
188
-
189
- const widgetDefineFile = path.resolve(widgetDir, `${name}.widget.ts`)
190
- const widgetFile = path.resolve(widgetDir, `${name}Widget.vue`)
191
- const widgetViewFile = path.resolve(widgetDir, `${name}WidgetView.vue`)
192
- const widgetRoutesFile = path.resolve(widgetDir, `${name}WidgetRoutes.ts`)
193
-
194
- await EJSUtils.renderToFile('WidgetDefine.ejs', widgetDefineFile, 'typescript', renderOptions)
195
- // EJSUtils.renderToFile('Widget.ejs', widgetFile, 'vue', renderOptions)
196
- await EJSUtils.renderToFile('WidgetView.ejs', widgetViewFile, 'vue', renderOptions)
197
- await EJSUtils.renderToFile('WidgetRoutes.ejs', widgetRoutesFile, 'typescript', renderOptions)
198
- if (configurable) {
199
- const configFile = path.resolve(widgetDir, `${name}ConfigView.vue`)
200
- await EJSUtils.renderToFile('WidgetConfig.ejs', configFile, 'vue', renderOptions)
201
- }
202
- // 注册路由
203
- const routeFile = path.join(widgetRootDir, 'widget-router.ts')
204
- let routeContent
205
- if (fs.existsSync(routeFile)) {
206
- routeContent = fs.readFileSync(routeFile, 'utf8')
207
- }
208
- else {
209
- routeContent = fs.readFileSync(path.join(__dirname, '../template/widget-router.ts'), 'utf8')
210
- }
211
- const importRouteStr = `import ${name}WidgetRoutes from "./${paramCaseName}/${name}WidgetRoutes";`
212
- const routeStr = `...${name}WidgetRoutes,`
213
- if (!routeContent.includes(importRouteStr)) {
214
- routeContent = routeContent.replaceAll(
215
- '//FBI WANING! IMPORT PLACE',
216
- `${importRouteStr}\n//FBI WANING! IMPORT PLACE`,
217
- )
218
- }
219
- if (!routeContent.includes(routeStr)) {
220
- routeContent = routeContent.replaceAll('//FBI WANING! ROUTE PLACE', `${routeStr}\n //FBI WANING! ROUTE PLACE`)
221
- }
222
-
223
- fs.writeFileSync(routeFile, routeContent)
224
-
225
- // 添加到版本控制
226
- const gitAdd = `git add ${widgetDir}`
227
- consola.info(chalk.grey(gitAdd))
228
- shell.exec(gitAdd)
229
- consola.log('=================')
230
- consola.info(`已创建组件:${widgetDir}`)
231
- consola.success('Happy coding!')
232
- }
1
+ import path from 'node:path'
2
+ import fs from 'node:fs'
3
+ import { fileURLToPath } from 'node:url'
4
+ import consola from 'consola'
5
+ import { paramCase, snakeCase } from 'change-case'
6
+ import inquirer from 'inquirer'
7
+ import shell from 'shelljs'
8
+ import chalk from 'chalk'
9
+ import { scanWidgetPackage } from '@widget-js/utils'
10
+ import exit from './utils.js'
11
+ import promptChecker from './promts/promptChecker'
12
+ import { EJSUtils } from './utils/EJSUtils'
13
+ import { WidgetPackageUtils } from './utils/WidgetPackageUtils'
14
+
15
+ // import vm from "vm";
16
+ const __filename = fileURLToPath(import.meta.url)
17
+ const __dirname = path.dirname(__filename)
18
+
19
+ interface RenderOptions {
20
+ name: string
21
+ snakeCaseName: string
22
+ paramCaseName: string
23
+ packageName: string
24
+ widgetName: string
25
+ title: string
26
+ configurable: string
27
+ width: number
28
+ height: number
29
+ maxWidth: number
30
+ minHeight: number
31
+ maxHeight: number
32
+ minWidth: number
33
+ }
34
+
35
+ export default async function createWidget() {
36
+ const widgetPackage = await scanWidgetPackage()
37
+ if (!widgetPackage) {
38
+ consola.error('widget.ts or widget.json not found')
39
+ return
40
+ }
41
+ const widgetRootDir = WidgetPackageUtils.getRootDir(widgetPackage)
42
+ if (widgetPackage.devOptions && widgetPackage.devOptions.folder) {
43
+ consola.info(`组件路径:${widgetRootDir}`)
44
+ }
45
+ else {
46
+ consola.info(`没有配置devOptions.folder,使用默认路径${widgetRootDir}`)
47
+ }
48
+
49
+ const getMiddleValue = (arr: number[]) => {
50
+ if (arr.length === 1) {
51
+ return arr[0]
52
+ }
53
+ else if (arr.length === 2) {
54
+ return Math.max(...arr)
55
+ }
56
+ else {
57
+ const max = Math.max(...arr)
58
+ const min = Math.min(...arr)
59
+ const sum = arr[0] + arr[1] + arr[2]
60
+ return sum - max - min
61
+ }
62
+ }
63
+
64
+ const name = await promptChecker(
65
+ {
66
+ type: 'input',
67
+ name: 'name',
68
+ message: chalk.blue('请输入组件名(大驼峰式),如:CountdownClock'),
69
+ },
70
+ (answer) => {
71
+ const name = answer.name
72
+ if (name == null || name === '') {
73
+ consola.log(chalk.red('组件名不能为空'))
74
+ return false
75
+ }
76
+ return true
77
+ },
78
+ )
79
+
80
+ consola.log(chalk.green(name))
81
+
82
+ const title = await promptChecker({
83
+ type: 'input',
84
+ name: 'title',
85
+ message: chalk.blue('请输入组件标题,如:倒计时'),
86
+ })
87
+ consola.log(chalk.green(title))
88
+ const answerW = await promptChecker(
89
+ {
90
+ type: 'checkbox',
91
+ name: 'w',
92
+ message: chalk.blue(
93
+ '请选择组件宽度,最多选3个,例如选中2,4,6,代表组件最小宽为2,默认宽为4,最大宽为6,单选代表不可拉伸',
94
+ ),
95
+ choices: [1, 2, 3, 4, 5, 6],
96
+ },
97
+ (answer) => {
98
+ if (answer.w.length === 0) {
99
+ consola.log(chalk.red('宽度必须选择'))
100
+ return false
101
+ }
102
+
103
+ if (answer.w.length > 3) {
104
+ consola.log(chalk.red('宽度最多选择3个'))
105
+ return false
106
+ }
107
+ return true
108
+ },
109
+ )
110
+ consola.log(chalk.green(answerW))
111
+
112
+ const answerH = await promptChecker(
113
+ {
114
+ type: 'checkbox',
115
+ name: 'h',
116
+ message: chalk.blue(
117
+ '请选择组件高度,最多选3个,例如选中1,2,代表组件最小高为1,默认高为2,最大高为2,单选代表不可拉伸',
118
+ ),
119
+ choices: [1, 2, 3, 4, 5, 6],
120
+ },
121
+ (answer) => {
122
+ if (answer.h.length === 0) {
123
+ consola.log(chalk.red('高度必须选择'))
124
+ return false
125
+ }
126
+
127
+ if (answer.h.length > 3) {
128
+ consola.log(chalk.red('高度最多选择3个'))
129
+ return false
130
+ }
131
+ return true
132
+ },
133
+ )
134
+
135
+ consola.log(chalk.green(answerH))
136
+
137
+ const configurable = await promptChecker({
138
+ type: 'confirm',
139
+ name: 'configurable',
140
+ message: chalk.blue('组件是否可配置,例如修改背景颜色,字体大小等'),
141
+ })
142
+
143
+ consola.log(chalk.green(configurable))
144
+
145
+ const width = getMiddleValue(answerW)
146
+ const height = getMiddleValue(answerH)
147
+ const minWidth = Math.min(...answerW)
148
+ const maxWidth = Math.max(...answerW)
149
+ const minHeight = Math.min(...answerH)
150
+ const maxHeight = Math.max(...answerH)
151
+ const snakeCaseName = snakeCase(name)
152
+ const paramCaseName = paramCase(name)
153
+
154
+ const widgetName = `${widgetPackage.name}.${snakeCaseName}`
155
+
156
+ const widgetDir = path.join(widgetRootDir, paramCaseName)
157
+ if (!fs.existsSync(widgetDir)) {
158
+ fs.mkdirSync(widgetDir)
159
+ }
160
+ else {
161
+ const answer = await inquirer.prompt([
162
+ {
163
+ type: 'confirm',
164
+ name: 'override',
165
+ message: chalk.red('组件名已存在,是否继续?'),
166
+ },
167
+ ])
168
+ if (!answer.override) {
169
+ exit()
170
+ }
171
+ }
172
+
173
+ const renderOptions: RenderOptions = {
174
+ name,
175
+ snakeCaseName,
176
+ paramCaseName,
177
+ packageName: widgetPackage.name,
178
+ widgetName,
179
+ title,
180
+ configurable,
181
+ width,
182
+ height,
183
+ maxWidth,
184
+ minHeight,
185
+ maxHeight,
186
+ minWidth,
187
+ }
188
+
189
+ const widgetDefineFile = path.resolve(widgetDir, `${name}.widget.ts`)
190
+ const widgetFile = path.resolve(widgetDir, `${name}Widget.vue`)
191
+ const widgetViewFile = path.resolve(widgetDir, `${name}WidgetView.vue`)
192
+ const widgetRoutesFile = path.resolve(widgetDir, `${name}WidgetRoutes.ts`)
193
+
194
+ await EJSUtils.renderToFile('WidgetDefine.ejs', widgetDefineFile, 'typescript', renderOptions)
195
+ // EJSUtils.renderToFile('Widget.ejs', widgetFile, 'vue', renderOptions)
196
+ await EJSUtils.renderToFile('WidgetView.ejs', widgetViewFile, 'vue', renderOptions)
197
+ await EJSUtils.renderToFile('WidgetRoutes.ejs', widgetRoutesFile, 'typescript', renderOptions)
198
+ if (configurable) {
199
+ const configFile = path.resolve(widgetDir, `${name}ConfigView.vue`)
200
+ await EJSUtils.renderToFile('WidgetConfig.ejs', configFile, 'vue', renderOptions)
201
+ }
202
+ // 注册路由
203
+ const routeFile = path.join(widgetRootDir, 'widget-router.ts')
204
+ let routeContent
205
+ if (fs.existsSync(routeFile)) {
206
+ routeContent = fs.readFileSync(routeFile, 'utf8')
207
+ }
208
+ else {
209
+ routeContent = fs.readFileSync(path.join(__dirname, '../template/widget-router.ts'), 'utf8')
210
+ }
211
+ const importRouteStr = `import ${name}WidgetRoutes from "./${paramCaseName}/${name}WidgetRoutes";`
212
+ const routeStr = `...${name}WidgetRoutes,`
213
+ if (!routeContent.includes(importRouteStr)) {
214
+ routeContent = routeContent.replaceAll(
215
+ '//FBI WANING! IMPORT PLACE',
216
+ `${importRouteStr}\n//FBI WANING! IMPORT PLACE`,
217
+ )
218
+ }
219
+ if (!routeContent.includes(routeStr)) {
220
+ routeContent = routeContent.replaceAll('//FBI WANING! ROUTE PLACE', `${routeStr}\n //FBI WANING! ROUTE PLACE`)
221
+ }
222
+
223
+ fs.writeFileSync(routeFile, routeContent)
224
+
225
+ // 添加到版本控制
226
+ const gitAdd = `git add ${widgetDir}`
227
+ consola.info(chalk.grey(gitAdd))
228
+ shell.exec(gitAdd)
229
+ consola.log('=================')
230
+ consola.info(`已创建组件:${widgetDir}`)
231
+ consola.success('Happy coding!')
232
+ }
@@ -6,7 +6,7 @@ export default async function (options: any) {
6
6
  const type = options.type as DependenciesOptions
7
7
  if (type == 'remote') {
8
8
  await RemoteDependencies.start()
9
- }
9
+ }
10
10
  else {
11
11
  await LocalDependencies.start()
12
12
  }
package/src/index.ts CHANGED
@@ -10,7 +10,7 @@ import { dirname } from 'dirname-filename-esm'
10
10
  //
11
11
  const packageJsonPath = path.join(dirname(import.meta), '../package.json')
12
12
  const cliPackage = JSON.parse(fs.readFileSync(packageJsonPath).toString())
13
-
13
+ // eslint-disable-next-line no-console
14
14
  console.log(gradient.pastel.multiline(figlet.textSync('widget-cli', { horizontalLayout: 'full' })))
15
15
  program.version(`@widget-js/cli ${cliPackage.version}`).usage('<command> [options]')
16
16
  program
@@ -40,6 +40,16 @@ program
40
40
  await init.init()
41
41
  })
42
42
 
43
+ const keyOption = new Option('-k, --key <key>')
44
+ program
45
+ .command('publish')
46
+ .description('Publish widget package with developer key')
47
+ .addOption(keyOption)
48
+ .action(async (options) => {
49
+ const publishImport = await import('./publish/index')
50
+ await publishImport.publish(options)
51
+ })
52
+
43
53
  const typeOption = new Option('-t, --type <type>').choices(['ftp', 'oss'])
44
54
  const fileOption = new Option('-f, --file <file>')
45
55
  program
@@ -47,8 +57,7 @@ program
47
57
  .description('通过FTP/OSS发布文件,仅内部使用')
48
58
  .addOption(typeOption)
49
59
  .addOption(fileOption)
50
- .action(async (options, command) => {
51
- // @ts-expect-error
60
+ .action(async (options) => {
52
61
  const release = await import('./release/release')
53
62
  await release.default(options)
54
63
  })