Skip to content

Instantly share code, notes, and snippets.

@DavidBabel
Last active November 21, 2017 17:29
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 DavidBabel/4ad8dcc823eef53faf0fb04da458bc22 to your computer and use it in GitHub Desktop.
Save DavidBabel/4ad8dcc823eef53faf0fb04da458bc22 to your computer and use it in GitHub Desktop.
help to install vscode intellisense aliases integration when babel-plugin-module-resolver is already installed and configured
const fs = require('fs');
var _require = (filePath, required = false) => {
try {
console.log(`looking for ${filePath}`);
if(filePath.endsWith('.json')) {
return require(filePath);
} else {
return JSON.parse(fs.readFileSync(filePath));
}
} catch (error) {
console.log(`cannot find ${filePath}`);
if(required) {
console.log(`${filePath} is needed. Exiting.`);
process.exit(1);
} else {
return {};
}
}
};
const babelConfFilePath = process.cwd() + '/' + '.babelrc';
const settingPath = '.vscode';
const settingsDirPath = process.cwd() + '/' + settingPath ;
const settingsFilePath = process.cwd() + '/' + settingPath + '/settings.json';
const jsconfigFilePath = process.cwd() + '/' + 'jsconfig.json';
const babelConf = _require(babelConfFilePath, true);
const settings = _require(settingsFilePath);
const jsconfig = _require(jsconfigFilePath);
let alias = {};
if (babelConf.plugins) {
babelConf.plugins.forEach((elem) => {
if(elem[0] === 'module-resolver') {
for (var key in elem[1].alias) {
alias[key] = elem[1].alias[key];
}
}
});
}
if( Object.keys(alias).length === 0 ) {
console.log(`no plugin config in ${babelConfFilePath}`);
process.exit(1);
}
const newSettings = {};
const newJsconfig = {};
// prepare jsconfig.json
newJsconfig.compilerOptions = jsconfig.compilerOptions || {};
newJsconfig.compilerOptions.baseUrl = './';
newJsconfig.compilerOptions.paths = {};
newJsconfig.exclude = jsconfig.exclude || [];
if (newJsconfig.exclude.indexOf('node_modules') === -1) {
newJsconfig.exclude.push('node_modules');
}
Object.keys(alias).forEach((name) => {
const value = alias[name];
const settingsName = name;
const settingsValue = value.replace('./', '${workspaceRoot}/');
const JsconfigName = name+'/*';
let JsconfigValue = value.replace('./','')+'/*';
if (JsconfigValue === '/*' ) {
JsconfigValue = '*';
}
const JsconfigName2 = name;
const JsconfigValue2 = value.replace('./','');
newJsconfig.compilerOptions.paths[JsconfigName] = [JsconfigValue];
newJsconfig.compilerOptions.paths[JsconfigName2] = [JsconfigValue2];
newSettings[settingsName] = settingsValue;
});
// prepare vscode/settings.json
settings['path-intellisense.mappings'] = newSettings;
settings['files.exclude'] = settings['files.exclude'] || {};
settings['files.exclude']["jsconfig.json"] = true;
// write settings
if (!fs.existsSync(settingsDirPath)){
fs.mkdirSync(settingsDirPath);
}
if (fs.existsSync(settingsFilePath)) {
fs.truncateSync(settingsFilePath, 0);
}
console.log(`writing ${settingsFilePath} ...`);
fs.writeFileSync(settingsFilePath, JSON.stringify(settings, null, 2));
// write jsconfig
if (fs.existsSync(jsconfigFilePath)) {
fs.truncateSync(jsconfigFilePath, 0);
}
console.log(`writing ${jsconfigFilePath} ...`);
fs.writeFileSync(jsconfigFilePath, JSON.stringify(newJsconfig, null, 2));
console.log('done.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment