-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #814 from chromaui/tom/ap-3623-add-cli-support-for…
…-a-chromaticjs-config-file-that-allows Add support for a JSON configuration file
- Loading branch information
Showing
20 changed files
with
371 additions
and
44 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ jobs: | |
build: | ||
docker: | ||
- image: cimg/node:16.18.0 | ||
resource_class: xlarge | ||
|
||
working_directory: ~/repo | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { readFile } from 'jsonfile'; | ||
import { getConfiguration } from './getConfiguration'; | ||
|
||
jest.mock('jsonfile'); | ||
const mockedReadFile = jest.mocked(readFile); | ||
|
||
beforeEach(() => { | ||
mockedReadFile.mockReset(); | ||
}); | ||
|
||
it('reads configuration successfully', async () => { | ||
mockedReadFile.mockResolvedValue({ projectToken: 'json-file-token' }); | ||
|
||
expect(await getConfiguration()).toEqual({ projectToken: 'json-file-token' }); | ||
}); | ||
|
||
it('reads from chromatic.config.json by default', async () => { | ||
mockedReadFile.mockResolvedValue({ projectToken: 'json-file-token' }).mockClear(); | ||
await getConfiguration(); | ||
|
||
expect(mockedReadFile).toHaveBeenCalledWith('chromatic.config.json'); | ||
}); | ||
|
||
it('can read from a different location', async () => { | ||
mockedReadFile.mockResolvedValue({ projectToken: 'json-file-token' }).mockClear(); | ||
await getConfiguration('test.file'); | ||
|
||
expect(mockedReadFile).toHaveBeenCalledWith('test.file'); | ||
}); | ||
|
||
it('returns nothing if there is no config file and it was not specified', async () => { | ||
mockedReadFile.mockRejectedValue(new Error('ENOENT')); | ||
|
||
expect(await getConfiguration()).toEqual({}); | ||
}); | ||
|
||
it('returns nothing if there is no config file and it was specified', async () => { | ||
mockedReadFile.mockRejectedValue(new Error('ENOENT')); | ||
|
||
await expect(getConfiguration('test.file')).rejects.toThrow(/could not be found/); | ||
}); | ||
|
||
it('errors if config file contains invalid data', async () => { | ||
mockedReadFile.mockResolvedValue({ projectToken: 1 }); | ||
|
||
await expect(getConfiguration('test.file')).rejects.toThrow(/projectToken/); | ||
}); | ||
|
||
it('errors if config file contains unknown keys', async () => { | ||
mockedReadFile.mockResolvedValue({ random: 1 }); | ||
|
||
await expect(getConfiguration('test.file')).rejects.toThrow(/random/); | ||
}); |
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,63 @@ | ||
import { readFile } from 'jsonfile'; | ||
import { ZodError, z } from 'zod'; | ||
import { missingConfigurationFile } from '../ui/messages/errors/missingConfigurationFile'; | ||
import { unparseableConfigurationFile } from '../ui/messages/errors/unparseableConfigurationFile'; | ||
import { invalidConfigurationFile } from '../ui/messages/errors/invalidConfigurationFile'; | ||
|
||
const configurationSchema = z | ||
.object({ | ||
projectId: z.string(), | ||
projectToken: z.string(), // deprecated | ||
|
||
onlyChanged: z.union([z.string(), z.boolean()]), | ||
onlyStoryFiles: z.array(z.string()), | ||
onlyStoryNames: z.array(z.string()), | ||
untraced: z.array(z.string()), | ||
externals: z.array(z.string()), | ||
debug: z.boolean(), | ||
diagnostics: z.union([z.string(), z.boolean()]), | ||
junitReport: z.union([z.string(), z.boolean()]), | ||
zip: z.boolean(), | ||
autoAcceptChanges: z.union([z.string(), z.boolean()]), | ||
exitZeroOnChanges: z.union([z.string(), z.boolean()]), | ||
exitOnceUploaded: z.union([z.string(), z.boolean()]), | ||
ignoreLastBuildOnBranch: z.string(), | ||
|
||
buildScriptName: z.string(), | ||
outputDir: z.string(), | ||
|
||
storybookBuildDir: z.string(), | ||
storybookBaseDir: z.string(), | ||
storybookConfigDir: z.string(), | ||
}) | ||
.partial() | ||
.strict(); | ||
|
||
export type Configuration = z.infer<typeof configurationSchema>; | ||
|
||
export async function getConfiguration(configFile?: string) { | ||
const usedConfigFile = configFile || 'chromatic.config.json'; | ||
try { | ||
const rawJson = await readFile(usedConfigFile); | ||
|
||
return configurationSchema.parse(rawJson); | ||
} catch (err) { | ||
// Config file does not exist | ||
if (err.message.match(/ENOENT/)) { | ||
// The user passed no configFile option so it's OK for the file not to exist | ||
if (!configFile) { | ||
return {}; | ||
} | ||
if (configFile) { | ||
throw new Error(missingConfigurationFile(configFile)); | ||
} | ||
} | ||
if (err.message.match('Unexpected string')) { | ||
throw new Error(unparseableConfigurationFile(usedConfigFile, err)); | ||
} | ||
if (err instanceof ZodError) { | ||
throw new Error(invalidConfigurationFile(usedConfigFile, err)); | ||
} | ||
throw err; | ||
} | ||
} |
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
Oops, something went wrong.