tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sys.ts (1150B)


      1 /* eslint-disable no-process-exit, n/no-process-exit */
      2 /* eslint-disable @typescript-eslint/no-namespace */
      3 
      4 function node() {
      5  /* eslint-disable-next-line n/no-restricted-require */
      6  const { readFile, existsSync } = require('fs');
      7 
      8  return {
      9    type: 'node',
     10    readFile,
     11    existsSync,
     12    args: process.argv.slice(2),
     13    cwd: () => process.cwd(),
     14    exit: (code?: number | undefined) => process.exit(code),
     15  };
     16 }
     17 
     18 declare global {
     19  namespace Deno {
     20    function readFile(
     21      path: string,
     22      callback?: (error: unknown, data: string) => void
     23    ): Promise<Uint8Array>;
     24    function readFileSync(path: string): Uint8Array;
     25    const args: string[];
     26    const cwd: () => string;
     27    function exit(code?: number): never;
     28  }
     29 }
     30 
     31 function deno() {
     32  function existsSync(path: string) {
     33    try {
     34      Deno.readFileSync(path);
     35      return true;
     36    } catch (err) {
     37      return false;
     38    }
     39  }
     40 
     41  return {
     42    type: 'deno',
     43    existsSync,
     44    readFile: Deno.readFile,
     45    args: Deno.args,
     46    cwd: Deno.cwd,
     47    exit: Deno.exit,
     48  };
     49 }
     50 
     51 const sys = typeof globalThis.process !== 'undefined' ? node() : deno();
     52 
     53 export default sys;