Last active
April 12, 2020 11:48
-
-
Save sempostma/c0ee628e3beb468d7145aa2a5f463610 to your computer and use it in GitHub Desktop.
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') | |
const os = require('os') | |
const minimatch = require('minimatch') | |
const options = { matchBase: true } | |
const commit = process.argv[2] === 'commit' | |
const trim = x => x.trim() | |
const gitignore = fs.readFileSync(path.resolve(process.cwd(), '.gitignore'), { encoding: 'utf8' }) | |
.split(os.EOL) | |
.map(trim) | |
.filter(Boolean) | |
.filter(x => !x.startsWith('#')) | |
gitignore.push('.git') | |
const ignore = gitignore | |
.map(pattern => new minimatch.Minimatch(pattern, options)) | |
const filter = file => { | |
return !ignore.some(matcher => matcher.match('/' + file)) | |
} | |
var isJS = new minimatch.Minimatch("*.{js,jsx}", { matchBase: true }) | |
const recurseDir = (p) => { | |
const files = fs.readdirSync(path.join(process.cwd(), p)) | |
.map(x => path.join(p, x)) | |
.filter(filter) | |
for (let i = 0; i < files.length; i++) { | |
const file = files[i]; | |
const stat = fs.lstatSync(file) | |
if (stat.isDirectory()) { | |
recurseDir(file) | |
} else if (isJS.match(file)) { | |
processFile(file) | |
} | |
} | |
} | |
const hasMaterialUIComponentInName = line => line.includes('@material-ui/core\'') || line.includes('@material-ui/core"') | |
const processFile = file => { | |
const lines = fs.readFileSync(file, { encoding: 'utf8' }) | |
.split(os.EOL) | |
const newLines = lines.map(line => { | |
if (hasMaterialUIComponentInName(line)) { | |
const importParser = /import\s+{(\s+.+)}\s+from\s+/ | |
const result = line.match(importParser) | |
if (!result) { | |
console.warn(`Unable to parse line "${line}". Please format this line manually`) | |
return line | |
} | |
const [_, cg] = result | |
const components = cg.split(',').map(trim) | |
const newLines = [] | |
for (let i = 0; i < components.length; i++) { | |
const component = components[i]; | |
if (component === 'withStyles') { | |
newLines.push('import { withStyles } from \'@material-ui/core/styles\'') | |
} else if (component === 'makeStyles') { | |
newLines.push('import { makeStyles } from \'@material-ui/core/styles\'') | |
} else { | |
newLines.push(`import ${component} from \'@material-ui/core/${component}\'`) | |
} | |
} | |
console.log(`Transformed material ui lines to \n ${newLines.join(',\n ')}`) | |
return newLines.join(os.EOL) | |
} else { | |
return line | |
} | |
}) | |
const newContents = newLines.join(os.EOL) | |
if (commit) { | |
fs.writeFileSync(path.join(process.cwd(), file), newContents) | |
} | |
} | |
recurseDir('.') | |
if (!commit) { | |
console.log('Please pass "commit" to this command to prevent a dry run. Remember to git commit or backup before running this command.') | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment