docusaurus/website/waitForCrowdin.js
Paul O’Shannessy 8341527fc9
refactor: rename Git master branch to main (#5345)
* Prepare to renaming master branch to main

* Update CONTRIBUTING.md

* Update packages/docusaurus-migrate/src/__tests__/__fixtures__/missing_version_website/website/package.json

* Update packages/docusaurus-migrate/src/__tests__/__fixtures__/complex_website/website/package.json

* Update packages/docusaurus-migrate/src/__tests__/__fixtures__/simple_website/website/package.json

* missing master -> main replaces

* useless char

* For GH Pages org deploy, DEPLOYMENT_BRANCH env is now required

* fix versioning page

* fix vercel links

* update deployment.mdx

Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
Co-authored-by: slorber <lorber.sebastien@gmail.com>
2021-08-17 18:41:53 +02:00

70 lines
1.8 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const {Translations} = require('@crowdin/crowdin-api-client');
/*
Crowdin does not support concurrent "project builds" (downloads of translations).
The Crowdin CLI fails with error 409, and it leads to failures on Netlify.
On Docusaurus, when we commit on main, we have 2 Netlify deployments triggered:
- prod
- i18n-staging (work-in-progress locales)
This script helps the 2 deployments to not download translations concurrently from Crowdin.
*/
const pollInterval = 5000;
const timeout = 5 * 60 * 1000;
const projectId = 428890;
const token = process.env.CROWDIN_PERSONAL_TOKEN; // set on Netlify
const translations = new Translations({
token,
});
async function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function hasBuildInProgress() {
const projectBuilds = await translations.listProjectBuilds(projectId);
return projectBuilds.data.some((build) => build.data.status === 'inProgress');
}
async function run() {
const timeBefore = Date.now();
while (true) {
if (Date.now() - timeBefore > timeout) {
console.warn(
'[Crowdin] Timeout of wait script reached => will try to proceed but download translations is likely to fail...',
);
break;
}
const inProgress = await hasBuildInProgress();
if (inProgress) {
console.log('[Crowdin] A build is still in progress => waiting...');
await delay(pollInterval);
} else {
console.warn('[Crowdin] No build in progress => lets continue');
break;
}
}
}
run().then(
() => {
process.exit(0);
},
(e) => {
console.error(e.message);
console.error(e.stack);
process.exit(1);
},
);