CLI: detect package manager using artifacts if they exist (#261)
* Detect package manager from artifacts (e.g. package-lock.json) and fallback to process.env.npm_config_user_agent * Added a changeset
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@trigger.dev/cli": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Detect package manager from artifacts if they exist
|
||||||
@@ -30,10 +30,12 @@ export type InitCommandOptions = {
|
|||||||
type ResolvedOptions = Required<InitCommandOptions>;
|
type ResolvedOptions = Required<InitCommandOptions>;
|
||||||
|
|
||||||
export const initCommand = async (options: InitCommandOptions) => {
|
export const initCommand = async (options: InitCommandOptions) => {
|
||||||
renderTitle();
|
|
||||||
|
|
||||||
telemetryClient.init.started(options);
|
telemetryClient.init.started(options);
|
||||||
|
|
||||||
|
const resolvedPath = resolvePath(options.projectPath);
|
||||||
|
|
||||||
|
await renderTitle(resolvedPath);
|
||||||
|
|
||||||
if (options.triggerUrl === CLOUD_TRIGGER_URL) {
|
if (options.triggerUrl === CLOUD_TRIGGER_URL) {
|
||||||
logger.info(`✨ Initializing project in Trigger.dev Cloud`);
|
logger.info(`✨ Initializing project in Trigger.dev Cloud`);
|
||||||
} else if (typeof options.triggerUrl === "string") {
|
} else if (typeof options.triggerUrl === "string") {
|
||||||
@@ -42,7 +44,6 @@ export const initCommand = async (options: InitCommandOptions) => {
|
|||||||
logger.info(`✨ Initializing Trigger.dev in project`);
|
logger.info(`✨ Initializing Trigger.dev in project`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolvedPath = resolvePath(options.projectPath);
|
|
||||||
// Detect if are are in a Next.js project
|
// Detect if are are in a Next.js project
|
||||||
const isNextJsProject = await detectNextJsProject(resolvedPath);
|
const isNextJsProject = await detectNextJsProject(resolvedPath);
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import chalk from "chalk";
|
|||||||
import { execa } from "execa";
|
import { execa } from "execa";
|
||||||
import ora, { type Ora } from "ora";
|
import ora, { type Ora } from "ora";
|
||||||
import pathModule from "path";
|
import pathModule from "path";
|
||||||
import { getUserPkgManager, type PackageManager } from "./getUserPkgManager.js";
|
import { getUserPackageManager, type PackageManager } from "./getUserPkgManager.js";
|
||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
@@ -18,7 +18,7 @@ export type InstalledPackage = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export async function addDependencies(projectDir: string, packages: Array<InstallPackage>) {
|
export async function addDependencies(projectDir: string, packages: Array<InstallPackage>) {
|
||||||
const pkgManager = getUserPkgManager();
|
const pkgManager = await getUserPackageManager(projectDir);
|
||||||
|
|
||||||
const spinner = ora("Adding @trigger.dev dependencies to package.json...").start();
|
const spinner = ora("Adding @trigger.dev dependencies to package.json...").start();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,17 @@
|
|||||||
|
import pathModule from "path";
|
||||||
|
import { pathExists } from "./fileSystem.js";
|
||||||
|
|
||||||
export type PackageManager = "npm" | "pnpm" | "yarn";
|
export type PackageManager = "npm" | "pnpm" | "yarn";
|
||||||
|
|
||||||
export const getUserPkgManager: () => PackageManager = () => {
|
export async function getUserPackageManager(path: string): Promise<PackageManager> {
|
||||||
|
try {
|
||||||
|
return detectPackageManagerFromArtifacts(path);
|
||||||
|
} catch (error) {
|
||||||
|
return detectPackageManagerFromCurrentCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function detectPackageManagerFromCurrentCommand(): PackageManager {
|
||||||
// This environment variable is set by npm and yarn but pnpm seems less consistent
|
// This environment variable is set by npm and yarn but pnpm seems less consistent
|
||||||
const userAgent = process.env.npm_config_user_agent;
|
const userAgent = process.env.npm_config_user_agent;
|
||||||
|
|
||||||
@@ -16,4 +27,22 @@ export const getUserPkgManager: () => PackageManager = () => {
|
|||||||
// If no user agent is set, assume npm
|
// If no user agent is set, assume npm
|
||||||
return "npm";
|
return "npm";
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
async function detectPackageManagerFromArtifacts(path: string): Promise<PackageManager> {
|
||||||
|
const packageFiles = [
|
||||||
|
{ name: "yarn.lock", pm: "yarn" } as const,
|
||||||
|
{ name: "pnpm-lock.yaml", pm: "pnpm" } as const,
|
||||||
|
{ name: "package-lock.json", pm: "npm" } as const,
|
||||||
|
{ name: "npm-shrinkwrap.json", pm: "npm" } as const,
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const { name, pm } of packageFiles) {
|
||||||
|
const exists = await pathExists(pathModule.join(path, name));
|
||||||
|
if (exists) {
|
||||||
|
return pm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("Could not detect package manager from artifacts");
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getUserPkgManager, type PackageManager } from "./getUserPkgManager.js";
|
import { getUserPackageManager, type PackageManager } from "./getUserPkgManager.js";
|
||||||
import { logger } from "./logger.js";
|
import { logger } from "./logger.js";
|
||||||
import ora, { type Ora } from "ora";
|
import ora, { type Ora } from "ora";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
@@ -7,7 +7,7 @@ import { execa } from "execa";
|
|||||||
export async function installDependencies(projectDir: string) {
|
export async function installDependencies(projectDir: string) {
|
||||||
logger.info("Installing dependencies...");
|
logger.info("Installing dependencies...");
|
||||||
|
|
||||||
const pkgManager = getUserPkgManager();
|
const pkgManager = await getUserPackageManager(projectDir);
|
||||||
|
|
||||||
const installSpinner = await runInstallCommand(pkgManager, projectDir);
|
const installSpinner = await runInstallCommand(pkgManager, projectDir);
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import gradient from "gradient-string";
|
import gradient from "gradient-string";
|
||||||
import { TITLE_TEXT } from "../consts.js";
|
import { TITLE_TEXT } from "../consts.js";
|
||||||
import { getUserPkgManager } from "./getUserPkgManager.js";
|
import { getUserPackageManager } from "./getUserPkgManager.js";
|
||||||
|
|
||||||
// colors brought in from vscode poimandres theme
|
// colors brought in from vscode poimandres theme
|
||||||
const poimandresTheme = {
|
const poimandresTheme = {
|
||||||
@@ -12,11 +12,11 @@ const poimandresTheme = {
|
|||||||
yellow: "#fffac2",
|
yellow: "#fffac2",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const renderTitle = () => {
|
export const renderTitle = async (projectDirectory: string) => {
|
||||||
const triggerGradient = gradient(Object.values(poimandresTheme));
|
const triggerGradient = gradient(Object.values(poimandresTheme));
|
||||||
|
|
||||||
// resolves weird behavior where the ascii is offset
|
// resolves weird behavior where the ascii is offset
|
||||||
const pkgManager = getUserPkgManager();
|
const pkgManager = await getUserPackageManager(projectDirectory);
|
||||||
if (pkgManager === "yarn" || pkgManager === "pnpm") {
|
if (pkgManager === "yarn" || pkgManager === "pnpm") {
|
||||||
console.log("");
|
console.log("");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user