├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── license ├── package.json ├── index.js ├── readme.md ├── index.test-d.ts ├── test.js └── index.d.ts /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/vinyl-file 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 18 14 | - 16 15 | steps: 16 | - uses: actions/checkout@v3 17 | - uses: actions/setup-node@v3 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vinyl-file", 3 | "version": "5.0.0", 4 | "description": "Create a vinyl file from an actual file", 5 | "license": "MIT", 6 | "repository": "sindresorhus/vinyl-file", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "engines": { 19 | "node": ">=14.16" 20 | }, 21 | "scripts": { 22 | "test": "xo && ava && tsd" 23 | }, 24 | "files": [ 25 | "index.js", 26 | "index.d.ts" 27 | ], 28 | "keywords": [ 29 | "vinyl", 30 | "fs", 31 | "file", 32 | "read", 33 | "virtual", 34 | "format", 35 | "gulp", 36 | "gulpfriendly" 37 | ], 38 | "dependencies": { 39 | "@types/vinyl": "^2.0.7", 40 | "strip-bom-buf": "^3.0.1", 41 | "strip-bom-stream": "^5.0.0", 42 | "vinyl": "^3.0.0" 43 | }, 44 | "devDependencies": { 45 | "ava": "^5.1.0", 46 | "is-stream": "^3.0.0", 47 | "tsd": "^0.25.0", 48 | "xo": "^0.53.1" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import path from 'node:path'; 3 | import fs, {promises as fsPromises} from 'node:fs'; 4 | import stripBomBuffer from 'strip-bom-buf'; 5 | import stripBomStream from 'strip-bom-stream'; 6 | import File from 'vinyl'; 7 | 8 | export async function vinylFile(path_, options = {}) { 9 | const { 10 | cwd = process.cwd(), 11 | base = cwd, 12 | buffer = true, 13 | read = true, 14 | } = options; 15 | 16 | path_ = path.resolve(cwd, path_); 17 | 18 | let contents; 19 | if (read) { 20 | contents = buffer 21 | ? stripBomBuffer(await fsPromises.readFile(path_)) 22 | : fs.createReadStream(path_).pipe(stripBomStream()); 23 | } 24 | 25 | return new File({ 26 | cwd, 27 | base, 28 | path: path_, 29 | stat: await fsPromises.stat(path_), 30 | contents, 31 | }); 32 | } 33 | 34 | export function vinylFileSync(path_, options = {}) { 35 | const { 36 | cwd = process.cwd(), 37 | base = cwd, 38 | buffer = true, 39 | read = true, 40 | } = options; 41 | 42 | path_ = path.resolve(cwd, path_); 43 | 44 | let contents; 45 | 46 | if (read) { 47 | contents = buffer 48 | ? stripBomBuffer(fs.readFileSync(path_)) 49 | : fs.createReadStream(path_).pipe(stripBomStream()); 50 | } 51 | 52 | return new File({ 53 | cwd, 54 | base, 55 | path: path_, 56 | stat: fs.statSync(path_), 57 | contents, 58 | }); 59 | } 60 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # vinyl-file 2 | 3 | > Create a [Vinyl file](https://github.com/gulpjs/vinyl) from an actual file 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install vinyl-file 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import {vinylFile} from 'vinyl-file'; 15 | 16 | const file = await vinylFile('index.js'); 17 | 18 | console.log(file.path); 19 | //=> '/Users/sindresorhus/dev/vinyl-file/index.js' 20 | 21 | console.log(file.cwd); 22 | //=> '/Users/sindresorhus/dev/vinyl-file' 23 | ``` 24 | 25 | ## API 26 | 27 | ### vinylFile(path, options?) 28 | 29 | Create a Vinyl file asynchronously and return it. 30 | 31 | ### vinylFileSync(path, options?) 32 | 33 | Create a Vinyl file synchronously and return it. 34 | 35 | #### options 36 | 37 | Type: `object` 38 | 39 | ##### base 40 | 41 | Type: `string`\ 42 | Default: `process.cwd()` 43 | 44 | Override the `base` of the Vinyl file. 45 | 46 | ##### cwd 47 | 48 | Type: `string`\ 49 | Default: `process.cwd()` 50 | 51 | Override the `cwd` (current working directory) of the Vinyl file. 52 | 53 | ##### buffer 54 | 55 | Type: `boolean`\ 56 | Default: `true` 57 | 58 | Setting this to `false` will return `file.contents` as a stream. This is useful when working with large files. 59 | 60 | **Note:** Plugins might not implement support for streams. 61 | 62 | ##### read 63 | 64 | Type: `boolean`\ 65 | Default: `true` 66 | 67 | Setting this to `false` will return `file.contents` as `null` and not read the file at all. 68 | 69 | ## Related 70 | 71 | - [vinyl-read](https://github.com/SamVerschueren/vinyl-read) - Create vinyl files from glob patterns 72 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import {type BufferFile, type NullFile, type StreamFile} from 'vinyl'; 3 | import {vinylFile, vinylFileSync} from './index.js'; 4 | 5 | expectType>(vinylFile('./', {read: false})); 6 | expectType>(vinylFile('./', {read: false, buffer: true})); 7 | expectType>(vinylFile('./', {read: false, buffer: false})); 8 | 9 | expectType>(vinylFile('./', {buffer: false})); 10 | expectType>(vinylFile('./', {read: true, buffer: false})); 11 | 12 | expectType>(vinylFile('./', {base: undefined, cwd: undefined, buffer: true, read: true})); 13 | expectType>(vinylFile('./', {buffer: true})); 14 | expectType>(vinylFile('./', {buffer: undefined, read: true})); 15 | expectType>(vinylFile('./', {read: undefined})); 16 | expectType>(vinylFile('./', {})); 17 | expectType>(vinylFile('./')); 18 | 19 | expectType(vinylFileSync('./', {read: false})); 20 | expectType(vinylFileSync('./', {read: false, buffer: true})); 21 | expectType(vinylFileSync('./', {read: false, buffer: false})); 22 | 23 | expectType(vinylFileSync('./', {buffer: false})); 24 | expectType(vinylFileSync('./', {read: true, buffer: false})); 25 | 26 | expectType(vinylFileSync('./', {base: undefined, cwd: undefined, buffer: true, read: true})); 27 | expectType(vinylFileSync('./', {buffer: true})); 28 | expectType(vinylFileSync('./', {buffer: undefined, read: true})); 29 | expectType(vinylFileSync('./', {read: undefined})); 30 | expectType(vinylFileSync('./', {})); 31 | expectType(vinylFileSync('./')); 32 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | import process from 'node:process'; 3 | import {fileURLToPath} from 'node:url'; 4 | import {Buffer} from 'node:buffer'; 5 | import test from 'ava'; 6 | import {isStream} from 'is-stream'; 7 | import {vinylFile, vinylFileSync} from './index.js'; 8 | 9 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); 10 | 11 | test('async', async t => { 12 | const index = await vinylFile('index.js'); 13 | t.is(index.cwd, process.cwd()); 14 | t.is(index.base, process.cwd()); 15 | t.is(index.path, path.join(__dirname, 'index.js')); 16 | t.true(Buffer.isBuffer(index.contents)); 17 | t.true(index.contents.length > 10); 18 | 19 | const readme = await vinylFile('readme.md', {cwd: 'node_modules/ava'}); 20 | t.regex(readme.contents.toString('utf8'), /How is the name written and pronounced/); 21 | 22 | const wow = await vinylFile('index.js', {base: 'wow'}); 23 | t.is(wow.base, 'wow'); 24 | 25 | const noContents = await vinylFile('index.js', {read: false}); 26 | t.is(noContents.contents, null); 27 | 28 | const stream = await vinylFile('index.js', {buffer: false}); 29 | t.true(isStream(stream.contents)); 30 | }); 31 | 32 | test('sync', t => { 33 | const index = vinylFileSync('index.js'); 34 | t.is(index.cwd, process.cwd()); 35 | t.is(index.base, process.cwd()); 36 | t.is(index.path, path.join(__dirname, 'index.js')); 37 | t.true(Buffer.isBuffer(index.contents)); 38 | t.true(index.contents.length > 10); 39 | 40 | const readme = vinylFileSync('readme.md', {cwd: 'node_modules/ava'}); 41 | t.regex(readme.contents.toString('utf8'), /How is the name written and pronounced/); 42 | 43 | const wow = vinylFileSync('index.js', {base: 'wow'}); 44 | t.is(wow.base, 'wow'); 45 | 46 | const noContents = vinylFileSync('index.js', {read: false}); 47 | t.is(noContents.contents, null); 48 | 49 | const stream = vinylFileSync('index.js', {buffer: false}); 50 | t.true(isStream(stream.contents)); 51 | }); 52 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import {type BufferFile, type NullFile, type StreamFile} from 'vinyl'; 2 | 3 | export type Options = { 4 | /** 5 | Override the `base` of the Vinyl file. 6 | 7 | @default process.cwd() 8 | */ 9 | base?: string; 10 | 11 | /** 12 | Override the `cwd` (current working directory) of the Vinyl file. 13 | 14 | @default process.cwd() 15 | */ 16 | cwd?: string; 17 | 18 | /** 19 | Setting this to `false` will return `file.contents` as a stream. This is useful when working with large files. 20 | 21 | __Note:__ Plugins might not implement support for streams. 22 | 23 | @default true 24 | */ 25 | buffer?: boolean; 26 | 27 | /** 28 | Setting this to `false` will return `file.contents` as `null` and not read the file at all. 29 | 30 | @default true 31 | */ 32 | read?: boolean; 33 | }; 34 | 35 | /** 36 | Create a Vinyl file asynchronously and return it. 37 | 38 | @param path - The path to the file to create a Vinyl file of. 39 | 40 | @example 41 | ``` 42 | import {vinylFile} from 'vinyl-file'; 43 | 44 | const file = await vinylFile('index.js'); 45 | 46 | console.log(file.path); 47 | //=> '/Users/sindresorhus/dev/vinyl-file/index.js' 48 | 49 | console.log(file.cwd); 50 | //=> '/Users/sindresorhus/dev/vinyl-file' 51 | ``` 52 | */ 53 | export function vinylFile(path: string, options: Options & {read: false}): Promise; 54 | export function vinylFile(path: string, options: Options & {buffer: false}): Promise; 55 | export function vinylFile(path: string, options?: Options): Promise; 56 | 57 | /** 58 | Create a Vinyl file synchronously and return it. 59 | 60 | @param path - The path to the file to create a Vinyl file of. 61 | 62 | @example 63 | ``` 64 | import {vinylFileSync} from 'vinyl-file'; 65 | 66 | const file = vinylFileSync('index.js'); 67 | 68 | console.log(file.path); 69 | //=> '/Users/sindresorhus/dev/vinyl-file/index.js' 70 | 71 | console.log(file.cwd); 72 | //=> '/Users/sindresorhus/dev/vinyl-file' 73 | ``` 74 | */ 75 | export function vinylFileSync(path: string, options: Options & {read: false}): NullFile; 76 | export function vinylFileSync(path: string, options: Options & {buffer: false}): StreamFile; 77 | export function vinylFileSync(path: string, options?: Options): BufferFile; 78 | --------------------------------------------------------------------------------