Last active
December 6, 2017 17:52
-
-
Save JakeSidSmith/6664afbc933e4c07e5c679658be671bf to your computer and use it in GitHub Desktop.
Create aliasify config from tsconfig.json
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 tsconfig = require('./tsconfig.json'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const ASTERISK = /\*/g; | |
const LEADING_DOT_SLASH = /^\.\//; | |
const CARET = /\^/g; | |
const { compilerOptions: { paths } } = tsconfig; | |
function formatTSConfig () { | |
let formatted = {}; | |
for (let key in paths) { | |
const from = key | |
.replace(CARET, '\\^') | |
.replace(ASTERISK, '(.+)'); | |
let index = 0; | |
const to = path.join(__dirname, paths[key][0] | |
.replace(LEADING_DOT_SLASH, '') | |
.replace(ASTERISK, () => { | |
return `$${index += 1}`; | |
})); | |
formatted[from] = (alias, regexMatch, regexObject) => { | |
let result = alias.replace(regexObject, to); | |
if (fs.existsSync(result) && fs.lstatSync(result).isDirectory()) { | |
result = path.join(result, 'index'); | |
} | |
if (fs.existsSync(result)) { | |
return result; | |
} | |
if (fs.existsSync(result + '.ts')) { | |
return result + '.ts'; | |
} | |
if (fs.existsSync(result + '.tsx')) { | |
return result + '.tsx'; | |
} | |
return result; | |
}; | |
} | |
return formatted; | |
} | |
const replacements = formatTSConfig(); | |
const aliasifyConfig = { | |
configDir: __dirname, | |
replacements, | |
appliesTo: { | |
includeExtensions: [ | |
'.ts', | |
'.tsx' | |
] | |
}, | |
verbose: true | |
}; | |
console.log(aliasifyConfig); | |
module.exports = aliasifyConfig; |
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
{ | |
"aliasify": "./aliasify-tsconfig.js" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment