提交 4a9223c0 编写于 作者: B Benjamin Pasero

files - properly add stream support to create/write

上级 2d401764
......@@ -105,7 +105,7 @@ export interface IFileService {
/**
* Updates the content replacing its previous value.
*/
writeFile(resource: URI, bufferOrReadable: VSBuffer | VSBufferReadable, options?: IWriteFileOptions): Promise<IFileStatWithMetadata>;
writeFile(resource: URI, bufferOrReadableOrStream: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: IWriteFileOptions): Promise<IFileStatWithMetadata>;
/**
* Moves the file/folder to a new path identified by the resource.
......@@ -127,7 +127,7 @@ export interface IFileService {
*
* The optional parameter content can be used as value to fill into the new file.
*/
createFile(resource: URI, bufferOrReadable?: VSBuffer | VSBufferReadable, options?: ICreateFileOptions): Promise<IFileStatWithMetadata>;
createFile(resource: URI, bufferOrReadableOrStream?: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: ICreateFileOptions): Promise<IFileStatWithMetadata>;
/**
* Creates a new folder with the given path. The returned promise
......
......@@ -264,7 +264,7 @@ export class FileService extends Disposable implements IFileService {
//#region File Reading/Writing
async createFile(resource: URI, bufferOrReadable: VSBuffer | VSBufferReadable = VSBuffer.fromString(''), options?: ICreateFileOptions): Promise<IFileStatWithMetadata> {
async createFile(resource: URI, bufferOrReadableOrStream: VSBuffer | VSBufferReadable | VSBufferReadableStream = VSBuffer.fromString(''), options?: ICreateFileOptions): Promise<IFileStatWithMetadata> {
// validate overwrite
const overwrite = !!(options && options.overwrite);
......@@ -273,7 +273,7 @@ export class FileService extends Disposable implements IFileService {
}
// do write into file (this will create it too)
const fileStat = await this.writeFile(resource, bufferOrReadable);
const fileStat = await this.writeFile(resource, bufferOrReadableOrStream);
// events
this._onAfterOperation.fire(new FileOperationEvent(resource, FileOperation.CREATE, fileStat));
......
......@@ -20,7 +20,7 @@ import { NullLogService } from 'vs/platform/log/common/log';
import { isLinux, isWindows } from 'vs/base/common/platform';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { isEqual } from 'vs/base/common/resources';
import { VSBuffer, VSBufferReadable, writeableBufferStream, VSBufferReadableStream } from 'vs/base/common/buffer';
import { VSBuffer, VSBufferReadable, writeableBufferStream, VSBufferReadableStream, bufferToReadable, bufferToStream } from 'vs/base/common/buffer';
function getByName(root: IFileStat, name: string): IFileStat | null {
if (root.children === undefined) {
......@@ -1336,12 +1336,24 @@ suite('Disk File Service', () => {
});
test('createFile', async () => {
assertCreateFile(contents => VSBuffer.fromString(contents));
});
test('createFile (readable)', async () => {
assertCreateFile(contents => bufferToReadable(VSBuffer.fromString(contents)));
});
test('createFile (stream)', async () => {
assertCreateFile(contents => bufferToStream(VSBuffer.fromString(contents)));
});
async function assertCreateFile(converter: (content: string) => VSBuffer | VSBufferReadable | VSBufferReadableStream): Promise<void> {
let event: FileOperationEvent;
disposables.add(service.onAfterOperation(e => event = e));
const contents = 'Hello World';
const resource = URI.file(join(testDir, 'test.txt'));
const fileStat = await service.createFile(resource, VSBuffer.fromString(contents));
const fileStat = await service.createFile(resource, converter(contents));
assert.equal(fileStat.name, 'test.txt');
assert.equal(existsSync(fileStat.resource.fsPath), true);
assert.equal(readFileSync(fileStat.resource.fsPath), contents);
......@@ -1350,7 +1362,7 @@ suite('Disk File Service', () => {
assert.equal(event!.resource.fsPath, resource.fsPath);
assert.equal(event!.operation, FileOperation.CREATE);
assert.equal(event!.target!.resource.fsPath, resource.fsPath);
});
}
test('createFile (does not overwrite by default)', async () => {
const contents = 'Hello World';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册