-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f49ee9
commit 7c5df2f
Showing
6 changed files
with
157 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'mdxts': minor | ||
--- | ||
|
||
Fixes data source ordering to use strings instead of `parseInt` to ensure that the items are always ordered correctly. | ||
|
||
### Breaking Changes | ||
|
||
The `order` property for a data source item is now a padded string instead of a number. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/mdxts/src/utils/get-source-files-sort-order.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Project, type Directory } from 'ts-morph' | ||
|
||
import { getSourceFilesOrderMap } from './get-source-files-sort-order' | ||
|
||
describe('getSourceFilesOrderMap', () => { | ||
let project: Project | ||
let rootDirectory: Directory | ||
|
||
beforeEach(() => { | ||
project = new Project({ | ||
useInMemoryFileSystem: true, | ||
}) | ||
|
||
rootDirectory = project.createDirectory('/src') | ||
|
||
const componentsDirectory = rootDirectory.createDirectory('components') | ||
|
||
componentsDirectory.createSourceFile('Button.tsx', '') | ||
componentsDirectory.createSourceFile('index.ts', '') | ||
|
||
const codeBlockDirectory = componentsDirectory.createDirectory('CodeBlock') | ||
codeBlockDirectory.createSourceFile('CodeBlock.tsx', '') | ||
|
||
rootDirectory.createDirectory('utils').createSourceFile('helpers.ts', '') | ||
}) | ||
|
||
test('should return an empty object when no files or directories are present', () => { | ||
const emptyDirectory = project.createDirectory('/empty') | ||
const orderMap = getSourceFilesOrderMap(emptyDirectory, []) | ||
expect(orderMap).toEqual({}) | ||
}) | ||
|
||
test('should return the correct order for files and directories', () => { | ||
const allPublicPaths = [ | ||
'/src/components/Button.tsx', | ||
'/src/components/CodeBlock/CodeBlock.tsx', | ||
'/src/utils/helpers.ts', | ||
] | ||
|
||
const orderMap = getSourceFilesOrderMap(rootDirectory, allPublicPaths) | ||
|
||
const expectedOrderMap = { | ||
'/src/components': '01', | ||
'/src/components/Button.tsx': '01.01', | ||
'/src/components/CodeBlock': '01.02', | ||
'/src/components/CodeBlock/CodeBlock.tsx': '01.02.01', | ||
'/src/utils': '02', | ||
'/src/utils/helpers.ts': '02.01', | ||
} | ||
expect(orderMap).toEqual(expectedOrderMap) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Directory } from 'ts-morph' | ||
|
||
/** Returns a map of source file paths to their sort order. */ | ||
export function getSourceFilesOrderMap( | ||
directory: Directory, | ||
allPublicPaths: string[] | ||
): Record<string, string> { | ||
const orderMap: Record<string, string> = {} | ||
traverseDirectory(directory, '', orderMap, allPublicPaths) | ||
return orderMap | ||
} | ||
|
||
/** Recursively traverses a directory adding each file and directory to path order map. */ | ||
function traverseDirectory( | ||
directory: Directory, | ||
prefix: string, | ||
orderMap: Record<string, string>, | ||
allPublicPaths: string[], | ||
level: number = 1, | ||
index: number = 1 | ||
) { | ||
const entries: { | ||
name: string | ||
path: string | ||
directory?: Directory | ||
}[] = [] | ||
const directories = directory.getDirectories() | ||
|
||
directories.forEach((sourceDirectory) => { | ||
entries.push({ | ||
name: sourceDirectory.getBaseName(), | ||
path: sourceDirectory.getPath(), | ||
directory: sourceDirectory, | ||
}) | ||
}) | ||
|
||
const files = directory | ||
.getSourceFiles() | ||
.filter((file) => allPublicPaths.includes(file.getFilePath())) | ||
|
||
files.forEach((file) => { | ||
entries.push({ | ||
name: file.getBaseName(), | ||
path: file.getFilePath(), | ||
}) | ||
}) | ||
|
||
// Sort alphabetically by name | ||
entries.sort((a, b) => a.name.localeCompare(b.name)) | ||
|
||
// Iterate through each entry and assign an order | ||
for (let entryIndex = 0; entryIndex < entries.length; entryIndex++) { | ||
const entry = entries[entryIndex] | ||
const orderString = `${prefix}${String(index).padStart(2, '0')}` | ||
|
||
if (entry.directory) { | ||
orderMap[entry.path] = orderString | ||
|
||
traverseDirectory( | ||
entry.directory, | ||
`${orderString}.`, | ||
orderMap, | ||
allPublicPaths, | ||
level + 1, | ||
1 | ||
) | ||
} else { | ||
orderMap[entry.path] = orderString | ||
} | ||
|
||
index++ | ||
} | ||
} |