Guest User

Untitled

a guest
Nov 22nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. const fs = require('fs');
  2.  
  3. var _require = (filePath, required = false) => {
  4. try {
  5. console.log(`looking for ${filePath}`);
  6. if(filePath.endsWith('.json')) {
  7. return require(filePath);
  8. } else {
  9. return JSON.parse(fs.readFileSync(filePath));
  10. }
  11. } catch (error) {
  12. console.log(`cannot find ${filePath}`);
  13. if(required) {
  14. console.log(`${filePath} is needed. Exiting.`);
  15. process.exit(1);
  16. } else {
  17. return {};
  18. }
  19. }
  20. };
  21.  
  22. const babelConfFilePath = process.cwd() + '/' + '.babelrc';
  23. const settingPath = '.vscode';
  24. const settingsDirPath = process.cwd() + '/' + settingPath ;
  25. const settingsFilePath = process.cwd() + '/' + settingPath + '/settings.json';
  26. const jsconfigFilePath = process.cwd() + '/' + 'jsconfig.json';
  27.  
  28. const babelConf = _require(babelConfFilePath, true);
  29. const settings = _require(settingsFilePath);
  30. const jsconfig = _require(jsconfigFilePath);
  31.  
  32. let alias = {};
  33.  
  34. if (babelConf.plugins) {
  35. babelConf.plugins.forEach((elem) => {
  36. if(elem[0] === 'module-resolver') {
  37. for (var key in elem[1].alias) {
  38. alias[key] = elem[1].alias[key];
  39. }
  40. }
  41. });
  42. }
  43.  
  44. if( Object.keys(alias).length === 0 ) {
  45. console.log(`no plugin config in ${babelConfFilePath}`);
  46. process.exit(1);
  47. }
  48.  
  49. const newSettings = {};
  50. const newJsconfig = {};
  51.  
  52. // prepare jsconfig.json
  53. newJsconfig.compilerOptions = jsconfig.compilerOptions || {};
  54. newJsconfig.compilerOptions.baseUrl = './';
  55. newJsconfig.compilerOptions.paths = {};
  56. newJsconfig.exclude = jsconfig.exclude || [];
  57. if (newJsconfig.exclude.indexOf('node_modules') === -1) {
  58. newJsconfig.exclude.push('node_modules');
  59. }
  60.  
  61. Object.keys(alias).forEach((name) => {
  62. const value = alias[name];
  63. const settingsName = name;
  64. const settingsValue = value.replace('./', '${workspaceRoot}/');
  65. const JsconfigName = name+'/*';
  66. let JsconfigValue = value.replace('./','')+'/*';
  67. if (JsconfigValue === '/*' ) {
  68. JsconfigValue = '*';
  69. }
  70. const JsconfigName2 = name;
  71. const JsconfigValue2 = value.replace('./','');
  72.  
  73. newJsconfig.compilerOptions.paths[JsconfigName] = [JsconfigValue];
  74. newJsconfig.compilerOptions.paths[JsconfigName2] = [JsconfigValue2];
  75. newSettings[settingsName] = settingsValue;
  76. });
  77.  
  78. // prepare vscode/settings.json
  79. settings['path-intellisense.mappings'] = newSettings;
  80. settings['files.exclude'] = settings['files.exclude'] || {};
  81. settings['files.exclude']["jsconfig.json"] = true;
  82.  
  83. // write settings
  84. if (!fs.existsSync(settingsDirPath)){
  85. fs.mkdirSync(settingsDirPath);
  86. }
  87. if (fs.existsSync(settingsFilePath)) {
  88. fs.truncateSync(settingsFilePath, 0);
  89. }
  90. console.log(`writing ${settingsFilePath} ...`);
  91. fs.writeFileSync(settingsFilePath, JSON.stringify(settings, null, 2));
  92.  
  93. // write jsconfig
  94. if (fs.existsSync(jsconfigFilePath)) {
  95. fs.truncateSync(jsconfigFilePath, 0);
  96. }
  97. console.log(`writing ${jsconfigFilePath} ...`);
  98. fs.writeFileSync(jsconfigFilePath, JSON.stringify(newJsconfig, null, 2));
  99.  
  100. console.log('done.');
Add Comment
Please, Sign In to add comment