Last active
October 10, 2023 08:08
-
-
Save heycqing/acbc19f1f64d6fb7cb4340e410a35e31 to your computer and use it in GitHub Desktop.
require to import to kye
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| const path = require('path'); | |
| function findRequireStatements(dir, results = []) { | |
| const files = fs.readdirSync(dir); | |
| files.forEach(file => { | |
| const filePath = path.join(dir, file); | |
| const stats = fs.statSync(filePath); | |
| if (stats.isDirectory()) { | |
| findRequireStatements(filePath, results); | |
| } else if (stats.isFile() && (filePath.endsWith('.js') || filePath.endsWith('.vue'))) { | |
| let content = fs.readFileSync(filePath, 'utf8'); | |
| /** TODO: 正则替换 | |
| * 使用情况分布如下: | |
| * 1. const a = require('./a'); | |
| * 2. return require('./a'); | |
| * 3. '1': require('../../images/title-icon.png') | |
| * src: require('../images/sales-Amt-All.png') | |
| * 4.GRAY_TOP.src = require('public/components/kye-canvas-table/images/graytop.png') | |
| */ | |
| const regex = /(const\s+\w+\s+=\s+require\(.+\))/g; | |
| let match; | |
| while ((match = regex.exec(content)) !== null) { | |
| results.push({ | |
| match: filePath, | |
| find: match[0], | |
| replace: match[0].replace('require', 'import') | |
| }); | |
| } | |
| } | |
| }); | |
| return results; | |
| } | |
| // 使用方法:console.log(findRequireStatements('<你的目录路径>')); | |
| // console.log(findRequireStatements('D:\code\base-vite\src\kyetree\pages\crm\client-track\dialog\followUpRemarks\progress-status.vue')) | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| function findRequireStatements(dir, results = []) { | |
| const files = fs.readdirSync(dir); | |
| files.forEach(file => { | |
| const filePath = path.join(dir, file); | |
| const stats = fs.statSync(filePath); | |
| if (stats.isDirectory()) { | |
| findRequireStatements(filePath, results); | |
| } else if (stats.isFile() && (filePath.endsWith('.js') || filePath.endsWith('.vue'))) { | |
| let content = fs.readFileSync(filePath, 'utf8'); | |
| const regexes = [ | |
| /(const\s+\w+\s+=\s+require\(.+\))/g, | |
| /(return\s+require\(.+\))/g, | |
| /('\w+'\s*:\s*require\(.+\))/g, | |
| /(\w+\.src\s*=\s*require\(.+\))/g | |
| ]; | |
| regexes.forEach(regex => { | |
| let match; | |
| while ((match = regex.exec(content)) !== null) { | |
| results.push({ | |
| match: filePath, | |
| find: match[0], | |
| replace: match[0].replace('require', 'import') | |
| }); | |
| } | |
| }); | |
| } | |
| }); | |
| return results; | |
| } | |
| // 使用方法:console.log(findRequireStatements('<你的目录路径>')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment