Skip to content

Commit

Permalink
'fs' import fix (#9941)
Browse files Browse the repository at this point in the history
* switch imports of 'fs' module to be either sync require statements (for sync functions) or async imports (for async functions)

This should fix the 'fs' is not a module issue that comes up when using contractkit in a Create React App

There are still some fs imports but they are all in scripts so i believe they can be left

* move 'net' import to its local function so its not called when that isnt used, (should fix module not found issues)
  • Loading branch information
aaronmgdr authored Oct 12, 2022
1 parent 38a8511 commit 4c615ac
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/contractkit/src/identity/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { AddressType, SignatureType } from '@celo/utils/lib/io'
import { guessSigner, verifySignature } from '@celo/utils/lib/signatureUtils'
import fetch from 'cross-fetch'
import { isLeft } from 'fp-ts/lib/Either'
import { readFileSync } from 'fs'
import * as t from 'io-ts'
import { PathReporter } from 'io-ts/lib/PathReporter'
import { ContractKit } from '../kit'
Expand Down Expand Up @@ -62,6 +61,7 @@ export class IdentityMetadataWrapper {
}

static fromFile(contractKitOrAccountsWrapper: KitOrAccountsWrapper, path: string) {
const { readFileSync } = require('fs')
return this.fromRawString(contractKitOrAccountsWrapper, readFileSync(path, 'utf-8'))
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/contractkit/src/setupForKits.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import net from 'net'
import Web3 from 'web3'
import { HttpProviderOptions as Web3HttpProviderOptions } from 'web3-providers-http'
export type HttpProviderOptions = Web3HttpProviderOptions
Expand All @@ -25,6 +24,7 @@ export function ensureCurrentProvider(web3: Web3) {
export function getWeb3ForKit(url: string, options: Web3HttpProviderOptions | undefined) {
let web3: Web3
if (url.endsWith('.ipc')) {
const net = require('net')
web3 = new Web3(new Web3.providers.IpcProvider(url, net))
} else if (url.toLowerCase().startsWith('http')) {
web3 = new Web3(new Web3.providers.HttpProvider(url, options))
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/identity/src/offchain/storage-writers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { spawnSync } from 'child_process'
import * as fs from 'fs'
import { join, parse } from 'path'
import { resolvePath } from './utils'

Expand All @@ -16,9 +15,10 @@ export class LocalStorageWriter extends StorageWriter {
}

protected async writeToFs(data: string | Buffer, dataPath: string): Promise<void> {
const { promises } = await import('fs')
const directory = parse(dataPath).dir
await fs.promises.mkdir(join(this.root, directory), { recursive: true })
await fs.promises.writeFile(join(this.root, dataPath), data)
await promises.mkdir(join(this.root, directory), { recursive: true })
await promises.writeFile(join(this.root, dataPath), data)
}
}

Expand Down

0 comments on commit 4c615ac

Please sign in to comment.