Skip to content

More robust clean up after unexpected docgen process exits. #8260

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

Merged
merged 2 commits into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 52 additions & 13 deletions scripts/docgen/docgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,50 @@ yargs
.demandCommand()
.help().argv;

process.on('exit', cleanup);
process.on('SIGINT', cleanup);

let authApiReportOriginal: string;
let authApiConfigOriginal: string;

function cleanup() {
try {
// Restore original auth api-extractor.json contents.
if (authApiReportOriginal) {
console.log(`Restoring original auth api-extractor.json contents.`);
fs.writeFileSync(
`${projectRoot}/packages/auth/api-extractor.json`,
authApiConfigOriginal
);
}
// Restore original auth.api.md
if (authApiConfigOriginal) {
console.log(`Restoring original auth.api.md contents.`);
fs.writeFileSync(
`${projectRoot}/common/api-review/auth.api.md`,
authApiReportOriginal
);
}
for (const excludedPackage of EXCLUDED_PACKAGES) {
if (fs.existsSync(`${projectRoot}/temp/${excludedPackage}.skip`)) {
console.log(
`Restoring json files for excluded package: ${excludedPackage}.`
);
fs.renameSync(
`${projectRoot}/temp/${excludedPackage}.skip`,
`${projectRoot}/temp/${excludedPackage}.api.json`
);
}
}
} catch (e) {
console.error(
'Error cleaning up files on exit - ' +
'check for temp modifications to md and json files.'
);
console.error(e);
}
}

async function generateToc() {
console.log(`Temporarily renaming excluded packages' json files.`);
for (const excludedPackage of EXCLUDED_PACKAGES) {
Expand All @@ -115,15 +159,7 @@ async function generateToc() {
{ stdio: 'inherit' }
);
} finally {
console.log(`Restoring excluded packages' json files.`);
for (const excludedPackage of EXCLUDED_PACKAGES) {
if (fs.existsSync(`${projectRoot}/temp/${excludedPackage}.skip`)) {
fs.renameSync(
`${projectRoot}/temp/${excludedPackage}.skip`,
`${projectRoot}/temp/${excludedPackage}.api.json`
);
}
}
cleanup();
}
}

Expand All @@ -137,12 +173,12 @@ async function generateDocs(

console.log(`Temporarily modifying auth api-extractor.json for docgen.`);
// Use a special d.ts file for auth for doc gen only.
const authApiConfigOriginal = fs.readFileSync(
authApiConfigOriginal = fs.readFileSync(
`${projectRoot}/packages/auth/api-extractor.json`,
'utf8'
);
// Save original auth.md as well.
const authApiReportOriginal = fs.readFileSync(
authApiReportOriginal = fs.readFileSync(
`${projectRoot}/common/api-review/auth.api.md`,
'utf8'
);
Expand Down Expand Up @@ -193,10 +229,13 @@ async function generateDocs(
);
}

if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir);
if (fs.existsSync(tmpDir)) {
console.log(`Removing old json temp dir: ${tmpDir}.`);
fs.rmSync(tmpDir, { recursive: true, force: true });
}

fs.mkdirSync(tmpDir);

// TODO: Throw error if path doesn't exist once all packages add markdown support.
const apiJsonDirectories = (
await mapWorkspaceToPackages([`${projectRoot}/packages/*`])
Expand Down
Loading