import fastGlob from "fast-glob" ; import * as fs from "fs" ; import minimatch from "minimatch" ; import mkdirp from "mkdirp" ; import * as os from "os" ; import * as path from "path" ; import { Runtime , RuntimeFileSystem , RuntimePath } from "./Runtime" ; export class NodeRuntime implements Runtime { fs = new NodeRuntimeFileSystem ( ) ; path = new NodeRuntimePath ( ) ; getEnvVar ( name ) { return process ?. env [ name ] ; } getEndOfLine ( ) { return os . EOL ; } getPathMatchesPattern ( path , pattern ) { return minimatch ( path , pattern ) ; } } class NodeRuntimePath implements RuntimePath { join ( ... paths ) { return path . join ( ... paths ) ; } normalize ( pathToNormalize ) { return path . normalize ( pathToNormalize ) ; } relative ( from , to ) { return path . relative ( from , to ) ; } } class NodeRuntimeFileSystem implements RuntimeFileSystem { delete ( path ) { return new Promise < void > ( ( resolve , reject ) => { fs . unlink ( path , err => { if ( err ) reject ( err ) ; else resolve ( ) ; } ) ; } ) ; } deleteSync ( path ) { fs . unlinkSync ( path ) ; } readDirSync ( dirPath ) { const entries = fs . readdirSync ( dirPath , { withFileTypes : true , } ) ; return entries . map ( e => ( { name : e . name , isFile : e . isFile ( ) , isDirectory : e . isDirectory ( ) , isSymlink : e . isSymbolicLink ( ) , } ) ) ; } readFile ( filePath , encoding = "utf-8" ) { return new Promise < string > ( ( resolve , reject ) => { fs . readFile ( filePath , encoding as any , ( err , data ) => { if ( err ) reject ( err ) ; else resolve ( data as any as string ) ; } ) ; } ) ; } readFileSync ( filePath , encoding = "utf-8" ) { return fs . readFileSync ( filePath , encoding as "utf-8" ) ; } async writeFile ( filePath , fileText ) { await new Promise < void > ( ( resolve , reject ) => { fs . writeFile ( filePath , fileText , err => { if ( err ) reject ( err ) ; else resolve ( ) ; } ) ; } ) ; } writeFileSync ( filePath , fileText ) { fs . writeFileSync ( filePath , fileText ) ; } async mkdir ( dirPath ) { await mkdirp ( dirPath ) ; } mkdirSync ( dirPath ) { mkdirp . sync ( dirPath ) ; } move ( srcPath , destPath ) { return new Promise < void > ( ( resolve , reject ) => { fs . rename ( srcPath , destPath , err => { if ( err ) reject ( err ) ; else resolve ( ) ; } ) ; } ) ; } moveSync ( srcPath , destPath ) { fs . renameSync ( srcPath , destPath ) ; } copy ( srcPath , destPath ) { return new Promise < void > ( ( resolve , reject ) => { fs . copyFile ( srcPath , destPath , err => { if ( err ) reject ( err ) ; else resolve ( ) ; } ) ; } ) ; } copySync ( srcPath , destPath ) { fs . copyFileSync ( srcPath , destPath ) ; } fileExists ( filePath ) { return new Promise < boolean > ( resolve => { fs . stat ( filePath , ( err , stat ) => { if ( err ) resolve ( false ) ; else resolve ( stat . isFile ( ) ) ; } ) ; } ) ; } fileExistsSync ( filePath ) { try { return fs . statSync ( filePath ) . isFile ( ) ; } catch ( err ) { return false ; } } directoryExists ( dirPath ) { return new Promise < boolean > ( resolve => { fs . stat ( dirPath , ( err , stat ) => { if ( err ) resolve ( false ) ; else resolve ( stat . isDirectory ( ) ) ; } ) ; } ) ; } directoryExistsSync ( dirPath ) { try { return fs . statSync ( dirPath ) . isDirectory ( ) ; } catch ( err ) { return false ; } } realpathSync ( path ) { return fs . realpathSync ( path ) ; } getCurrentDirectory ( ) { return path . resolve ( ) ; } glob ( patterns ) { return fastGlob ( patterns as string [ ] , { cwd : this . getCurrentDirectory ( ) , absolute : true , } ) ; } globSync ( patterns ) { return fastGlob . sync ( patterns as string [ ] , { cwd : this . getCurrentDirectory ( ) , absolute : true , } ) ; } isCaseSensitive ( ) { const platform = process ?. platform ; return platform !== "win32" && platform !== "darwin" ; } }