【问题标题】:vscode extension development: npm tar module + rejected promise not handled within 1 secondvscode 扩展开发:npm tar 模块 + 拒绝承诺未在 1 秒内处理
【发布时间】:2021-02-11 11:27:39
【问题描述】:

我正在开发一个 vscode 扩展,我需要在其中创建当前工作区文件夹的 tar 文件(工作区文件夹中的所有文件都将被 tar 化)。我正在使用npm tar module。我已经用 vscode 注册了命令,当我运行它并点击命令时,我得到以下错误。

rejected promise not handled within 1 second: Error: EROFS: read-only file system, open 'mysampletar.tar' 
stack trace: Error: EROFS: read-only file system, open 'mysampletar.tar'

在命令执行时运行的代码如下:

export function tarit(): void {
    let workspaceFolder = vscode.workspace.workspaceFolders;
    let path: string;
    if (!workspaceFolder) {
        path = vscode.workspace.rootPath || '';
    } else {
        let root: vscode.WorkspaceFolder;
        if (workspaceFolder.length === 1) {
            root = workspaceFolder[0];
            path = root.uri.fsPath;

            console.log('path', path); // logs /Users/vishwakumar/vscode-ext/sample-tar

            let tarz = require('tar');
            tarz.c({
                gzip: true,
                file: 'mysampletar.tar',
                onwarn: (code: any, message: any, data: any) => {
                    console.log('code', code);
                    console.log('message', message);
                    console.log('data', data);
                }
            },
                ['/Users/vishwakumar/vscode-ext/sample-tar/dummy.txt']
            ).then(() => {
                vscode.window.showInformationMessage('tar file generated');
            }).then((undefined: undefined, err: any) => {
                console.log('I am error', err);
             });
        }
    }
}

可能是什么问题?我想它正在尝试读取 tar 文件,我们如何停止读取以及在哪里可以看到创建的 tar?

谁能给我指点 npm tar 的例子?

【问题讨论】:

    标签: node.js npm visual-studio-code tar


    【解决方案1】:

    经过多次试验和错误并多次阅读文档找到了解决方案,这里是工作代码。问题是 cwd 选项默认设置为 process.cwd(),您需要将其设置为您要存档的文件夹或文件所在的目录

    export function tarit(): void {
            let workspaceFolder = vscode.workspace.workspaceFolders;
            let path: string;
            if (!workspaceFolder) {
                path = vscode.workspace.rootPath || '';
            } else {
                let root: vscode.WorkspaceFolder;
                if (workspaceFolder.length === 1) {
                    root = workspaceFolder[0];
                    path = root.uri.fsPath;
        
                    console.log('path', path); // logs /Users/vishwakumar/vscode-ext/sample-tar
        
                    let tarz = require('tar');
                    tarz.c({
                        gzip: true,
                        file: '/Users/vishwakumar/vscode-ext/sample-tar/mysampletar.tar', // this is the location where the tar file will be generated
                        cwd: '/Users/vishwakumar/vscode-ext/', // set current working directory to parent directory 
                        onwarn: (code: any, message: any, data: any) => {
                            console.log('code', code);
                            console.log('message', message);
                            console.log('data', data);
                        }
                    },
                        ['sample-tar'], // set file list to directory which needs to be archived, this will tar all the files and folders inside recursively, 
                        () => {}        // third parameter is a empty callback, which will be called after tar completion
                    ).then(() => {
                        vscode.window.showInformationMessage('tar file generated');
                    }).then((undefined: undefined, err: any) => {
                        console.log('I am error', err);
                     });
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 2019-07-08
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多