Files
smartgulp/ts/smartgulp.gulpapi.ts
T

71 lines
2.3 KiB
TypeScript
Raw Normal View History

2017-04-30 00:25:31 +02:00
// this file contains the implementation of the standard gulp api
2022-06-09 19:59:21 +02:00
import * as plugins from './smartgulp.plugins.js';
import { GulpStream } from './smartgulp.classes.gulpstream.js';
import { Transform } from 'node:stream';
2017-04-30 00:25:31 +02:00
2023-11-25 18:11:31 +01:00
import { SmartFile } from '@push.rocks/smartfile';
2017-04-30 00:25:31 +02:00
const smartFileFactory = plugins.smartfile.SmartFileFactory.nodeFs();
const toPosixPath = (pathArg: string): string => pathArg.split(plugins.path.sep).join('/');
const getGlobBaseAndPattern = (globPatternArg: string): { baseDir: string; matchPattern: string } => {
const normalizedPattern = toPosixPath(globPatternArg);
const pathParts = normalizedPattern.split('/');
const firstGlobIndex = pathParts.findIndex((pathPartArg) => /[*?[\]{}]/.test(pathPartArg));
if (firstGlobIndex === -1) {
return {
baseDir: plugins.path.dirname(globPatternArg),
matchPattern: plugins.path.basename(globPatternArg),
};
}
return {
baseDir: pathParts.slice(0, firstGlobIndex).join('/') || '.',
matchPattern: pathParts.slice(firstGlobIndex).join('/'),
};
};
2017-04-30 00:25:31 +02:00
export let src = (minimatchPathArrayArg: string[]): Transform => {
const gulpStream = new GulpStream();
const handleFiles = async (): Promise<void> => {
2023-11-25 18:11:31 +01:00
let smartfileArray: SmartFile[] = [];
for (const minimatchPath of minimatchPathArrayArg) {
const { baseDir, matchPattern } = getGlobBaseAndPattern(minimatchPath);
const virtualDirectory = await smartFileFactory.virtualDirectoryFromPath(
plugins.path.resolve(process.cwd(), baseDir)
2018-03-03 13:57:55 +01:00
);
const localSmartfileArray = virtualDirectory
.listFiles()
.filter((fileArg) => plugins.minimatch(toPosixPath(fileArg.relative), matchPattern, { dot: true }));
2019-02-20 23:54:38 +01:00
smartfileArray = [...smartfileArray, ...localSmartfileArray];
2017-04-30 00:25:31 +02:00
}
await gulpStream.pipeSmartfileArray(smartfileArray);
2018-03-03 13:57:55 +01:00
};
2022-06-09 19:59:21 +02:00
handleFiles().catch((err) => {
2018-03-03 13:57:55 +01:00
console.log(err);
});
return gulpStream.stream;
};
2017-04-30 00:25:31 +02:00
2023-11-25 18:11:31 +01:00
export let dest = (dirArg: string) => {
const stream = new plugins.smartstream.SmartDuplex({
objectMode: true,
writeFunction: async (fileArg: SmartFile) => {
await fileArg.writeToDir(dirArg);
},
2023-11-25 18:11:31 +01:00
});
return stream;
};
2018-02-16 00:46:55 +01:00
export let replace = () => {
2023-11-25 18:11:31 +01:00
const stream = new plugins.smartstream.SmartDuplex({
objectMode: true,
writeFunction: async (fileArg: SmartFile) => {
await fileArg.write();
},
2023-11-25 18:11:31 +01:00
});
return stream;
2018-03-03 13:57:55 +01:00
};