Skip to content

v3: cli and misc fixes for windows #1027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a4ce144
fix: otlp-importer script
Kritik-J Feb 29, 2024
5122a83
update pluginPath
Kritik-J Mar 1, 2024
e20c6eb
Refactor script to ensure compatibility with both Windows and Unix pl…
Kritik-J Mar 1, 2024
4a872ec
Revert command changes
Kritik-J Mar 2, 2024
0617775
Fix: Init command was failing on Windows because of bad template paths
matt-aitken Apr 9, 2024
cfcb1c5
Removed resolveInternalFilePath
matt-aitken Apr 9, 2024
7c2b0df
Path fixes for the dev command
matt-aitken Apr 9, 2024
5f59246
Fix for the import paths being wrong on Windows because the contain b…
matt-aitken Apr 9, 2024
8a780dc
The metaOutputKey should have forward slashes in it
matt-aitken Apr 9, 2024
ae5ffb9
Print the banner immediately, otherwise with bad internet you get a b…
matt-aitken Apr 9, 2024
af9f26f
Try normalizing the trigger.config import path
matt-aitken Apr 10, 2024
b7f4ef5
Allow tsx and jsx files
matt-aitken Apr 10, 2024
ae45b97
Improved task file names and paths
matt-aitken Apr 10, 2024
e122f4a
Removed closing bracket for the upgrade message
matt-aitken Apr 10, 2024
5c0a3bc
Log out the metafile outputs for debugging
matt-aitken Apr 10, 2024
4c85ef5
Log out the entryPointContents before esbuild
matt-aitken Apr 10, 2024
b365afb
Log the metafile out
matt-aitken Apr 10, 2024
553ce05
Merge branch 'main' into fixes/windows-init
nicktrn Apr 12, 2024
60482de
Merge branch 'pr/920' into fixes/windows-init
nicktrn Apr 12, 2024
38844ea
ballmerize the cli
nicktrn Apr 12, 2024
b79a4fc
replace npm-watch with plain old nodemon
nicktrn Apr 12, 2024
601cded
fix text inputs on windows
nicktrn Apr 12, 2024
3ee5f9e
changeset
nicktrn Apr 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/silly-suits-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@trigger.dev/otlp-importer": patch
"trigger.dev": patch
---

Fix package builds and CLI commands on Windows
5 changes: 5 additions & 0 deletions .changeset/tidy-balloons-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---

Init command was failing on Windows because of bad template paths
7 changes: 2 additions & 5 deletions packages/cli-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"@types/semver": "^7.3.13",
"@types/ws": "^8.5.3",
"cpy-cli": "^5.0.0",
"nodemon": "^3.0.1",
"npm-run-all": "^4.1.5",
"npm-watch": "^0.11.0",
"open": "^10.0.3",
"p-retry": "^6.1.0",
"rimraf": "^3.0.2",
Expand All @@ -56,9 +56,6 @@
"vitest": "^0.34.4",
"xdg-app-paths": "^8.3.0"
},
"watch": {
"build:prod-containerfile": "src/Containerfile.prod"
},
"scripts": {
"typecheck": "tsc -p tsconfig.check.json",
"build": "npm run clean && run-p build:**",
Expand All @@ -68,7 +65,7 @@
"dev": "npm run clean && run-p dev:**",
"dev:main": "tsup --watch",
"dev:workers": "tsup --config tsup.workers.config.ts --watch",
"dev:prod-containerfile": "npm-watch",
"dev:test": "nodemon -w src/Containerfile.prod -x npm run build:prod-containerfile",
"clean": "rimraf dist",
"start": "node dist/index.js",
"test": "vitest"
Expand Down
39 changes: 18 additions & 21 deletions packages/cli-v3/src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intro, log, outro, spinner } from "@clack/prompts";
import { intro, log, outro } from "@clack/prompts";
import { depot } from "@depot/cli";
import { context, trace } from "@opentelemetry/api";
import {
Expand All @@ -12,11 +12,10 @@ import chalk from "chalk";
import { Command, Option as CommandOption } from "commander";
import { Metafile, build } from "esbuild";
import { execa } from "execa";
import { resolve as importResolve } from "import-meta-resolve";
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
import { dirname, join, relative } from "node:path";
import { dirname, join, relative, posix } from "node:path";
import { setTimeout } from "node:timers/promises";
import terminalLink from "terminal-link";
import invariant from "tiny-invariant";
Expand Down Expand Up @@ -56,6 +55,8 @@ import {
} from "../utilities/deployErrors";
import { safeJsonParse } from "../utilities/safeJsonParse";
import { JavascriptProject } from "../utilities/javascriptProject";
import { cliRootPath } from "../utilities/resolveInternalFilePath";
import { escapeImportPath, spinner } from "../utilities/windows";

const DeployCommandOptions = CommonCommandOptions.extend({
skipTypecheck: z.boolean().default(false),
Expand Down Expand Up @@ -939,27 +940,27 @@ async function compileProject(

const taskFiles = await gatherTaskFiles(config);
const workerFacade = readFileSync(
new URL(importResolve("./workers/prod/worker-facade.js", import.meta.url)).href.replace(
"file://",
""
),
join(cliRootPath(), "workers", "prod", "worker-facade.js"),
"utf-8"
);

const workerSetupPath = new URL(
importResolve("./workers/prod/worker-setup.js", import.meta.url)
).href.replace("file://", "");
const workerSetupPath = join(cliRootPath(), "workers", "dev", "worker-setup.js");

let workerContents = workerFacade
.replace("__TASKS__", createTaskFileImports(taskFiles))
.replace("__WORKER_SETUP__", `import { tracingSDK } from "${workerSetupPath}";`);
.replace(
"__WORKER_SETUP__",
`import { tracingSDK } from "${escapeImportPath(workerSetupPath)}";`
);

if (configPath) {
logger.debug("Importing project config from", { configPath });

workerContents = workerContents.replace(
"__IMPORTED_PROJECT_CONFIG__",
`import * as importedConfigExports from "${configPath}"; const importedConfig = importedConfigExports.config; const handleError = importedConfigExports.handleError;`
`import * as importedConfigExports from "${escapeImportPath(
configPath
)}"; const importedConfig = importedConfigExports.config; const handleError = importedConfigExports.handleError;`
);
} else {
workerContents = workerContents.replace(
Expand Down Expand Up @@ -1015,10 +1016,7 @@ async function compileProject(
}

const entryPointContents = readFileSync(
new URL(importResolve("./workers/prod/entry-point.js", import.meta.url)).href.replace(
"file://",
""
),
join(cliRootPath(), "workers", "prod", "entry-point.js"),
"utf-8"
);

Expand Down Expand Up @@ -1076,12 +1074,13 @@ async function compileProject(
logger.debug(`Writing compiled files to ${tempDir}`);

// Get the metaOutput for the result build
const metaOutput = result.metafile!.outputs[join("out", "stdin.js")];
const metaOutput = result.metafile!.outputs[posix.join("out", "stdin.js")];

invariant(metaOutput, "Meta output for the result build is missing");

// Get the metaOutput for the entryPoint build
const entryPointMetaOutput = entryPointResult.metafile!.outputs[join("out", "stdin.js")];
const entryPointMetaOutput =
entryPointResult.metafile!.outputs[posix.join("out", "stdin.js")];

invariant(entryPointMetaOutput, "Meta output for the entryPoint build is missing");

Expand Down Expand Up @@ -1156,9 +1155,7 @@ async function compileProject(
}

// Write the Containerfile to /tmp/dir/Containerfile
const containerFilePath = new URL(
importResolve("./Containerfile.prod", import.meta.url)
).href.replace("file://", "");
const containerFilePath = join(cliRootPath(), "Containerfile.prod");
// Copy the Containerfile to /tmp/dir/Containerfile
await copyFile(containerFilePath, join(tempDir, "Containerfile"));

Expand Down
34 changes: 19 additions & 15 deletions packages/cli-v3/src/commands/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import {
import { watch } from "chokidar";
import { Command } from "commander";
import { BuildContext, Metafile, context } from "esbuild";
import { resolve as importResolve } from "import-meta-resolve";
import { render, useInput } from "ink";
import { createHash } from "node:crypto";
import fs, { readFileSync } from "node:fs";
import { ClientRequestArgs } from "node:http";
import { basename, dirname, join } from "node:path";
import { basename, dirname, join, normalize } from "node:path";
import pDebounce from "p-debounce";
import { WebSocket } from "partysocket";
import React, { Suspense, useEffect } from "react";
Expand Down Expand Up @@ -52,6 +51,8 @@ import {
parseNpmInstallError,
} from "../utilities/deployErrors";
import { findUp, pathExists } from "find-up";
import { cliRootPath } from "../utilities/resolveInternalFilePath";
import { escapeImportPath } from "../utilities/windows";

let apiClient: CliApiClient | undefined;

Expand Down Expand Up @@ -338,28 +339,27 @@ function useDev({

const taskFiles = await gatherTaskFiles(config);

const workerFacade = readFileSync(
new URL(importResolve("./workers/dev/worker-facade.js", import.meta.url)).href.replace(
"file://",
""
),
"utf-8"
);
const workerFacadePath = join(cliRootPath(), "workers", "dev", "worker-facade.js");
const workerFacade = readFileSync(workerFacadePath, "utf-8");

const workerSetupPath = new URL(
importResolve("./workers/dev/worker-setup.js", import.meta.url)
).href.replace("file://", "");
const workerSetupPath = join(cliRootPath(), "workers", "dev", "worker-setup.js");

let entryPointContents = workerFacade
.replace("__TASKS__", createTaskFileImports(taskFiles))
.replace("__WORKER_SETUP__", `import { tracingSDK, sender } from "${workerSetupPath}";`);
.replace(
"__WORKER_SETUP__",
`import { tracingSDK, sender } from "${escapeImportPath(workerSetupPath)}";`
);

if (configPath) {
configPath = normalize(configPath);
logger.debug("Importing project config from", { configPath });

entryPointContents = entryPointContents.replace(
"__IMPORTED_PROJECT_CONFIG__",
`import * as importedConfigExports from "${configPath}"; const importedConfig = importedConfigExports.config; const handleError = importedConfigExports.handleError;`
`import * as importedConfigExports from "${escapeImportPath(
configPath
)}"; const importedConfig = importedConfigExports.config; const handleError = importedConfigExports.handleError;`
);
} else {
entryPointContents = entryPointContents.replace(
Expand Down Expand Up @@ -414,7 +414,11 @@ function useDev({
logger.log(chalkGrey("○ Building background worker…"));
}

const metaOutputKey = join("out", `stdin.js`);
const metaOutputKey = join("out", `stdin.js`).replace(/\\/g, "/");

logger.debug("Metafile", {
metafileOutputs: JSON.stringify(result.metafile?.outputs),
});

const metaOutput = result.metafile!.outputs[metaOutputKey];

Expand Down
15 changes: 9 additions & 6 deletions packages/cli-v3/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intro, isCancel, log, outro, select, spinner, text } from "@clack/prompts";
import { intro, isCancel, log, outro, select, text } from "@clack/prompts";
import { context, trace } from "@opentelemetry/api";
import {
GetProjectResponseBody,
Expand Down Expand Up @@ -30,8 +30,9 @@ import { createFile, pathExists, readFile } from "../utilities/fileSystem";
import { getUserPackageManager } from "../utilities/getUserPackageManager";
import { printStandloneInitialBanner } from "../utilities/initialBanner.js";
import { logger } from "../utilities/logger";
import { resolveInternalFilePath } from "../utilities/resolveInternalFilePath";
import { cliRootPath } from "../utilities/resolveInternalFilePath";
import { login } from "./login";
import { spinner } from "../utilities/windows";

const InitCommandOptions = CommonCommandOptions.extend({
projectRef: z.string().optional(),
Expand Down Expand Up @@ -185,7 +186,7 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
async function createTriggerDir(dir: string, options: InitCommandOptions) {
return await tracer.startActiveSpan("createTriggerDir", async (span) => {
try {
const defaultValue = `${dir}/src/trigger`;
const defaultValue = join(dir, "src", "trigger");

const location = await text({
message: "Where would you like to create the Trigger.dev directory?",
Expand All @@ -199,6 +200,8 @@ async function createTriggerDir(dir: string, options: InitCommandOptions) {

const triggerDir = resolve(process.cwd(), location);

logger.debug({ triggerDir });

span.setAttributes({
"cli.triggerDir": triggerDir,
});
Expand Down Expand Up @@ -239,11 +242,11 @@ async function createTriggerDir(dir: string, options: InitCommandOptions) {
return { location, isCustomValue: location !== defaultValue };
}

const exampleFile = resolveInternalFilePath(`./templates/examples/${example}.ts.template`);
const templatePath = join(cliRootPath(), "templates", "examples", `${example}.ts.template`);
const outputPath = join(triggerDir, "example.ts");

await createFileFromTemplate({
templatePath: exampleFile,
templatePath,
outputPath,
replacements: {},
});
Expand Down Expand Up @@ -440,7 +443,7 @@ async function writeConfigFile(
spnnr.start("Creating config file");

const projectDir = resolve(process.cwd(), dir);
const templatePath = resolveInternalFilePath("./templates/trigger.config.ts.template");
const templatePath = join(cliRootPath(), "templates", "trigger.config.ts.template");
const outputPath = join(projectDir, "trigger.config.ts");

span.setAttributes({
Expand Down
3 changes: 2 additions & 1 deletion packages/cli-v3/src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intro, log, outro, select, spinner } from "@clack/prompts";
import { intro, log, outro, select } from "@clack/prompts";
import { recordSpanException } from "@trigger.dev/core/v3";
import { Command } from "commander";
import open from "open";
Expand All @@ -20,6 +20,7 @@ import { printInitialBanner } from "../utilities/initialBanner.js";
import { LoginResult } from "../utilities/session.js";
import { whoAmI } from "./whoami.js";
import { logger } from "../utilities/logger.js";
import { spinner } from "../utilities/windows.js";

export const LoginCommandOptions = CommonCommandOptions.extend({
apiUrl: z.string(),
Expand Down
3 changes: 2 additions & 1 deletion packages/cli-v3/src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { confirm, spinner } from "@clack/prompts";
import { confirm } from "@clack/prompts";
import { RunOptions, run } from "npm-check-updates";
import path from "path";
import { z } from "zod";
import { chalkError, chalkSuccess } from "../utilities/cliOutput.js";
import { readJSONFileSync, writeJSONFile } from "../utilities/fileSystem.js";
import { installDependencies } from "../utilities/installDependencies.js";
import { spinner } from "../utilities/windows.js";

export const UpdateCommandOptionsSchema = z.object({
to: z.string().optional(),
Expand Down
3 changes: 2 additions & 1 deletion packages/cli-v3/src/commands/whoami.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intro, note, spinner } from "@clack/prompts";
import { intro, note } from "@clack/prompts";
import { chalkLink } from "../utilities/cliOutput.js";
import { logger } from "../utilities/logger.js";
import { isLoggedIn } from "../utilities/session.js";
Expand All @@ -12,6 +12,7 @@ import {
} from "../cli/common.js";
import { z } from "zod";
import { CliApiClient } from "../apiClient.js";
import { spinner } from "../utilities/windows.js";

type WhoAmIResult =
| {
Expand Down
5 changes: 4 additions & 1 deletion packages/cli-v3/src/utilities/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { readFileSync } from "node:fs";
import { extname, isAbsolute } from "node:path";
import tsConfigPaths from "tsconfig-paths";
import { logger } from "./logger";
import { escapeImportPath } from "./windows";

export function bundleTriggerDevCore(buildIdentifier: string, tsconfigPath?: string): Plugin {
return {
Expand Down Expand Up @@ -56,7 +57,9 @@ export function workerSetupImportConfigPlugin(configPath?: string): Plugin {

workerSetupContents = workerSetupContents.replace(
"__SETUP_IMPORTED_PROJECT_CONFIG__",
`import * as setupImportedConfigExports from "${configPath}"; const setupImportedConfig = setupImportedConfigExports.config;`
`import * as setupImportedConfigExports from "${escapeImportPath(
configPath
)}"; const setupImportedConfig = setupImportedConfigExports.config;`
);

logger.debug("Loading worker setup", {
Expand Down
8 changes: 4 additions & 4 deletions packages/cli-v3/src/utilities/initialBanner.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { spinner } from "@clack/prompts";
import chalk from "chalk";
import type { Result } from "update-check";
import checkForUpdate from "update-check";
import pkg from "../../package.json";
import { chalkGrey, chalkRun, chalkTask, chalkWorker, green, logo } from "./cliOutput.js";
import { getVersion } from "./getVersion.js";
import { logger } from "./logger.js";
import { spinner } from "./windows";

export async function printInitialBanner(performUpdateCheck = true) {
const packageVersion = getVersion();
Expand Down Expand Up @@ -40,18 +40,18 @@ After installation, run Trigger.dev with \`npx trigger.dev\`.`
export async function printStandloneInitialBanner(performUpdateCheck = true) {
const packageVersion = getVersion();

let text = `\n${logo()} ${chalkGrey("(v3 Developer Preview)")}`;
logger.log(`\n${logo()} ${chalkGrey("(v3 Developer Preview)")}`);

if (performUpdateCheck) {
const maybeNewVersion = await updateCheck();

// Log a slightly more noticeable message if this is a major bump
if (maybeNewVersion !== undefined) {
text = `${text} (update available ${chalk.green(maybeNewVersion)})`;
logger.log(`Update available ${chalk.green(maybeNewVersion)}`);
}
}

logger.log(text + "\n" + chalkGrey("-".repeat(54)));
logger.log(`${chalkGrey("-".repeat(54))}`);
}

export function printDevBanner() {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-v3/src/utilities/installDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { spinner } from "@clack/prompts";
import chalk from "chalk";
import { execa } from "execa";
import { getUserPackageManager, type PackageManager } from "./getUserPackageManager.js";
import { logger } from "./logger.js";
import { spinner } from "./windows.js";

export async function installDependencies(projectDir: string) {
logger.info("Installing dependencies...");
Expand Down
9 changes: 6 additions & 3 deletions packages/cli-v3/src/utilities/resolveInternalFilePath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { resolve as importResolve } from "import-meta-resolve";
import path from "path";
import { fileURLToPath } from "url";

export function resolveInternalFilePath(filePath: string): string {
return new URL(importResolve(filePath, import.meta.url)).href.replace("file://", "");
export function cliRootPath() {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
return __dirname;
}
Loading