Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sync-translated-content): sync case changes + move adjacent files #7886

Merged
merged 13 commits into from
Feb 17, 2023
26 changes: 24 additions & 2 deletions content/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ function errorOnDuplicated(pairs: Pairs) {
}
}

function fixRedirectsCase(oldPairs: Pairs, caseChangedTargets: string[]) {
const newTargets = new Map(
caseChangedTargets.map((p) => [p.toLowerCase(), p])
);
const newPairs = oldPairs.map(([from, to]): Pair => {
const target = newTargets.get(to.toLowerCase()) ?? to;
return [from, target];
});
return newPairs;
}

function removeConflictingOldRedirects(oldPairs: Pairs, updatePairs: Pairs) {
if (oldPairs.length === 0) {
return oldPairs;
Expand Down Expand Up @@ -226,8 +237,19 @@ function loadLocaleAndAdd(
pairs.push(...loadPairsFromFile(redirectsFilePath, locale, strict && !fix));
}

const cleanPairs = removeConflictingOldRedirects(pairs, updatePairs);
cleanPairs.push(...updatePairs);
const caseChangedTargets = [];
const newPairs = [];
for (const [from, to] of updatePairs) {
if (from.toLowerCase() === to.toLowerCase()) {
caseChangedTargets.push(to);
} else {
newPairs.push([from, to]);
}
}

let cleanPairs = removeConflictingOldRedirects(pairs, newPairs);
cleanPairs = fixRedirectsCase(cleanPairs, caseChangedTargets);
cleanPairs.push(...newPairs);

let simplifiedPairs = shortCuts(cleanPairs);
if (fix) {
Expand Down
51 changes: 26 additions & 25 deletions tool/sync-translated-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,25 @@ export function syncAllTranslatedContent(locale) {
return stats;
}

function resolve(slug) {
function resolve(slug: string) {
if (!slug) {
return slug;
}
const url = buildURL("en-us", slug);
const resolved = Redirect.resolve(url);
if (url !== resolved) {
const doc = Document.read(Document.urlToFolderPath(resolved));
if (!doc) {
return slug;
}
const resolvedSlug = doc.metadata.slug;
if (slug !== resolvedSlug) {
return resolvedSlug;
}
}
return slug;
const doc = Document.read(Document.urlToFolderPath(resolved));
return doc?.metadata.slug ?? slug;
}

function mdOrHtmlExists(filePath) {
function mdOrHtmlExists(filePath: string) {
const dir = path.dirname(filePath);
return (
fs.existsSync(path.join(dir, MARKDOWN_FILENAME)) ||
fs.existsSync(path.join(dir, HTML_FILENAME))
);
}

export function syncTranslatedContent(inFilePath, locale) {
export function syncTranslatedContent(inFilePath: string, locale: string) {
if (!CONTENT_TRANSLATED_ROOT) {
throw new Error(
"CONTENT_TRANSLATED_ROOT must be set to sync translated content!"
Expand Down Expand Up @@ -135,7 +126,10 @@ export function syncTranslatedContent(inFilePath, locale) {
) {
return status;
}
status.moved = oldMetadata.slug.toLowerCase() !== metadata.slug.toLowerCase();
status.moved = oldMetadata.slug !== metadata.slug; // slug changes (case sensitive)
const filePathChanged =
status.moved &&
oldMetadata.slug.toLowerCase() !== metadata.slug.toLowerCase();
caugner marked this conversation as resolved.
Show resolved Hide resolved

if (status.moved) {
log.log(
Expand Down Expand Up @@ -200,8 +194,8 @@ export function syncTranslatedContent(inFilePath, locale) {
log.log(`${inFilePath} → ${filePath}`);
throw new Error(`file: ${filePath} already exists!`);
}
} else if (mdOrHtmlExists(filePath)) {
`unrooting ${inFilePath} (conflicting translation)`;
} else if (filePathChanged && mdOrHtmlExists(filePath)) {
console.log(`unrooting ${inFilePath} (conflicting translation)`);
metadata.slug = `${CONFLICTING}/${metadata.slug}`;
status.conflicting = true;
status.moved = true;
Expand All @@ -226,17 +220,24 @@ export function syncTranslatedContent(inFilePath, locale) {
oldMetadata.slug,
metadata.slug
);
fs.mkdirSync(path.dirname(filePath), { recursive: true });
execGit(["mv", inFilePath, filePath], { cwd: CONTENT_TRANSLATED_ROOT });
metadata.original_slug = oldMetadata.slug;
if (filePathChanged) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
execGit(["mv", inFilePath, filePath], { cwd: CONTENT_TRANSLATED_ROOT });

metadata.original_slug = oldMetadata.slug;
}
Document.saveFile(filePath, Document.trimLineEndings(rawBody), metadata);
try {
fs.rmdirSync(path.dirname(inFilePath));
} catch (e: any) {
if (e.code !== "ENOTEMPTY") {
throw e;

if (filePathChanged) {
try {
fs.rmdirSync(path.dirname(inFilePath));
} catch (e: any) {
if (e.code !== "ENOTEMPTY") {
throw e;
}
}
}

return status;
}

Expand Down