Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.');
Add Comment
Please, Sign In to add comment