English 中文(简体)
VS Code extension: How to convert workspace folder uri path to filesystem path?
原标题:

I have a Visual Studio Code extension I m writing that uses the workspace folder path (File > Open Folder) to determine the base directory to run shell commands from. I can get the path from vscode.workspace.workspaceFolders[0].uri.path, but it causes problems when I use it with fs.readdir() because it s repeating the drive letter at the beginning.

Error: ENOENT: no such file or directory, scandir  C:c:UsersDaveDesktopESP Labs 

Notice the extra C:

I would like to find a way to convert the URI path to a suitable filesystem path that will work with fs.readdir(). I tried url.fileURLToPath() but it didn t work and actually caused the extension to stop functioning until I commented out the console.debug line where I tried to display the result.

There s also a property called vscode.workspace.workspaceFolders[0].uri._fspath This does what I want, but I m thinking the underscore indicates the property is private and should not be used directly.

So my question is this...

Is there a method to convert uri.path to a filesystem path or should I just forget about it and use uri._fspath?

The code:

/*
 *  Copy entire project directory to remote flash file system. 
 */
let syncCommand = vscode.commands.registerCommand( mpremote.sync , async () => {
    if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length != 1) {
        vscode.window.showErrorMessage( Unable to sync. Open a folder first. )
    }
    else {
        console.debug( vscode.workspace.workspaceFolders[0] , vscode.workspace.workspaceFolders[0])
        console.debug( uri._fsPath: , vscode.workspace.workspaceFolders[0].uri._fsPath)
        //console.debug( url.fileURLToPath: , url.fileURLToPath(vscode.workspace.workspaceFolders[0].uri.path))
        let projectRoot = vscode.workspace.workspaceFolders[0].uri._fsPath
        console.debug( Project folder path: , projectRoot)
        let port = await getDevicePort()
        term.sendText(`cd  ${projectRoot} `)
        fs.readdir(projectRoot, { withFileTypes: true }, (err, entries) => {
            if (err) {
                console.error(err)
                vscode.window.showErrorMessage( Unable to read directory. )
            }
            else {
                console.debug( Directory entries found: , entries.length)
                entries.forEach(entry => {
                    console.debug( Examining directory entry: , entry)
                    if (entry.isDirectory()) {
                        if (SYNC_IGNORE.includes(entry.name)) {
                            console.debug( Skipping directory: , entry.name)
                        }
                        else {
                            term.sendText(`${PYTHON_BIN} -m mpremote connect ${port} fs cp -r ${entry.name} :`)
                        }
                    }
                    else {
                        term.sendText(`${PYTHON_BIN} -m mpremote connect ${port} fs cp ${entry.name} :`)
                    }
                })
            }
        })
    }   
})

Debug output showing available uri properties:

vscode.workspace.workspaceFolders[0] {uri: v, name:  ESP Labs , index: 0}
vscode.workspace.workspaceFolders[0] {
  uri: v {
    scheme:  file ,
    authority:   ,
    path:  /c:/Users/Dave/Desktop/ESP Labs ,
    query:   ,
    fragment:   ,
    _formatted:  file:///c%3A/Users/Dave/Desktop/ESP%20Labs ,
    _fsPath:  c:\Users\Dave\Desktop\ESP Labs 
  },
  name:  ESP Labs ,
  index: 0
}
uri._fsPath: c:UsersDaveDesktopESP Labs
Project folder path: c:UsersDaveDesktopESP Labs
最佳回答

Well, there is a vscode.workspace.workspaceFolders[0].uri.fsPath property you could use - note no underscore. It returns the same thing as the _fsPath property.

See vscode api docs: Uri.

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签