dgeni-packages
Version:
A collection of dgeni packages for generating documentation from source code
114 lines • 5.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomCompilerHost = void 0;
const fs = require("fs");
const typescript_1 = require("typescript");
const path = require('canonical-path');
// We need to provide our own version of CompilerHost because we want to set the
// base directory and specify what extensions to consider when trying to load a source
// file
class CustomCompilerHost {
constructor(options, baseDir, extensions, log) {
this.options = options;
this.baseDir = baseDir;
this.extensions = extensions;
this.log = log;
this.writeFile = () => { };
}
getSourceFile(fileName, languageVersion, onError) {
var _a;
let text;
let resolvedPath;
let resolvedPathWithExt;
// let's just try loading the file as-is initially
try {
resolvedPath = path.resolve(this.baseDir, fileName);
text = fs.readFileSync(resolvedPath, { encoding: this.options.charset });
this.log.debug('found source file:', fileName);
return (0, typescript_1.createSourceFile)(resolvedPath, text, languageVersion);
}
catch (e) {
// if it is a folder then try loading the index file of that folder
if (e.code === 'EISDIR') {
return this.getSourceFile(fileName + '/index.ts', languageVersion, onError);
}
// otherwise ignore the error and move on to the strategy below...
}
// Special case for `/// <reference types="x">` directives.
// Typescript does not fully resolve these before calling getSourceFile
// so fileName ends up looking like `node_modules/@types/x/index.d.ts`.
// In the case that node_modules is below the baseDir, we look
// for ../node_modules/@types/x/index.d.ts, ../..node_modules/@types/x/index.d.ts, etc...
if (fileName.startsWith(path.join('node_modules', '@types')) && fileName.endsWith('index.d.ts')) {
// The base directory path is already posix normalized through TypeScript. In order to
// properly determine the depth of the base dir, the delimiter is always a forward slash.
const baseDirDepth = this.baseDir.split('/').length;
let maybe = path.join('..', fileName);
for (let i = 0; i < baseDirDepth; ++i) {
try {
resolvedPath = path.resolve(this.baseDir, maybe);
text = fs.readFileSync(resolvedPath, { encoding: this.options.charset });
this.log.debug('found source file:', fileName);
return (0, typescript_1.createSourceFile)(resolvedPath, text, languageVersion);
}
catch (e) {
// ignore the error and move on to the next maybe...
}
maybe = path.join('..', maybe);
}
}
// Strip off the extension and resolve relative to the baseDir
resolvedPath = path.resolve(this.baseDir, fileName).replace(/\.[^./]+$/, '');
// Iterate through each possible extension and return the first source file that is actually found
for (const extension of this.extensions) {
// Try reading the content from files using each of the given extensions
try {
resolvedPathWithExt = resolvedPath + extension;
this.log.silly('getSourceFile:', resolvedPathWithExt);
text = fs.readFileSync(resolvedPathWithExt, { encoding: this.options.charset });
this.log.debug('found source file:', fileName, resolvedPathWithExt);
return (0, typescript_1.createSourceFile)(resolvedPathWithExt, text, languageVersion);
}
catch (e) {
// Try again if the file simply did not exist, otherwise report the error as a warning
if (((_a = e) === null || _a === void 0 ? void 0 : _a.code) !== 'ENOENT') {
const errorMessage = (e instanceof Error) ? e.message : `${e}`;
if (onError)
onError(errorMessage);
this.log.warn('Error reading ' + resolvedPathWithExt + ' : ' + errorMessage);
}
}
}
throw new Error('No SourceFile found with path ' + fileName);
}
getDefaultLibFileName(options) {
return path.resolve(path.dirname(typescript_1.sys.getExecutingFilePath()), (0, typescript_1.getDefaultLibFileName)(options));
}
getCurrentDirectory() {
return this.baseDir;
}
getDirectories(p) {
return [];
}
useCaseSensitiveFileNames() {
return typescript_1.sys.useCaseSensitiveFileNames;
}
getCanonicalFileName(fileName) {
// if underlying system can distinguish between two files whose names differs only in cases then file name already in canonical form.
// otherwise use toLowerCase as a canonical form.
return typescript_1.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
}
getNewLine() {
return typescript_1.sys.newLine;
}
fileExists(fileName) {
return fs.existsSync(fileName);
}
readFile(fileName) {
/* tslint:disable:no-console */
this.log.debug('readFile', fileName);
return fs.readFileSync(fileName, 'utf-8');
}
}
exports.CustomCompilerHost = CustomCompilerHost;
//# sourceMappingURL=CustomCompilerHost.js.map