Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/virtual_file_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { relative } from 'node:path/posix'
import lodash from '@poppinss/utils/lodash'
import string from '@poppinss/utils/string'
import { readFile } from 'node:fs/promises'
import { naturalSort } from '@poppinss/utils'
import { Lang, parse, type SgNode } from '@ast-grep/napi'
import picomatch, { type PicomatchOptions, type Matcher } from 'picomatch'

Expand All @@ -23,6 +22,7 @@ import { type RecursiveFileTree, type VirtualFileSystemOptions } from './types/c

const BYPASS_FN = <T>(input: T) => input
const DEFAULT_GLOB = ['**/!(*.d).ts', '**/*.tsx', '**/*.js']
const NATURAL_SORT = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare

/**
* Virtual file system for managing and tracking files with AST parsing capabilities.
Expand Down Expand Up @@ -105,7 +105,7 @@ export class VirtualFileSystem {

const filesList = await crawler.crawl(this.#source).withPromise()
debug('scanned files %O', filesList)
const sortedFiles = filesList.sort(naturalSort)
const sortedFiles = filesList.sort(NATURAL_SORT)
this.#files.clear()

for (let filePath of sortedFiles) {
Expand Down
30 changes: 30 additions & 0 deletions tests/virtual_file_system.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@
)
})

test('naturally sort file paths', async ({ fs, assert }) => {
await fs.create('controllers/item_10.ts', '')
await fs.create('controllers/item_2.ts', '')
await fs.create('controllers/item_01.ts', '')
await fs.create('controllers/item_1.ts', '')
await fs.create('controllers/café.ts', '')
await fs.create('controllers/cafe.ts', '')
await fs.create('controllers/alpha.ts', '')
await fs.create('controllers/Alpha.ts', '')
await fs.create('controllers/nested_10/item_2.ts', '')
await fs.create('controllers/nested_2/item_10.ts', '')

const source = string.toUnixSlash(join(fs.basePath, 'controllers'))
const vfs = new VirtualFileSystem(source)
await vfs.scan()

assert.deepEqual(Object.keys(vfs.asList()), [

Check failure on line 63 in tests/virtual_file_system.spec.ts

View workflow job for this annotation

GitHub Actions / test / test_windows (latest)

Virtual file system / naturally sort file paths

expected [ 'alpha', 'cafe', 'café', …(6) ] to deeply equal [ 'Alpha', 'alpha', 'cafe', …(7) ] - Expected - 1 + Received + 0 Array [ - "Alpha", "alpha", "cafe", "café", "item_01", "item_1", "item_2", "item_10", "nested_2/item_10", "nested_10/item_2", ]

Check failure on line 63 in tests/virtual_file_system.spec.ts

View workflow job for this annotation

GitHub Actions / test / test_windows (latest)

Virtual file system / naturally sort file paths

expected [ 'alpha', 'cafe', 'café', …(6) ] to deeply equal [ 'Alpha', 'alpha', 'cafe', …(7) ] - Expected - 1 + Received + 0 Array [ - "Alpha", "alpha", "cafe", "café", "item_01", "item_1", "item_2", "item_10", "nested_2/item_10", "nested_10/item_2", ]
'Alpha',
'alpha',
'cafe',
'café',
'item_01',
'item_1',
'item_2',
'item_10',
'nested_2/item_10',
'nested_10/item_2',
])
})

test('list files as a tree', async ({ fs, assert }) => {
await setupFakeAdonisproject(fs)
await createControllers()
Expand Down
Loading