提交 ded36ac6 编写于 作者: J Johannes Rieken

add mkdirp-option and honor it in the remote file service, #48062

上级 fc2609a5
......@@ -576,6 +576,11 @@ export interface IUpdateContentOptions {
* The etag of the file. This can be used to prevent dirty writes.
*/
etag?: string;
/**
* Run mkdirp before saving.
*/
mkdirp?: boolean;
}
export interface IResolveFileOptions {
......
......@@ -305,10 +305,15 @@ export class RemoteFileService extends FileService {
}
}
updateContent(resource: URI, value: string | ITextSnapshot, options?: IUpdateContentOptions): TPromise<IFileStat> {
async updateContent(resource: URI, value: string | ITextSnapshot, options?: IUpdateContentOptions): TPromise<IFileStat> {
if (resource.scheme === Schemas.file) {
return super.updateContent(resource, value, options);
} else {
if (options && options.mkdirp) {
// use the lack of options and or the lack of an etag as a hint that
// the parent directories might not exist.
await this._mkdirp(resource.with({ path: posix.dirname(resource.path) }));
}
return this._withProvider(resource).then(provider => {
const snapshot = typeof value === 'string' ? new StringSnapshot(value) : value;
return this._writeFile(provider, resource, snapshot, options || {}, FileOpenFlags.Write);
......@@ -346,6 +351,27 @@ export class RemoteFileService extends FileService {
});
}
private async _mkdirp(directory: URI): Promise<void> {
let basenames: string[] = [];
while (directory.path !== '/') {
try {
let stat = await this.resolveFile(directory);
if (!stat.isDirectory) {
throw new Error(`${directory.toString()} is not a directory`);
}
} catch (e) {
// ENOENT
basenames.push(posix.basename(directory.path));
directory = directory.with({ path: posix.dirname(directory.path) });
}
break;
}
for (let i = basenames.length - 1; i >= 0; i--) {
directory = directory.with({ path: posix.join(directory.path, basenames[i]) });
await this.createFolder(directory);
}
}
// --- delete
del(resource: URI, useTrash?: boolean): TPromise<void> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册