Skip to content

Commit

Permalink
More robust clean up after unexpected docgen process exits. (#8260)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 committed May 22, 2024
1 parent 3883133 commit aa060a7
Showing 1 changed file with 52 additions and 13 deletions.
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

0 comments on commit aa060a7

Please sign in to comment.