Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JakeSidSmith
Last active December 6, 2017 17:52
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 JakeSidSmith/6664afbc933e4c07e5c679658be671bf to your computer and use it in GitHub Desktop.
Save JakeSidSmith/6664afbc933e4c07e5c679658be671bf to your computer and use it in GitHub Desktop.
Create aliasify config from tsconfig.json
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;
{
"aliasify": "./aliasify-tsconfig.js"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment