UNPKG

@adonisjs/require-ts

Version:
49 lines (48 loc) 1.75 kB
"use strict"; /* * @adonisjs/require-ts * * (c) Harminder Virk <virk@adonisjs.comharminder@cav.ai> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadTypescript = exports.getCachePathForFile = exports.debug = void 0; const debug_1 = __importDefault(require("debug")); const normalize_path_1 = __importDefault(require("normalize-path")); exports.debug = (0, debug_1.default)('adonis:require-ts'); /** * Returns the cache directory path for a given file. The idea is to * use the filename as a directory and then drop files with their * hashes inside that directory. In case the file gets changed, * we just need to drop the directory */ function getCachePathForFile(cwd, location) { const tokens = (0, normalize_path_1.default)(location.replace(cwd, '')).split('/'); const fileName = tokens.pop(); tokens.shift(); if (!tokens.length) { return fileName.replace(/\.\w+$/, ''); } return `${tokens.join('-')}-${fileName.replace(/\.\w+$/, '')}`; } exports.getCachePathForFile = getCachePathForFile; /** * Loads typescript from the user project dependencies */ function loadTypescript(cwd) { try { return require(require.resolve('typescript', { paths: [cwd] })); } catch (error) { if (error.code === 'ENOENT') { throw new Error('"@adonisjs/require-ts" expects the "typescript" to be installed'); } throw error; } } exports.loadTypescript = loadTypescript;