This commit is contained in:
1518079148
2023-08-21 11:35:19 +08:00
parent 37125ab0c1
commit 64938d5c51
29 changed files with 291 additions and 137 deletions

12
单文件拆分/.gitignore vendored Normal file
View File

@@ -0,0 +1,12 @@
# dir
node_modules
dist
# file
stats.html
/build/auto/*.d.ts
# lock
yarn.lock
package-lock.json
pnpm-lock.yaml

View File

@@ -0,0 +1,30 @@
# 忽略的文件夹:
node_modules/
dist/
# 忽略的文件
package.json
package-lock.json
yarn.lock
yarn-error.log
.babelrc
.browserslistrc
.eslintignore
.gitignore
.prettierignore
.editorconfig
.DS_Store
.project
*.yml
*.yaml
*.svg
*.png
*.jpg
*.gif
*.md
*.ejs
*.min.js
*.min.css
*.html
*.js
*.d.ts

View File

@@ -0,0 +1,11 @@
{
"printWidth": 120,
"proseWrap": "always",
"tabWidth": 4,
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"endOfLine": "auto",
"quoteProps": "preserve",
"htmlWhitespaceSensitivity": "ignore"
}

32
单文件拆分/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,32 @@
{
"typescript.updateImportsOnFileMove.enabled": "always",
"javascript.updateImportsOnFileMove.enabled": "always",
"volar.codeLens.scriptSetupTools": true,
"volar.codeLens.pugTools": true,
"files.exclude": {
"**/*.meta": true
},
"explorer.confirmDragAndDrop": false,
"diffEditor.ignoreTrimWhitespace": false,
"window.zoomLevel": 0,
"[less]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": true,
"editor.formatOnType": true,
"eslint.codeAction.showDocumentation": {
"enable": true
},
"eslint.validate": ["typescript", "vue"],
"prettier.singleQuote": true,
"typescript.tsdk": "node_modules/typescript/lib"
}

35
单文件拆分/index.ts Normal file
View File

@@ -0,0 +1,35 @@
import { resolve, join } from 'path'
import FileUtil from './src/utils/FileUtil'
const siconUrl = resolve('E:/repo/code/wty-web-cloud-las-0/src/components/sicon')
const staticIconUrl = resolve('E:/repo/code/wty-web-cloud-las-0/public/static_res/sicon')
const init = async () => {
await FileUtil.deleteFileOrDirectory(staticIconUrl)
await FileUtil.readDirectory(siconUrl, async (filepath: string) => {
if (filepath.endsWith('.ts')) {
const filename =
filepath
.substring(filepath.lastIndexOf('\\') + 1)
.split('.')
.shift() || ''
try {
let fstr = await FileUtil.readFile(filepath)
fstr = fstr.split(`export const ${filename}Name`)[0]
fstr = fstr.split('=')[1]
const ctx = JSON.parse(fstr.replaceAll("'", '"'))
Object.keys(ctx).forEach((key) => {
let _res = JSON.stringify(ctx[key])
if(typeof ctx[key] === 'string'){
_res = _res.substring(1,_res.length-1)
}
FileUtil.writeFile(join(staticIconUrl, `./${filename}/${key}.json`), _res)
})
} catch {
console.log('处理失败', filename)
}
}
})
}
init()

View File

@@ -0,0 +1,12 @@
{
"scripts": {
"dev": "ts-node index.ts"
},
"devDependencies": {
"@types/node": "^18.15.11",
"prettier": "^2.8.7",
"ts-node": "^10.9.1",
"typescript": "^4.9.5",
"vm": "^0.1.0"
}
}

View File

@@ -0,0 +1,84 @@
import * as fs from 'fs'
import * as path from 'path'
export default class FileUtil {
static copyFile(source: string, destination: string): Promise<void> {
return new Promise((resolve, reject) => {
const rd = fs.createReadStream(source)
rd.on('error', (err) => {
reject(err)
})
const wr = fs.createWriteStream(destination)
wr.on('error', (err) => {
reject(err)
})
wr.on('close', () => {
resolve()
})
if (!fs.existsSync(path.dirname(destination))) {
fs.mkdirSync(path.dirname(destination), { recursive: true })
}
rd.pipe(wr)
})
}
static async deleteFileOrDirectory(filePath: string): Promise<void> {
try {
const stats = await fs.promises.lstat(filePath)
if (stats.isDirectory()) {
const files = await fs.promises.readdir(filePath)
const promises = files.map((file) => FileUtil.deleteFileOrDirectory(path.join(filePath, file)))
await Promise.all(promises)
await fs.promises.rmdir(filePath)
} else {
await fs.promises.unlink(filePath)
}
} catch (err) {
}
}
static readFile(filePath: string): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
}
static async readDirectory(directoryPath: string, fileCallback?: (filePath: string) => void): Promise<void> {
try {
const files = await fs.promises.readdir(directoryPath)
const promises = files.map(async (file) => {
const filePath = path.join(directoryPath, file)
const stats = await fs.promises.stat(filePath)
if (stats.isDirectory()) {
await FileUtil.readDirectory(filePath, fileCallback)
} else {
if (fileCallback) {
fileCallback(filePath)
}
}
})
await Promise.all(promises)
} catch (err) {
throw err
}
}
static async writeFile(filePath: string, content: string): Promise<void> {
try {
const directoryPath = path.dirname(filePath)
await fs.promises.mkdir(directoryPath, { recursive: true })
await fs.promises.writeFile(filePath, content, 'utf8')
} catch (err) {
throw err
}
}
}

View File

@@ -0,0 +1,66 @@
export default class StrUtil {
/**
* 获取uuid
* @returns
*/
static uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
private static id = 1000
/**
* 获取唯一递增id
*/
static getId = () => {
this.id++
return this.id + ''
}
/**
* 判断内容是否为空,null,undefined,NaN空数组空字符串返回true
*/
static isNull(str: any): boolean {
if (Array.isArray(str)) return str.length == 0
if (str + '' !== 'null' && str + '' !== 'undefined' && str !== '' && str + '' !== 'NaN') return false
return true
}
/**
* 验证邮箱
*/
static isEmail(obj: any): boolean {
var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
if (!myreg.test(obj)) return false
return true
}
/**
* 验证数字
*/
static isNumber(obj: any): boolean {
return /^[0-9]+$/.test(obj)
}
/**
* 获取url地址中的参数对象信息
* @param url 如/list?pageSize=1&pageNum=2
* @param return 如{pageSize:1,pageNum:2}
*/
static getParam(url: any): any {
const regex = /[?&]([^=#]+)=([^&#]*)/g
if (url.match) {
const matchVal = url.match(regex)
if (matchVal)
return matchVal.reduce((acc: any, cur: any) => {
const [param, value] = cur.substring(1).split('=')
acc[param] = decodeURIComponent(value)
return acc
}, {})
}
return {}
}
}

View File

@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"strict": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}