Last active
September 25, 2020 18:21
-
-
Save aesqe/e2a50a6fcbc1c1a11f3aa930510f9534 to your computer and use it in GitHub Desktop.
Convert aliased paths to relative paths
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 fs = require('fs') | |
const path = require('path') | |
const klaw = require('node-klaw') | |
const items = [] | |
const rootPath = path.resolve('./src') | |
const searchFor = / from '~\/([^']+)'/g | |
klaw(rootPath) | |
.on('data', item => { | |
if ( | |
(item.path.endsWith('.ts') || item.path.endsWith('.tsx')) && | |
!item.path.endsWith('.d.ts') && | |
!item.path.includes('node_modules') | |
) { | |
items.push(item.path) | |
} | |
}) | |
.on('end', () => { | |
items.forEach(itemPath => { | |
try { | |
const data = fs.readFileSync(itemPath, 'utf8') | |
const matches = data.match(searchFor) | |
if (matches) { | |
let replacedData = data | |
matches.forEach(match => { | |
const cleanMatch = match.replace(" from '~/", '') | |
const importPath = path.resolve(rootPath, cleanMatch) | |
const relativeImportPath = path.relative(path.dirname(itemPath), importPath) | |
replacedData = replacedData.replace(match, ` from '${relativeImportPath}`) | |
}) | |
fs.writeFileSync(itemPath, replacedData) | |
} | |
} catch (e) { | |
console.error(e) | |
} | |
}) | |
console.log('Successfully replaced all aliased paths with relative paths!') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment