Created
March 15, 2019 06:49
-
-
Save otakustay/0d821d3fd67347feb572145deb89c223 to your computer and use it in GitHub Desktop.
Upgrade to reskript
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
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