-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Showing
9 changed files
with
175 additions
and
59 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
92 changes: 92 additions & 0 deletions
92
...ugin/plugins/importUserCode/v1-design/getVikeConfig/crawlPlusFiles/crawlPlusFiles.spec.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,92 @@ | ||
import { expect, describe, it, assert } from 'vitest' | ||
import path from 'path' | ||
import fs from 'fs' | ||
// process.env.DEBUG = 'vike:crawl' | ||
const { crawlPlusFiles } = await import('../crawlPlusFiles') | ||
import { fileURLToPath } from 'url' | ||
const __dirname_ = path.dirname(fileURLToPath(import.meta.url)) | ||
const userRootDir = path.join(__dirname_, './test-file-structure') | ||
|
||
describe('crawlPlusFiles()', () => { | ||
it('works', async ({ onTestFinished }) => { | ||
const { clean } = createFiles([ | ||
'pages/about/+bla.mdx', | ||
'pages/git-ignored/+bla.mdx', | ||
'pages/about/+ignored.telefunc.ts', | ||
'pages/about/+ignored.generated.js', | ||
'pages/ejected/+ignored.js', | ||
'pages/node_modules/+ignored.js' | ||
]) | ||
onTestFinished(() => clean()) | ||
|
||
const filesWithGit = await crawl() | ||
expect(filesWithGit).toMatchInlineSnapshot(` | ||
[ | ||
"/+config.js", | ||
"/pages/+config.js", | ||
"/pages/about/+bla.mdx", | ||
] | ||
`) | ||
assert(!JSON.stringify(filesWithGit).includes('ignored')) | ||
|
||
process.env.VIKE_CRAWL = '{git:false}' | ||
const filesWithGlob = await crawl() | ||
expect(filesWithGlob).toMatchInlineSnapshot(` | ||
[ | ||
"/+config.js", | ||
"/pages/+config.js", | ||
"/pages/about/+bla.mdx", | ||
"/pages/git-ignored/+bla.mdx", | ||
] | ||
`) | ||
}) | ||
}) | ||
|
||
async function crawl() { | ||
return normalize(await crawlPlusFiles(userRootDir, null)) | ||
} | ||
|
||
function normalize(files: Awaited<ReturnType<typeof crawlPlusFiles>>) { | ||
return files.map((f) => f.filePathAbsoluteUserRootDir).sort() | ||
} | ||
|
||
function createFiles(files: string[]) { | ||
const filePaths = files.map((file) => path.join(userRootDir, file)) | ||
|
||
// Create empty files | ||
filePaths.forEach((filePath) => { | ||
fs.mkdirSync(path.dirname(filePath), { recursive: true }) | ||
fs.writeFileSync(filePath, '') | ||
}) | ||
|
||
return { | ||
clean: () => { | ||
filePaths.forEach((filePath) => { | ||
assert(fs.existsSync(filePath)) | ||
fs.unlinkSync(filePath) // Remove filePath | ||
}) | ||
removeEmptyDirectories(userRootDir) | ||
} | ||
} | ||
} | ||
|
||
function removeEmptyDirectories(dirPath: string): void { | ||
// Read the directory contents | ||
const files = fs.readdirSync(dirPath) | ||
|
||
// Iterate through the files and subdirectories | ||
for (const file of files) { | ||
const fullPath = path.join(dirPath, file) | ||
|
||
// Check if it's a directory | ||
if (fs.statSync(fullPath).isDirectory()) { | ||
// Recursively clean up the subdirectory | ||
removeEmptyDirectories(fullPath) | ||
} | ||
} | ||
|
||
// Re-check the directory; remove it if it's now empty | ||
if (fs.readdirSync(dirPath).length === 0) { | ||
fs.rmdirSync(dirPath) | ||
} | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
...gins/importUserCode/v1-design/getVikeConfig/crawlPlusFiles/test-file-structure/.gitignore
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 @@ | ||
/pages/git-ignored/ |
Empty file.
Oops, something went wrong.