All files dtsCreator.js

93.6% Statements 117/125
76.6% Branches 36/47
100% Functions 28/28
94.5% Lines 103/109

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237    1x     1x   9x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   8x   27x   1x     1x 1x     1x   15x 15x 15x 15x 15x 15x 15x 15x   15x   15x 15x 15x 15x 15x 15x 15x 15x     1x     1x   1x 1x     1x 1x 1x     1x               8x         6x 5x         1x         5x 5x         1x       1x     1x   12x   12x 12x 12x 12x 12x 12x 12x 12x 12x     1x     16x   16x   16x   16x     16x   16x     16x 15x 15x 15x 15x 15x 15x   15x   15x 14x 14x 14x 14x           15x 14x     15x                     15x         1x             15x   2x   1x   12x 11x                             1x 1x         1x  
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DtsCreator = undefined;
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _process = require('process');
 
var _process2 = _interopRequireDefault(_process);
 
var _fs = require('fs');
 
var _fs2 = _interopRequireDefault(_fs);
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _isThere = require('is-there');
 
var _isThere2 = _interopRequireDefault(_isThere);
 
var _mkdirp = require('mkdirp');
 
var _mkdirp2 = _interopRequireDefault(_mkdirp);
 
var _camelcase = require('camelcase');
 
var _camelcase2 = _interopRequireDefault(_camelcase);
 
var _tokenValidator = require('./tokenValidator');
 
var _fileSystemLoader = require('./fileSystemLoader');
 
var _fileSystemLoader2 = _interopRequireDefault(_fileSystemLoader);
 
var _os = require('os');
 
var _os2 = _interopRequireDefault(_os);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
var validator = new _tokenValidator.TokenValidator();
 
function removeExtension(filePath) {
  var ext = _path2.default.extname(filePath);
  return filePath.replace(new RegExp(ext + '$'), '');
}
 
var DtsContent = function () {
  function DtsContent(_ref) {
    var dropExtension = _ref.dropExtension,
        rootDir = _ref.rootDir,
        searchDir = _ref.searchDir,
        outDir = _ref.outDir,
        rInputPath = _ref.rInputPath,
        rawTokenList = _ref.rawTokenList,
        resultList = _ref.resultList,
        messageList = _ref.messageList;
 
    _classCallCheck(this, DtsContent);
 
    this.dropExtension = dropExtension;
    this.rootDir = rootDir;
    this.searchDir = searchDir;
    this.outDir = outDir;
    this.rInputPath = rInputPath;
    this.rawTokenList = rawTokenList;
    this.resultList = resultList;
    this.messageList = messageList;
  }
 
  _createClass(DtsContent, [{
    key: 'writeFile',
    value: function writeFile() {
      var _this = this;
 
      var outPathDir = _path2.default.dirname(this.outputFilePath);
      Iif (!(0, _isThere2.default)(outPathDir)) {
        _mkdirp2.default.sync(outPathDir);
      }
      return new Promise(function (resolve, reject) {
        _fs2.default.writeFile(_this.outputFilePath, _this.formatted + _os2.default.EOL, 'utf8', function (err) {
          Iif (err) {
            reject(err);
          } else {
            resolve(_this);
          }
        });
      });
    }
  }, {
    key: 'contents',
    get: function get() {
      return this.resultList;
    }
  }, {
    key: 'formatted',
    get: function get() {
      if (!this.resultList || !this.resultList.length) return '';
      return this.resultList.join(_os2.default.EOL);
    }
  }, {
    key: 'tokens',
    get: function get() {
      return this.rawTokenList;
    }
  }, {
    key: 'outputFilePath',
    get: function get() {
      var outputFileName = this.dropExtension ? removeExtension(this.rInputPath) : this.rInputPath;
      return _path2.default.join(this.rootDir, this.outDir, outputFileName + '.d.ts');
    }
  }, {
    key: 'inputFilePath',
    get: function get() {
      return _path2.default.join(this.rootDir, this.searchDir, this.rInputPath);
    }
  }]);
 
  return DtsContent;
}();
 
var DtsCreator = exports.DtsCreator = function () {
  function DtsCreator(options) {
    _classCallCheck(this, DtsCreator);
 
    if (!options) options = {};
    this.rootDir = options.rootDir || _process2.default.cwd();
    this.searchDir = options.searchDir || '';
    this.outDir = options.outDir || this.searchDir;
    this.loader = new _fileSystemLoader2.default(this.rootDir);
    this.inputDirectory = _path2.default.join(this.rootDir, this.searchDir);
    this.outputDirectory = _path2.default.join(this.rootDir, this.outDir);
    this.camelCase = options.camelCase;
    this.dropExtension = !!options.dropExtension;
  }
 
  _createClass(DtsCreator, [{
    key: 'create',
    value: function create(filePath, initialContents) {
      var _this2 = this;
 
      var clearCache = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
 
      return new Promise(function (resolve, reject) {
        var rInputPath;
        Iif (_path2.default.isAbsolute(filePath)) {
          rInputPath = _path2.default.relative(_this2.inputDirectory, filePath);
        } else {
          rInputPath = _path2.default.relative(_this2.inputDirectory, _path2.default.join(_process2.default.cwd(), filePath));
        }
        Iif (clearCache) {
          _this2.loader.tokensByFile = {};
        }
        _this2.loader.fetch(filePath, "/", undefined, initialContents).then(function (res) {
          Eif (res) {
            var tokens = res;
            var keys = Object.keys(tokens);
            var validKeys = [],
                invalidKeys = [];
            var messageList = [];
 
            var convertKey = _this2.getConvertKeyMethod(_this2.camelCase);
 
            keys.forEach(function (key) {
              var convertedKey = convertKey(key);
              var ret = validator.validate(convertedKey);
              Eif (ret.isValid) {
                validKeys.push(convertedKey);
              } else {
                messageList.push(ret.message);
              }
            });
 
            var result = validKeys.map(function (k) {
              return 'export const ' + k + ': string;';
            });
 
            var content = new DtsContent({
              dropExtension: _this2.dropExtension,
              rootDir: _this2.rootDir,
              searchDir: _this2.searchDir,
              outDir: _this2.outDir,
              rInputPath: rInputPath,
              rawTokenList: keys,
              resultList: result,
              messageList: messageList
            });
 
            resolve(content);
          } else {
            reject(res);
          }
        }).catch(function (err) {
          return reject(err);
        });
      });
    }
  }, {
    key: 'getConvertKeyMethod',
    value: function getConvertKeyMethod(camelCaseOption) {
      switch (camelCaseOption) {
        case true:
          return _camelcase2.default;
        case 'dashes':
          return this.dashesCamelCase;
        default:
          return function (key) {
            return key;
          };
      }
    }
 
    /**
     * Replaces only the dashes and leaves the rest as-is.
     *
     * Mirrors the behaviour of the css-loader:
     * https://github.com/webpack-contrib/css-loader/blob/1fee60147b9dba9480c9385e0f4e581928ab9af9/lib/compile-exports.js#L3-L7
     */
 
  }, {
    key: 'dashesCamelCase',
    value: function dashesCamelCase(str) {
      return str.replace(/-+(\w)/g, function (match, firstLetter) {
        return firstLetter.toUpperCase();
      });
    }
  }]);
 
  return DtsCreator;
}();