@@ -5,17 +5,58 @@ import path from "node:path";
55import sea from "node:sea" ;
66import fsAsync from "node:fs/promises" ;
77import fs from "node:fs" ;
8- import { nativeResourcesPath , shellResourcesPath } from "./constants.js" ;
8+ import { nativeResourcesPath , shellResourcesPath , specResourcesPath } from "./constants.js" ;
99
10- export const unpackNativeModules = async ( ) : Promise < void > => {
11- if ( ! sea . isSea ( ) ) return ;
10+ const ASSET_PATH_SEP = "____" ;
11+
12+ type AssetType = "native" | "shell" | "spec" ;
13+
14+ const getAssetKeys = ( assetType : AssetType ) => {
15+ if ( ! sea . isSea ( ) ) return [ ] ;
16+
17+ const allKeys = sea . getAssetKeys ( ) ;
18+ switch ( assetType ) {
19+ case "native" :
20+ return allKeys . filter ( ( key ) => ! key . includes ( "shellIntegration" ) && ! key . includes ( "preexec" ) && ! key . endsWith ( ".js" ) ) ;
21+ case "shell" :
22+ return allKeys . filter ( ( key ) => key . includes ( "shellIntegration" ) || key . includes ( "preexec" ) ) ;
23+ case "spec" :
24+ return allKeys . filter ( ( key ) => key . endsWith ( ".js" ) ) ;
25+ default :
26+ return [ ] ;
27+ }
28+ } ;
29+
30+ const getAssetFolder = ( assetType : AssetType ) => {
31+ switch ( assetType ) {
32+ case "native" :
33+ return nativeResourcesPath ;
34+ case "shell" :
35+ return shellResourcesPath ;
36+ case "spec" :
37+ return specResourcesPath ;
38+ default :
39+ return "" ;
40+ }
41+ } ;
1242
13- const assetKeys = sea . getAssetKeys ( ) . filter ( ( key ) => ! key . includes ( "shellIntegration" ) && ! key . includes ( "preexec" ) ) ;
43+ const copyFiles = async ( assetType : AssetType , files : string [ ] , sourceFolder : string ) => {
44+ await Promise . all (
45+ files . map ( async ( file ) => {
46+ const sourcePath = path . join ( sourceFolder , file ) ;
47+ const destPath = path . join ( getAssetFolder ( assetType ) , file ) ;
48+ if ( fs . existsSync ( destPath ) ) return ;
49+ await fsAsync . mkdir ( path . dirname ( destPath ) , { recursive : true } ) ;
50+ await fsAsync . copyFile ( sourcePath , destPath ) ;
51+ } ) ,
52+ ) ;
53+ } ;
1454
55+ const copyAssets = async ( assetType : AssetType ) => {
1556 await Promise . all (
16- assetKeys . map ( async ( assetKey ) => {
17- const assetPath = assetKey == "conpty.dll" || assetKey == "OpenConsole.exe" ? path . join ( "conpty" , assetKey ) : assetKey ;
18- const outputPath = path . join ( nativeResourcesPath , assetPath ) ;
57+ getAssetKeys ( assetType ) . map ( async ( assetKey ) => {
58+ const assetPath = assetKey . replaceAll ( ASSET_PATH_SEP , path . sep ) ;
59+ const outputPath = path . join ( getAssetFolder ( assetType ) , assetPath ) ;
1960 if ( fs . existsSync ( outputPath ) ) return ;
2061 const assetBlob = sea . getRawAsset ( assetKey ) ;
2162 await fsAsync . mkdir ( path . dirname ( outputPath ) , { recursive : true } ) ;
@@ -24,7 +65,13 @@ export const unpackNativeModules = async (): Promise<void> => {
2465 ) ;
2566} ;
2667
27- export const permissionNativeModules = async ( ) : Promise < void > => {
68+ const unpackNativeModules = async ( ) : Promise < void > => {
69+ if ( ! sea . isSea ( ) ) return ;
70+
71+ await copyAssets ( "native" ) ;
72+ } ;
73+
74+ const permissionNativeModules = async ( ) : Promise < void > => {
2875 if ( ! sea . isSea ( ) ) return ;
2976
3077 const spawnHelper = path . join ( nativeResourcesPath , "spawn-helper" ) ;
@@ -33,31 +80,36 @@ export const permissionNativeModules = async (): Promise<void> => {
3380 }
3481} ;
3582
36- export const unpackShellFiles = async ( ) : Promise < void > => {
83+ const unpackSpecs = async ( ) : Promise < void > => {
84+ if ( ! sea . isSea ( ) ) {
85+ const autocompleteSpecFolderPath = path . join ( process . cwd ( ) , "node_modules" , "@withfig" , "autocomplete" , "build" ) ;
86+ const entries = await fsAsync . readdir ( autocompleteSpecFolderPath , { recursive : true } ) ;
87+ const files = entries . filter ( ( f ) => {
88+ const fullPath = path . join ( autocompleteSpecFolderPath , f . toString ( ) ) ;
89+ return fs . statSync ( fullPath ) . isFile ( ) ;
90+ } ) . map ( ( f ) => f . toString ( ) ) ;
91+
92+ await copyFiles ( "spec" , files , autocompleteSpecFolderPath ) ;
93+ }
94+ else {
95+ await copyAssets ( "spec" ) ;
96+ }
97+ } ;
98+
99+ const unpackShellFiles = async ( ) : Promise < void > => {
37100 if ( ! sea . isSea ( ) ) {
38101 const shellFolderPath = path . join ( process . cwd ( ) , "shell" ) ;
39102 const files = ( await fsAsync . readdir ( shellFolderPath ) ) . map ( ( f ) => path . basename ( f ) ) ;
40103
41- await Promise . all (
42- files . map ( async ( file ) => {
43- const sourcePath = path . join ( shellFolderPath , file ) ;
44- const destPath = path . join ( shellResourcesPath , file ) ;
45- if ( fs . existsSync ( destPath ) ) return ;
46- await fsAsync . mkdir ( path . dirname ( destPath ) , { recursive : true } ) ;
47- await fsAsync . copyFile ( sourcePath , destPath ) ;
48- } ) ,
49- ) ;
104+ await copyFiles ( "shell" , files , shellFolderPath ) ;
50105 } else {
51- const assetKeys = sea . getAssetKeys ( ) . filter ( ( key ) => key . includes ( "shellIntegration" ) || key . includes ( "preexec" ) ) ;
52-
53- await Promise . all (
54- assetKeys . map ( async ( assetKey ) => {
55- const outputPath = path . join ( shellResourcesPath , assetKey ) ;
56- if ( fs . existsSync ( outputPath ) ) return ;
57- const assetBlob = sea . getRawAsset ( assetKey ) ;
58- await fsAsync . mkdir ( path . dirname ( outputPath ) , { recursive : true } ) ;
59- await fsAsync . writeFile ( outputPath , Buffer . from ( assetBlob ) ) ;
60- } ) ,
61- ) ;
106+ await copyAssets ( "shell" ) ;
62107 }
63108} ;
109+
110+ export const unpackResources = async ( ) : Promise < void > => {
111+ await unpackNativeModules ( ) ;
112+ await permissionNativeModules ( ) ;
113+ await unpackShellFiles ( ) ;
114+ await unpackSpecs ( ) ;
115+ }
0 commit comments