Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
update sha sorting algorithm to coerce to semver sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
baparham committed Mar 28, 2023
1 parent fbac6b2 commit 8015eb2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/generate-expected-shas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { log } from './log';
import { version } from '../package.json';
import { nodeBinarySortFunction } from './utils';

async function main() {
const { argv } = yargs
Expand All @@ -15,6 +16,8 @@ async function main() {
const shaFileContent = readFileSync(argv.input).toString();
const shaMap: { [id: string]: string } = {};
for (const line of shaFileContent.split('\n')) {
// Expect line to be of the form
// <sha> node-v<version>-host-arch
const lineParts = line.split(/\s+/);
if (lineParts.length === 2) {
// eslint-disable-next-line prefer-destructuring
Expand All @@ -24,14 +27,12 @@ async function main() {

// Sort map
const sortedShaMap: { [id: string]: string } = {};
for (const nodeVersion of Object.keys(shaMap).sort()) {
for (const nodeVersion of Object.keys(shaMap).sort(nodeBinarySortFunction)) {
sortedShaMap[nodeVersion] = shaMap[nodeVersion];
}

writeFileSync(join(__dirname, '../lib/expected-shas.json'),
`${JSON.stringify(sortedShaMap, null, 2)}\n`);
// console.log(JSON.stringify(sortedShaMap, null, 2));

}

main().catch((error) => {
Expand Down
5 changes: 3 additions & 2 deletions lib/print-hashes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { EXPECTED_HASHES } from './expected';
import { nodeBinarySortFunction } from './utils';

/* eslint-disable no-console */

function main() {
for (const nodeVersion of Object.keys(EXPECTED_HASHES).sort()) {
console.log(`${nodeVersion} ${EXPECTED_HASHES[nodeVersion]}`);
for (const nodeVersion of Object.keys(EXPECTED_HASHES).sort(nodeBinarySortFunction)) {
console.log(`${EXPECTED_HASHES[nodeVersion]} ${nodeVersion}`);
}
}

Expand Down
10 changes: 10 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import path from 'path';
import { spawnSync, SpawnSyncOptions } from 'child_process';
import stream from 'stream';

import { coerce } from 'semver';
import { log, wasReported } from './log';

export async function downloadUrl(url: string, file: string): Promise<void> {
Expand Down Expand Up @@ -97,3 +98,12 @@ export async function spawn(
throw error;
}
}

export function nodeBinarySortFunction(a: string, b: string): number {
const coercedVersionA = coerce(a);
const coercedVersionB = coerce(b);
if (coercedVersionA && coercedVersionB) {
return coercedVersionA.compare(coercedVersionB);
}
return 0;
}

0 comments on commit 8015eb2

Please sign in to comment.