Skip to content

Commit 464e7a5

Browse files
authored
Ensure docgen script restores original files even if build fails (#7151)
1 parent 3435ba9 commit 464e7a5

File tree

1 file changed

+62
-54
lines changed

1 file changed

+62
-54
lines changed

scripts/docgen/docgen.ts

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,29 @@ async function generateToc() {
9999
);
100100
}
101101
}
102-
await spawn(
103-
'yarn',
104-
[
105-
'api-documenter-devsite',
106-
'toc',
107-
'--input',
108-
'temp',
109-
'-p',
110-
'/docs/reference/js',
111-
'-j'
112-
],
113-
{ stdio: 'inherit' }
114-
);
115-
console.log(`Restoring excluded packages' json files.`);
116-
for (const excludedPackage of EXCLUDED_PACKAGES) {
117-
if (fs.existsSync(`${projectRoot}/temp/${excludedPackage}.skip`)) {
118-
fs.renameSync(
119-
`${projectRoot}/temp/${excludedPackage}.skip`,
120-
`${projectRoot}/temp/${excludedPackage}.api.json`
121-
);
102+
try {
103+
await spawn(
104+
'yarn',
105+
[
106+
'api-documenter-devsite',
107+
'toc',
108+
'--input',
109+
'temp',
110+
'-p',
111+
'/docs/reference/js',
112+
'-j'
113+
],
114+
{ stdio: 'inherit' }
115+
);
116+
} finally {
117+
console.log(`Restoring excluded packages' json files.`);
118+
for (const excludedPackage of EXCLUDED_PACKAGES) {
119+
if (fs.existsSync(`${projectRoot}/temp/${excludedPackage}.skip`)) {
120+
fs.renameSync(
121+
`${projectRoot}/temp/${excludedPackage}.skip`,
122+
`${projectRoot}/temp/${excludedPackage}.api.json`
123+
);
124+
}
122125
}
123126
}
124127
}
@@ -131,6 +134,7 @@ async function generateDocs(
131134
const outputFolder = forDevsite ? 'docs-devsite' : 'docs';
132135
const command = forDevsite ? 'api-documenter-devsite' : 'api-documenter';
133136

137+
console.log(`Temporarily modifying auth api-extractor.json for docgen.`);
134138
// Use a special d.ts file for auth for doc gen only.
135139
const authApiConfigOriginal = fs.readFileSync(
136140
`${projectRoot}/packages/auth/api-extractor.json`,
@@ -145,45 +149,49 @@ async function generateDocs(
145149
`"mainEntryPointFilePath": "<projectFolder>/dist/esm5/index.d.ts"`,
146150
`"mainEntryPointFilePath": "<projectFolder>/dist/esm5/index.doc.d.ts"`
147151
);
148-
fs.writeFileSync(
149-
`${projectRoot}/packages/auth/api-extractor.json`,
150-
authApiConfigModified
151-
);
152152

153-
if (skipBuild) {
154-
await spawn('yarn', ['api-report'], {
155-
stdio: 'inherit'
156-
});
157-
} else {
158-
// api-report is run as part of every build
159-
await spawn(
160-
'yarn',
161-
[
162-
'lerna',
163-
'run',
164-
'--scope',
165-
'@firebase/*',
166-
'--ignore',
167-
'@firebase/*-compat',
168-
'build'
169-
],
170-
{
153+
try {
154+
fs.writeFileSync(
155+
`${projectRoot}/packages/auth/api-extractor.json`,
156+
authApiConfigModified
157+
);
158+
159+
if (skipBuild) {
160+
await spawn('yarn', ['api-report'], {
171161
stdio: 'inherit'
172-
}
162+
});
163+
} else {
164+
// api-report is run as part of every build
165+
await spawn(
166+
'yarn',
167+
[
168+
'lerna',
169+
'run',
170+
'--scope',
171+
'@firebase/*',
172+
'--ignore',
173+
'@firebase/*-compat',
174+
'build'
175+
],
176+
{
177+
stdio: 'inherit'
178+
}
179+
);
180+
}
181+
} finally {
182+
console.log(`Restoring original auth api-extractor.json contents.`);
183+
// Restore original auth api-extractor.json contents.
184+
fs.writeFileSync(
185+
`${projectRoot}/packages/auth/api-extractor.json`,
186+
authApiConfigOriginal
187+
);
188+
// Restore original auth.api.md
189+
fs.writeFileSync(
190+
`${projectRoot}/common/api-review/auth.api.md`,
191+
authApiReportOriginal
173192
);
174193
}
175194

176-
// Restore original auth api-extractor.json contents.
177-
fs.writeFileSync(
178-
`${projectRoot}/packages/auth/api-extractor.json`,
179-
authApiConfigOriginal
180-
);
181-
// Restore original auth.api.md
182-
fs.writeFileSync(
183-
`${projectRoot}/common/api-review/auth.api.md`,
184-
authApiReportOriginal
185-
);
186-
187195
if (!fs.existsSync(tmpDir)) {
188196
fs.mkdirSync(tmpDir);
189197
}

0 commit comments

Comments
 (0)