Files
GDevelop/newIDE/app/scripts/codemod-replace-ui-components.js
Florian Rival d2be0e815e Upgrade some components to material-ui v4
- Tables
- TextField
- SelectField (using native fields)
- Dialog
- Buttons
- Background texts
- Divider
- Menu
- Lists
- IconButton
- ObjectSelector
- Autocomplete (with keyboard support)
  - [x] Proper focus support
- Subheaders
- Toggle
- LinearProgress
- CircularProgress
- Checkbox
- Avatar
- Chip
- Tabs
- LinearProgress
- Toolbar
- Stepper
- Snackbar (dismissable and won't be shown again until reactivated)
- ClosableTabs (with context close all/close others context menu)
2019-09-17 22:24:08 +01:00

147 lines
3.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const shell = require('shelljs');
const path = require('path');
const fs = require('fs');
const recursive = require('recursive-readdir');
const newIdeAppSrcPath = path.join(__dirname, '../src');
const verbose = true;
const dryRun = true;
const excludedPaths = ['UI', 'locales'];
const importsToReplace = [
{
oldImport: 'material-ui/FlatButton',
newImport: path.join(newIdeAppSrcPath, 'UI/FlatButton'),
},
{
oldImport: 'material-ui/RaisedButton',
newImport: path.join(newIdeAppSrcPath, 'UI/RaisedButton'),
},
{
oldImport: 'material-ui/TextField',
newImport: path.join(newIdeAppSrcPath, 'UI/TextField'),
},
{
oldImport: 'material-ui/IconButton',
newImport: path.join(newIdeAppSrcPath, 'UI/IconButton'),
},
{
oldImport: 'material-ui/SelectField',
newImport: path.join(newIdeAppSrcPath, 'UI/SelectField'),
},
{
oldImport: 'material-ui/MenuItem',
newImport: path.join(newIdeAppSrcPath, 'UI/MenuItem'),
},
{
oldImport: 'material-ui/Checkbox',
newImport: path.join(newIdeAppSrcPath, 'UI/Checkbox'),
},
{
oldImport: 'material-ui/Table',
newImport: path.join(newIdeAppSrcPath, 'UI/Table'),
},
{
oldImport: 'material-ui/Toggle',
newImport: path.join(newIdeAppSrcPath, 'UI/Toggle'),
},
{
oldImport: 'material-ui/Tabs',
newImport: path.join(newIdeAppSrcPath, 'UI/Tabs'),
},
{
oldImport: 'material-ui/List',
newImport: path.join(newIdeAppSrcPath, 'UI/List'),
},
{
oldImport: 'material-ui/Subheader',
newImport: path.join(newIdeAppSrcPath, 'UI/Subheader'),
},
{
oldImport: 'material-ui/Toolbar',
newImport: path.join(newIdeAppSrcPath, 'UI/Toolbar'),
},
];
/**
* Replace an import by another.
*/
const replaceImport = (oldImport, newImport) => source => {
const regex = new RegExp(` from '${oldImport}';`, '');
return source.replace(regex, match => {
const replacement = ` from '${newImport}';`;
if (verbose)
console.log('Replace "' + match + '" by "' + replacement + '"');
return replacement;
});
};
const replaceImportsInFile = filePath =>
new Promise((resolve, reject) =>
fs.readFile(filePath, { encoding: 'utf8' }, (err, source) => {
if (err) return reject(err);
let newSource = source;
importsToReplace.forEach(({ oldImport, newImport }) => {
const newRelativeImport = path.relative(
path.dirname(filePath),
newImport
);
newSource = replaceImport(oldImport, newRelativeImport)(newSource);
});
if (source === newSource) {
return resolve({
filePath,
changed: false,
});
}
if (!dryRun) {
fs.writeFile(filePath, newSource, { encoding: 'utf8' }, err => {
if (err) {
shell.echo(`❌ Error while writing file ${filePath}: `, err);
return reject(err);
}
resolve({
filePath,
changed: true,
});
});
} else {
shell.echo('️ Would write ' + filePath);
resolve({
filePath,
changed: true,
});
}
})
);
recursive(newIdeAppSrcPath, function(err, files) {
if (err) {
shell.echo(`❌ Error(s) occurred while reading files `, err);
shell.exit(1);
}
Promise.all(
files.map(filePath => {
if (excludedPaths.some(excludedPath => filePath.includes(excludedPath)))
return null;
return replaceImportsInFile(filePath);
})
).then(results => {
if (dryRun) {
shell.echo(
`⚠️ This was a dry-run, set dryRun to false to do real changes.`
);
shell.exit(0);
}
});
});