@jqassistant/ts-lce
Version:
Tool to extract language concepts from a TypeScript codebase and export them to a JSON file.
95 lines (94 loc) • 3.75 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileUtils = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
/**
* Utility class that provides functionality with regard to file system interactions and the handling of file system paths.
*/
class FileUtils {
/**
* Normalizes a path, so that every '\' is replaced by a '/'
* This function should be used to convert all externally provided paths into a coherent internal representation.
*/
static normalizePath(path) {
return path.replace(/\\/g, "/");
}
/**
* Returns a list of paths to all files within a directory and all its subdirectories.
*
* @param dirPath absolute path to the directory of which all files should be listed
* @param arrayOfFiles used for recursion (just leave empty on call)
* @param ignoredDirs directories to ignore
*/
static getAllFiles(dirPath, arrayOfFiles = [], ignoredDirs = []) {
const files = fs.readdirSync(dirPath);
files.forEach(function (file) {
if (fs.statSync(path.join(dirPath, file)).isDirectory() && !ignoredDirs.includes(file)) {
FileUtils.getAllFiles(path.join(dirPath, file), arrayOfFiles, ignoredDirs);
}
else {
arrayOfFiles.push(path.join(dirPath, file));
}
});
return arrayOfFiles;
}
/**
* Determines the common directory for a list of paths.
*/
static commonDir(paths) {
if (paths.length === 0)
throw new Error("Provide a non-empty list of paths to determine the common path");
if (paths.length === 1) {
if (fs.statSync(paths[0]).isFile()) {
return path.dirname(paths[0]);
}
}
let commonSegments = paths[0].split(path.sep);
for (const filePath of paths) {
const segments = filePath.split(path.sep);
commonSegments = commonSegments.slice(0, segments.length);
for (let i = 0; i < segments.length; i++) {
if (commonSegments[i] !== segments[i]) {
commonSegments = commonSegments.slice(0, i);
break;
}
}
}
return commonSegments.join(path.sep);
}
}
exports.FileUtils = FileUtils;