Skip to content

Commit

Permalink
CLI: Added timeout for checkUpdate middleware (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
aumkar authored Feb 9, 2025
1 parent cdcc1e4 commit 751b56c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 41 deletions.
16 changes: 13 additions & 3 deletions packages/cli/src/cli/commands/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { exit } from "process";
import nunjucks from "nunjucks";
import ora from "ora";
import { getApiConfig } from "../../lib/apiConfig";
Expand All @@ -10,15 +11,24 @@ import { errorHandler } from "../exceptions";

nunjucks.configure({ autoescape: true });

export const setTargetEnvironment = errorHandler<"production" | "staging">(
async (target: "production" | "staging") => {
export const setTargetEnvironment = errorHandler<string>(
async (target: string) => {
return new Promise<void>(
// eslint-disable-next-line no-async-promise-executor -- Handling promise rejection in the executor
async (resolve, reject) => {
const spinner = ora("Updating config file").start();

// Validate target value
if (!["production", "staging"].includes(target)) {
spinner.fail(
"The provided value should either be 'production' or `staging'",
);
exit(1);
}

try {
await persistConfigDetails({
targetEnvironment: target,
targetEnvironment: target as "production" | "staging",
});

spinner.succeed(`Successfully updated config file`);
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ yargs(hideBin(process.argv))
"Set the target environment.",
(yargs) => {
yargs.positional("<target>", {
describe: "Target environment: either 'production' or 'staging'.",
describe: "Target environment.",
demandOption: true,
choices: ["production", "staging"],
type: "string",
});
},
Expand Down
90 changes: 53 additions & 37 deletions packages/cli/src/lib/checkUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,60 @@ export function getPackageDetails() {
return { name, version };
}

const checkUpdate = async () => {
const { name, version } = getPackageDetails();
const { versions: allPublishedVersions } = await pkgJson(name, {
allVersions: true,
});
// Max time limit for checkUpdate execution
const CHECK_UPDATE_TIMEOUT_MS = 1000;

const checkUpdate = async (): Promise<void> => {
return new Promise((resolve) => {
let isResolved = false;
const timer = setTimeout(() => {
isResolved = true;
resolve();
}, CHECK_UPDATE_TIMEOUT_MS);

(async () => {
const { name, version } = getPackageDetails();
const { versions: allPublishedVersions } = await pkgJson(name, {
allVersions: true,
});
if (isResolved) return;

const versionKeys = Object.keys(allPublishedVersions);
let latestVersion = versionKeys.pop();

const versionKeys = Object.keys(allPublishedVersions);
let latestVersion = versionKeys.pop();

while (latestVersion != null) {
if (
!semver.prerelease(latestVersion) &&
semver.gt(latestVersion, version)
) {
break;
}

latestVersion = versionKeys.pop();
}

const updateAvailable =
latestVersion != null && semver.lt(version, latestVersion as string);
if (updateAvailable) {
const msg = {
updateAvailable: `Update available! ${chalk.dim(version)}${chalk.green(
latestVersion,
)}.`,
runUpdate: `Run ${chalk.cyan(`npm i -g ${name}`)} to update.`,
};

console.log(
boxen(`${msg.updateAvailable}\n${msg.runUpdate}`, {
margin: 1,
padding: 1,
align: "center",
}),
);
}
while (latestVersion != null) {
if (
!semver.prerelease(latestVersion) &&
semver.gt(latestVersion, version)
) {
break;
}

latestVersion = versionKeys.pop();
}

const updateAvailable =
latestVersion != null && semver.lt(version, latestVersion as string);
if (updateAvailable) {
const msg = {
updateAvailable: `Update available! ${chalk.dim(version)}${chalk.green(
latestVersion,
)}.`,
runUpdate: `Run ${chalk.cyan(`npm i -g ${name}`)} to update.`,
};

console.log(
boxen(`${msg.updateAvailable}\n${msg.runUpdate}`, {
margin: 1,
padding: 1,
align: "center",
}),
);
}
clearTimeout(timer);
resolve();
})();
});
};

export default checkUpdate;

0 comments on commit 751b56c

Please sign in to comment.