@jsxtools/rollup-plugin-utils
Advanced tools
+7
-2
| import { type Stats } from "node:fs"; | ||
| import type { PathLike } from "./path.js"; | ||
| import * as path from "./path.js"; | ||
| export { readFile, stat as getFileStats, unlink as deleteFile, writeFile } from "node:fs/promises"; | ||
| /** Copies a file using the fastest method available. @deprecated */ | ||
| export declare const copy: (src: PathLike, dest: PathLike) => Promise<void>; | ||
| /** Copies a file using the fastest method available. */ | ||
| export declare const copy: (src: PathLike, dest: PathLike) => Promise<void>; | ||
| export declare const copyFile: (src: PathLike, dest: PathLike) => Promise<void>; | ||
| /** Returns an async generator yielding file paths. */ | ||
@@ -18,4 +20,7 @@ export declare const glob: (options?: GlobOptions) => AsyncGenerator<string, void, void>; | ||
| export declare const mkdir: (path: PathLike) => Promise<string | undefined>; | ||
| /** Ensures directories for the given file or files exist. */ | ||
| export declare const ensureFileDir: (...files: PathLike[]) => Promise<void>; | ||
| /** Reads a file as parsed JSON. */ | ||
| export declare const readJSON: <T>(file: PathLike) => Promise<NonNullable<T>>; | ||
| export type FileStats = Stats; | ||
| export type PathLike = path.PathLike; |
+30
-7
| import { createHash } from "node:crypto"; | ||
| import { createReadStream } from "node:fs"; | ||
| import { constants, copyFile, glob as globWithNode, mkdir as mkdirWithNode, readFile as readFileWithNode, } from "node:fs/promises"; | ||
| import { constants as fsConstants, copyFile as fsCopyFile, glob as fsGlob, mkdir as fsMkdir, readFile as fsReadFile, } from "node:fs/promises"; | ||
| import * as array from "./array.js"; | ||
| import * as json from "./json.js"; | ||
| import * as path from "./path.js"; | ||
| export { readFile, stat as getFileStats, unlink as deleteFile, writeFile } from "node:fs/promises"; | ||
| /** Copies a file using the fastest method available. */ | ||
| /** Copies a file using the fastest method available. @deprecated */ | ||
| export const copy = async (src, dest) => { | ||
| try { | ||
| return copyFile(src, dest, constants.COPYFILE_FICLONE); | ||
| return await fsCopyFile(src, dest, fsConstants.COPYFILE_FICLONE); | ||
| } | ||
| catch { | ||
| return copyFile(src, dest); | ||
| return await fsCopyFile(src, dest); | ||
| } | ||
| }; | ||
| /** Copies a file using the fastest method available. */ | ||
| export const copyFile = async (src, dest) => { | ||
| try { | ||
| return await fsCopyFile(src, dest, fsConstants.COPYFILE_FICLONE); | ||
| } | ||
| catch { | ||
| return await fsCopyFile(src, dest); | ||
| } | ||
| }; | ||
| /** Returns an async generator yielding file paths. */ | ||
@@ -23,3 +33,3 @@ export const glob = (options) => (async function* () { | ||
| }; | ||
| for await (const path of globWithNode(globPattern, globOptions)) { | ||
| for await (const path of fsGlob(globPattern, globOptions)) { | ||
| yield new URL(path, globOptions.cwd).pathname; | ||
@@ -39,5 +49,18 @@ } | ||
| /** Creates a directory recursively. */ | ||
| export const mkdir = async (path) => mkdirWithNode(path, { recursive: true }); | ||
| export const mkdir = async (path) => fsMkdir(path, { recursive: true }); | ||
| /** Ensures directories for the given file or files exist. */ | ||
| export const ensureFileDir = async (...files) => { | ||
| const fileDirs = files.map(path.toParentURL).sort(__reverseSortURL); | ||
| const makeDirs = []; | ||
| for (const fileDir of fileDirs) { | ||
| if (makeDirs.every(({ pathname }) => !pathname.startsWith(fileDir.pathname))) { | ||
| makeDirs.push(fileDir); | ||
| } | ||
| } | ||
| await Promise.all(makeDirs.map((makeDir) => fsMkdir(makeDir, { recursive: true }))); | ||
| }; | ||
| /** Reads a file as parsed JSON. */ | ||
| export const readJSON = (file) => readFileWithNode(file, "utf-8").then((data) => json.from(data)); | ||
| export const readJSON = (file) => fsReadFile(file, "utf-8").then((data) => json.from(data)); | ||
| // #region Internals | ||
| const __reverseSortURL = ({ pathname: a }, { pathname: b }) => (a > b ? -1 : a < b ? 1 : 0); | ||
| //# sourceMappingURL=file.js.map |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"file.js","sourceRoot":"","sources":["../src/file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,gBAAgB,EAAc,MAAM,SAAS,CAAA;AACtD,OAAO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AAQ,EAAE,CAAC;QAChC,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC;YAC9E,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACvB,CAAC;IACF,CAAC;IAED,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAA;AACpF,CAAC,CAAA;AAED,mCAAmC;AACnC,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAI,IAAc,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAI,IAAI,CAAE,CAAC,CAAA;AAO5G,oBAAoB;AAEpB,MAAM,gBAAgB,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAO,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAO,EAAc,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA"} |
+2
-1
| { | ||
| "name": "@jsxtools/rollup-plugin-utils", | ||
| "version": "0.2.0", | ||
| "description": "A collection of utilities for authoring Rollup plugins.", | ||
| "version": "0.2.1", | ||
| "type": "module", | ||
@@ -5,0 +6,0 @@ "exports": { |
+148
-44
| # @jsxtools/rollup-plugin-utils | ||
| **@jsxtools/rollup-plugin-utils** is a collection of utilities for Rollup plugins. | ||
| **@jsxtools/rollup-plugin-utils** is a collection of utilities for authoring Rollup plugins. | ||
| This package includes utilities for virtual asset management, file hashing, path resolution, and option handling. | ||
| This package includes utilities for virtual asset management, file operations, path resolution, array/string manipulation, JSON handling, and option processing. | ||
@@ -18,6 +18,10 @@ ## Installation | ||
| ```javascript | ||
| import * as fs from '@jsxtools/rollup-plugin-utils/file' | ||
| import * as arr from '@jsxtools/rollup-plugin-utils/array' | ||
| import * as json from '@jsxtools/rollup-plugin-utils/json' | ||
| import * as str from '@jsxtools/rollup-plugin-utils/string' | ||
| import * as path from '@jsxtools/rollup-plugin-utils/path' | ||
| import { VirtualAsset } from '@jsxtools/rollup-plugin-utils/virtual-asset' | ||
| import { getFileHash, getFileHashAndData } from '@jsxtools/rollup-plugin-utils/get-file-hash' | ||
| import { toArray, toMergedArray } from '@jsxtools/rollup-plugin-utils/options' | ||
| import { resolve, relative, resolveDir } from '@jsxtools/rollup-plugin-utils/path' | ||
| import { toArray, toMergedArray, assignInput } from '@jsxtools/rollup-plugin-utils/options' | ||
| ``` | ||
@@ -29,3 +33,3 @@ | ||
| A utility class for managing virtual assets in Rollup builds. | ||
| A utility class for managing virtual assets in Rollup builds. The constructor takes a required `id` string as the first parameter and automatically generates virtual module IDs using Rollup built-ins. | ||
@@ -37,11 +41,9 @@ ```javascript | ||
| load(context, id) { | ||
| if (id === this.id) { | ||
| if (id === this.virtualId) { | ||
| return { code: 'export const data = "virtual";' } | ||
| } | ||
| return null | ||
| } | ||
| }) | ||
| // ... and in a plugin | ||
| // Use in a plugin | ||
| export default { | ||
@@ -64,31 +66,97 @@ name: 'my-plugin', | ||
| ### File Hash | ||
| ### File Utilities | ||
| Utilities for generating SHA-1 file hashes. | ||
| Utilities for file operations including hashing, copying, globbing, and reading. | ||
| ```javascript | ||
| import { getFileHash } from '@jsxtools/rollup-plugin-utils/get-file-hash' | ||
| import { hash, copyFile, glob, readJSON, ensureFileDir } from '@jsxtools/rollup-plugin-utils/file' | ||
| const hash = await getFileHash('path/to/file.js') | ||
| // Generate SHA-256 hash of a file | ||
| const fileHash = await hash('path/to/file.js') | ||
| console.log(fileHash) // "a1b2c3d4e5f6..." | ||
| console.log(hash) // "a1b2c3d4e5f6..." | ||
| // Copy file with CoW optimization when available | ||
| await copyFile('src/file.js', 'dist/file.js') | ||
| // Glob for files | ||
| for await (const filePath of glob({ | ||
| include: '**/*.js', | ||
| exclude: '**/*.test.js', | ||
| cwd: new URL('.', import.meta.url) | ||
| })) { | ||
| console.log(filePath) | ||
| } | ||
| // Read and parse JSON file | ||
| const config = await readJSON('config.json') | ||
| // Ensure directory exists for files | ||
| await ensureFileDir('dist/nested/file.js', 'dist/other/file.js') | ||
| ``` | ||
| ### Array Utilities | ||
| Utilities for array manipulation and validation. | ||
| ```javascript | ||
| import { getFileHashAndData } from '@jsxtools/rollup-plugin-utils/get-file-hash' | ||
| import { from, merge, every, isArray } from '@jsxtools/rollup-plugin-utils/array' | ||
| const { hash, data } = await getFileHashAndData('path/to/file.js') | ||
| // Convert to array, filtering nulls | ||
| const files = from('file.js') // ['file.js'] | ||
| const moreFiles = from(['a.js', 'b.js']) // ['a.js', 'b.js'] | ||
| const noFiles = from(null) // [] | ||
| const filtered = from([1, null, 2, undefined, 3]) // [1, 2, 3] | ||
| console.log(hash) // "a1b2c3d4e5f6..." | ||
| console.log(data) // <Buffer ...> | ||
| // Merge arrays safely | ||
| const merged = merge(['a.js'], ['b.js']) // ['a.js', 'b.js'] | ||
| const withNulls = merge(null, ['b.js']) // ['b.js'] | ||
| // Check if all items match predicate | ||
| const allStrings = every(['a', 'b'], (x): x is string => typeof x === 'string') // true | ||
| ``` | ||
| ### JSON Utilities | ||
| Utilities for JSON parsing and stringification with error handling. | ||
| ```javascript | ||
| import { from, to } from '@jsxtools/rollup-plugin-utils/json' | ||
| // Parse JSON safely (returns undefined on error) | ||
| const data = from('{"key": "value"}') // { key: 'value' } | ||
| const invalid = from('invalid json') // undefined | ||
| // Stringify JSON | ||
| const json = to({ key: 'value' }) // '{"key":"value"}' | ||
| const pretty = to({ key: 'value' }, null, 2) // formatted with 2 spaces | ||
| ``` | ||
| ### String Utilities | ||
| Utilities for string manipulation and validation. | ||
| ```javascript | ||
| import { from, trim, hasTrimmedValue } from '@jsxtools/rollup-plugin-utils/string' | ||
| // Convert to string (null/undefined becomes empty string) | ||
| const str = from('hello') // 'hello' | ||
| const empty = from(null) // '' | ||
| // Trim string | ||
| const trimmed = trim(' hello ') // 'hello' | ||
| // Check if string has non-empty trimmed value | ||
| if (hasTrimmedValue(input)) { | ||
| // TypeScript knows input is a non-empty string | ||
| } | ||
| ``` | ||
| ### Options | ||
| Utilities for handling plugin options, including array manipulation and merging. | ||
| Utilities for handling plugin options. | ||
| ```javascript | ||
| import { toArray, toMergedArray } from '@jsxtools/rollup-plugin-utils/options' | ||
| import { toArray, toMergedArray, assignInput } from '@jsxtools/rollup-plugin-utils/options' | ||
| // convert single values or arrays to arrays | ||
| // Convert single values or arrays to arrays | ||
| const files = toArray('file.js') // ['file.js'] | ||
@@ -98,10 +166,7 @@ const moreFiles = toArray(['a.js', 'b.js']) // ['a.js', 'b.js'] | ||
| // merge arrays safely | ||
| // Merge arrays safely | ||
| const merged = toMergedArray(['a.js'], ['b.js']) // ['a.js', 'b.js'] | ||
| const withNulls = toMergedArray(null, ['b.js']) // ['b.js'] | ||
| ``` | ||
| ```javascript | ||
| import { assignInput } from '@jsxtools/rollup-plugin-utils/options' | ||
| // Add input to Rollup options | ||
| const plugin = { | ||
@@ -117,14 +182,23 @@ name: 'my-plugin', | ||
| Path resolution and manipulation utilities. | ||
| Path resolution and manipulation utilities using URL objects. | ||
| ```javascript | ||
| import { resolve, relative, resolveDir } from '@jsxtools/rollup-plugin-utils/path' | ||
| import { toURL, toDirURL, toRelativePath, toParentURL } from '@jsxtools/rollup-plugin-utils/path' | ||
| // Standard Node.js path functions | ||
| const absolutePath = resolve('src', 'index.js') | ||
| const relativePath = relative('/project', '/project/src/index.js') | ||
| // Convert to URL (absolute path) | ||
| const fileUrl = toURL('src', 'index.js') // URL { href: 'file:///absolute/path/to/src/index.js' } | ||
| // Directory resolution with trailing separator | ||
| const srcDir = resolveDir('src') // '/absolute/path/to/src/' | ||
| const distDir = resolveDir('dist', 'assets') // '/absolute/path/to/dist/assets/' | ||
| // Convert to directory URL (with trailing slash) | ||
| const srcDir = toDirURL('src') // URL { href: 'file:///absolute/path/to/src/' } | ||
| const distDir = toDirURL('dist', 'assets') // URL { href: 'file:///absolute/path/to/dist/assets/' } | ||
| // Get relative path between URLs | ||
| const rel = toRelativePath( | ||
| new URL('file:///project/src/'), | ||
| new URL('file:///project/dist/file.js') | ||
| ) // '../dist/file.js' | ||
| // Get parent directory URL | ||
| const parent = toParentURL(new URL('file:///project/src/file.js')) | ||
| // URL { href: 'file:///project/src/' } | ||
| ``` | ||
@@ -136,3 +210,5 @@ | ||
| - `constructor(options?)` - Create a virtual asset with optional configuration | ||
| - `constructor(id: string, hooks?: Partial<VirtualAsset.Hooks>)` - Create a virtual asset with required ID and optional hooks | ||
| - `id` - Get/set the asset ID | ||
| - `virtualId` - Get the virtual module ID (prefixed with `\0` and suffixed with `/`) | ||
| - `buildStart(context, options)` - Initialize the virtual asset during build start | ||
@@ -143,7 +219,30 @@ - `resolveId(context, id, importer, options)` - Resolve virtual asset IDs | ||
| ### File Hash | ||
| ### File Utilities | ||
| - `getFileHash(filePath: string): Promise<string | null>` - Generate SHA-1 hash of file | ||
| - `getFileHashAndData(filePath: string): Promise<FileHashAndData>` - Generate hash and read file data | ||
| - `hash(file: PathLike): Promise<string>` - Generate SHA-256 hash of file using streams | ||
| - `copyFile(src: PathLike, dest: PathLike): Promise<void>` - Copy file with CoW optimization | ||
| - `glob(options?: GlobOptions): AsyncGenerator<string>` - Async generator yielding file paths | ||
| - `readJSON<T>(file: PathLike): Promise<T>` - Read and parse JSON file | ||
| - `ensureFileDir(...files: PathLike[]): Promise<void>` - Ensure directories exist for files | ||
| - `mkdir(path: PathLike): Promise<string | undefined>` - Create directory recursively | ||
| - `readFile`, `writeFile`, `deleteFile`, `getFileStats` - Re-exports from `node:fs/promises` | ||
| ### Array Utilities | ||
| - `from<T>(items: T | T[] | null | undefined, predicate?): T[]` - Convert to array, filtering by predicate | ||
| - `merge<T>(a?: T[], b?: T[]): T[]` - Merge two arrays safely | ||
| - `every<T>(value: unknown, predicate): value is T[]` - Check if value is array and all items match predicate | ||
| - `isArray(value: unknown): value is Array` - Check if value is an array | ||
| ### JSON Utilities | ||
| - `from<T>(json: string, reviver?): T | undefined` - Parse JSON safely (returns undefined on error) | ||
| - `to(value: unknown, replacer?, space?): string` - Stringify JSON | ||
| ### String Utilities | ||
| - `from(value: unknown): string` - Convert to string (null/undefined becomes empty string) | ||
| - `trim(value: unknown): string` - Convert to trimmed string | ||
| - `hasTrimmedValue<T>(value: T): value is HasValue<T>` - Check if string has non-empty trimmed value | ||
| ### Options | ||
@@ -153,13 +252,18 @@ | ||
| - `toMergedArray<T>(a?: T[], b?: T[]): T[]` - Merge two arrays safely | ||
| - `assignInput<T extends Record<string, string> | string[]>(input: T, id: string) => T` - Add an ID to an input option | ||
| - `assignInput<T>(input: T, id: string): T` - Add an ID to Rollup input option | ||
| ### Path | ||
| - `relative(from: string, to: string): string` - Get relative path (re-export from Node.js) | ||
| - `resolve(...paths: string[]): string` - Resolve absolute path (re-export from Node.js) | ||
| - `resolveDir(...paths: string[]): string` - Resolve directory with trailing separator | ||
| - `toURL(...paths: string[]): URL` - Resolve paths to file URL | ||
| - `toDirURL(...paths: string[]): URL` - Resolve paths to directory URL (with trailing slash) | ||
| - `toRelativePath(from: URL, to: URL): string` - Get relative POSIX path between URLs | ||
| - `toParentURL(url: URL): URL` - Get parent directory URL | ||
| - `PathLike` - Type alias for `string | URL` | ||
| ## Exports | ||
| - `@jsxtools/rollup-plugin-utils/get-file-hash` - File hashing utilities | ||
| - `@jsxtools/rollup-plugin-utils/array` - Array manipulation utilities | ||
| - `@jsxtools/rollup-plugin-utils/file` - File operation utilities | ||
| - `@jsxtools/rollup-plugin-utils/json` - JSON parsing/stringification utilities | ||
| - `@jsxtools/rollup-plugin-utils/string` - String manipulation utilities | ||
| - `@jsxtools/rollup-plugin-utils/options` - Options handling utilities | ||
@@ -166,0 +270,0 @@ - `@jsxtools/rollup-plugin-utils/path` - Path utilities |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
34574
23.94%295
10.49%271
62.28%0
-100%