ethcode
ethcode copied to clipboard
use vscode files API instead
we have been using native fs in multiple places like -
We should be using vscode file system API instead whereever possible.
To migrate from node fs module to vscode File System API we will have to change following things
Documentation
- Structure change
- initially we are using the normal file system used in os for eg . Path type :
C:\Users\...\hardhat\contracts\greeter.solwould change tovscode.URItypefile://Users\...\hardhat\contracts\greeter.solso initiall we woild want to change path type tovscode.URI - All the function that are available in
fsmodule are not exactly available in vscodeFile System APIthus we would have to rewrite the function where ever there is a use offsmodule inethcode, but as per my observation we have all the required api for our current need - We can observe changes in code base by following examples
- if we read a file using normal
fsmodule it would work like this
const getDeployedInputs: any = (context: vscode.ExtensionContext) => { try { const contract = context.workspaceState.get( 'contract' ) as CompiledJSONOutput const fullPath = getDeployedFullPath(contract) const inputs = fs.readFileSync(fullPath).toString() return JSON.parse(inputs) } catch (e) { return undefined } }- if we read a file using vscode
File System APIit would work like this
const getFunctionInputFile: any = async ( context: ExtensionContext, name: string ): Promise<object | undefined> => { try { const contracts = context.workspaceState.get('contracts') as Record<string, CompiledJSONOutput> if (contracts === undefined || Object.keys(contracts).length === 0) return const contract = Object.values(contracts).find(contract => contract.name === name) if (contract != null) { const link = getFunctionInputFullPath(contract) const linkchnage = link.replace(/\\/g, '/') const parsedUrl = new URL(linkchnage) const fileUrl: vscode.Uri = vscode.Uri.parse(`file://${parsedUrl.pathname}`) const contents: Uint8Array = await vscode.workspace.fs.readFile(fileUrl) const decoder = new TextDecoder() const jsonString = decoder.decode(contents) const json = JSON.parse(jsonString) return json } } catch (error) { console.log(error) } } - if we read a file using normal
- initially we are using the normal file system used in os for eg . Path type :
- Output change
- We are not sure that how much role does
fsmodule play in terms of performance and how better is it going to be if we use vscodeFile System API - Right now ethcode extension is working fine with this
fsmodule - If we plan to migrate or standardise vscode
File System APIfor file related operation in ethcode we should make sure that further whatever development are should be made in vscodeFile System APIand gradually migrate previous code base to the same
- We are not sure that how much role does