configuration.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. var fs = require("fs");
  3. var path = require("path");
  4. var findup = require("findup-sync");
  5. exports.CONFIG_FILENAME = "tslint.json";
  6. exports.DEFAULT_CONFIG = {
  7. "rules": {
  8. "class-name": true,
  9. "comment-format": [true, "check-space"],
  10. "indent": [true, "spaces"],
  11. "no-duplicate-variable": true,
  12. "no-eval": true,
  13. "no-internal-module": true,
  14. "no-trailing-whitespace": true,
  15. "no-var-keyword": true,
  16. "one-line": [true, "check-open-brace", "check-whitespace"],
  17. "quotemark": [true, "double"],
  18. "semicolon": true,
  19. "triple-equals": [true, "allow-null-check"],
  20. "typedef-whitespace": [true, {
  21. "call-signature": "nospace",
  22. "index-signature": "nospace",
  23. "parameter": "nospace",
  24. "property-declaration": "nospace",
  25. "variable-declaration": "nospace"
  26. }],
  27. "variable-name": [true, "ban-keywords"],
  28. "whitespace": [true,
  29. "check-branch",
  30. "check-decl",
  31. "check-operator",
  32. "check-separator",
  33. "check-type"
  34. ],
  35. }
  36. };
  37. var moduleDirectory = path.dirname(module.filename);
  38. function findConfiguration(configFile, inputFileLocation) {
  39. if (configFile == null) {
  40. configFile = findup("package.json", { cwd: inputFileLocation, nocase: true });
  41. if (configFile) {
  42. var content = require(configFile);
  43. if (content.tslintConfig) {
  44. return content.tslintConfig;
  45. }
  46. }
  47. var homeDir = getHomeDir();
  48. if (!homeDir) {
  49. return undefined;
  50. }
  51. var defaultPath = path.join(homeDir, exports.CONFIG_FILENAME);
  52. configFile = findup(exports.CONFIG_FILENAME, { cwd: inputFileLocation, nocase: true }) || defaultPath;
  53. }
  54. if (fs.existsSync(configFile)) {
  55. var fileData = fs.readFileSync(configFile, "utf8");
  56. fileData = fileData.replace(/^\uFEFF/, "");
  57. return JSON.parse(fileData);
  58. }
  59. else {
  60. return exports.DEFAULT_CONFIG;
  61. }
  62. }
  63. exports.findConfiguration = findConfiguration;
  64. function getHomeDir() {
  65. var environment = global.process.env;
  66. var paths = [
  67. environment.USERPROFILE,
  68. environment.HOME,
  69. environment.HOMEPATH,
  70. environment.HOMEDRIVE + environment.HOMEPATH
  71. ];
  72. for (var _i = 0, paths_1 = paths; _i < paths_1.length; _i++) {
  73. var homePath = paths_1[_i];
  74. if (homePath != null && fs.existsSync(homePath)) {
  75. return homePath;
  76. }
  77. }
  78. }
  79. function getRelativePath(directory) {
  80. if (directory != null) {
  81. return path.relative(moduleDirectory, directory);
  82. }
  83. }
  84. exports.getRelativePath = getRelativePath;
  85. function getRulesDirectories(directories) {
  86. var rulesDirectories = [];
  87. if (directories != null) {
  88. if (typeof directories === "string") {
  89. rulesDirectories = [getRelativePath(directories)];
  90. }
  91. else {
  92. rulesDirectories = directories.map(function (dir) { return getRelativePath(dir); });
  93. }
  94. }
  95. return rulesDirectories;
  96. }
  97. exports.getRulesDirectories = getRulesDirectories;