Skip to content

Commit

Permalink
Flatten functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Oct 15, 2023
1 parent bec26e9 commit 2788d1a
Showing 1 changed file with 56 additions and 59 deletions.
115 changes: 56 additions & 59 deletions lib/dependency-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class DependencyProvider {
elmJson,
false,
extra,
fetchElmJsonOffline(options, this.elmVersion),
(pkg, version) =>
fetchElmJsonOffline(options, this.elmVersion, pkg, version),
(/** @type {string} */ pkg) => lister.list(this.elmVersion, pkg)
);
} catch (errorMessage) {
Expand All @@ -74,7 +75,8 @@ class DependencyProvider {
elmJson,
false,
extra,
fetchElmJsonOnline(options, this.elmVersion),
(pkg, version) =>
fetchElmJsonOnline(options, this.elmVersion, pkg, version),
(pkg) => lister.list(this.elmVersion, pkg)
);
} catch (errorMessage) {
Expand Down Expand Up @@ -286,71 +288,66 @@ function readVersionsInElmHomeAndSort(elmVersion, pkg) {
*
* @param {Options} options
* @param {string} elmVersion
* @returns {(pkg: string, version: string) => string}
* @param {string} pkg
* @param {string} version
* @returns {string}
*/
function fetchElmJsonOnline(options, elmVersion) {
return (pkg, version) => {
try {
return fetchElmJsonOffline(options, elmVersion)(pkg, version);
} catch (_) {
// `fetchElmJsonOffline` can only fail in ways that are either expected
// (such as file does not exist or no permissions)
// or because there was an error parsing `pkg` and `version`.
// In such case, this will throw again with `cacheElmJsonPath()` so it's fine.
const remoteUrl = remoteElmJsonUrl(pkg, version);
if (!syncGetWorker) {
return '';
}

const elmJson = syncGetWorker.get(remoteUrl);
const cachePath = ProjectJsonFiles.elmReviewDependencyCache(
options,
elmVersion,
pkg,
version,
'elm.json'
);
const parentDir = path.dirname(cachePath);
fs.mkdirSync(parentDir, {recursive: true});
fs.writeFileSync(cachePath, elmJson);
return elmJson;
function fetchElmJsonOnline(options, elmVersion, pkg, version) {
try {
return fetchElmJsonOffline(options, elmVersion, pkg, version);
} catch (_) {
// `fetchElmJsonOffline` can only fail in ways that are either expected
// (such as file does not exist or no permissions)
// or because there was an error parsing `pkg` and `version`.
// In such case, this will throw again with `cacheElmJsonPath()` so it's fine.
const remoteUrl = remoteElmJsonUrl(pkg, version);
if (!syncGetWorker) {
return '';
}
};

const elmJson = syncGetWorker.get(remoteUrl);
const cachePath = ProjectJsonFiles.elmReviewDependencyCache(
options,
elmVersion,
pkg,
version,
'elm.json'
);
const parentDir = path.dirname(cachePath);
fs.mkdirSync(parentDir, {recursive: true});
fs.writeFileSync(cachePath, elmJson);
return elmJson;
}
}

/**
* @param {Options} options
* @param {string} elmVersion
* @returns {(pkg: string, version: string) => string}
* @param {string} pkg
* @param {string} version
* @returns {string}
*/
function fetchElmJsonOffline(options, elmVersion) {
/**
* @param {string} pkg
* @param {string} version
* @returns {string}
*/
return (pkg, version) => {
try {
return fs.readFileSync(
ProjectJsonFiles.getElmJsonFromElmHomePath(elmVersion, pkg, version),
'utf8'
);
} catch (_) {
// The read can only fail if the elm.json file does not exist
// or if we don't have the permissions to read it so it's fine to catch all.
// Otherwise, it means that `homeElmJsonPath()` failed while processing `pkg` and `version`.
// In such case, again, it's fine to catch all since the next call to `cacheElmJsonPath()`
// will fail the same anyway.
const cachePath = ProjectJsonFiles.elmReviewDependencyCache(
options,
elmVersion,
pkg,
version,
'elm.json'
);
return fs.readFileSync(cachePath, 'utf8');
}
};
function fetchElmJsonOffline(options, elmVersion, pkg, version) {
try {
return fs.readFileSync(
ProjectJsonFiles.getElmJsonFromElmHomePath(elmVersion, pkg, version),
'utf8'
);
} catch (_) {
// The read can only fail if the elm.json file does not exist
// or if we don't have the permissions to read it so it's fine to catch all.
// Otherwise, it means that `homeElmJsonPath()` failed while processing `pkg` and `version`.
// In such case, again, it's fine to catch all since the next call to `cacheElmJsonPath()`
// will fail the same anyway.
const cachePath = ProjectJsonFiles.elmReviewDependencyCache(
options,
elmVersion,
pkg,
version,
'elm.json'
);
return fs.readFileSync(cachePath, 'utf8');
}
}

/** Reset the cache of existing versions from scratch with a request to the package server.
Expand Down

0 comments on commit 2788d1a

Please sign in to comment.