@@ -2,20 +2,70 @@ import { execSync } from 'node:child_process'
22import { existsSync , mkdirSync } from 'node:fs'
33import { join } from 'node:path'
44
5+ const WIN_OUTPUT_NAME = 'Git_Commit_Analytics_win.exe'
6+ const MAC_OUTPUT_NAME = 'Git_Commit_Analytics_mac'
7+ const SEA_BLOB = 'sea-prep.blob'
8+ const OUTPUT_DIR = 'dist'
9+
10+ const NODE_PATH = process . execPath // Node.js executable file path
11+
12+ async function buildWin ( ) {
13+ const OUTPUT_PATH = join ( OUTPUT_DIR , WIN_OUTPUT_NAME )
14+
15+ try {
16+ console . log ( `📦 Creating standalone executable for Windows...` )
17+
18+ execSync (
19+ `node -e "require('fs').copyFileSync(process.execPath, ${ OUTPUT_PATH } )" ` ,
20+ )
21+
22+ execSync ( `signtool remove /s ${ OUTPUT_PATH } ` )
23+
24+ execSync (
25+ [
26+ `npx postject ${ OUTPUT_PATH } NODE_SEA_BLOB ${ SEA_BLOB } ` ,
27+ '--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2' ,
28+ ] . join ( ' ' ) ,
29+ )
30+
31+ execSync ( `codesign --sign - ${ OUTPUT_PATH } ` )
32+ return true
33+ } catch ( error ) {
34+ console . error ( '❌ Failed to create executable for Windows:' , error )
35+ return false
36+ }
37+ }
38+
39+ async function buildMac ( ) {
40+ const OUTPUT_PATH = join ( OUTPUT_DIR , MAC_OUTPUT_NAME )
41+
42+ try {
43+ console . log ( `📦 Creating standalone executable for macOS...` )
44+
45+ execSync ( `cp ${ NODE_PATH } ${ OUTPUT_PATH } ` )
46+
47+ execSync ( `codesign --remove-signature ${ OUTPUT_PATH } ` )
48+
49+ execSync (
50+ [
51+ `npx postject ${ OUTPUT_PATH } NODE_SEA_BLOB ${ SEA_BLOB } ` ,
52+ '--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2' ,
53+ '--macho-segment-name NODE_SEA' ,
54+ ] . join ( ' ' ) ,
55+ )
56+
57+ execSync ( `codesign --sign - ${ OUTPUT_PATH } ` )
58+ return true
59+ } catch ( error ) {
60+ console . error ( '❌ Failed to create executable for macOS:' , error )
61+ return false
62+ }
63+ }
64+
565// https://nodejs.org/api/single-executable-applications.html
666async function build ( ) {
7- const WIN_OUTPUT_NAME = 'Git_Commit_Analytics_win.exe'
8- const MAC_OUTPUT_NAME = 'Git_Commit_Analytics_mac'
9- const SEA_BLOB = 'sea-prep.blob'
10- const OUTPUT_DIR = 'dist'
11-
1267 const PLATFORM = process . platform // 'win32' | 'darwin' | 'linux'
1368 const IS_WIN = PLATFORM === 'win32'
14- const IS_MAC = PLATFORM === 'darwin'
15-
16- const OUTPUT_NAME = IS_WIN ? WIN_OUTPUT_NAME : MAC_OUTPUT_NAME
17- const OUTPUT_PATH = join ( OUTPUT_DIR , OUTPUT_NAME )
18- const NODE_PATH = process . execPath // Node.js executable file path
1969
2070 if ( ! existsSync ( OUTPUT_DIR ) ) {
2171 mkdirSync ( OUTPUT_DIR , { recursive : true } )
@@ -36,40 +86,14 @@ async function build() {
3686 process . exit ( 1 )
3787 }
3888
39- console . log ( `📦 Creating standalone executable for ${ PLATFORM } ...` )
89+ const buildTask = IS_WIN ? buildWin : buildMac
4090
41- try {
42- if ( IS_WIN ) {
43- execSync (
44- `cmd /c "copy /b ${ NODE_PATH } + ${ SEA_BLOB } ${ OUTPUT_PATH } && exit /b"` ,
45- {
46- stdio : 'inherit' ,
47- shell : true ,
48- } ,
49- )
50- } else {
51- execSync ( `cp ${ NODE_PATH } ${ OUTPUT_PATH } ` )
52-
53- if ( IS_MAC ) {
54- execSync ( `codesign --remove-signature ${ OUTPUT_PATH } ` )
55-
56- execSync (
57- [
58- `npx postject ${ OUTPUT_PATH } NODE_SEA_BLOB ${ SEA_BLOB } ` ,
59- '--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2' ,
60- '--macho-segment-name NODE_SEA' ,
61- ] . join ( ' ' ) ,
62- )
63-
64- execSync ( `codesign --sign - ${ OUTPUT_PATH } ` )
65- }
66- }
67- } catch ( error ) {
68- console . error ( '❌ Failed to create executable:' , error )
91+ const isSuccess = await buildTask ( )
92+ if ( ! isSuccess ) {
6993 process . exit ( 1 )
7094 }
7195
72- console . log ( `✅ Build complete: ${ OUTPUT_PATH } ` )
96+ console . log ( `✅ Build complete! ` )
7397}
7498
7599build ( ) . catch ( console . error )
0 commit comments