Skip to content

Instantly share code, notes, and snippets.

@otakustay
Created March 15, 2019 06:49
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 otakustay/0d821d3fd67347feb572145deb89c223 to your computer and use it in GitHub Desktop.
Save otakustay/0d821d3fd67347feb572145deb89c223 to your computer and use it in GitHub Desktop.
Upgrade to reskript
const path = require('path');
const fs = require('fs');
const {execSync} = require('child_process');
const {sync: rimraf} = require('rimraf');
const {sync: glob} = require('glob');
const {mapValues} = require('lodash');
const topLevelModuleNames = glob('src/*/').map(path => path.slice(4, -1));
const replaceImportAlias = content => topLevelModuleNames.reduce(
(content, moduleName) => {
const regex = new RegExp(`(from|import) '(${moduleName}['/])`, 'g');
return content.replace(regex, `$1 '@/$2`);
},
content
);
const removeLegacyTools = () => {
console.log('Removing legacy @baidu/ee-fe-tools...');
execSync('npm rm @baidu/ee-fe-tools', {stdio: 'ignore'});
};
const migrateToYarn = () => {
console.log('Migrating to yarn...');
execSync('rm -rf node_modules', {stdio: 'ignore'});
execSync('yarn import', {stdio: 'ignore'});
fs.unlinkSync('package-lock.json');
execSync('yarn install', {stdio: 'ignore'});
};
const installReskript = () => {
console.log('Installing reskript...');
execSync('yarn add reskripts @baidu/reskripts-plugins', {stdio: 'ignore'});
};
const updateScripts = () => {
console.log('Updating scripts to reskript...');
const packageInfo = require(path.join(process.cwd(), 'package.json'));
const scripts = mapValues(packageInfo.scripts, value => value.replace(/(^| )et /, '$1skr '));
const updatedPackageInfo = {...packageInfo, scripts};
fs.writeFileSync(
path.join(process.cwd(), 'package.json'),
JSON.stringify(updatedPackageInfo, null, ' ') + '\n',
'utf-8'
);
};
const fixImports = () => {
console.log('Fixing import alias...');
const files = glob('src/**/*.js');
for (const file of files) {
const content = fs.readFileSync(file, 'utf-8');
const newContent = replaceImportAlias(content);
fs.writeFileSync(file, newContent, 'utf-8');
}
};
const fixLintErrors = () => {
console.log('Adopting new lint rules...');
try {
execSync('skr lint --fix', {stdio: 'ignore'});
}
catch (ex) {
console.warn('Lint still errors');
}
};
const main = () => {
removeLegacyTools();
migrateToYarn();
installReskript();
updateScripts();
fixImports();
fixLintErrors();
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment