Skip to content

Commit

Permalink
Fix ESLint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Oct 14, 2023
1 parent 4eca814 commit 806f8bf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
13 changes: 12 additions & 1 deletion lib/dependency-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
const wasm = require('elm-solve-deps-wasm');
const ProjectJsonFiles = require('./project-json-files');
const SyncGet = require('./sync-get');
const collator = new Intl.Collator('en', {numeric: true}); // for sorting SemVer strings
const collator = new Intl.Collator('en', {numeric: true}); // For sorting SemVer strings

/** @type {boolean} */
let wasmWasInitialized = false;
Expand Down Expand Up @@ -39,6 +39,7 @@ class OnlineVersionsCache {
this.map = onlineVersionsFromScratch(cachePath, remotePackagesUrl);
return;
}

try {
this.map = parseOnlineVersions(JSON.parse(cacheFile));
} catch (error) {
Expand All @@ -47,6 +48,7 @@ class OnlineVersionsCache {
);
}
}

this.updateWithRequestSince(cachePath, remotePackagesUrl);
}

Expand All @@ -68,12 +70,14 @@ class OnlineVersionsCache {
if (!syncGetWorker) {
return;
}

const newVersions = JSON.parse(syncGetWorker.get(remoteUrl));
if (newVersions.length === 0) {
// Reload from scratch since it means at least one package was deleted from the registry.
this.map = onlineVersionsFromScratch(cachePath, remotePackagesUrl);
return;
}

// Check that the last package in the list was already in cache
// since the list returned by the package server is sorted newest first.
const {pkg, version} = splitPkgVersion(newVersions.pop());
Expand All @@ -92,6 +96,7 @@ class OnlineVersionsCache {
versionsOfPkg.push(version);
}
}

// Save the updated onlineVersionsCache to disk.
const onlineVersions = Object.fromEntries(this.map.entries());
fs.writeFileSync(cachePath, JSON.stringify(onlineVersions));
Expand Down Expand Up @@ -138,12 +143,14 @@ class OnlineAvailableVersionLister {
if (memoVersions !== undefined) {
return memoVersions;
}

const offlineVersions = readVersionsInElmHomeAndSort(elmVersion, pkg);
const allVersionsSet = new Set(this.onlineCache.getVersions(pkg));
// Combine local and online versions.
for (const version of offlineVersions) {
allVersionsSet.add(version);
}

const allVersions = [...allVersionsSet].sort(flippedSemverCompare);
this.memoCache.set(pkg, allVersions);
return allVersions;
Expand Down Expand Up @@ -208,6 +215,7 @@ class DependencyProvider {
wasm.init();
wasmWasInitialized = true;
}

syncGetWorker = SyncGet.startWorker();
}

Expand Down Expand Up @@ -263,6 +271,7 @@ class DependencyProvider {
if (!syncGetWorker) {
return;
}

syncGetWorker.shutDown();
syncGetWorker = null;
}
Expand All @@ -286,6 +295,7 @@ function fetchElmJsonOnline(elmVersion) {
if (!syncGetWorker) {
return '';
}

const elmJson = syncGetWorker.get(remoteUrl);
const cachePath = cacheElmJsonPath(pkg, version);
const parentDir = path.dirname(cachePath);
Expand Down Expand Up @@ -328,6 +338,7 @@ function onlineVersionsFromScratch(cachePath, remotePackagesUrl) {
if (!syncGetWorker) {
return new Map();
}

const onlineVersionsJson = syncGetWorker.get(remotePackagesUrl);
fs.writeFileSync(cachePath, onlineVersionsJson);
const onlineVersions = JSON.parse(onlineVersionsJson);
Expand Down
1 change: 1 addition & 0 deletions lib/project-json-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function getElmJsonFromElmHome(elmVersion, name, packageVersion) {
if (promise) {
return promise;
}

const elmJsonPath = getElmJsonFromElmHomePath(
elmVersion,
name,
Expand Down
1 change: 1 addition & 0 deletions lib/remote-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async function getRemoteElmJson(
// Major version for which the configuration exists is not compatible
MinVersion.validate(options, reviewElmJsonPath, packageVersion);
}

elmJson.dependencies.direct['jfmengels/elm-review'] =
MinVersion.updateToAtLeastMinimalVersion(packageVersion);
}
Expand Down
11 changes: 6 additions & 5 deletions lib/sync-get-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if (parentPort) {
} catch (error) {
requestPort.postMessage({error});
}

Atomics.notify(sharedLockArray, 0, Infinity);
});
}
Expand All @@ -21,18 +22,18 @@ if (parentPort) {
* @return {Promise<string>}
*/
async function getBody(url) {
return new Promise(function (resolve, reject) {
return new Promise((resolve, reject) => {
https
.get(url, function (res) {
.get(url, (res) => {
let body = '';
res.on('data', function (chunk) {
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', function () {
res.on('end', () => {
resolve(body);
});
})
.on('error', function (err) {
.on('error', (err) => {
reject(err);
});
});
Expand Down
5 changes: 4 additions & 1 deletion lib/sync-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ function startWorker() {
});
function get(url) {
worker.postMessage(url);
Atomics.wait(sharedLockArray, 0, 0); // blocks until notified at index 0.
Atomics.wait(sharedLockArray, 0, 0); // Blocks until notified at index 0.
const response = receiveMessageOnPort(localPort);
if (!response || !response.message) {
return '';
}

if (response.message.error) {
throw response.message.error;
} else {
return response.message;
}
}

function shutDown() {
localPort.close();
worker.terminate();
}

return {get, shutDown};
}

Expand Down

0 comments on commit 806f8bf

Please sign in to comment.