Skip to content

Commit

Permalink
Merge pull request #2982 from newrelic/2931_fix_document_doesnt_exist…
Browse files Browse the repository at this point in the history
…_bug

enable translation workflow to handle 'sending' documents that dont exist
  • Loading branch information
MoonlightKomorebi committed Jul 8, 2021
2 parents 8b71e06 + ebe3ca8 commit 96e4fd0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions .prettierrc.js
Expand Up @@ -4,4 +4,5 @@ module.exports = {
tabWidth: 2,
semi: true,
singleQuote: true,
arrowParens: 'always',
};
@@ -0,0 +1,23 @@
const fs = require('fs');

const { getContent } = require('../send-and-update-translation-queue');

jest.mock('../serialize-mdx');
jest.mock('fs');

test('getContent skips over files that dont exist', async () => {
fs.existsSync
.mockReturnValueOnce(false)
.mockReturnValueOnce(false)
.mockReturnValueOnce(false)
.mockReturnValue(true);

const mockInput = {
jp: ['slug1', 'slug2', 'slug3', 'slug4', 'slug5'],
};

const fileContent = getContent(mockInput);
const pages = await fileContent['jp'];

expect(pages.length).toBe(2); // 5 submitted slugs - 3 false mock returns = 2 files not skipped
});
41 changes: 32 additions & 9 deletions scripts/actions/send-and-update-translation-queue.js
Expand Up @@ -28,19 +28,31 @@ const DOCS_SITE_URL = 'https://docs.newrelic.com';
* @param {Object<string, string[]>} locales The queue of slugs to be translated.
* @returns {Object<string, Promise<Page[]>>}
*/
const getContent = (locales) =>
Object.entries(locales).reduce((acc, [locale, slugs]) => {
const getContent = (locales) => {
return Object.entries(locales).reduce((acc, [locale, slugs]) => {
return {
...acc,
[locale]: Promise.all(
slugs.map(async (slug) => {
const mdx = fs.readFileSync(path.join(process.cwd(), slug));
const html = await serializeMDX(mdx);
return { file: slug, html };
})
slugs
.filter((slug) => {
/**
* If a doc doesn't exist, it must have been renamed or deleted. In
* that case, it is safe to ignore. If we skip including a doc in
* this step, it won't become a failed upload, and will then be
* cleaned up from the queue.
*/
console.log(`Skipping over -- ${slug} -- since it no longer exists.`);
return fs.existsSync(path.join(process.cwd(), slug));
})
.map(async (slug) => {
const mdx = fs.readFileSync(path.join(process.cwd(), slug));
const html = await serializeMDX(mdx);
return { file: slug, html };
})
),
};
}, {});
};

/**
* @param {string} locale The locale that this file should be translated to.
Expand Down Expand Up @@ -80,7 +92,9 @@ const uploadFile = (locale, batchUid, accessToken) => async (page) => {
console.log(`[*] Successfully uploaded ${page.file}.`);
await sendPageContext(page.file, accessToken);
} else {
console.error(`[!] Unable to upload ${page.file}. Code was ${code}. Response status: ${resp.status} -- ${resp.statusText}`);
console.error(
`[!] Unable to upload ${page.file}. Code was ${code}. Response status: ${resp.status} -- ${resp.statusText}`
);
}

return { code, locale, slug: page.file };
Expand Down Expand Up @@ -270,4 +284,13 @@ const main = async () => {
}
};

main();
/**
* This allows us to check if the script was invoked directly from the command line, i.e 'node validate_packs.js', or if it was imported.
* This would be true if this was used in one of our GitHub workflows, but false when imported for use in a test.
* See here: https://nodejs.org/docs/latest/api/modules.html#modules_accessing_the_main_module
*/
if (require.main === module) {
main();
}

module.exports = { main, getContent };

0 comments on commit 96e4fd0

Please sign in to comment.