// this file contains the implementation of the standard gulp api import * as plugins from './smartgulp.plugins.js'; import { GulpStream } from './smartgulp.classes.gulpstream.js'; import { Transform } from 'node:stream'; import { SmartFile } from '@push.rocks/smartfile'; 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('/'), }; }; export let src = (minimatchPathArrayArg: string[]): Transform => { const gulpStream = new GulpStream(); const handleFiles = async (): Promise => { let smartfileArray: SmartFile[] = []; for (const minimatchPath of minimatchPathArrayArg) { const { baseDir, matchPattern } = getGlobBaseAndPattern(minimatchPath); const virtualDirectory = await smartFileFactory.virtualDirectoryFromPath( plugins.path.resolve(process.cwd(), baseDir) ); const localSmartfileArray = virtualDirectory .listFiles() .filter((fileArg) => plugins.minimatch(toPosixPath(fileArg.relative), matchPattern, { dot: true })); smartfileArray = [...smartfileArray, ...localSmartfileArray]; } await gulpStream.pipeSmartfileArray(smartfileArray); }; handleFiles().catch((err) => { console.log(err); }); return gulpStream.stream; }; export let dest = (dirArg: string) => { const stream = new plugins.smartstream.SmartDuplex({ objectMode: true, writeFunction: async (fileArg: SmartFile) => { await fileArg.writeToDir(dirArg); }, }); return stream; }; export let replace = () => { const stream = new plugins.smartstream.SmartDuplex({ objectMode: true, writeFunction: async (fileArg: SmartFile) => { await fileArg.write(); }, }); return stream; };