Skip to content

Commit

Permalink
perf(scripts/diff-features): fetch enumerations async
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Nov 6, 2024
1 parent ab82d7e commit edb9ff9
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions scripts/diff-features.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */

import { execSync } from 'node:child_process';
import { exec, execSync } from 'node:child_process';
import fs from 'node:fs';
import { promisify } from 'node:util';
import path from 'node:path';

import esMain from 'es-main';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { temporaryDirectoryTask } from 'tempy';

/**
* Executes a command asynchronously.
* @param command The command to execute asynchronously.
* @returns The output of the command.
*/
const execAsync = async (command: string): Promise<string> => {
const result = await promisify(exec)(command, { encoding: 'utf-8' });

return result.stdout.trim();
};

/**
* Compare two references and print diff as Markdown or JSON
Expand Down Expand Up @@ -87,7 +101,7 @@ const enumerate = async (
): Promise<Set<string>> => {
if (!skipGithub) {
try {
return new Set(getEnumerationFromGithub(ref));
return new Set(await getEnumerationFromGithub(ref));
} catch (e) {
if (!quiet) {
console.error(
Expand All @@ -105,50 +119,28 @@ const enumerate = async (
* @param ref Reference to obtain features for
* @returns Feature list from reference
*/
const getEnumerationFromGithub = (ref: string): string[] => {
const getEnumerationFromGithub = async (ref: string): Promise<string[]> => {
const ENUMERATE_WORKFLOW = '15595228';
const ENUMERATE_WORKFLOW_ARTIFACT = 'enumerate-features';
const ENUMERATE_WORKFLOW_FILE = 'features.json';

/**
* Unlinks the workflow file
*/
const unlinkFile = () => {
try {
fs.unlinkSync(ENUMERATE_WORKFLOW_FILE);
} catch (err: any) {
if (err.code == 'ENOENT') {
return;
}
throw err;
}
};

const hash = execSync(`git rev-parse ${ref}`, {
encoding: 'utf-8',
}).trim();
const workflowRun = execSync(
const hash = await execAsync(`git rev-parse ${ref}`);
const workflowRun = await execAsync(
`gh api /repos/:owner/:repo/actions/workflows/${ENUMERATE_WORKFLOW}/runs\\?branch=main\\&head_sha=${hash}\\&per_page=1 --jq '[.workflow_runs[] | select(.head_sha=="${hash}") | .id] | first'`,
{
encoding: 'utf-8',
},
).trim();
);

if (!workflowRun) {
throw Error('No workflow run found for commit.');
}

try {
unlinkFile();
execSync(
`gh run download ${workflowRun} -n ${ENUMERATE_WORKFLOW_ARTIFACT}`,
);
return JSON.parse(
fs.readFileSync(ENUMERATE_WORKFLOW_FILE, { encoding: 'utf-8' }),
return await temporaryDirectoryTask(async (tempdir) => {
await execAsync(
`gh run download ${workflowRun} -n ${ENUMERATE_WORKFLOW_ARTIFACT} --dir ${tempdir}`,
);
} finally {
unlinkFile();
}
const file = path.join(tempdir, ENUMERATE_WORKFLOW_FILE);

return JSON.parse(fs.readFileSync(file, { encoding: 'utf-8' }));
});
};

/**
Expand Down

0 comments on commit edb9ff9

Please sign in to comment.