Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

369
Visualizações
How to get unknown file path - the extension path - using Javascript?

I am trying to develop an extension for VSCode and I need to search for the exact file path where my extension is saved. I've tried this code:

var text;

const directoryPath = path.join(Os.homedir(), '.vscode/extensions');
fs.readdir(directoryPath, function (err, files) {
    if (err) {
        y2.appendLine('Unable to scan directory: ' + err);
    }
    for (let i=0;i<files.length;i++){
        if (files[i].startsWith("MY_EXTENSION")){
            text = files[i];
            y2.appendLine(text);
            break;
        }
    }
});
y2.appendLine(text);

y2 is only an output channel I added. The outcome of this code is:

undefined
MY_EXTENSION

If I take out the second y2.appendLine(text) I receive only MY_EXTENSION but I don't get how to make text global or how to modify the value of the global variable text inside readdir.

I also don't understand why it first executes y2.appendLine(text) and then readdir.

It is really important and I would be grateful to have your help!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I'm not sure what exactly you are trying to do there, but looking at your code it seems like you are trying to save some data for your extension in particular.

For this purpose, you probably want to use the globalStorage object instead.

You can learn more about it in the Data storage section and the ExtensionContext documentation.

about 4 years ago · Juan Pablo Isaza Relatório

0

For the path to your extension, use the context object and its properties:

extensionPath

The absolute file path of the directory containing the extension. Shorthand notation for ExtensionContext.extensionUri.fsPath (independent of the uri scheme).

extensionUri

The uri of the directory containing the extension.

From ExtensionContext. If it is just for the purpose of storage, see @mausworks's answer about globalStorage or storagePath, ets.

about 4 years ago · Juan Pablo Isaza Relatório

0

You could put it in a function like this:

function searchExt() {
    const directoryPath = path.join(Os.homedir(), '.vscode/extensions');
    fs.readdir(directoryPath, (err, files) => {
        let text = null;
        if (err) {
            y2.appendLine(`Unable to scan directory: ${err}`);
        } else {
            for (let i = 0; i < files.length; i++) {
                if (files[i].startsWith('MY_EXTENSION')) {
                    text = files[i];
                    break;
                }
            }
        }

        return text;
    });
}

const searchResult = searchExt();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda