Cli: improve Next.js project detection (#262)

* Detect presence of a “next” dependency

* Use a strict undefined check instead

* Changeset: Detect Next.js project by looking at dependencies, not next.config.js

* Read a package json file

* First check for next.config file, otherwise use next dependency

* Update changeset description
This commit is contained in:
Matt Aitken
2023-08-07 13:01:03 +01:00
committed by GitHub
parent a31705e198
commit e740297829
3 changed files with 35 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/cli": patch
---
Detect Next.js project by looking at dependencies if can't find next.config.js
+20 -6
View File
@@ -1,15 +1,29 @@
import fs from "fs/promises";
import pathModule from "path";
import { readPackageJson } from "./readPackageJson.js";
/** Detects if the project is a Next.js project at path */
export async function detectNextJsProject(path: string): Promise<boolean> {
// Checks for the presence of a next.config.js file
try {
// Check if next.config.js file exists in the given path
await fs.access(pathModule.join(path, "next.config.js"));
const hasNextConfigFile = await detectNextConfigFile(path);
if (hasNextConfigFile) {
return true;
} catch (error) {
// If next.config.js file doesn't exist, it's not a Next.js project
}
return await detectNextDependency(path);
}
async function detectNextConfigFile(path: string): Promise<boolean> {
return fs
.access(pathModule.join(path, "next.config.js"))
.then(() => true)
.catch(() => false);
}
async function detectNextDependency(path: string): Promise<boolean> {
const packageJsonContent = await readPackageJson(path);
if (!packageJsonContent) {
return false;
}
return packageJsonContent.dependencies?.next !== undefined;
}
+10
View File
@@ -0,0 +1,10 @@
import pathModule from "path";
import { type PackageJson } from "type-fest";
import { readJSONFile } from "./fileSystem.js";
export async function readPackageJson(directory: string): Promise<PackageJson | undefined> {
const packageJsonPath = pathModule.join(directory, "package.json");
return readJSONFile(packageJsonPath)
.then((f) => f as PackageJson)
.catch(() => undefined);
}