Files
smartgulp/readme.md
T

166 lines
7.0 KiB
Markdown
Raw Normal View History

2024-04-14 17:39:50 +02:00
# @push.rocks/smartgulp
2019-02-20 23:54:38 +01:00
`@push.rocks/smartgulp` is a tiny, modern Gulp-style file pipeline for TypeScript and Node.js projects. It gives you the familiar `src(...).pipe(...).pipe(dest(...))` workflow without pulling in the full Gulp task runner: read files by glob, process them as `SmartFile` objects, write them somewhere else, or replace them in place.
Use it when you want stream-based build steps that stay small, explicit, and easy to compose with regular Node streams.
## Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
## Installation
Install it as a development dependency:
2024-04-14 17:39:50 +02:00
```bash
pnpm add -D @push.rocks/smartgulp
2024-04-14 17:39:50 +02:00
```
## What It Provides
2019-02-20 23:54:38 +01:00
`@push.rocks/smartgulp` exports three pipeline primitives:
2019-02-20 23:54:38 +01:00
| API | Purpose |
| --- | --- |
| `src(patterns)` | Reads matching files from disk and emits `SmartFile` objects into an object-mode stream. |
| `dest(directory)` | Writes incoming `SmartFile` objects to a target directory. |
| `replace()` | Writes incoming `SmartFile` objects back to their original location. |
2024-04-14 17:39:50 +02:00
The files flowing through a `smartgulp` pipeline are `SmartFile` instances from [`@push.rocks/smartfile`](https://www.npmjs.com/package/@push.rocks/smartfile). That means transforms can inspect paths, read or mutate contents, and then pass each file along to the next stream.
2024-04-14 17:39:50 +02:00
## Quick Start
2024-04-14 17:39:50 +02:00
Copy Markdown files from one directory to another:
```ts
2024-04-14 17:39:50 +02:00
import { src, dest } from '@push.rocks/smartgulp';
src(['./docs/**/*.md']).pipe(dest('./dist/docs'));
```
`src()` accepts an array of glob patterns. Each matching file is emitted as a `SmartFile`, and `dest()` writes every received file to the target directory.
## Transform Files
Because `smartgulp` uses object-mode streams, you can use any transform that accepts and emits `SmartFile` objects. The companion package [`@push.rocks/gulp-function`](https://www.npmjs.com/package/@push.rocks/gulp-function) is a convenient fit:
```ts
import { src, dest } from '@push.rocks/smartgulp';
import * as gulpFunction from '@push.rocks/gulp-function';
import type { SmartFile } from '@push.rocks/smartfile';
src(['./src/**/*.txt'])
2024-04-14 17:39:50 +02:00
.pipe(
gulpFunction.forEach(async (fileArg: SmartFile) => {
const currentContent = fileArg.contents.toString();
fileArg.contents = Buffer.from(`Processed by smartgulp\n\n${currentContent}`);
})
2024-04-14 17:39:50 +02:00
)
.pipe(dest('./dist'));
2024-04-14 17:39:50 +02:00
```
This pattern keeps transformations focused: read files with `src()`, mutate or inspect each `SmartFile`, then write the result with `dest()`.
2024-04-14 17:39:50 +02:00
## Replace Files In Place
2024-04-14 17:39:50 +02:00
Use `replace()` when a transform should update the original files instead of writing to a new output directory:
2024-04-14 17:39:50 +02:00
```ts
import { src, replace } from '@push.rocks/smartgulp';
import * as gulpFunction from '@push.rocks/gulp-function';
import type { SmartFile } from '@push.rocks/smartfile';
src(['./content/**/*.md'])
.pipe(
gulpFunction.forEach(async (fileArg: SmartFile) => {
fileArg.contents = Buffer.from(
fileArg.contents.toString().replaceAll('http://', 'https://')
);
})
)
.pipe(replace());
2024-04-14 17:39:50 +02:00
```
`replace()` calls `write()` on each incoming `SmartFile`, so it persists the modified contents back to the file's known path.
2024-04-14 17:39:50 +02:00
## Wait For A Pipeline To Finish
2024-04-14 17:39:50 +02:00
Node streams are asynchronous. If your script needs to wait for the final file before exiting or starting the next step, add an end-of-stream transform:
2024-04-14 17:39:50 +02:00
```ts
2024-04-14 17:39:50 +02:00
import { src, dest } from '@push.rocks/smartgulp';
import * as gulpFunction from '@push.rocks/gulp-function';
import * as smartpromise from '@push.rocks/smartpromise';
2024-04-14 17:39:50 +02:00
const done = smartpromise.defer<void>();
src(['./test/testfiles/**/*.md'])
.pipe(dest('./dist/testfiles'))
2024-04-14 17:39:50 +02:00
.pipe(
gulpFunction.atEnd(async () => {
done.resolve();
})
);
await done.promise;
2024-04-14 17:39:50 +02:00
```
This is useful in tests, build scripts, and one-off automation where the surrounding code needs a promise.
## Glob Behavior
`src()` resolves each provided pattern from the current working directory. It derives the static base directory before the first glob segment, loads that directory, and filters files with `minimatch`.
Examples:
| Pattern | Base directory | Matched portion |
| --- | --- | --- |
| `./src/**/*.ts` | `./src` | `**/*.ts` |
| `./docs/*.md` | `./docs` | `*.md` |
| `./package.json` | `.` | `package.json` |
Dotfiles are included in matching, so patterns can pick up files such as `.config.json` when the glob allows them.
2024-04-14 17:39:50 +02:00
## Typical Use Cases
- Copy assets into build directories.
- Run text or metadata transformations across many files.
- Update generated files in place.
- Build small project-specific pipelines without a full task runner.
- Compose `SmartFile`-aware transforms with regular Node stream flow.
## API Reference
### `src(patterns: string[])`
Creates an object-mode `Transform` stream and asynchronously pushes every matching `SmartFile` into it. The stream ends after all matched files have been pushed.
### `dest(directory: string)`
Creates an object-mode writable stream. Each incoming `SmartFile` is written to `directory` through `SmartFile.writeToDir()`.
### `replace()`
Creates an object-mode writable stream. Each incoming `SmartFile` is persisted through `SmartFile.write()`.
2024-04-14 17:39:50 +02:00
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
2024-04-14 17:39:50 +02:00
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
2022-06-09 19:59:21 +02:00
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
2022-06-09 19:59:21 +02:00
2024-04-14 17:39:50 +02:00
### Company Information
2022-06-09 19:59:21 +02:00
2024-04-14 17:39:50 +02:00
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
2019-02-20 23:54:38 +01:00
For any legal inquiries or further information, please contact us via email at hello@task.vc.
2019-02-20 23:54:38 +01:00
2024-04-14 17:39:50 +02:00
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.