-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): support json configuration for
tokens create
(#2847)
Closes #2846 Some things to note: - Most CLI options are no longer required, although they must be set either in JSON or through CLI. This could be confusing. I tried to look in to building options conditionally, so that e.g. options could be required if `--json` was not used or default config was not found, but this doesn't seem to be possible - The complete config is validated, so users will still get an error if options are missing. We could probably work on making the errors more clear. - CLI options take priority over JSON config. This is usually how CLI tools work. - Because of this, we need to know whether an option was supplied or not. This means we have to check if the option is set through the CLI or is a default value, before we decide whether it should overwrite the JSON config. The function `getExplicitOptionOnly()` has been added for this purpose. --------- Co-authored-by: Michael Marszalek <[email protected]>
- Loading branch information
Showing
11 changed files
with
319 additions
and
48 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,5 @@ | ||
--- | ||
"@digdir/designsystemet": patch | ||
--- | ||
|
||
Add json config file support for `tokens create` |
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,29 @@ | ||
import { z } from 'zod'; | ||
import { convertToHex } from '../src/colors/index.js'; | ||
|
||
/** | ||
* This defines the structure of the JSON config file | ||
*/ | ||
export const configFileSchema = z.object({ | ||
outDir: z.string().optional(), | ||
themes: z.record( | ||
z.object({ | ||
colors: z.object({ | ||
main: z.record(z.string().transform(convertToHex)), | ||
support: z.record(z.string().transform(convertToHex)), | ||
neutral: z.string().transform(convertToHex), | ||
}), | ||
typography: z.object({ | ||
fontFamily: z.string(), | ||
}), | ||
borderRadius: z.number(), | ||
}), | ||
), | ||
}); | ||
|
||
/** | ||
* This defines the structure of the final configuration after combining the config file, | ||
* command-line options and default values. | ||
*/ | ||
export const combinedConfigSchema = configFileSchema.required(); | ||
export type CombinedConfigSchema = z.infer<typeof combinedConfigSchema>; |
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,29 @@ | ||
import type { Command, OptionValueSource, OptionValues } from '@commander-js/extra-typings'; | ||
|
||
const getOptionIfMatchingSource = | ||
(...sources: OptionValueSource[]) => | ||
<Args extends unknown[], Opts extends OptionValues, K extends keyof Opts>( | ||
command: Command<Args, Opts>, | ||
option: K, | ||
) => { | ||
const source = command.getOptionValueSource(option); | ||
console.log(sources, source); | ||
if (sources.includes(source)) { | ||
return command.getOptionValue(option); | ||
} | ||
}; | ||
|
||
export type OptionGetter = ReturnType<typeof getOptionIfMatchingSource>; | ||
|
||
/** | ||
* Get an option value if it is explicitly supplied to the CLI command. | ||
* The difference between this and using the option directly is that we return undefined | ||
* instead of the default value if the option was not explicitly set. | ||
*/ | ||
export const getExplicitOptionOnly = getOptionIfMatchingSource('cli'); | ||
|
||
/** | ||
* This function is basically the default behaviour, unlike {@link getExplicitOptionOnly}. | ||
* It is provided so that the program can choose its behaviour as needed. | ||
*/ | ||
export const getExplicitOrDefaultOption = getOptionIfMatchingSource('cli', 'default'); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { type CreateTokensOptions, createTokens, colorCliOptions } from './create.js'; | ||
export { createTokens, colorCliOptions } from './create.js'; | ||
export type { Theme as CreateTokensOptions } from './types.js'; |
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.