40 lines
888 B
TypeScript
40 lines
888 B
TypeScript
import * as path from 'path';
|
|
|
|
export class CLIUtils {
|
|
static resolveDirectory(dir: string): string {
|
|
if (path.isAbsolute(dir)) {
|
|
return dir;
|
|
}
|
|
return path.resolve(process.cwd(), dir);
|
|
}
|
|
|
|
static resolveOutputPath(
|
|
output: string,
|
|
format: 'json' | 'yaml'
|
|
): string {
|
|
if (path.isAbsolute(output)) {
|
|
return output;
|
|
}
|
|
|
|
if (!output.endsWith(`.${format}`)) {
|
|
return `${output}.${format}`;
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
static formatBytes(bytes: number): string {
|
|
if (bytes === 0) return '0 B';
|
|
const k = 1024;
|
|
const sizes = ['B', 'KB', 'MB', 'GB'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
|
|
}
|
|
|
|
static sanitizePattern(pattern: string): string {
|
|
return pattern
|
|
.replace(/\*/g, '.*')
|
|
.replace(/\?/g, '.');
|
|
}
|
|
}
|