tsc-prog
Version:
Build your TypeScript projects programmatically.
63 lines (62 loc) • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.changeExtension = exports.changeDir = exports.fileIsWithin = exports.parentPaths = exports.relativeToCWD = exports.ensureAbsolutePath = void 0;
const p = require("path");
/**
* @internal
*/
function ensureAbsolutePath(path, basePath = process.cwd()) {
if (!path)
return '';
return p.isAbsolute(path) ? path : p.join(basePath, path);
}
exports.ensureAbsolutePath = ensureAbsolutePath;
/**
* @internal
*/
function relativeToCWD(path) {
return p.relative(process.cwd(), path);
}
exports.relativeToCWD = relativeToCWD;
/**
* @internal
*/
function parentPaths(path) {
// tslint:disable-next-line: prefer-const
let { root, dir } = p.parse(path);
const parents = [];
while (dir !== root) {
parents.push(dir);
dir = p.dirname(dir);
}
return parents;
}
exports.parentPaths = parentPaths;
/**
* @internal
*/
function fileIsWithin(file, dir) {
const rel = p.relative(dir, file);
return !rel.startsWith('../') && rel !== '..';
}
exports.fileIsWithin = fileIsWithin;
/**
* @internal
*/
function changeDir(file, fromDir, toDir) {
return p.resolve(toDir, p.relative(fromDir, file));
}
exports.changeDir = changeDir;
/**
* @param matchExtensions - extensions to replace, match everything if empty. Items should start with a dot.
* @param newExtension - should start with a dot.
* @internal
*/
function changeExtension(file, matchExtensions, newExtension) {
const oldExtension = p.extname(file);
if (matchExtensions.length === 0 || matchExtensions.includes(oldExtension)) {
return p.join(p.dirname(file), p.basename(file, oldExtension) + newExtension);
}
return file;
}
exports.changeExtension = changeExtension;