Skip to content

Commit 6f34a8d

Browse files
author
Guy Kedem
committed
accessing runtime specific functions through JsRuntime"
1 parent c8d305a commit 6f34a8d

File tree

3 files changed

+24
-53
lines changed

3 files changed

+24
-53
lines changed

packages/cli/src/commands/dev.ts

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import childProcess from "child_process";
33
import chokidar from "chokidar";
44
import fs from "fs/promises";
55
import ngrok from "ngrok";
6-
import { run as ncuRun } from "npm-check-updates";
6+
import run, { run as ncuRun } from "npm-check-updates";
77
import ora, { Ora } from "ora";
88
import pathModule from "path";
99
import util from "util";
@@ -19,6 +19,7 @@ import { resolvePath } from "../utils/parseNameAndPath";
1919
import { RequireKeys } from "../utils/requiredKeys";
2020
import { TriggerApi } from "../utils/triggerApi";
2121
import { standardWatchIgnoreRegex, standardWatchFilePaths } from "../frameworks/watchConfig";
22+
import { JsRuntime, getJsRuntime } from "../utils/jsRuntime";
2223

2324
const asyncExecFile = util.promisify(childProcess.execFile);
2425

@@ -41,6 +42,7 @@ const formattedDate = new Intl.DateTimeFormat("en", {
4142
second: "numeric",
4243
});
4344

45+
let runtime: JsRuntime;
4446
export async function devCommand(path: string, anyOptions: any) {
4547
telemetryClient.dev.started(path, anyOptions);
4648

@@ -53,12 +55,12 @@ export async function devCommand(path: string, anyOptions: any) {
5355
const options = result.data;
5456

5557
const resolvedPath = resolvePath(path);
56-
58+
runtime = await getJsRuntime(resolvedPath, logger);
5759
//check for outdated packages, don't await this
58-
checkForOutdatedPackages(resolvedPath);
60+
runtime.checkForOutdatedPackages();
5961

6062
// Read from package.json to get the endpointId
61-
const endpointId = await getEndpointIdFromPackageJson(resolvedPath, options);
63+
const endpointId = await getEndpointId(runtime, options);
6264
if (!endpointId) {
6365
logger.error(
6466
"You must run the `init` command first to setup the project – you are missing \n'trigger.dev': { 'endpointId': 'your-client-id' } from your package.json file, or pass in the --client-id option to this command"
@@ -69,8 +71,8 @@ export async function devCommand(path: string, anyOptions: any) {
6971
logger.success(`✔️ [trigger.dev] Detected TriggerClient id: ${endpointId}`);
7072

7173
//resolve the options using the detected framework (use default if there isn't a matching framework)
72-
const packageManager = await getUserPackageManager(resolvedPath);
73-
const framework = await getFramework(resolvedPath, packageManager);
74+
const packageManager = await runtime.getUserPackageManager();
75+
const framework = await runtime.getFramework();
7476
const resolvedOptions = await resolveOptions(framework, resolvedPath, options);
7577

7678
// Read from .env.local or .env to get the TRIGGER_API_KEY and TRIGGER_API_URL
@@ -115,7 +117,7 @@ export async function devCommand(path: string, anyOptions: any) {
115117
const refresh = async () => {
116118
connectingSpinner.start();
117119

118-
const refreshedEndpointId = await getEndpointIdFromPackageJson(resolvedPath, resolvedOptions);
120+
const refreshedEndpointId = await getEndpointId(runtime, resolvedOptions);
119121

120122
// Read from env file to get the TRIGGER_API_KEY and TRIGGER_API_URL
121123
const apiDetails = await getTriggerApiDetails(resolvedPath, envFile);
@@ -304,43 +306,10 @@ async function verifyEndpoint(
304306
return;
305307
}
306308

307-
export async function checkForOutdatedPackages(path: string) {
308-
const updates = (await ncuRun({
309-
packageFile: `${path}/package.json`,
310-
filter: "/trigger.dev/.+$/",
311-
upgrade: false,
312-
})) as {
313-
[key: string]: string;
314-
};
315-
316-
if (typeof updates === "undefined" || Object.keys(updates).length === 0) {
317-
return;
318-
}
319-
320-
const packageFile = await fs.readFile(`${path}/package.json`);
321-
const data = JSON.parse(Buffer.from(packageFile).toString("utf8"));
322-
const dependencies = data.dependencies;
323-
console.log(chalk.bgYellow("Updates available for trigger.dev packages"));
324-
console.log(chalk.bgBlue("Run npx @trigger.dev/cli@latest update"));
325-
326-
for (let dep in updates) {
327-
console.log(`${dep} ${dependencies[dep]}${updates[dep]}`);
328-
}
329-
}
330-
331-
export async function getEndpointIdFromPackageJson(path: string, options: DevCommandOptions) {
309+
export function getEndpointId(runtime: JsRuntime, options: DevCommandOptions) {
332310
if (options.clientId) {
333311
return options.clientId;
334-
}
335-
336-
const pkgJsonPath = pathModule.join(path, "package.json");
337-
const pkgBuffer = await fs.readFile(pkgJsonPath);
338-
const pkgJson = JSON.parse(pkgBuffer.toString());
339-
340-
const value = pkgJson["trigger.dev"]?.endpointId;
341-
if (!value || typeof value !== "string") return;
342-
343-
return value as string;
312+
} else return runtime.getEndpointId();
344313
}
345314

346315
async function resolveEndpointUrl(apiUrl: string, port: number, hostname: string) {

packages/cli/src/commands/whoami.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { z } from "zod";
22
import { logger } from "../utils/logger";
33
import { resolvePath } from "../utils/parseNameAndPath";
44
import { TriggerApi } from "../utils/triggerApi";
5-
import { DevCommandOptions, getEndpointIdFromPackageJson } from "./dev";
5+
import { DevCommandOptions, getEndpointId } from "./dev";
66
import ora from "ora";
77
import { getTriggerApiDetails } from "../utils/getTriggerApiDetails";
88

@@ -26,7 +26,7 @@ export async function whoamiCommand(path: string, anyOptions: any) {
2626
const resolvedPath = resolvePath(path);
2727

2828
// Read from package.json to get the endpointId
29-
const endpointId = await getEndpointIdFromPackageJson(resolvedPath, options as DevCommandOptions);
29+
const endpointId = await getEndpointId(resolvedPath, options as DevCommandOptions);
3030
if (!endpointId) {
3131
logger.error(
3232
"You must run the `init` command first to setup the project – you are missing \n'trigger.dev': { 'endpointId': 'your-client-id' } from your package.json file, or pass in the --client-id option to this command"

packages/cli/src/utils/jsRuntime.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import pathModule from "path";
88

99
type SupportedRuntimeId = "node" | "deno";
1010

11-
abstract class JsRuntime {
11+
export abstract class JsRuntime {
1212
logger: Logger;
1313
projectRootPath: string;
1414
constructor(projectRootPath: string, logger: Logger) {
@@ -19,14 +19,15 @@ abstract class JsRuntime {
1919
abstract getUserPackageManager(): Promise<PackageManager | undefined>;
2020
abstract getFramework(): Promise<Framework | undefined>;
2121
abstract getEndpointId(): Promise<string | undefined>;
22-
static async getJsRuntime(projectRootPath: string, logger: Logger) {
23-
if (await NodeJsRuntime.isNodeJsRuntime(projectRootPath)) {
24-
return new NodeJsRuntime(projectRootPath, logger);
25-
} else if (await DenoRuntime.isDenoJsRuntime(projectRootPath)) {
26-
return new DenoRuntime(projectRootPath, logger);
27-
}
28-
throw new Error("Unsupported runtime");
22+
}
23+
24+
export async function getJsRuntime(projectRootPath: string, logger: Logger): Promise<JsRuntime> {
25+
if (await NodeJsRuntime.isNodeJsRuntime(projectRootPath)) {
26+
return new NodeJsRuntime(projectRootPath, logger);
27+
} else if (await DenoRuntime.isDenoJsRuntime(projectRootPath)) {
28+
return new DenoRuntime(projectRootPath, logger);
2929
}
30+
throw new Error("Unsupported runtime");
3031
}
3132

3233
class NodeJsRuntime extends JsRuntime {
@@ -79,7 +80,8 @@ class NodeJsRuntime extends JsRuntime {
7980
const pkgJsonPath = pathModule.join(this.projectRootPath, "package.json");
8081
const pkgBuffer = await fs.readFile(pkgJsonPath);
8182
const pkgJson = JSON.parse(pkgBuffer.toString());
82-
return pkgJson["trigger.dev"]?.endpointId;
83+
const value = pkgJson["trigger.dev"]?.endpointId;
84+
if (!value || typeof value !== "string") return undefined;
8385
}
8486
}
8587

0 commit comments

Comments
 (0)