(feature) add support for expanding paths on Windows + added more usage examples to cli

This commit is contained in:
Kim Biesbjerg
2020-03-06 13:53:46 +01:00
parent 7b94c9b9a5
commit b813ec0063
6 changed files with 81 additions and 36 deletions

26
src/utils/fs-helpers.ts Normal file
View File

@@ -0,0 +1,26 @@
import * as os from 'os';
import * as fs from 'fs';
import * as braces from 'braces';
export function normalizeHomeDir(path: string): string {
if (path.substring(0, 1) === '~') {
return `${os.homedir()}/${path.substring(1)}`;
}
return path;
}
export function expandPattern(pattern: string): string[] {
return braces(pattern, { expand: true });
}
export function normalizePaths(patterns: string[], defaultPatterns: string[] = []): string[] {
return patterns.map(pattern =>
expandPattern(pattern).map(path => {
path = normalizeHomeDir(path);
if (fs.existsSync(path) && fs.statSync(path).isDirectory()) {
return defaultPatterns.map(defaultPattern => path + defaultPattern);
}
return path;
}).flat()
).flat();
}