formatterLoader.js 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var fs = require("fs");
  20. var path = require("path");
  21. var resolve = require("resolve");
  22. var utils_1 = require("./utils");
  23. var CORE_FORMATTERS_DIRECTORY = path.resolve(__dirname, "formatters");
  24. function findFormatter(name, formattersDirectory) {
  25. if (typeof name === "function") {
  26. return name;
  27. }
  28. else if (typeof name === "string") {
  29. name = name.trim();
  30. var camelizedName = utils_1.camelize(name + "Formatter");
  31. // first check for core formatters
  32. var Formatter = loadFormatter(CORE_FORMATTERS_DIRECTORY, camelizedName, true);
  33. if (Formatter !== undefined) {
  34. return Formatter;
  35. }
  36. // then check for rules within the first level of rulesDirectory
  37. if (formattersDirectory !== undefined) {
  38. Formatter = loadFormatter(formattersDirectory, camelizedName);
  39. if (Formatter !== undefined) {
  40. return Formatter;
  41. }
  42. }
  43. // else try to resolve as module
  44. return loadFormatterModule(name);
  45. }
  46. else {
  47. // If an something else is passed as a name (e.g. object)
  48. throw new Error("Name of type " + typeof name + " is not supported.");
  49. }
  50. }
  51. exports.findFormatter = findFormatter;
  52. function loadFormatter(directory, name, isCore) {
  53. var formatterPath = path.resolve(path.join(directory, name));
  54. var fullPath;
  55. if (isCore) {
  56. fullPath = formatterPath + ".js";
  57. if (!fs.existsSync(fullPath)) {
  58. return undefined;
  59. }
  60. }
  61. else {
  62. // Resolve using node's path resolution to allow developers to write custom formatters in TypeScript which can be loaded by TS-Node
  63. try {
  64. fullPath = require.resolve(formatterPath);
  65. }
  66. catch (_a) {
  67. return undefined;
  68. }
  69. }
  70. return require(fullPath).Formatter;
  71. }
  72. function loadFormatterModule(name) {
  73. var src;
  74. try {
  75. // first try to find a module in the dependencies of the currently linted project
  76. src = resolve.sync(name, { basedir: process.cwd() });
  77. }
  78. catch (_a) {
  79. try {
  80. // if there is no local module, try relative to the installation of TSLint (might be global)
  81. src = require.resolve(name);
  82. }
  83. catch (_b) {
  84. return undefined;
  85. }
  86. }
  87. return require(src).Formatter;
  88. }