feat(smartgulp): modernize file glob handling and refresh package metadata
This commit is contained in:
@@ -1,103 +1,165 @@
|
||||
# @push.rocks/smartgulp
|
||||
lightweight gulp replacement
|
||||
|
||||
## Install
|
||||
To install `@push.rocks/smartgulp`, use the following command in your terminal:
|
||||
`@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:
|
||||
|
||||
```bash
|
||||
npm install @push.rocks/smartgulp --save-dev
|
||||
pnpm add -D @push.rocks/smartgulp
|
||||
```
|
||||
|
||||
This will add `smartgulp` to your project's `devDependencies`. `smartgulp` is designed to be a lightweight replacement for `gulp`, thus assuming its role in development processes rather than in production environments.
|
||||
## What It Provides
|
||||
|
||||
## Usage
|
||||
`@push.rocks/smartgulp` exports three pipeline primitives:
|
||||
|
||||
In this section, we'll dive into how to effectively utilize `@push.rocks/smartgulp` within your project. `smartgulp` aims to present a simplified interface for handling file streams and processing, similar to traditional `gulp` but with a modernized API and reduced complexity.
|
||||
| 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. |
|
||||
|
||||
### Basic Stream Creation and File Manipulation
|
||||
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.
|
||||
|
||||
First, let's demonstrate how to create a stream from source files and then manipulate these files, ultimately saving the transformed files to a desired directory.
|
||||
## Quick Start
|
||||
|
||||
```typescript
|
||||
// Import smartgulp methods
|
||||
Copy Markdown files from one directory to another:
|
||||
|
||||
```ts
|
||||
import { src, dest } from '@push.rocks/smartgulp';
|
||||
|
||||
// Create a stream from source files using the `src` function
|
||||
src(['src/**/*.ts']) // Specify the glob pattern
|
||||
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'])
|
||||
.pipe(
|
||||
// Example transformation: Convert TypeScript to JavaScript
|
||||
// Use your desired transformer here
|
||||
ts() // Assuming `ts()` is a function returning a Transform stream
|
||||
gulpFunction.forEach(async (fileArg: SmartFile) => {
|
||||
const currentContent = fileArg.contents.toString();
|
||||
fileArg.contents = Buffer.from(`Processed by smartgulp\n\n${currentContent}`);
|
||||
})
|
||||
)
|
||||
.pipe(dest('./dist'));
|
||||
```
|
||||
|
||||
This pattern keeps transformations focused: read files with `src()`, mutate or inspect each `SmartFile`, then write the result with `dest()`.
|
||||
|
||||
## Replace Files In Place
|
||||
|
||||
Use `replace()` when a transform should update the original files instead of writing to a new output directory:
|
||||
|
||||
```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(
|
||||
// Save the transformed files to the 'dist' directory
|
||||
dest('dist')
|
||||
gulpFunction.forEach(async (fileArg: SmartFile) => {
|
||||
fileArg.contents = Buffer.from(
|
||||
fileArg.contents.toString().replaceAll('http://', 'https://')
|
||||
);
|
||||
})
|
||||
)
|
||||
.pipe(replace());
|
||||
```
|
||||
|
||||
`replace()` calls `write()` on each incoming `SmartFile`, so it persists the modified contents back to the file's known path.
|
||||
|
||||
## Wait For A Pipeline To Finish
|
||||
|
||||
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:
|
||||
|
||||
```ts
|
||||
import { src, dest } from '@push.rocks/smartgulp';
|
||||
import * as gulpFunction from '@push.rocks/gulp-function';
|
||||
import * as smartpromise from '@push.rocks/smartpromise';
|
||||
|
||||
const done = smartpromise.defer<void>();
|
||||
|
||||
src(['./test/testfiles/**/*.md'])
|
||||
.pipe(dest('./dist/testfiles'))
|
||||
.pipe(
|
||||
gulpFunction.atEnd(async () => {
|
||||
done.resolve();
|
||||
})
|
||||
);
|
||||
|
||||
await done.promise;
|
||||
```
|
||||
|
||||
In the example above, `src(['src/**/*.ts'])` creates a stream that reads all TypeScript files in the `src` directory. Each file is then transformed (for instance, compiled from TypeScript to JavaScript, though the actual transformation function `ts()` is illustrative) and finally written to the `dist` directory via `dest('dist')`.
|
||||
This is useful in tests, build scripts, and one-off automation where the surrounding code needs a promise.
|
||||
|
||||
### Advanced Usage: Custom Transformations
|
||||
## Glob Behavior
|
||||
|
||||
`smartgulp` can be extended with custom transformations, allowing you to perform virtually any file manipulation task. Here’s how you can create and use custom transform streams:
|
||||
`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`.
|
||||
|
||||
```typescript
|
||||
import { src, dest } from '@push.rocks/smartgulp';
|
||||
import { Transform } from 'stream';
|
||||
Examples:
|
||||
|
||||
// Creating a custom transform to prefix file contents
|
||||
const prefixTransform = new Transform({
|
||||
objectMode: true,
|
||||
transform(file, encoding, callback) {
|
||||
const prefix = '/* Prefixed content */\n';
|
||||
file.contents = Buffer.from(prefix + file.contents.toString());
|
||||
callback(null, file); // Pass the modified file along the stream
|
||||
}
|
||||
});
|
||||
| Pattern | Base directory | Matched portion |
|
||||
| --- | --- | --- |
|
||||
| `./src/**/*.ts` | `./src` | `**/*.ts` |
|
||||
| `./docs/*.md` | `./docs` | `*.md` |
|
||||
| `./package.json` | `.` | `package.json` |
|
||||
|
||||
// Usage in a pipeline
|
||||
src(['src/**/*.js']) // Adjust glob pattern as needed
|
||||
.pipe(prefixTransform) // Apply custom prefixing
|
||||
.pipe(dest('dist')); // Output to 'dist' directory
|
||||
```
|
||||
Dotfiles are included in matching, so patterns can pick up files such as `.config.json` when the glob allows them.
|
||||
|
||||
In this scenario, `prefixTransform` is a custom transformation that prepends a comment to the contents of each `.js` file fetched through `src(['src/**/*.js'])`. After transformation, the files are written to the `dist` directory.
|
||||
## Typical Use Cases
|
||||
|
||||
### Combining with Other Libraries
|
||||
- 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.
|
||||
|
||||
`@push.rocks/smartgulp` can be seamlessly integrated with other libraries, making it easy to enhance your build pipeline with additional functionality like linting, minification, and more.
|
||||
## API Reference
|
||||
|
||||
```typescript
|
||||
import { src, dest } from '@push.rocks/smartgulp';
|
||||
import someOtherLib from 'some-other-lib';
|
||||
### `src(patterns: string[])`
|
||||
|
||||
src(['src/**/*.css']) // Example with CSS files
|
||||
.pipe(
|
||||
// Assuming `someOtherLib` provides a method for CSS minification
|
||||
someOtherLib.minifyCSS()
|
||||
)
|
||||
.pipe(dest('dist'));
|
||||
```
|
||||
Creates an object-mode `Transform` stream and asynchronously pushes every matching `SmartFile` into it. The stream ends after all matched files have been pushed.
|
||||
|
||||
### Conclusion
|
||||
### `dest(directory: string)`
|
||||
|
||||
`@push.rocks/smartgulp` offers a simplified, promise-based approach to stream processing and file manipulation. By leveraging its functionality along with custom and third-party transforms, you can create powerful and efficient build pipelines tailored to your project's needs. Whether you're compiling, bundling, minifying, or simply copying files, `smartgulp` provides a lightweight, flexible foundation for your automated workflows.
|
||||
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()`.
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
||||
|
||||
**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
|
||||
|
||||
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 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, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
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.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
||||
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user