diff --git a/lib/dependency-provider.js b/lib/dependency-provider.js index 8fbc7008e..cfbe19f4d 100644 --- a/lib/dependency-provider.js +++ b/lib/dependency-provider.js @@ -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; @@ -39,6 +39,7 @@ class OnlineVersionsCache { this.map = onlineVersionsFromScratch(cachePath, remotePackagesUrl); return; } + try { this.map = parseOnlineVersions(JSON.parse(cacheFile)); } catch (error) { @@ -47,6 +48,7 @@ class OnlineVersionsCache { ); } } + this.updateWithRequestSince(cachePath, remotePackagesUrl); } @@ -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()); @@ -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)); @@ -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; @@ -208,6 +215,7 @@ class DependencyProvider { wasm.init(); wasmWasInitialized = true; } + syncGetWorker = SyncGet.startWorker(); } @@ -263,6 +271,7 @@ class DependencyProvider { if (!syncGetWorker) { return; } + syncGetWorker.shutDown(); syncGetWorker = null; } @@ -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); @@ -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); diff --git a/lib/project-json-files.js b/lib/project-json-files.js index 4978109bf..68aee0bc0 100644 --- a/lib/project-json-files.js +++ b/lib/project-json-files.js @@ -62,6 +62,7 @@ function getElmJsonFromElmHome(elmVersion, name, packageVersion) { if (promise) { return promise; } + const elmJsonPath = getElmJsonFromElmHomePath( elmVersion, name, diff --git a/lib/remote-template.js b/lib/remote-template.js index 2583cc2e6..25c8aa722 100644 --- a/lib/remote-template.js +++ b/lib/remote-template.js @@ -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); } diff --git a/lib/sync-get-worker.js b/lib/sync-get-worker.js index b392d5b9f..0cb137797 100644 --- a/lib/sync-get-worker.js +++ b/lib/sync-get-worker.js @@ -12,6 +12,7 @@ if (parentPort) { } catch (error) { requestPort.postMessage({error}); } + Atomics.notify(sharedLockArray, 0, Infinity); }); } @@ -21,18 +22,18 @@ if (parentPort) { * @return {Promise} */ 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); }); }); diff --git a/lib/sync-get.js b/lib/sync-get.js index 210587647..506b0a5e8 100644 --- a/lib/sync-get.js +++ b/lib/sync-get.js @@ -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}; }