Skip to content

Commit bad8c0d

Browse files
authored
Refactor: 重构cli (#238)
* refactor: 重构shared/utils * fix: 修复shared/utils类型错误 * refactor: 重构cli
1 parent 9f2fd3d commit bad8c0d

File tree

4 files changed

+88
-67
lines changed

4 files changed

+88
-67
lines changed

internals/cli/src/commands/build/handlebars.render.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import Handlebars from 'handlebars'
88
let RegCache = {}
99
let HandlebarsCompile
1010

11-
const replaceDelimiters = function (str, sourceReg, escape) {
11+
const replaceDelimiters = function (str, sourceReg, escape?: boolean) {
1212
let regex = RegCache[sourceReg] || (RegCache[sourceReg] = new RegExp(sourceReg, 'g'))
1313
let match
1414

15+
// eslint-disable-next-line no-cond-assign
1516
while ((match = regex.exec(str))) {
1617
let prefix = str.slice(0, match.index)
1718
let inner = (escape ? '\\' : '') + '{{' + match[1] + '}}'
@@ -23,31 +24,34 @@ const replaceDelimiters = function (str, sourceReg, escape) {
2324
return str
2425
}
2526

26-
Handlebars.setDelimiter = function (delimiters) {
27-
if (delimiters[0].slice(-1) !== '=') {
28-
delimiters[0] += '(?!=)'
29-
}
27+
Object.defineProperty(Handlebars, 'setDelimiter', {
28+
value(delimiters: string[]) {
29+
if (delimiters[0].slice(-1) !== '=') {
30+
delimiters[0] += '(?!=)'
31+
}
3032

31-
let source = delimiters[0] + '([\\s\\S]+?)' + delimiters[1]
33+
let source = delimiters[0] + '([\\s\\S]+?)' + delimiters[1]
3234

33-
if (!HandlebarsCompile) {
34-
HandlebarsCompile = Handlebars.compile
35-
}
35+
if (!HandlebarsCompile) {
36+
HandlebarsCompile = Handlebars.compile
37+
}
3638

37-
Handlebars.compile = function (str) {
38-
let args = [].slice.call(arguments)
39+
Handlebars.compile = function (str) {
40+
// eslint-disable-next-line prefer-rest-params
41+
let args = [].slice.call(arguments)
3942

40-
if (typeof str === 'string') {
41-
if (delimiters[0] !== '{{' && delimiters[1] !== '}}') {
42-
args[0] = replaceDelimiters(args[0], '{{([\\s\\S]+?)}}', true)
43+
if (typeof str === 'string') {
44+
if (delimiters[0] !== '{{' && delimiters[1] !== '}}') {
45+
args[0] = replaceDelimiters(args[0], '{{([\\s\\S]+?)}}', true)
46+
}
47+
48+
args[0] = replaceDelimiters(args[0], source)
4349
}
4450

45-
args[0] = replaceDelimiters(args[0], source)
51+
return HandlebarsCompile.apply(Handlebars, args)
4652
}
47-
48-
return HandlebarsCompile.apply(Handlebars, args)
4953
}
50-
}
54+
})
5155

5256
/**
5357
* 格式化模板
@@ -59,8 +63,13 @@ Handlebars.setDelimiter = function (delimiters) {
5963
* @param {Object} delimiter
6064
* @returns {String}
6165
*/
62-
export default function ({ delimiter, template, options, data }) {
63-
delimiter && Handlebars.setDelimiter(delimiter)
64-
65-
return Handlebars.compile(template, options)(data)
66+
export default function ({ delimiter, template, options, data }: {
67+
delimiter?: string[]
68+
template: string
69+
options?: CompileOptions
70+
data: any
71+
}) {
72+
delimiter && (Handlebars as typeof Handlebars & { setDelimiter: (delimiter: string[]) => any }).setDelimiter(delimiter)
73+
const compile = Handlebars.compile(template, options)
74+
return compile(data)
6675
}

internals/cli/src/commands/create/create-ui.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
* yarn create:ui img-preview -single 输出纯净模板(没有 pc 等模板/单层组件)
66
* yarn create:ui img-preview -mobile 创建纯移动组件
77
*/
8-
const path = require('path')
9-
const fs = require('fs-extra')
10-
const semver = require('semver')
11-
const utils = require('./utils')
12-
const { createModuleMapping } = require('./module-utils')
13-
const handlebarsRender = require('./handlebars.render')
8+
import path from 'path'
9+
import fs from 'fs-extra'
10+
import semver from 'semver'
11+
import * as utils from '../../shared/utils'
12+
import { createModuleMapping } from '../../shared/module-utils'
13+
import handlebarsRender from '../build/handlebars.render'
1414

1515
const args = utils.getInputCmd()
1616

1717
if (args.length > 0) {
18-
const commands = []
19-
const components = []
20-
const templateDir = utils.pathJoin('../template/component')
21-
const componetDir = utils.pathJoin('../../packages/vue/components')
22-
const { version } = fs.readJSONSync(utils.pathJoin('../../packages/vue/package.json'))
18+
const commands: string[] = []
19+
const components: string[] = []
20+
const templateDir = utils.pathJoin('../../public/template/component')
21+
const componetDir = utils.pathJoin('../../../../packages/vue/src')
22+
const { version } = fs.readJSONSync(utils.pathJoin('../../../../packages/vue/package.json'))
2323

2424
args.forEach((item) => {
2525
if (item.indexOf('-') === 0) {

internals/cli/src/shared/module-utils.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ export interface Module {
4747
* @param {Boolean} isSort 是否需要排序
4848
* @returns 模块对象
4949
*/
50-
const getAllModules = (isSort) => {
50+
const getAllModules = (isSort: boolean) => {
5151
return getSortModules({ filterIntercept: () => true, isSort })
5252
}
5353

5454
/**
5555
* @param {String} key 根据模块对象的 Key 获取对应的值
5656
* @returns 模块对象
5757
*/
58-
const getModuleInfo = (key) => {
58+
const getModuleInfo = (key: string) => {
5959
return moduleMap[key] || {}
6060
}
6161

@@ -66,7 +66,10 @@ const getModuleInfo = (key) => {
6666
* @param {Boolean} isOriginal 是否取原始数据
6767
* @param {Boolean} isSort 是否需要排序
6868
*/
69-
const getByName = ({ name, inversion = false, isOriginal = false, isSort = true }) => {
69+
const getByName = (
70+
{ name, inversion = false, isOriginal = false, isSort = true }:
71+
{ name: string;inversion: boolean;isOriginal: boolean;isSort: boolean }
72+
) => {
7073
const callback = (item) => {
7174
const result = new RegExp(`/${name}/|^vue-${name}/`).test(item.path)
7275
return inversion ? !result : result
@@ -80,7 +83,7 @@ const getByName = ({ name, inversion = false, isOriginal = false, isSort = true
8083
* @private
8184
* @param {Function} filterIntercept 搜索条件
8285
*/
83-
const getModules = (filterIntercept) => {
86+
const getModules = (filterIntercept: Function) => {
8487
let modules = {}
8588

8689
if (typeof filterIntercept === 'function') {
@@ -104,7 +107,7 @@ const getModules = (filterIntercept) => {
104107
* @param {Function} filterIntercept 搜索条件
105108
* @param {Boolean} isSort 是否需要排序
106109
*/
107-
const getSortModules = ({ filterIntercept, isSort = true }) => {
110+
const getSortModules = ({ filterIntercept, isSort = true }: { filterIntercept: Function; isSort: boolean }) => {
108111
let modules: Module[] = []
109112
let componentCount = 0
110113
const importName = '@opentiny/vue'
@@ -119,7 +122,7 @@ const getSortModules = ({ filterIntercept, isSort = true }) => {
119122
// 这段逻辑暂时没有用到
120123
const componentName = dirs.slice(1, dirs.indexOf('src'))
121124
// UpperName: Todo
122-
component.UpperName = utils.capitalizeKebabCase(componentName.pop())
125+
component.UpperName = utils.capitalizeKebabCase(componentName.pop() ?? '')
123126

124127
// LowerName: todo
125128
component.LowerName = utils.kebabCase({ str: component.UpperName })
@@ -331,7 +334,7 @@ const isNotArrayObject = (sortData, key, setIndex) => {
331334
let sortItem = {}
332335

333336
if (typeof dataItem !== 'object') {
334-
sortItem.__real_value = dataItem
337+
(sortItem as unknown as Record<string, any>).__real_value = dataItem
335338
} else {
336339
sortItem = {
337340
...sortData[sortKey]
@@ -375,11 +378,13 @@ const getComponents = (mode, isSort = true) => {
375378
* 获取模块项的模块
376379
* @param {String} componentName 组件名称(大写,例如:img-preview)
377380
* @param {Oject} newObj 新增对象
378-
* @param {Boolean} isMobile 是否为移动组件
379381
* @returns 模块对象
380382
*/
381-
export const addModule = ({ componentName, templateName, newObj = {}, isMobile = false }) => {
382-
const isEntry = templateName.endsWith('index')
383+
export const addModule = (
384+
{ componentName, templateName, newObj = {} }:
385+
{ componentName: string; templateName?: string; newObj?: object; isMobile: boolean }
386+
) => {
387+
const isEntry = templateName?.endsWith('index') ?? false
383388
return {
384389
path: `vue/src/${componentName}/` + (isEntry ? `${templateName}.ts` : `src/${templateName}.vue`),
385390
type: isEntry ? 'component' : 'template',

0 commit comments

Comments
 (0)