-
Notifications
You must be signed in to change notification settings - Fork 176
New option to install packages #574
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { execSync } from "child_process"; | ||
import * as fs from "fs"; | ||
import * as os from "os"; | ||
import * as path from "path"; | ||
import { InstallOptions } from "types/open-next"; | ||
|
||
import logger from "../logger.js"; | ||
|
||
export function installDependencies( | ||
outputDir: string, | ||
installOptions?: InstallOptions, | ||
) { | ||
try { | ||
if (!installOptions) { | ||
return; | ||
} | ||
const name = outputDir.split("/").pop(); | ||
// First we create a tempDir | ||
const tempInstallDir = fs.mkdtempSync( | ||
path.join(os.tmpdir(), `open-next-install-${name}`), | ||
); | ||
logger.info(`Installing dependencies for ${name}...`); | ||
// We then need to run install in the tempDir | ||
// We don't install in the output dir directly because it could contain a package.json, and npm would then try to reinstall not complete deps from tracing the files | ||
const installCommand = `npm install --arch=${installOptions.arch ?? "arm64"} --platform=linux --target=${installOptions.nodeVersion ?? "18"} --libc=${installOptions.libc ?? "glibc"} ${installOptions.packages.join(" ")}`; | ||
Check failure on line 25 in packages/open-next/src/build/installDeps.ts
|
||
|
||
execSync(installCommand, { | ||
stdio: "pipe", | ||
cwd: tempInstallDir, | ||
env: { | ||
...process.env, | ||
SHARP_IGNORE_GLOBAL_LIBVIPS: "1", | ||
}, | ||
}); | ||
|
||
// Copy the node_modules to the outputDir | ||
fs.cpSync( | ||
path.join(tempInstallDir, "node_modules"), | ||
path.join(outputDir, "node_modules"), | ||
|
||
{ recursive: true, force: true, dereference: true }, | ||
); | ||
|
||
// Cleanup tempDir | ||
fs.rmSync(tempInstallDir, { recursive: true, force: true }); | ||
logger.info(`Dependencies installed for ${name}`); | ||
} catch (e: any) { | ||
logger.error(e.stdout.toString()); | ||
logger.error("Could not install dependencies"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,6 +184,31 @@ export interface OverrideOptions extends DefaultOverrideOptions { | |
queue?: IncludedQueue | LazyLoadedOverride<Queue>; | ||
} | ||
|
||
export interface InstallOptions { | ||
/** | ||
* List of packages to install | ||
* @example | ||
* ```ts | ||
* install: { | ||
* packages: ["[email protected]"] | ||
* } | ||
* ``` | ||
*/ | ||
packages: string[]; | ||
/** | ||
* @default "arm64" | ||
*/ | ||
arch?: "x64" | "arm64"; | ||
/** | ||
* @default "18" | ||
*/ | ||
nodeVersion?: "18" | "20" | "22"; | ||
/** | ||
* @default "glibc" | ||
*/ | ||
libc?: "glibc" | "musl"; | ||
} | ||
|
||
export interface DefaultFunctionOptions< | ||
E extends BaseEventOrResult = InternalEvent, | ||
R extends BaseEventOrResult = InternalResult, | ||
|
@@ -202,6 +227,14 @@ export interface DefaultFunctionOptions< | |
* Enable overriding the default lambda. | ||
*/ | ||
override?: DefaultOverrideOptions<E, R>; | ||
|
||
/** | ||
* Install options for the function. | ||
* This is used to install additional packages to this function. | ||
* For image optimization, it will install sharp by default. | ||
* @default undefined | ||
*/ | ||
install?: InstallOptions; | ||
} | ||
|
||
export interface FunctionOptions extends DefaultFunctionOptions { | ||
|
@@ -318,15 +351,6 @@ export interface OpenNextConfig { | |
* @default "s3" | ||
*/ | ||
loader?: IncludedImageLoader | LazyLoadedOverride<ImageLoader>; | ||
/** | ||
* @default "arm64" | ||
*/ | ||
arch?: "x64" | "arm64"; | ||
/** | ||
* @default "18" | ||
*/ | ||
|
||
nodeVersion?: "18" | "20"; | ||
}; | ||
|
||
/** | ||
|
Uh oh!
There was an error while loading. Please reload this page.