Files
gulp-function/ts/index.ts
T

102 lines
3.2 KiB
TypeScript
Raw Normal View History

import type { Transform } from 'node:stream';
import through2 from 'through2';
export type TExecutionMode = 'forEach' | 'forFirst' | 'atEnd';
export type TGulpFunctionEncoding = BufferEncoding | string | null;
export type TGulpFunctionResult = PromiseLike<unknown> | unknown;
export interface IPromiseFunction<TFile = unknown> {
(file?: TFile | null, enc?: TGulpFunctionEncoding): TGulpFunctionResult;
2016-10-19 00:35:41 +02:00
}
type TTransformCallback = (errorArg?: Error | null, dataArg?: unknown) => void;
type TFlushCallback = (errorArg?: Error | null) => void;
const normalizeError = (errorArg: unknown): Error => {
return errorArg instanceof Error ? errorArg : new Error(String(errorArg));
};
const defaultExport = <TFile = unknown>(
functionsToExecuteArg: IPromiseFunction<TFile> | Array<IPromiseFunction<TFile>>,
executionModeArg: TExecutionMode = 'forEach'
2016-10-19 00:35:41 +02:00
): Transform => {
const runFunction = async (
functionArg: IPromiseFunction<TFile>,
fileArg: TFile | null,
encArg: TGulpFunctionEncoding
): Promise<void> => {
await functionArg(fileArg, encArg);
2018-01-29 23:28:03 +01:00
};
2015-09-17 20:58:19 +00:00
const checkAndRunFunction = async (
fileArg: TFile | null,
encArg: TGulpFunctionEncoding
): Promise<void> => {
if (typeof functionsToExecuteArg === 'function') {
await runFunction(functionsToExecuteArg, fileArg, encArg);
2017-04-30 00:44:11 +02:00
} else if (Array.isArray(functionsToExecuteArg)) {
await Promise.all(
functionsToExecuteArg.map(async (functionArg) => runFunction(functionArg, fileArg, encArg))
);
2017-04-30 00:44:11 +02:00
} else {
throw new Error('gulp-callfunction: something is strange with the given arguments');
2016-10-19 00:35:41 +02:00
}
2018-01-29 23:28:03 +01:00
};
2015-10-25 22:45:49 +01:00
2018-01-29 23:28:03 +01:00
let hasExecutedOnce = false;
const transformFunction = (fileArg: TFile, encArg: BufferEncoding, cbArg: TTransformCallback) => {
2017-04-30 00:44:11 +02:00
switch (executionModeArg) {
case 'forEach':
checkAndRunFunction(fileArg, encArg).then(
() => cbArg(null, fileArg),
(errorArg) => cbArg(normalizeError(errorArg))
);
2018-01-29 23:28:03 +01:00
break;
case 'forFirst':
if (!hasExecutedOnce) {
hasExecutedOnce = true;
checkAndRunFunction(fileArg, encArg).then(
() => cbArg(null, fileArg),
(errorArg) => cbArg(normalizeError(errorArg))
);
} else {
cbArg(null, fileArg);
2015-11-30 09:49:52 +01:00
}
2018-01-29 23:28:03 +01:00
break;
case 'atEnd':
cbArg(null, fileArg);
2018-01-29 23:28:03 +01:00
break;
2017-04-30 00:44:11 +02:00
default:
cbArg(null, fileArg);
2018-01-29 23:28:03 +01:00
break;
2017-04-30 00:44:11 +02:00
}
2018-01-29 23:28:03 +01:00
};
2017-04-30 00:44:11 +02:00
const flushFunction = (cbArg: TFlushCallback) => {
if (executionModeArg === 'atEnd') {
checkAndRunFunction(null, null).then(
() => cbArg(),
(errorArg) => cbArg(normalizeError(errorArg))
);
2017-04-30 00:44:11 +02:00
} else {
cbArg();
2016-10-19 00:35:41 +02:00
}
2018-01-29 23:28:03 +01:00
};
return through2.obj(transformFunction, flushFunction) as Transform;
2018-01-29 23:28:03 +01:00
};
2016-10-19 01:10:45 +02:00
export const forEach = <TFile = unknown>(funcArg: IPromiseFunction<TFile>): Transform => {
return defaultExport(funcArg, 'forEach');
2018-01-29 23:28:03 +01:00
};
2016-10-19 07:36:32 +02:00
export const forFirst = <TFile = unknown>(funcArg: IPromiseFunction<TFile>): Transform => {
return defaultExport(funcArg, 'forFirst');
2018-01-29 23:28:03 +01:00
};
2016-10-19 07:36:32 +02:00
export const atEnd = <TFile = unknown>(funcArg: IPromiseFunction<TFile>): Transform => {
return defaultExport(funcArg, 'atEnd');
2018-01-29 23:28:03 +01:00
};
2016-10-19 07:36:32 +02:00
2018-01-29 23:28:03 +01:00
export default defaultExport;