-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support to look for default config names in the same places as …
…vite (#168) This Action will now check all possible vite- and vitest-config-file extensions by default, so by the pattern "vite[st].config.{t|mt|ct|j|mj|cj}s"). Thank you to @goibon 🙏🏻
- Loading branch information
Showing
5 changed files
with
85 additions
and
3 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
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,32 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import path from "path"; | ||
import * as core from "@actions/core" | ||
import { getViteConfigPath } from "./getViteConfigPath"; | ||
|
||
describe("getViteConfigPath", () => { | ||
const mockWorkingDirectory = path.resolve( | ||
__dirname, | ||
"..", | ||
"test", | ||
"mockConfig" | ||
); | ||
|
||
it("resolves with a full path if a file at the provided path exists", async (): Promise<void> => { | ||
await expect( | ||
getViteConfigPath(mockWorkingDirectory, "vitest.config.all.js") | ||
).resolves.toMatch('test/mockConfig/vitest.config.all.js'); | ||
}); | ||
|
||
it("resolves with a full path if no path is provided but a file with a default name exists", async (): Promise<void> => { | ||
await expect(getViteConfigPath(mockWorkingDirectory, "")).resolves.toMatch( | ||
'test/mockConfig/vitest.config.js' | ||
); | ||
}); | ||
|
||
it("rejects if config file can not be found", async (): Promise<void> => { | ||
vi.spyOn(core, 'setFailed').mockImplementationOnce(() => { }) | ||
await expect( | ||
getViteConfigPath(mockWorkingDirectory, "doesNotExist") | ||
).rejects.toThrow(/unable to find config file/i); | ||
}); | ||
}); |
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,50 @@ | ||
import * as core from "@actions/core"; | ||
import path from "node:path"; | ||
import { constants, promises as fs } from "fs"; | ||
import { stripIndent } from "common-tags"; | ||
|
||
const testFilePath = async (workingDirectory: string, filePath: string) => { | ||
const resolvedPath = path.resolve(workingDirectory, filePath); | ||
await fs.access(resolvedPath, constants.R_OK); | ||
return resolvedPath; | ||
}; | ||
|
||
const defaultPaths = [ | ||
"vitest.config.ts", | ||
"vitest.config.mts", | ||
"vitest.config.cts", | ||
"vitest.config.js", | ||
"vitest.config.mjs", | ||
"vitest.config.cjs", | ||
"vite.config.ts", | ||
"vite.config.mts", | ||
"vite.config.cts", | ||
"vite.config.js", | ||
"vite.config.mjs", | ||
"vite.config.cjs", | ||
]; | ||
|
||
const getViteConfigPath = async (workingDirectory: string, input: string) => { | ||
try { | ||
if (input === "") { | ||
return await Promise.any( | ||
defaultPaths.map((filePath) => testFilePath(workingDirectory, filePath)) | ||
); | ||
} | ||
|
||
return await testFilePath(workingDirectory, input); | ||
} catch (error) { | ||
core.setFailed(stripIndent` | ||
Failed to read vite config file"${workingDirectory}/${input}" or any of the default locations. | ||
Make sure you provide the vite-config-path option if you're using a non-default location or name of your config file. | ||
`); | ||
throw new Error( | ||
`Unable to find config file "${workingDirectory}/${input}".`, | ||
{ | ||
cause: error, | ||
} | ||
); | ||
} | ||
}; | ||
|
||
export { getViteConfigPath }; |
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
Empty file.