Refactor: 重构cli (#238)

* refactor: 重构shared/utils

* fix: 修复shared/utils类型错误

* refactor: 重构cli
This commit is contained in:
GaoNeng 2023-05-20 20:25:21 +08:00 committed by Kagol
parent b319be37ef
commit 8cf6d4adaa
No known key found for this signature in database
GPG Key ID: 9E995265BF6376D0
4 changed files with 93 additions and 72 deletions

View File

@ -8,10 +8,11 @@ import Handlebars from 'handlebars'
let RegCache = {}
let HandlebarsCompile
const replaceDelimiters = function (str, sourceReg, escape) {
const replaceDelimiters = function (str, sourceReg, escape?: boolean) {
let regex = RegCache[sourceReg] || (RegCache[sourceReg] = new RegExp(sourceReg, 'g'))
let match
// eslint-disable-next-line no-cond-assign
while ((match = regex.exec(str))) {
let prefix = str.slice(0, match.index)
let inner = (escape ? '\\' : '') + '{{' + match[1] + '}}'
@ -23,7 +24,8 @@ const replaceDelimiters = function (str, sourceReg, escape) {
return str
}
Handlebars.setDelimiter = function (delimiters) {
Object.defineProperty(Handlebars, 'setDelimiter', {
value(delimiters: string[]) {
if (delimiters[0].slice(-1) !== '=') {
delimiters[0] += '(?!=)'
}
@ -35,6 +37,7 @@ Handlebars.setDelimiter = function (delimiters) {
}
Handlebars.compile = function (str) {
// eslint-disable-next-line prefer-rest-params
let args = [].slice.call(arguments)
if (typeof str === 'string') {
@ -48,6 +51,7 @@ Handlebars.setDelimiter = function (delimiters) {
return HandlebarsCompile.apply(Handlebars, args)
}
}
})
/**
*
@ -59,8 +63,13 @@ Handlebars.setDelimiter = function (delimiters) {
* @param {Object} delimiter
* @returns {String}
*/
export default function ({ delimiter, template, options, data }) {
delimiter && Handlebars.setDelimiter(delimiter)
return Handlebars.compile(template, options)(data)
export default function ({ delimiter, template, options, data }: {
delimiter?: string[]
template: string
options?: CompileOptions
data: any
}) {
delimiter && (Handlebars as typeof Handlebars & { setDelimiter: (delimiter: string[]) => any }).setDelimiter(delimiter)
const compile = Handlebars.compile(template, options)
return compile(data)
}

View File

@ -5,21 +5,21 @@
* yarn create:ui img-preview -single pc /
* yarn create:ui img-preview -mobile
*/
const path = require('path')
const fs = require('fs-extra')
const semver = require('semver')
const utils = require('./utils')
const { createModuleMapping } = require('./module-utils')
const handlebarsRender = require('./handlebars.render')
import path from 'path'
import fs from 'fs-extra'
import semver from 'semver'
import * as utils from '../../shared/utils'
import { createModuleMapping } from '../../shared/module-utils'
import handlebarsRender from '../build/handlebars.render'
const args = utils.getInputCmd()
if (args.length > 0) {
const commands = []
const components = []
const templateDir = utils.pathJoin('../template/component')
const componetDir = utils.pathJoin('../../packages/vue/components')
const { version } = fs.readJSONSync(utils.pathJoin('../../packages/vue/package.json'))
const commands: string[] = []
const components: string[] = []
const templateDir = utils.pathJoin('../../public/template/component')
const componetDir = utils.pathJoin('../../../../packages/vue/src')
const { version } = fs.readJSONSync(utils.pathJoin('../../../../packages/vue/package.json'))
args.forEach((item) => {
if (item.indexOf('-') === 0) {

View File

@ -47,7 +47,7 @@ export interface Module {
* @param {Boolean} isSort
* @returns
*/
const getAllModules = (isSort) => {
const getAllModules = (isSort: boolean) => {
return getSortModules({ filterIntercept: () => true, isSort })
}
@ -55,7 +55,7 @@ const getAllModules = (isSort) => {
* @param {String} key Key
* @returns
*/
const getModuleInfo = (key) => {
const getModuleInfo = (key: string) => {
return moduleMap[key] || {}
}
@ -66,7 +66,10 @@ const getModuleInfo = (key) => {
* @param {Boolean} isOriginal
* @param {Boolean} isSort
*/
const getByName = ({ name, inversion = false, isOriginal = false, isSort = true }) => {
const getByName = (
{ name, inversion = false, isOriginal = false, isSort = true }:
{ name: string;inversion: boolean;isOriginal: boolean;isSort: boolean }
) => {
const callback = (item) => {
const result = new RegExp(`/${name}/|^vue-${name}/`).test(item.path)
return inversion ? !result : result
@ -80,7 +83,7 @@ const getByName = ({ name, inversion = false, isOriginal = false, isSort = true
* @private
* @param {Function} filterIntercept
*/
const getModules = (filterIntercept) => {
const getModules = (filterIntercept: Function) => {
let modules = {}
if (typeof filterIntercept === 'function') {
@ -104,7 +107,7 @@ const getModules = (filterIntercept) => {
* @param {Function} filterIntercept
* @param {Boolean} isSort
*/
const getSortModules = ({ filterIntercept, isSort = true }) => {
const getSortModules = ({ filterIntercept, isSort = true }: { filterIntercept: Function; isSort: boolean }) => {
let modules: Module[] = []
let componentCount = 0
const importName = '@opentiny/vue'
@ -119,7 +122,7 @@ const getSortModules = ({ filterIntercept, isSort = true }) => {
// 这段逻辑暂时没有用到
const componentName = dirs.slice(1, dirs.indexOf('src'))
// UpperName: Todo
component.UpperName = utils.capitalizeKebabCase(componentName.pop())
component.UpperName = utils.capitalizeKebabCase(componentName.pop() ?? '')
// LowerName: todo
component.LowerName = utils.kebabCase({ str: component.UpperName })
@ -331,7 +334,7 @@ const isNotArrayObject = (sortData, key, setIndex) => {
let sortItem = {}
if (typeof dataItem !== 'object') {
sortItem.__real_value = dataItem
(sortItem as unknown as Record<string, any>).__real_value = dataItem
} else {
sortItem = {
...sortData[sortKey]
@ -375,11 +378,13 @@ const getComponents = (mode, isSort = true) => {
*
* @param {String} componentName img-preview
* @param {Oject} newObj
* @param {Boolean} isMobile
* @returns
*/
export const addModule = ({ componentName, templateName, newObj = {}, isMobile = false }) => {
const isEntry = templateName.endsWith('index')
export const addModule = (
{ componentName, templateName, newObj = {} }:
{ componentName: string; templateName?: string; newObj?: object; isMobile: boolean }
) => {
const isEntry = templateName?.endsWith('index') ?? false
return {
path: `vue/src/${componentName}/` + (isEntry ? `${templateName}.ts` : `src/${templateName}.vue`),
type: isEntry ? 'component' : 'template',

View File

@ -8,7 +8,7 @@ import { fileURLToPath } from 'node:url'
import { searchForWorkspaceRoot } from 'vite'
const workspaceRoot = searchForWorkspaceRoot(process.cwd())
const pathFromWorkspaceRoot = (...args) => path.resolve(workspaceRoot, ...args)
const pathFromWorkspaceRoot = (...args: string[]) => path.resolve(workspaceRoot, ...args)
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
@ -17,7 +17,7 @@ const dirname = path.dirname(filename)
*
* @returns
*/
const resolveCwd = (...args) => {
const resolveCwd = (...args: any[]) => {
return path.join(process.cwd(), ...args)
}
@ -26,7 +26,7 @@ const resolveCwd = (...args) => {
* @param {String} posixPath
* @returns
*/
const assetsPath = (posixPath) => {
const assetsPath = (posixPath: string) => {
return path.posix.join('static', posixPath)
}
@ -42,7 +42,7 @@ const getComponentName = () => {
*
* @returns
*/
const pathJoin = (...args) => {
const pathJoin = (...args: string[]) => {
return path.join(dirname, ...args)
}
@ -79,7 +79,7 @@ const getCurrentCliTool = () => {
* node
* @param {String} cmdStr
*/
const execCmd = (cmdStr) => {
const execCmd = (cmdStr: string) => {
cmdStr && execSync(cmdStr, { stdio: 'inherit' })
}
@ -88,7 +88,7 @@ const execCmd = (cmdStr) => {
* @param {String} str
* @returns
*/
const capitalize = (str) => {
const capitalize = (str: string) => {
return typeof str === 'string' ? str.slice(0, 1).toUpperCase() + str.slice(1) : str
}
@ -97,7 +97,7 @@ const capitalize = (str) => {
* @param {String} str
* @returns
*/
const capitalizeKebabCase = (str, splitChar = '-') => {
const capitalizeKebabCase = (str: string, splitChar = '-') => {
return typeof str === 'string' ? str.split(splitChar).map(capitalize).join('') : str
}
@ -109,7 +109,7 @@ const capitalizeKebabCase = (str, splitChar = '-') => {
* @param str
* @param splitChar
*/
const kebabCase = ({ str, splitChar = '-' }) => {
const kebabCase = ({ str, splitChar = '-' }: { str: string; splitChar?: string }) => {
if (!str || typeof str !== 'string') return str
return str
@ -119,7 +119,7 @@ const kebabCase = ({ str, splitChar = '-' }) => {
if (charCod < 65 || charCod > 122) return char
return (charCod >= 65 && charCod) <= 90 ? (index !== 0 ? splitChar : '') + char.toLowerCase() : char
return (charCod >= 65 && charCod <= 90) ? (index !== 0 ? splitChar : '') + char.toLowerCase() : char
})
.join('')
}
@ -129,7 +129,7 @@ const kebabCase = ({ str, splitChar = '-' }) => {
* @param {String} str
* @param {Object} options
*/
const prettierFormat = ({ str, options = {} }) => {
const prettierFormat = ({ str, options = {} }: { str: string; options: object }) => {
return prettier.format(
str,
Object.assign(
@ -156,7 +156,10 @@ const prettierFormat = ({ str, options = {} }) => {
* @param {Function} fileFilter
* @param {Function} callback
*/
const walkFileTree = ({ dirPath, isDeep = false, fileFilter, callback }) => {
const walkFileTree = (
{ dirPath, isDeep = false, fileFilter, callback }:
{ dirPath: string; isDeep: boolean; fileFilter?: Function; callback: Function }
) => {
if (!dirPath || typeof callback !== 'function') {
return
}
@ -192,7 +195,7 @@ const walkFileTree = ({ dirPath, isDeep = false, fileFilter, callback }) => {
* @param {Boolean} vue2
* @returns
*/
const getVersion = ({ name, context, isVue2 }) => {
const getVersion = ({ name, context, isVue2 }: { name: string; context: string; isVue2: boolean }) => {
return getComponentVersion({ name, context, dir: 'node_modules', isVue2 })
}
@ -204,8 +207,11 @@ const getVersion = ({ name, context, isVue2 }) => {
* @param {Boolean} vue2
* @returns
*/
const getComponentVersion = ({ name, context = '..', dir = 'packages', isOrigin = false, isVue2 }) => {
let version
const getComponentVersion = (
{ name, context = '..', dir = 'packages', isOrigin = false, isVue2 }:
{ name: string; context?: string; dir?: string; isOrigin?: boolean; isVue2: boolean }
) => {
let version: string
const packageJSONPath = pathJoin(context, dir, name, 'package.json')
if (fs.existsSync(packageJSONPath)) {
@ -228,7 +234,7 @@ const getComponentVersion = ({ name, context = '..', dir = 'packages', isOrigin
* @param {Boolean} vue2
* @returns
*/
const getPublichVersion = ({ version, isVue2 }) => {
const getPublichVersion = ({ version, isVue2 }: { version: string; isVue2: boolean }) => {
if (isVue2) {
return version.replace(/^4/, '3').replace('?v=4', '?v=3')
}
@ -243,7 +249,7 @@ const getPublichVersion = ({ version, isVue2 }) => {
* @param {Boolean} vue2
* @returns
*/
const getPackageVersion = ({ name, isRoot = false, isVue2 = false }) => {
const getPackageVersion = ({ name, isRoot = false, isVue2 = false }: { name: string; isRoot: boolean; isVue2: boolean }) => {
let version = isRoot ? getopentinyVersion({ key: name }) : getComponentVersion({ name, isOrigin: true, isVue2 })
return getBigVersion(version)
@ -259,7 +265,7 @@ const getBigVersion = (version) => {
* @param {Boolean} vue2
* @returns
*/
const getComponentOriginVersion = ({ name, isVue2 }) => {
const getComponentOriginVersion = ({ name, isVue2 }: { name: string; isVue2: boolean }) => {
return getComponentVersion({ name, isOrigin: true, isVue2 })
}
@ -269,7 +275,7 @@ const getComponentOriginVersion = ({ name, isVue2 }) => {
* @param {Boolean} vue2
* @returns
*/
const getopentinyVersion = ({ key = 'version' }) => {
const getopentinyVersion = ({ key = 'version' }: { key: string }) => {
const packageJson = fs.readJsonSync(pathFromWorkspaceRoot('packages/vue/package.json'))
const packageJsonOption = packageJson[key] || packageJson
@ -318,7 +324,7 @@ const getInnerDependenciesVersion = ({ isTestEnv, tag, version }) => {
* 绿
* @param {String}
*/
const logGreen = (str) => {
const logGreen = (str: string) => {
/* eslint-disable no-console */
console.log(chalk.green('### ' + str))
}
@ -327,7 +333,7 @@ const logGreen = (str) => {
*
* @param {String}
*/
const logYellow = (str) => {
const logYellow = (str: string) => {
console.log(chalk.yellow('### ' + str))
}
@ -335,7 +341,7 @@ const logYellow = (str) => {
*
* @param {String}
*/
const logCyan = (str) => {
const logCyan = (str: string) => {
console.log(chalk.cyan('### ' + str))
}
@ -343,7 +349,7 @@ const logCyan = (str) => {
*
* @param {String}
*/
const logRed = (str) => {
const logRed = (str: string) => {
console.log(chalk.red('### ' + str))
}
@ -421,7 +427,7 @@ const getBuildTag = ({ version }) => {
*
* @return {Array<string>} filesPath
*/
const getFilesPath = (folderPath) => {
const getFilesPath = (folderPath: string): Array<string> => {
let filesPath: string[] = []
try {
let files = fs.readdirSync(folderPath)
@ -430,6 +436,7 @@ const getFilesPath = (folderPath) => {
let stats = fs.statSync(tempPath)
if (stats.isDirectory()) {
// eslint-disable-next-line prefer-spread
filesPath.push.apply(filesPath, getFilesPath(tempPath))
} else {
filesPath.push(tempPath)
@ -469,7 +476,7 @@ const fragmentReplace = (filePath, regExpStr, targetStr) => {
* @param {Array<string> | string} regExpStr
* @param {Array<string> | string} targetStr
*/
const filesFragmentReplace = (folderPath, regExpStr, targetStr) => {
const filesFragmentReplace = (folderPath, regExpStr: Array<string> | string, targetStr: Array<string> | string) => {
let filesPath = getFilesPath(folderPath)
if (filesPath) {