Created
February 7, 2022 17:31
-
-
Save CodeWitchBella/1e967f2d4ba295b62ca73369b10c4693 to your computer and use it in GitHub Desktop.
This file contains 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
// files was created by running | |
// grep -R createFactory modules > files | |
const fs = require('fs') | |
const files = Array.from( | |
new Set( | |
fs | |
.readFileSync('files', 'utf-8') | |
.trim() | |
.split('\n') | |
.map((v) => v.split(':')[0]), | |
), | |
) | |
for (const f of files) { | |
console.log(f) | |
let text = fs.readFileSync(f, 'utf-8') | |
const list = [] | |
const fixes = [] | |
text = text.replace(/\nconst ([^ ]+) = createFactory\((.*)\)\n/, (text, ...args) => { | |
list.push(args[0]) | |
if (`${args[0]}Component` === args[1]) { | |
fixes.push([args[1], args[0]]) | |
return '\n' | |
} | |
return `\nconst ${args[0]} = ${args[1]}\n` | |
}) | |
for (const [a, b] of fixes) text = text.replace(a, b) | |
for (const item of list) { | |
text = text.replaceAll(new RegExp(`([^a-zA-Z])${item}\\(`, 'g'), (text, one, idx, all) => { | |
const next = all.substring(idx + text.length, idx + text.length + 1) | |
console.log(text, one, idx, JSON.stringify(next)) | |
return `${one}React.createElement(${item}${next !== ')' ? ', ' : ''}` | |
}) | |
} | |
if (text.lastIndexOf('createFactory') === text.indexOf('createFactory')) { | |
text = text.replace(`import { createFactory } from 'modules/core/common/create-factory'\n`, '') | |
} | |
fs.writeFileSync(f, text) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment