1+ import path from "path" ;
2+ import { fileURLToPath } from "url" ;
13import { CommandModule } from "yargs" ;
24import chalk from "chalk" ;
3- import { defaultSkipDownload , documentationPageUrls } from "../../config.js" ;
5+ import { defaultSkipDownload , documentationPageUrls , defaultNodeLlamaCppPostinstall } from "../../config.js" ;
46import { getLlamaForOptions } from "../../bindings/getLlama.js" ;
57import { setForceShowConsoleLogPrefix } from "../../state.js" ;
68import { isRunningUnderRosetta } from "../utils/isRunningUnderRosetta.js" ;
79import { getConsoleLogPrefix } from "../../utils/getConsoleLogPrefix.js" ;
10+ import { parsePackageJsonConfig , resolvePackageJsonConfig } from "../utils/packageJsonConfig.js" ;
11+ import { detectCurrentPackageManager } from "../utils/packageManager.js" ;
12+
13+ const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
814
915type OnPostInstallCommand = null ;
1016
@@ -13,7 +19,22 @@ export const OnPostInstallCommand: CommandModule<object, OnPostInstallCommand> =
1319 describe : false ,
1420 async handler ( ) {
1521 if ( defaultSkipDownload )
16- return ;
22+ return void process . exit ( 0 ) ;
23+
24+ const nlcConfig = parsePackageJsonConfig ( await resolvePackageJsonConfig ( __dirname ) ) ;
25+ const postinstallConfig = ( defaultNodeLlamaCppPostinstall == null || defaultNodeLlamaCppPostinstall === "auto" )
26+ ? nlcConfig . nodeLlamaCppPostinstall ?? defaultNodeLlamaCppPostinstall
27+ : defaultNodeLlamaCppPostinstall ;
28+
29+ // set via a `--node-llama-cpp-postinstall=skip` flag on an `npm install` command
30+ // (prefer `--node-llama-cpp-postinstall=ignoreFailedBuild` if you really need it)
31+ if ( postinstallConfig === "skip" ) {
32+ console . info (
33+ getConsoleLogPrefix ( false , false ) ,
34+ "Skipping node-llama-cpp postinstall due to a 'skip' configuration"
35+ ) ;
36+ return void process . exit ( 0 ) ;
37+ }
1738
1839 setForceShowConsoleLogPrefix ( true ) ;
1940
@@ -34,7 +55,10 @@ export const OnPostInstallCommand: CommandModule<object, OnPostInstallCommand> =
3455 "troubleshooting: " + documentationPageUrls . troubleshooting . RosettaIllegalHardwareInstruction
3556 ) ;
3657
37- process . exit ( 1 ) ;
58+ if ( postinstallConfig === "ignoreFailedBuild" )
59+ process . exit ( 0 ) ;
60+ else
61+ process . exit ( 1 ) ;
3862 }
3963
4064 try {
@@ -47,7 +71,25 @@ export const OnPostInstallCommand: CommandModule<object, OnPostInstallCommand> =
4771 process . exit ( 0 ) ;
4872 } catch ( err ) {
4973 console . error ( err ) ;
50- process . exit ( 1 ) ;
74+
75+ const packageManager = detectCurrentPackageManager ( ) ;
76+ if ( postinstallConfig === "auto" && packageManager === "npm" )
77+ console . info (
78+ getConsoleLogPrefix ( false , false ) ,
79+ "To disable node-llama-cpp's postinstall for this 'npm install', use the '--node-llama-cpp-postinstall=skip' flag when running 'npm install' command"
80+ ) ;
81+
82+ if ( postinstallConfig === "auto" )
83+ console . info (
84+ getConsoleLogPrefix ( false , false ) ,
85+ "To customize node-llama-cpp's postinstall behavior, see the troubleshooting guide: " +
86+ documentationPageUrls . troubleshooting . PostinstallBehavior
87+ ) ;
88+
89+ if ( postinstallConfig === "ignoreFailedBuild" )
90+ process . exit ( 0 ) ;
91+ else
92+ process . exit ( 1 ) ;
5193 }
5294 }
5395} ;
0 commit comments