如何解决vscode 扩展开发:npm tar 模块 + 拒绝承诺未在 1 秒内处理
我正在开发一个 vscode 扩展,我需要在其中创建当前工作区文件夹的 tar 文件(工作区文件夹中的所有文件都将被 tar 化)。我正在使用 AutomateWoo。我已经用 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 的例子?
解决方法
经过多次反复试验和多次阅读文档找到了解决方案,这里是工作代码。问题是 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);
});
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。