|
1 | 1 | import { Translations, Message, PoData, PoDataCompact } from "./parser"; |
2 | 2 | import generate from "@babel/generator"; |
3 | 3 | import { Node } from "@babel/types"; |
| 4 | +import { globSync } from "glob"; |
| 5 | +import * as fs from "fs"; |
| 6 | +const isGlob = require("is-glob"); |
4 | 7 |
|
5 | 8 | const pluralNumRegex = /^nplurals ?= ?(\d);/; |
6 | 9 |
|
@@ -70,3 +73,46 @@ export function convert2Compact(poData: PoData): PoDataCompact { |
70 | 73 | delete compactPo.contexts[""][""]; |
71 | 74 | return compactPo; |
72 | 75 | } |
| 76 | + |
| 77 | +/** |
| 78 | + * Helper function that calculates all the file paths from a src array with optional ignore paths. |
| 79 | + * The folder paths are ignored, since further directory traversing is not necessary. |
| 80 | + */ |
| 81 | +export function resolvePaths( |
| 82 | + /** Regular paths or globs */ |
| 83 | + src: string[], |
| 84 | + /** |
| 85 | + * Could be a string or an array of strings, depending on how many paths are passed by the cli. |
| 86 | + * |
| 87 | + * `--ignore path1` => 'path1' |
| 88 | + * |
| 89 | + * `--ignore path1 --ignore path2` => ['path1', 'path2'] |
| 90 | + */ |
| 91 | + ignore?: string | string[] |
| 92 | +): string[] { |
| 93 | + const toGlob = (path: string): string => { |
| 94 | + if (fs.lstatSync(path).isDirectory()) { |
| 95 | + if (!path.endsWith("/")) { |
| 96 | + path = `${path}/`; |
| 97 | + } |
| 98 | + return `${path}**/*`; |
| 99 | + } else { |
| 100 | + return path; |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + /** All paths are internally transformed to globs, to keep backwards compatibility with 'src' paths */ |
| 105 | + const srcGlob = src.map(path => (isGlob(path) ? path : toGlob(path))); |
| 106 | + if (typeof ignore === "string") { |
| 107 | + ignore = [ignore]; |
| 108 | + } |
| 109 | + const ignoreGlob = ignore?.map(path => |
| 110 | + isGlob(path) ? path : toGlob(path) |
| 111 | + ); |
| 112 | + |
| 113 | + return globSync(srcGlob, { |
| 114 | + absolute: true, |
| 115 | + nodir: true, |
| 116 | + ignore: ignoreGlob |
| 117 | + }); |
| 118 | +} |
0 commit comments