Skip to content

Commit

Permalink
Merge pull request #411 from mrmlnc/update_nodelib
Browse files Browse the repository at this point in the history
ISSUE-371: Update @nodelib/fs.* dependencies
  • Loading branch information
mrmlnc authored Jul 29, 2023
2 parents aca6499 + 99dc34e commit 9158524
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 73 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ This package provides methods for traversing the file system and returning pathn
<summary><strong>Details</strong></summary>

* [Highlights](#highlights)
* [Old and modern mode](#old-and-modern-mode)
* [Pattern syntax](#pattern-syntax)
* [Basic syntax](#basic-syntax)
* [Advanced syntax](#advanced-syntax)
Expand Down Expand Up @@ -74,15 +73,6 @@ This package provides methods for traversing the file system and returning pathn
* Object mode. Can return more than just strings.
* Error-tolerant.

## Old and modern mode

This package works in two modes, depending on the environment in which it is used.

* **Old mode**. Node.js below 10.10 or when the [`stats`](#stats) option is *enabled*.
* **Modern mode**. Node.js 10.10+ and the [`stats`](#stats) option is *disabled*.

The modern mode is faster. Learn more about the [internal mechanism][nodelib_fs_scandir_old_and_modern_modern].

## Pattern syntax

> :warning: Always use forward-slashes in glob expressions (patterns and [`ignore`](#ignore) option). Use backslashes for escaping characters.
Expand Down Expand Up @@ -532,8 +522,6 @@ fg.sync('*', { stats: true }); // [{ name: 'index.js', path: 'src/index.js', di
```

> :book: Returns `fs.stat` instead of `fs.lstat` for symbolic links when the [`followSymbolicLinks`](#followsymboliclinks) option is specified.
>
> :warning: Unlike [object mode](#objectmode) this mode requires additional calls to the file system. On average, this mode is slower at least twice. See [old and modern mode](#old-and-modern-mode) for more details.
#### unique

Expand Down Expand Up @@ -816,7 +804,6 @@ This software is released under the terms of the MIT license.
[node_js_fs_class_fs_stats]: https://nodejs.org/api/fs.html#fs_class_fs_stats
[node_js_stream_readable_streams]: https://nodejs.org/api/stream.html#stream_readable_streams
[node_js]: https://nodejs.org/en
[nodelib_fs_scandir_old_and_modern_modern]: https://github.com/nodelib/nodelib/blob/master/packages/fs/fs.scandir/README.md#old-and-modern-mode
[npm_normalize_path]: https://www.npmjs.com/package/normalize-path
[npm_unixify]: https://www.npmjs.com/package/unixify
[picomatch_matching_behavior]: https://github.com/micromatch/picomatch#matching-behavior-vs-bash
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"implementation"
],
"devDependencies": {
"@nodelib/fs.macchiato": "^1.0.1",
"@nodelib/fs.macchiato": "^2.0.0",
"@types/glob-parent": "^5.1.0",
"@types/merge2": "^1.1.4",
"@types/micromatch": "^4.0.0",
Expand All @@ -49,8 +49,8 @@
"typescript": "^4.9.5"
},
"dependencies": {
"@nodelib/fs.stat": "^2.0.2",
"@nodelib/fs.walk": "^1.2.3",
"@nodelib/fs.stat": "^3.0.0",
"@nodelib/fs.walk": "^2.0.0",
"glob-parent": "^5.1.2",
"merge2": "^1.3.0",
"micromatch": "^4.0.4"
Expand Down
1 change: 1 addition & 0 deletions src/benchmark/suites/regression/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Glob {
public async measurePreviousVersion(): Promise<void> {
const glob = await utils.importAndMeasure(utils.importPreviousFastGlob);

// @ts-expect-error remove this line after the next major release.
await this._measure(() => glob(this._pattern, this._options));
}

Expand Down
1 change: 1 addition & 0 deletions src/benchmark/suites/regression/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Glob {
public async measurePreviousVersion(): Promise<void> {
const glob = await utils.importAndMeasure(utils.importPreviousFastGlob);

// @ts-expect-error remove this line after the next major release.
await this._measure(() => glob.stream(this._pattern, this._options));
}

Expand Down
1 change: 1 addition & 0 deletions src/benchmark/suites/regression/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Glob {
public async measurePreviousVersion(): Promise<void> {
const glob = await utils.importAndMeasure(utils.importPreviousFastGlob);

// @ts-expect-error remove this line after the next major release.
this._measure(() => glob.sync(this._pattern, this._options));
}

Expand Down
5 changes: 3 additions & 2 deletions src/readers/reader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';

import { Stats } from '@nodelib/fs.macchiato';
import { Stats, StatsMode } from '@nodelib/fs.macchiato';

import Settings, { Options } from '../settings';
import { Entry, Pattern } from '../types';
Expand Down Expand Up @@ -59,8 +59,9 @@ describe('Readers → Reader', () => {
it('should return created entry', () => {
const reader = getReader();
const pattern = 'config.json';
const stats = new Stats({ mode: StatsMode.File });

const actual = reader.makeEntry(new Stats(), pattern);
const actual = reader.makeEntry(stats, pattern);

assert.strictEqual(actual.name, pattern);
assert.strictEqual(actual.path, pattern);
Expand Down
24 changes: 6 additions & 18 deletions src/tests/e2e/patterns/root.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ const CWD = process.cwd().replace(/\\/g, '/');
const ROOT = path.parse(CWD).root;

function getRootEntries(root: string, withBase: boolean = false): string[] {
let result: string[] = [];

if (utils.platform.isSupportReaddirWithFileTypes()) {
result = getRootEntriesWithFileTypes(root);
} else {
result = getRootEntriesWithStatsCall(root);
}
let result = getRootEntriesWithFileTypes(root);

if (withBase) {
const separator = root.endsWith('/') ? '' : '/';
Expand All @@ -32,18 +26,12 @@ function getRootEntriesWithFileTypes(root: string): string[] {
.map((item) => item.name);
}

function getRootEntriesWithStatsCall(root: string): string[] {
return fs.readdirSync(root)
.filter((item) => !item.startsWith('.'))
.filter((item) => fs.lstatSync(`${root}/${item}`).isFile());
}

runner.suite('Patterns Root', {
tests: [
{
pattern: '/*',
condition: () => !utils.platform.isWindows(),
expected: () => getRootEntries(ROOT)
expected: () => getRootEntries(ROOT, /** withBase */ true)
},
{
pattern: '/tmp/*',
Expand All @@ -52,13 +40,13 @@ runner.suite('Patterns Root', {
},
{
pattern: '/*',
condition: () => utils.platform.isSupportReaddirWithFileTypes() && utils.platform.isWindows(),
condition: () => utils.platform.isWindows(),
expected: () => getRootEntries('/', /** withBase */ true)
},
// UNC pattern without dynamic sections in the base section
{
pattern: `//?/${ROOT}*`,
condition: () => utils.platform.isSupportReaddirWithFileTypes() && utils.platform.isWindows(),
condition: () => utils.platform.isWindows(),
expected: () => getRootEntries(`//?/${ROOT}`, /** withBase */ true)
}
]
Expand All @@ -71,7 +59,7 @@ runner.suite('Patterns Root (cwd)', {
options: {
cwd: ROOT
},
condition: () => !utils.platform.isWindows() || utils.platform.isSupportReaddirWithFileTypes(),
condition: () => !utils.platform.isWindows(),
expected: () => getRootEntries(ROOT)
},
// UNC on Windows
Expand All @@ -80,7 +68,7 @@ runner.suite('Patterns Root (cwd)', {
options: {
cwd: `//?/${ROOT}`
},
condition: () => utils.platform.isSupportReaddirWithFileTypes() && utils.platform.isWindows(),
condition: () => utils.platform.isWindows(),
expected: () => getRootEntries(`//?/${ROOT}`)
}
]
Expand Down
21 changes: 6 additions & 15 deletions src/tests/utils/entry.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as path from 'path';

import { Dirent, Stats } from '@nodelib/fs.macchiato';
import { Dirent, DirentType, Stats } from '@nodelib/fs.macchiato';

import { Entry } from '../../types';

class EntryBuilder {
private _isFile: boolean = true;
private _isDirectory: boolean = false;
private _isSymbolicLink: boolean = false;
private _entryType: DirentType = DirentType.Unknown;

private readonly _entry: Entry = {
name: '',
Expand All @@ -23,21 +21,19 @@ class EntryBuilder {
}

public file(): this {
this._isFile = true;
this._isDirectory = false;
this._entryType = DirentType.File;

return this;
}

public directory(): this {
this._isDirectory = true;
this._isFile = false;
this._entryType = DirentType.Directory;

return this;
}

public symlink(): this {
this._isSymbolicLink = true;
this._entryType = DirentType.Link;

return this;
}
Expand All @@ -49,12 +45,7 @@ class EntryBuilder {
}

public build(): Entry {
this._entry.dirent = new Dirent({
name: this._entry.name,
isFile: this._isFile,
isDirectory: this._isDirectory,
isSymbolicLink: this._isSymbolicLink
});
this._entry.dirent = new Dirent(this._entry.name, this._entryType);

return this._entry;
}
Expand Down
20 changes: 0 additions & 20 deletions src/tests/utils/platform.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
import * as os from 'os';

const NODE_PROCESS_VERSION_PARTS = process.versions.node.split('.');

const MAJOR_VERSION = parseInt(NODE_PROCESS_VERSION_PARTS[0], 10);
const MINOR_VERSION = parseInt(NODE_PROCESS_VERSION_PARTS[1], 10);

const SUPPORTED_MAJOR_VERSION = 10;
const SUPPORTED_MINOR_VERSION = 10;

const IS_MATCHED_BY_MAJOR = MAJOR_VERSION > SUPPORTED_MAJOR_VERSION;
const IS_MATCHED_BY_MAJOR_AND_MINOR = MAJOR_VERSION === SUPPORTED_MAJOR_VERSION && MINOR_VERSION >= SUPPORTED_MINOR_VERSION;

/**
* IS `true` for Node.js 10.10 and greater.
*/
export const IS_SUPPORT_READDIR_WITH_FILE_TYPES = IS_MATCHED_BY_MAJOR || IS_MATCHED_BY_MAJOR_AND_MINOR;

export function isSupportReaddirWithFileTypes(): boolean {
return IS_SUPPORT_READDIR_WITH_FILE_TYPES;
}

export function isWindows(): boolean {
return os.platform() === 'win32';
}
Expand Down
6 changes: 4 additions & 2 deletions src/utils/fs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as assert from 'assert';

import { Stats } from '@nodelib/fs.macchiato';
import { Stats, StatsMode } from '@nodelib/fs.macchiato';

import * as util from './fs';

describe('Utils → FS', () => {
describe('.createDirentFromStats', () => {
it('should convert fs.Stats to fs.Dirent', () => {
const actual = util.createDirentFromStats('name', new Stats());
const stats = new Stats({ mode: StatsMode.File });
const actual = util.createDirentFromStats('name', stats);

assert.strictEqual(actual.name, 'name');
assert.ok(!actual.isBlockDevice());
Expand Down

0 comments on commit 9158524

Please sign in to comment.