use vscode files API instead
Opened this issue · 1 comments
0mkara commented
Krish-bhardwaj commented
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.sol
would change tovscode.URI
typefile://Users\...\hardhat\contracts\greeter.sol
so initiall we woild want to change path type tovscode.URI
- All the function that are available in
fs
module are not exactly available in vscodeFile System API
thus we would have to rewrite the function where ever there is a use offs
module 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
fs
module 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 API
it 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
fs
module 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
fs
module - If we plan to migrate or standardise vscode
File System API
for file related operation in ethcode we should make sure that further whatever development are should be made in vscodeFile System API
and gradually migrate previous code base to the same
- We are not sure that how much role does