1313 * This module provides symbol resolution and metadata extraction.
1414 */
1515
16- import * as fs from 'fs' ;
17- import * as path from 'path' ;
18- import AdmZip from 'adm-zip' ;
16+ import { getFs , getPath } from '../../utils/io' ;
1917
2018/**
2119 * Default Grid 3 installation paths by platform
@@ -109,6 +107,38 @@ export interface SymbolResolutionResult {
109107 */
110108export const DEFAULT_LOCALE = 'en-GB' ;
111109
110+ function getNodeFs ( ) : typeof import ( 'fs' ) {
111+ try {
112+ return getFs ( ) ;
113+ } catch {
114+ throw new Error ( 'Symbol library access is not available in this environment.' ) ;
115+ }
116+ }
117+
118+ function getNodePath ( ) : typeof import ( 'path' ) {
119+ try {
120+ return getPath ( ) ;
121+ } catch {
122+ throw new Error ( 'Path utilities are not available in this environment.' ) ;
123+ }
124+ }
125+
126+ let cachedAdmZip : typeof import ( 'adm-zip' ) | null = null ;
127+ function getAdmZip ( ) : typeof import ( 'adm-zip' ) {
128+ if ( cachedAdmZip ) return cachedAdmZip ;
129+ try {
130+ // eslint-disable-next-line @typescript-eslint/no-var-requires
131+ const module = require ( 'adm-zip' ) as typeof import ( 'adm-zip' ) & {
132+ default ?: typeof import ( 'adm-zip' ) ;
133+ } ;
134+ const resolved = module . default || module ;
135+ cachedAdmZip = resolved ;
136+ return resolved ;
137+ } catch {
138+ throw new Error ( 'Symbol library access requires AdmZip in this environment.' ) ;
139+ }
140+ }
141+
112142/**
113143 * Parse a symbol reference string
114144 * @param reference - Symbol reference like "[widgit]/food/apple.png"
@@ -153,26 +183,33 @@ export function isSymbolReference(reference: string): boolean {
153183 * @returns Default Grid 3 path or empty string if not found
154184 */
155185export function getDefaultGrid3Path ( ) : string {
156- const platform = process . platform as keyof typeof DEFAULT_GRID3_PATHS ;
186+ const platform = (
187+ typeof process !== 'undefined' && process . platform ? process . platform : 'unknown'
188+ ) as keyof typeof DEFAULT_GRID3_PATHS ;
157189 const defaultPath = DEFAULT_GRID3_PATHS [ platform ] || '' ;
158190
159- if ( defaultPath && fs . existsSync ( defaultPath ) ) {
160- return defaultPath ;
161- }
162-
163- // Try to find Grid 3 in common locations
164- const commonPaths = [
165- 'C:\\Program Files (x86)\\Smartbox\\Grid 3' ,
166- 'C:\\Program Files\\Smartbox\\Grid 3' ,
167- 'C:\\Program Files\\Smartbox\\Grid 3' ,
168- '/Applications/Grid 3.app' ,
169- '/opt/smartbox/grid3' ,
170- ] ;
191+ try {
192+ const fs = getNodeFs ( ) ;
193+ if ( defaultPath && fs . existsSync ( defaultPath ) ) {
194+ return defaultPath ;
195+ }
171196
172- for ( const testPath of commonPaths ) {
173- if ( fs . existsSync ( testPath ) ) {
174- return testPath ;
197+ // Try to find Grid 3 in common locations
198+ const commonPaths = [
199+ 'C:\\Program Files (x86)\\Smartbox\\Grid 3' ,
200+ 'C:\\Program Files\\Smartbox\\Grid 3' ,
201+ 'C:\\Program Files\\Smartbox\\Grid 3' ,
202+ '/Applications/Grid 3.app' ,
203+ '/opt/smartbox/grid3' ,
204+ ] ;
205+
206+ for ( const testPath of commonPaths ) {
207+ if ( fs . existsSync ( testPath ) ) {
208+ return testPath ;
209+ }
175210 }
211+ } catch {
212+ return '' ;
176213 }
177214
178215 return '' ;
@@ -185,6 +222,7 @@ export function getDefaultGrid3Path(): string {
185222 * @returns Path to Symbol Libraries directory (e.g., "C:\...\Grid 3\Resources\Symbols")
186223 */
187224export function getSymbolLibrariesDir ( grid3Path : string ) : string {
225+ const path = getNodePath ( ) ;
188226 return path . join ( grid3Path , SYMBOLS_SUBDIR ) ;
189227}
190228
@@ -199,6 +237,7 @@ export function getSymbolSearchIndexesDir(
199237 grid3Path : string ,
200238 locale : string = DEFAULT_LOCALE
201239) : string {
240+ const path = getNodePath ( ) ;
202241 return path . join ( grid3Path , SYMBOLSEARCH_SUBDIR , locale , 'symbolsearch' ) ;
203242}
204243
@@ -218,6 +257,7 @@ export function getAvailableSymbolLibraries(
218257
219258 const symbolsDir = getSymbolLibrariesDir ( grid3Path ) ;
220259
260+ const fs = getNodeFs ( ) ;
221261 if ( ! fs . existsSync ( symbolsDir ) ) {
222262 return [ ] ;
223263 }
@@ -227,6 +267,7 @@ export function getAvailableSymbolLibraries(
227267
228268 for ( const file of files ) {
229269 if ( file . endsWith ( '.symbols' ) ) {
270+ const path = getNodePath ( ) ;
230271 const fullPath = path . join ( symbolsDir , file ) ;
231272 const stats = fs . statSync ( fullPath ) ;
232273 const libraryName = path . basename ( file , '.symbols' ) ;
@@ -271,7 +312,9 @@ export function getSymbolLibraryInfo(
271312 ] ;
272313
273314 for ( const file of variations ) {
315+ const path = getNodePath ( ) ;
274316 const fullPath = path . join ( symbolsDir , file ) ;
317+ const fs = getNodeFs ( ) ;
275318 if ( fs . existsSync ( fullPath ) ) {
276319 const stats = fs . statSync ( fullPath ) ;
277320 return {
@@ -329,6 +372,7 @@ export function resolveSymbolReference(
329372
330373 try {
331374 // .symbols files are ZIP archives
375+ const AdmZip = getAdmZip ( ) ;
332376 const zip = new AdmZip ( libraryInfo . pixFile ) ;
333377
334378 // The path in the symbol reference becomes the path within the symbols/ folder
@@ -527,7 +571,8 @@ export function analyzeSymbolUsage(tree: any): SymbolUsageStats {
527571 */
528572export function symbolReferenceToFilename ( reference : string , cellX : number , cellY : number ) : string {
529573 const parsed = parseSymbolReference ( reference ) ;
530- const ext = path . extname ( parsed . path ) || '.png' ;
574+ const dotIndex = parsed . path . lastIndexOf ( '.' ) ;
575+ const ext = dotIndex >= 0 ? parsed . path . slice ( dotIndex ) : '.png' ;
531576
532577 // Grid 3 format: {x}-{y}-0-text-0.{ext}
533578 return `${ cellX } -${ cellY } -0-text-0${ ext } ` ;
0 commit comments