feat: restore current project copies and choose text mapping
This commit is contained in:
@@ -255,9 +255,19 @@ function restorePattern(mapping: PlaceholderMapping, matchModes: ProjectRun["mat
|
||||
return new RegExp(parts.join("|"), "gu");
|
||||
}
|
||||
|
||||
export function restoreText(source: string, mapping: PlaceholderMapping, matchModes?: ProjectRun["matchModes"]): string {
|
||||
export function restoreTextWithCount(source: string, mapping: PlaceholderMapping,
|
||||
matchModes?: ProjectRun["matchModes"]): { text: string; replacements: number } {
|
||||
const pattern = restorePattern(mapping, matchModes);
|
||||
return pattern ? source.replace(pattern, (key) => mapping[key].original) : source;
|
||||
let replacements = 0;
|
||||
const text = pattern ? source.replace(pattern, (key) => {
|
||||
replacements++;
|
||||
return mapping[key].original;
|
||||
}) : source;
|
||||
return { text, replacements };
|
||||
}
|
||||
|
||||
export function restoreText(source: string, mapping: PlaceholderMapping, matchModes?: ProjectRun["matchModes"]): string {
|
||||
return restoreTextWithCount(source, mapping, matchModes).text;
|
||||
}
|
||||
|
||||
export function restorePath(relPath: string, mapping: PlaceholderMapping, matchModes?: ProjectRun["matchModes"]): string {
|
||||
@@ -305,6 +315,23 @@ export function runCopiesPath(run: ProjectRun, directory: string): string {
|
||||
return run.copyPath ?? join(directory, "files");
|
||||
}
|
||||
|
||||
export async function scanRestorableCopyFiles(run: ProjectRun, directory: string, current = true): Promise<{
|
||||
root: string; files: ProjectFile[]; skipped: number;
|
||||
}> {
|
||||
let root = current ? runCopiesPath(run, directory) : join(directory, "files");
|
||||
if (current && run.copyPath) {
|
||||
try {
|
||||
if (!(await fs.lstat(root)).isDirectory()) throw new Error("Kopienpfad ist kein regulärer Ordner.");
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
||||
root = join(directory, "files");
|
||||
}
|
||||
}
|
||||
const scan = await scanProjectFiles(root);
|
||||
const files = scan.files.filter((file) => file.kind === "text" || isRestorableDocument(file.relPath));
|
||||
return { root, files, skipped: scan.skipped.length + scan.files.length - files.length };
|
||||
}
|
||||
|
||||
/** Publish only output files next to the project, without the private mapping. */
|
||||
export async function publishProjectCopies(run: ProjectRun, directory: string): Promise<string> {
|
||||
const projectPath = canonicalProjectPath(run.projectPath);
|
||||
|
||||
Reference in New Issue
Block a user