Skip to content

Commit

Permalink
fix(script): add common cache, add skipCache option (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Mar 1, 2022
1 parent 9e5250c commit 3b0b43b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
20 changes: 15 additions & 5 deletions scripts/buildSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ async function buildSpec(
): Promise<void> {
createSpinner(`'${client}' spec`, verbose).start().info();
const cacheFile = toAbsolutePath(`specs/dist/${client}.cache`);
let hash = '';

if (useCache) {
const spinner = createSpinner(
`checking cache for '${client}'`,
Expand All @@ -21,7 +23,11 @@ async function buildSpec(
// check if file and cache exist
if (await exists(toAbsolutePath(`specs/bundled/${client}.yml`))) {
// compare with stored cache
const hash = (await hashElement(toAbsolutePath(`specs/${client}`))).hash;
const specHash = (await hashElement(toAbsolutePath(`specs/${client}`)))
.hash;
const commonHash = (await hashElement(toAbsolutePath(`specs/common`)))
.hash;
hash = `${specHash}-${commonHash}`;
if (await exists(cacheFile)) {
const storedHash = (await fsp.readFile(cacheFile)).toString();
if (storedHash === hash) {
Expand All @@ -45,14 +51,18 @@ async function buildSpec(
{ verbose }
);

spinner.text = `validating '${client}' spec`;
spinner.text = `validating '${client}' bundled spec`;
await run(`yarn openapi lint specs/bundled/${client}.${outputFormat}`, {
verbose,
});

spinner.text = `storing ${client} spec cache`;
const hash = (await hashElement(toAbsolutePath(`specs/${client}`))).hash;
await fsp.writeFile(cacheFile, hash);
spinner.text = `linting '${client}' bundled spec`;
await run(`yarn specs:lint bundled/${client}.${outputFormat}`, { verbose });

if (hash) {
spinner.text = `storing ${client} spec cache`;
await fsp.writeFile(cacheFile, hash);
}

spinner.succeed(`building complete for '${client}' spec`);
}
Expand Down
10 changes: 8 additions & 2 deletions scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ buildCommand
)
.option('-v, --verbose', 'make the verification verbose')
.option('-i, --interactive', 'open prompt to query parameters')
.option('-s, --skip-cache', 'skip cache checking to force building specs')
.action(
async (
client: string | undefined,
outputFormat: 'json' | 'yml' | undefined,
{ verbose, interactive }
{ verbose, interactive, skipCache }
) => {
client = await promptClient(client, interactive);

Expand All @@ -186,7 +187,12 @@ buildCommand
clientsTodo = CLIENTS;
}
// ignore cache when building from cli
await buildSpecs(clientsTodo, outputFormat!, Boolean(verbose), false);
await buildSpecs(
clientsTodo,
outputFormat!,
Boolean(verbose),
!skipCache
);
}
);

Expand Down

0 comments on commit 3b0b43b

Please sign in to comment.