Skip to content

Commit a5f3596

Browse files
authored
fix: Electron example build (#228)
* fix: Electron example build * refactor: rename `llamaBins` to `bins`
1 parent 4ea0c3c commit a5f3596

File tree

8 files changed

+144
-117
lines changed

8 files changed

+144
-117
lines changed

.github/workflows/build.yml

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ jobs:
193193
throw new Error("Could not find node versions");
194194
}
195195
196-
await $`mkdir -p llamaBins`;
196+
await $`mkdir -p bins`;
197197
198198
async function buildBinary(arch, flags = [], nodeTarget = nodeVersion) {
199199
console.log(`Building ${arch} for node ${nodeTarget} with flags`, flags);
@@ -218,9 +218,9 @@ jobs:
218218
await buildBinary("x64", ["--gpu", "false"]);
219219
}
220220
221-
// move binaries to llamaBins
221+
// move binaries to bins
222222
const localBuildsDirectoryPath = path.join(process.cwd(), "llama", "localBuilds");
223-
const llamaBinsDirectoryPath = path.join(process.cwd(), "llamaBins");
223+
const llamaBinsDirectoryPath = path.join(process.cwd(), "bins");
224224
for (const folderName of await fs.readdir(localBuildsDirectoryPath)) {
225225
await fs.move(
226226
path.join(localBuildsDirectoryPath, folderName, "Release"),
@@ -229,15 +229,15 @@ jobs:
229229
}
230230
231231
await $`echo "Built binaries:"`;
232-
await $`ls llamaBins`;
232+
await $`ls bins`;
233233
234234
EOF
235235
236236
- name: Publish artifact
237237
uses: actions/upload-artifact@v4
238238
with:
239239
name: "bins-${{ matrix.config.artifact }}"
240-
path: "llamaBins/*"
240+
path: "bins/*"
241241

242242
standalone-tests:
243243
name: Standalone tests
@@ -359,8 +359,8 @@ jobs:
359359
path: artifacts
360360
- name: Move artifacts
361361
run: |
362-
mkdir -p llamaBins
363-
mv artifacts/bins-*/* llamaBins/
362+
mkdir -p bins
363+
mv artifacts/bins-*/* bins/
364364
mv artifacts/build dist/
365365
366366
cp -r artifacts/llama.cpp/llama.cpp/grammars llama/grammars
@@ -379,7 +379,7 @@ jobs:
379379
rm -f ./templates/package-lock.json
380380
381381
echo "Built binaries:"
382-
ls llamaBins
382+
ls bins
383383
- name: Add "postinstall" script to package.json
384384
run: npm run addPostinstallScript
385385
- name: Release
@@ -475,8 +475,10 @@ jobs:
475475
run: npm ci
476476

477477
- name: Create Electron app project
478+
shell: bash
478479
env:
479480
DEPLOYED_PACKAGE_VERSION: ${{ needs.release.outputs.package-version }}
481+
NODE_LLAMA_CPP_SKIP_DOWNLOAD: true
480482
run: |
481483
npx --no vite-node ./scripts/scaffoldElectronExampleForCiBuild.ts --packageVersion "$DEPLOYED_PACKAGE_VERSION" --packageFolderPath ./electron-app-example
482484
cd electron-app-example
@@ -491,10 +493,15 @@ jobs:
491493
npm run build
492494
ls ./release
493495
494-
- name: Add builds to current release
495-
uses: svenstaro/upload-release-action@v2
496+
- name: Upload artifacts
497+
uses: actions/upload-artifact@v4
496498
with:
497-
file: "electron-app-example/release/*.{dmg,zip,exe,appx,AppImage,snap,deb,tar.gz}"
498-
file_glob: true
499-
tag: ${{ needs.release.outputs.package-version }}
500-
make_latest: false
499+
name: "electron-app-example-${{ matrix.config.name }}"
500+
path: "./electron-app-example/release"
501+
502+
- name: Add builds to current release
503+
shell: bash
504+
env:
505+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
506+
run: |
507+
gh release upload "${{ needs.release.outputs.package-version }}" ./electron-app-example/release/*.{dmg,zip,exe,appx,AppImage,snap,deb,tar.gz}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ node_modules
3131
/llama/xpack/store
3232
/llama/xpack/xpacks
3333
/llama/xpack/cmakeInstall.lock
34-
/llamaBins
34+
/bins

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"dist/",
1313
"templates/packed/",
1414
"llama/",
15-
"llamaBins/",
15+
"bins/",
1616
"package.json",
1717
"README.md",
1818
"LICENSE"

scripts/scaffoldElectronExampleForCiBuild.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const {packageVersion, packageFolderPath} = argv;
2525
if (packageVersion === "")
2626
throw new Error("packageVersion is empty");
2727

28+
console.info("node-llama-cpp version:", packageVersion);
29+
2830
const resolvedPackageFolderPath = path.resolve(process.cwd(), packageFolderPath);
2931

3032
const templateFilePath = path.join(packedProjectTemplatesDirectory, `${electronTemplateName}.json`);

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const env = envVar.from(process.env);
1616

1717
export const llamaDirectory = path.join(__dirname, "..", "llama");
1818
export const llamaToolchainsDirectory = path.join(llamaDirectory, "toolchains");
19-
export const llamaPrebuiltBinsDirectory = path.join(__dirname, "..", "llamaBins");
19+
export const llamaPrebuiltBinsDirectory = path.join(__dirname, "..", "bins");
2020
export const llamaLocalBuildBinsDirectory = path.join(llamaDirectory, "localBuilds");
2121
export const llamaBinsGrammarsDirectory = path.join(__dirname, "..", "llama", "grammars");
2222
export const projectTemplatesDirectory = path.join(__dirname, "..", "templates");

templates/electron-typescript-react/electron-builder.json5

Lines changed: 0 additions & 98 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import path from "node:path";
2+
import {$} from "zx";
3+
import type {Configuration} from "electron-builder";
4+
5+
/**
6+
* @see - https://www.electron.build/configuration/configuration
7+
*/
8+
export default {
9+
appId: "node-llama-cpp.electron.example",
10+
asar: true,
11+
productName: "node-llama-cpp Electron example",
12+
executableName: "node-llama-cpp-electron-example",
13+
directories: {
14+
output: "release"
15+
},
16+
17+
// remove this once you set up your own code signing for macOS
18+
async afterSign(context) {
19+
if (context.electronPlatformName === "darwin") {
20+
// check whether the app was already signed
21+
const appPath = path.join(context.appOutDir, `${context.packager.appInfo.productFilename}.app`);
22+
23+
// this is needed for the app to not appear as "damaged" on Apple Silicon Macs
24+
// https://github.com/electron-userland/electron-builder/issues/5850#issuecomment-1821648559
25+
await $`codesign --force --deep --sign - ${appPath}`;
26+
}
27+
},
28+
files: [
29+
"dist",
30+
"dist-electron",
31+
"!node_modules/node-llama-cpp/bins/*",
32+
"node_modules/node-llama-cpp/bins/${os}-${arch}*/**/*",
33+
"!node_modules/node-llama-cpp/llama/localBuilds/*",
34+
"node_modules/node-llama-cpp/llama/localBuilds/${os}-${arch}*/**/*"
35+
],
36+
asarUnpack: [
37+
"node_modules/node-llama-cpp/bins",
38+
"node_modules/node-llama-cpp/llama/localBuilds"
39+
],
40+
mac: {
41+
target: [{
42+
target: "dmg",
43+
arch: [
44+
"arm64",
45+
"x64"
46+
]
47+
}, {
48+
target: "zip",
49+
arch: [
50+
"arm64",
51+
"x64"
52+
]
53+
}],
54+
55+
artifactName: "${name}.macOS.${version}.${arch}.${ext}"
56+
},
57+
win: {
58+
target: [
59+
{
60+
target: "nsis",
61+
arch: [
62+
"x64",
63+
"arm64"
64+
]
65+
},
66+
{
67+
target: "appx",
68+
arch: [
69+
"x64",
70+
"arm64"
71+
]
72+
}
73+
],
74+
75+
artifactName: "${name}.Windows.${version}.${arch}.${ext}"
76+
},
77+
appx: {
78+
artifactName: "${name}.Windows.${version}.${arch}.${ext}"
79+
},
80+
nsis: {
81+
oneClick: true,
82+
perMachine: false,
83+
allowToChangeInstallationDirectory: false,
84+
deleteAppDataOnUninstall: true
85+
},
86+
linux: {
87+
target: [{
88+
target: "AppImage",
89+
arch: [
90+
"x64",
91+
"arm64"
92+
]
93+
}, {
94+
target: "snap",
95+
arch: [
96+
"x64",
97+
"arm64"
98+
]
99+
}, {
100+
target: "deb",
101+
arch: [
102+
"x64",
103+
"arm64"
104+
]
105+
}, {
106+
target: "tar.gz",
107+
arch: [
108+
"x64",
109+
"arm64"
110+
]
111+
}],
112+
113+
artifactName: "${name}.Linux.${version}.${arch}.${ext}"
114+
}
115+
} as Configuration;

templates/electron-typescript-react/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"start": "vite dev",
1111
"start:build": "electron ./dist-electron",
1212
"prebuild": "rimraf ./dist ./dist-electron ./release",
13-
"build": "tsc && vite build && electron-builder",
13+
"build": "tsc && vite build && electron-builder --config ./electron-builder.ts",
1414
"lint": "npm run lint:eslint",
1515
"lint:eslint": "eslint --ext .js --ext .ts --report-unused-disable-directives .",
1616
"format": "npm run lint:eslint -- --fix",
@@ -41,6 +41,7 @@
4141
"typescript": "^5.4.5",
4242
"vite": "^5.2.13",
4343
"vite-plugin-electron": "^0.28.7",
44-
"vite-plugin-electron-renderer": "^0.14.5"
44+
"vite-plugin-electron-renderer": "^0.14.5",
45+
"zx": "^8.1.2"
4546
}
4647
}

0 commit comments

Comments
 (0)