@ts-ast-parser/core
Version:
Reflects a simplified version of the TypeScript AST for generating documentation
173 lines • 4.92 kB
JavaScript
import { platform } from 'node:process';
import * as path from 'node:path';
import * as fs from 'node:fs';
import ts from 'typescript';
/**
* System interface to interact with the OS in a Node.js environment.
*
* All interaction of the TypeScript compiler with the operating system goes
* through a System interface.
*
* You can think of it as the Operating Environment (OE).
*/
export class NodeSystem {
get args() {
return ts.sys.args;
}
get newLine() {
return ts.sys.newLine;
}
get useCaseSensitiveFileNames() {
return ts.sys.useCaseSensitiveFileNames;
}
/**
* Writes the content
*
* @param content - Content to write to the file
*/
write(content) {
ts.sys.write(content);
}
/**
* Reads the data encoded inside a file
*
* @param filePath - The file path
* @param encoding - The file econding
*
* @returns The content of the file
*/
readFile(filePath, encoding) {
const absolutePath = this.getAbsolutePath(filePath);
return ts.sys.readFile(absolutePath, encoding) ?? '';
}
writeFile(filePath, data) {
const absolutePath = this.getAbsolutePath(filePath);
this._ensureDirectoryExistence(absolutePath);
ts.sys.writeFile(absolutePath, data);
}
resolvePath(filePath) {
return ts.sys.resolvePath(filePath);
}
/**
* Checks whether the file exists
*
* @param filePath
* @returns True if the file exists, otherwise false
*/
fileExists(filePath) {
const absolutePath = this.getAbsolutePath(filePath);
return ts.sys.fileExists(absolutePath);
}
directoryExists(filePath) {
const absolutePath = this.getAbsolutePath(filePath);
return ts.sys.directoryExists(absolutePath);
}
createDirectory(filePath) {
const absolutePath = this.getAbsolutePath(filePath);
this._ensureDirectoryExistence(absolutePath);
ts.sys.createDirectory(absolutePath);
}
getExecutingFilePath() {
return ts.sys.getExecutingFilePath();
}
/**
* The current working directory
*/
getCurrentDirectory() {
return ts.sys.getCurrentDirectory();
}
/**
* Returns the directory names (not the absolute path)
*
* @param filePath - The path from where to search
*/
getDirectories(filePath) {
const absolutePath = this.getAbsolutePath(filePath);
return ts.sys.getDirectories(absolutePath);
}
readDirectory(filePath, extensions, exclude, include, depth) {
const absolutePath = this.getAbsolutePath(filePath);
return ts.sys.readDirectory(absolutePath, extensions, exclude, include, depth);
}
exit(exitCode) {
ts.sys.exit(exitCode);
}
/**
* Normalizes the path based on the OS and makes it
* relative to the current working directory.
*
* @param filePath
*/
normalizePath(filePath) {
return path.normalize(path.relative(this.getCurrentDirectory(), filePath));
}
/**
* Returns the directory name
*
* @param filePath
*/
getDirectoryName(filePath) {
return path.dirname(filePath);
}
/**
* Returns a string with the filename portion of the path
*
* @param filePath
*/
getBaseName(filePath) {
return path.basename(filePath);
}
/**
* Joins the segments using the path separator of the OS/Browser
*
* @param segments
*/
join(...segments) {
return path.join(...segments);
}
/**
* Checks if the path is an absolute path. An absolute
* path is a path that starts with the ROOT directory.
*
* @param filePath
* @returns True if the path is absolute
*/
isAbsolute(filePath) {
return path.isAbsolute(filePath);
}
/**
* Resolves symlinks to get the real path
*
* @param filePath
*/
realpath(filePath) {
return fs.realpathSync.native
? platform === 'win32'
? this.fsRealPathHandlingLongPath(filePath)
: fs.realpathSync.native(filePath)
: fs.realpathSync(filePath);
}
/**
* Returns the absolute path
*
* @param filePath
*/
getAbsolutePath(filePath) {
if (this.isAbsolute(filePath)) {
return filePath;
}
return path.resolve(filePath);
}
_ensureDirectoryExistence(filePath) {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return;
}
this._ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
fsRealPathHandlingLongPath(filePath) {
return filePath.length < 260 ? fs.realpathSync.native(filePath) : fs.realpathSync(filePath);
}
}
//# sourceMappingURL=node-system.js.map