Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@
},
"homepage": "https://github.com/mcmath/gulp-transform#readme",
"dependencies": {
"@types/node": "*",
"@types/gulp-util": "*",
"gulp-util": "^3.0.8",
"tslib": "^1.7.1"
"plugin-error": "^1.0.1",
"tslib": "^1.7.1",
"vinyl": "^2.1.0"
},
"devDependencies": {
"@types/chai": "^4.0.1",
"@types/chai-as-promised": "0.0.31",
"@types/lodash": "^4.14.71",
"@types/mocha": "^2.2.41",
"@types/node": "*",
"@types/sinon": "^2.3.3",
"@types/sinon-chai": "^2.7.28",
"@types/vinyl": "^2.0.2",
"chai": "^4.1.0",
"chai-as-promised": "^7.1.1",
"coveralls": "^2.13.1",
Expand All @@ -58,6 +59,6 @@
"sinon-chai": "^2.12.0",
"ts-node": "^3.3.0",
"tslint": "^5.5.0",
"typescript": "^2.4.2"
"typescript": "~2.4.2"
}
}
22 changes: 11 additions & 11 deletions spec/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { File } from "gulp-util";
import Vinyl = require("vinyl");
import { Readable, Transform } from "stream";

/**
* A Vinyl File in buffer mode.
*/
export interface BufferFile extends File {
export interface BufferFile extends Vinyl {
contents: Buffer;
}

/**
* A Vinyl File in streaming mode.
*/
export interface StreamFile extends File {
export interface StreamFile extends Vinyl {
contents: NodeJS.ReadableStream;
}

/**
* A Vinyl File whose contents are null.
*/
export interface NullFile extends File {
export interface NullFile extends Vinyl {
contents: null;
}

Expand All @@ -35,7 +35,7 @@ export function createBufferFile(content: string, encoding: string): BufferFile;
export function createBufferFile(content: number[], encoding?: null): BufferFile;
export function createBufferFile(content: Buffer, encoding?: null): BufferFile;
export function createBufferFile(content: string | number[] | Buffer, encoding?: string | null): BufferFile {
return new File({
return new Vinyl({
cwd: "/root/src",
path: "/root/src/name.txt",
contents: toBuffer(content as any, encoding as any)
Expand All @@ -55,7 +55,7 @@ export function createStreamFile(content: string, encoding: string): StreamFile;
export function createStreamFile(content: number[], encoding?: null): StreamFile;
export function createStreamFile(content: Buffer, encoding?: null): StreamFile;
export function createStreamFile(content: string | number[] | Buffer, encoding?: string | null): StreamFile {
return new File({
return new Vinyl({
cwd: "/root/src",
path: "/root/src/name.txt",
contents: new Readable({
Expand All @@ -71,7 +71,7 @@ export function createStreamFile(content: string | number[] | Buffer, encoding?:
* Creates a Vinyl File object whose contents are null.
*/
export function createNullFile(): NullFile {
return new File({
return new Vinyl({
cwd: "/root/src",
path: "/root/src/name.txt",
contents: null
Expand Down Expand Up @@ -121,7 +121,7 @@ export async function read(stream: NodeJS.ReadableStream): Promise<File> {
* @param stream The object-mode stream to write to.
* @return Returns a promise that resolve to the stream itself.
*/
export async function write<T extends NodeJS.WritableStream>(file: File, stream: T): Promise<T> {
export async function write<T extends NodeJS.WritableStream>(file: Vinyl, stream: T): Promise<T> {
stream.write(file as any);
stream.end();

Expand All @@ -140,9 +140,9 @@ export async function write<T extends NodeJS.WritableStream>(file: File, stream:
* Defaults to null.
* @return Returns a promise with the contents of the file.
*/
export async function readFile(file: File, encoding: string): Promise<string>;
export async function readFile(file: File, encoding?: null): Promise<Buffer>;
export async function readFile(file: File, encoding?: string | null): Promise<string | Buffer> {
export async function readFile(file: Vinyl, encoding: string): Promise<string>;
export async function readFile(file: Vinyl, encoding?: null): Promise<Buffer>;
export async function readFile(file: Vinyl, encoding?: string | null): Promise<string | Buffer> {
if (file.isBuffer())
return encoding ? file.contents.toString(encoding) : file.contents;
if (!file.isStream())
Expand Down
11 changes: 6 additions & 5 deletions spec/mode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { expect } from "chai";
import { File, PluginError } from "gulp-util";
import PluginError = require("plugin-error");
import Vinyl = require("vinyl");
import { identity, noop } from "lodash";
import { createBufferFile, createStreamFile, createNullFile, toBuffer, write, read, readFile } from "./helpers";
import transform = require("../src");

describe("content mode", () => {

describe("buffer", () => {
let file1: File;
let file2: File;
let file1: Vinyl;
let file2: Vinyl;

beforeEach(() => {
file1 = createBufferFile([0xCF, 0x80]);
Expand Down Expand Up @@ -38,8 +39,8 @@ describe("content mode", () => {
});

describe("stream", () => {
let file1: File;
let file2: File;
let file1: Vinyl;
let file2: Vinyl;

beforeEach(() => {
file1 = createStreamFile([0xCF, 0x80]);
Expand Down
7 changes: 4 additions & 3 deletions spec/options.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from "chai";
import { File, PluginError } from "gulp-util";
import PluginError = require("plugin-error");
import Vinyl = require("vinyl");
import { identity } from "lodash";
import { SinonSpy, spy } from "sinon";
import { createBufferFile, toBuffer, write } from "./helpers";
Expand All @@ -8,7 +9,7 @@ import transform = require("../src");
describe("options", () => {

describe("encoding", () => {
let file: File;
let file: Vinyl;
let callback: SinonSpy;

beforeEach(() => {
Expand Down Expand Up @@ -86,7 +87,7 @@ describe("options", () => {
});

describe("thisArg", () => {
let file: File;
let file: Vinyl;
let callback: SinonSpy;
let thisArg: object;

Expand Down
5 changes: 3 additions & 2 deletions spec/params.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { expect } from "chai";
import { File, PluginError } from "gulp-util";
import PluginError = require("plugin-error");
import Vinyl = require("vinyl");
import { identity } from "lodash";
import { createBufferFile, write } from "./helpers";
import transform = require("../src");

describe("parameters", () => {

describe("callback", () => {
let file: File;
let file: Vinyl;

beforeEach(() => {
file = createBufferFile([0xCF, 0x80]);
Expand Down
2 changes: 1 addition & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PluginError } from "gulp-util";
import PluginError = require("plugin-error");
import { PLUGIN_NAME, isFunction, isNil, isObjectLike, isString } from "./common";

type OptionsOrEncoding = Options | string | null | undefined;
Expand Down
9 changes: 5 additions & 4 deletions src/ContentTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { File, PluginError } from "gulp-util";
import PluginError = require("plugin-error");
import Vinyl = require("vinyl");
import { Config } from "./Config";
import { PLUGIN_NAME, TransformFunction, isString } from "./common";

Expand Down Expand Up @@ -26,7 +27,7 @@ export class ContentTransformer {
return (contents, file) => this.transform(contents, file);
}

private async transform(contents: Buffer, file: File): Promise<Buffer> {
private async transform(contents: Buffer, file: Vinyl): Promise<Buffer> {
const decodedContents = this.decodeContents(contents);
const callbackResult = await this.invokeAndValidate(decodedContents, file);

Expand All @@ -40,7 +41,7 @@ export class ContentTransformer {
return contents;
}

private async invokeAndValidate(decodedContents: Buffer | string, file: File): Promise<Buffer | string> {
private async invokeAndValidate(decodedContents: Buffer | string, file: Vinyl): Promise<Buffer | string> {
const callbackResult = await this.tryInvokeCallback(decodedContents, file);

if (this.encoding && !isString(callbackResult))
Expand All @@ -52,7 +53,7 @@ export class ContentTransformer {
return callbackResult;
}

private async tryInvokeCallback(decodedContents: Buffer | string, file: File): Promise<Buffer | string> {
private async tryInvokeCallback(decodedContents: Buffer | string, file: Vinyl): Promise<Buffer | string> {
try {
return await this.callback.call(this.thisArg, decodedContents, file);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/FileContentStream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Transform } from "stream";
import { PluginError } from "gulp-util";
import PluginError = require("plugin-error");
import { PLUGIN_NAME, NodeCallback, StreamFile, TransformFunction } from "./common";

/**
Expand Down
5 changes: 3 additions & 2 deletions src/GulpTransformStream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Transform } from "stream";
import { File, PluginError } from "gulp-util";
import PluginError = require("plugin-error");
import Vinyl = require("vinyl");
import { FileContentStream } from "./FileContentStream";
import { PLUGIN_NAME, TransformFunction, BufferFile, StreamFile, NodeCallback } from "./common";

Expand All @@ -14,7 +15,7 @@ export class GulpTransformStream extends Transform {
private readonly transform: TransformFunction
) { super({ objectMode: true }); }

public _transform(file: File, _encoding: string, next: NodeCallback<File>): void {
public _transform(file: Vinyl, _encoding: string, next: NodeCallback<Vinyl>): void {
if (file.isBuffer())
return void this.transformBufferFile(file, next);

Expand Down
8 changes: 4 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { File } from "gulp-util";
import Vinyl = require("vinyl");

/**
* Accepts the contents of a File object as a Buffer, applies a
Expand All @@ -7,15 +7,15 @@ import { File } from "gulp-util";
* @internal
*/
export interface TransformFunction {
(contents: Buffer, file: File): Promise<Buffer>;
(contents: Buffer, file: Vinyl): Promise<Buffer>;
}

/**
* A Vinyl File object in buffer mode.
*
* @internal
*/
export interface BufferFile extends File {
export interface BufferFile extends Vinyl {
contents: Buffer;
}

Expand All @@ -24,7 +24,7 @@ export interface BufferFile extends File {
*
* @internal
*/
export interface StreamFile extends File {
export interface StreamFile extends Vinyl {
contents: NodeJS.ReadableStream;
}

Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { File } from "gulp-util";
import Vinyl = require("vinyl");
import { Config } from "./Config";
import { ContentTransformer } from "./ContentTransformer";
import { GulpTransformStream } from "./GulpTransformStream";
Expand All @@ -16,7 +16,7 @@ import { GulpTransformStream } from "./GulpTransformStream";
*/
function gulpTransform(
encoding: gulpTransform.Encoding,
callback: (contents: string, file: File) => string | PromiseLike<string>
callback: (contents: string, file: Vinyl) => string | PromiseLike<string>
): NodeJS.ReadWriteStream;

/**
Expand All @@ -32,7 +32,7 @@ function gulpTransform(
*/
function gulpTransform(
encoding: null,
callback: (contents: Buffer, file: File) => Buffer | PromiseLike<Buffer>
callback: (contents: Buffer, file: Vinyl) => Buffer | PromiseLike<Buffer>
): NodeJS.ReadWriteStream;

/**
Expand All @@ -52,7 +52,7 @@ function gulpTransform(
*/
function gulpTransform(
options: { encoding: gulpTransform.Encoding, thisArg?: any },
callback: (contents: string, file: File) => string | PromiseLike<string>
callback: (contents: string, file: Vinyl) => string | PromiseLike<string>
): NodeJS.ReadWriteStream;

/**
Expand All @@ -72,7 +72,7 @@ function gulpTransform(
*/
function gulpTransform(
options: { encoding?: null, thisArg?: any },
callback: (contents: Buffer, file: File) => Buffer | PromiseLike<Buffer>
callback: (contents: Buffer, file: Vinyl) => Buffer | PromiseLike<Buffer>
): NodeJS.ReadWriteStream;

/**
Expand All @@ -85,7 +85,7 @@ function gulpTransform(
* @return Returns a Gulp plugin stream.
*/
function gulpTransform(
callback: (contents: Buffer, file: File) => Buffer | PromiseLike<Buffer>
callback: (contents: Buffer, file: Vinyl) => Buffer | PromiseLike<Buffer>
): NodeJS.ReadWriteStream;

function gulpTransform(arg0: any, arg1?: any): NodeJS.ReadWriteStream {
Expand Down