Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuhairZain/ad6e3ce0f1d34050a6d89a3291b2bebb to your computer and use it in GitHub Desktop.
Save SuhairZain/ad6e3ce0f1d34050a6d89a3291b2bebb to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const execSync = require('child_process').execSync;
const regex = /^(import PropTypes from 'prop-types';)\n(\/\/ \(C\) Copyright.*)\n\n(import React(?:.*) from 'react';)([\s\S]*)$/;
const processDirectory = (dir) => {
fs.readdirSync(dir).forEach((file) => {
if (fs.lstatSync(path.join(dir, file)).isDirectory()) {
if (file !== 'base')
processDirectory(path.join(dir, file));
} else {
const fileName = path.join(dir, file);
execSync(
`jscodeshift -t ../react-codemod/transforms/React-PropTypes-to-prop-types.js ${fileName}`
);
const contents = fs.readFileSync(path.join(dir, file), 'utf-8');
if (regex.test(contents)) {
fs.writeFileSync(
path.join(dir, file),
contents.replace(regex, '$2\n\n$3\n$1$4'),
'utf-8');
}
}
});
};
processDirectory(path.join(__dirname, 'src', 'js'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment