-
Notifications
You must be signed in to change notification settings - Fork 1
Cli: Add mimic config #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2c2ec72
Add mimic config
alavarello 71c9b6d
Implement multi task
alavarello b86be30
Fix tests
alavarello b8a6027
Improve parsing and add test
alavarello b3157ad
Add --no-config
alavarello a4e4b5d
Remove .only
alavarello 0c34cf7
Rename task for functions
alavarello f1c3b7d
Use function path when name is not defined
alavarello 305f581
Fix const and add warnings
alavarello 1baf333
Fix typo
alavarello e6d9fb5
Improve testing command descriptions for include and exclude
alavarello 3f8122d
Fix functions description
alavarello 280435f
Fux test
alavarello 67ea294
Improve empty file error
alavarello 3a8d731
Improve error message
alavarello bcc3ad3
Hide helper commands
alavarello 0ca6c50
Fix comments
alavarello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
alavarello marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,133 @@ | ||
| import { Command, Flags } from '@oclif/core' | ||
| import * as fs from 'fs' | ||
| import * as yaml from 'js-yaml' | ||
| import { z } from 'zod' | ||
|
|
||
| import log from '../log' | ||
| import { FlagsType } from '../types' | ||
|
|
||
| export type FunctionsFlags = FlagsType<typeof Functions> | ||
|
|
||
| export const FunctionConfigSchema = z.object({ | ||
| name: z.string().min(1, 'Function name is required'), | ||
| manifest: z.string().min(1, 'Manifest path is required'), | ||
| function: z.string().min(1, 'Function path is required'), | ||
| 'build-directory': z.string().min(1, 'Build directory is required'), | ||
| 'types-directory': z.string().min(1, 'Types directory is required'), | ||
| }) | ||
|
|
||
| export type FunctionConfig = z.infer<typeof FunctionConfigSchema> | ||
|
|
||
| export const MimicConfigSchema = z.object({ | ||
| functions: z.array(FunctionConfigSchema).min(1, 'At least one function is required'), | ||
| }) | ||
|
|
||
| export const DefaultFunctionConfig = { | ||
| name: '', | ||
| manifest: 'manifest.yaml', | ||
| function: 'src/function.ts', | ||
| 'build-directory': './build', | ||
| 'types-directory': './src/types', | ||
| } as const | ||
|
|
||
| const MIMIC_CONFIG_FILE = 'mimic.yaml' | ||
|
|
||
| export default class Functions extends Command { | ||
| run(): Promise<void> { | ||
| throw new Error('Method not implemented.') | ||
alavarello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| static override hidden = true | ||
|
|
||
| static flags = { | ||
| 'config-file': Flags.string({ | ||
| description: `Path to the ${MIMIC_CONFIG_FILE} file, this overrides other parameters like build-directory and function`, | ||
| default: MIMIC_CONFIG_FILE, | ||
| }), | ||
| 'no-config': Flags.boolean({ | ||
| description: `Do not read ${MIMIC_CONFIG_FILE}; use defaults and explicit flags instead`, | ||
| default: false, | ||
| }), | ||
| include: Flags.string({ | ||
alavarello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| description: `When ${MIMIC_CONFIG_FILE} exists, only run tasks with these names (space-separated)`, | ||
| multiple: true, | ||
| exclusive: ['exclude'], | ||
| char: 'i', | ||
| }), | ||
| exclude: Flags.string({ | ||
| description: `When ${MIMIC_CONFIG_FILE} exists, exclude tasks with these names (space-separated)`, | ||
| multiple: true, | ||
| exclusive: ['include'], | ||
| char: 'e', | ||
| }), | ||
| } | ||
|
|
||
| public static async runFunctions<T extends FunctionsFlags & Partial<FunctionConfig>>( | ||
| cmd: Command, | ||
| flags: T, | ||
| cmdLogic: (cmd: Command, flags: T) => Promise<void>, | ||
| cmdActions: string | ||
| ): Promise<void> { | ||
| const functions = Functions.filterFunctions(cmd, flags) | ||
| for (const func of functions) { | ||
| log.startAction(`\nStarting ${cmdActions} for function ${func.name ? func.name : func.function}`) | ||
| await cmdLogic(cmd, { ...flags, ...func } as T) | ||
| } | ||
| } | ||
|
|
||
| public static filterFunctions(cmd: Command, flags: FunctionsFlags & Partial<FunctionConfig>): FunctionConfig[] { | ||
| if (flags['no-config']) { | ||
| return [{ ...DefaultFunctionConfig, ...flags }] | ||
| } | ||
|
|
||
| if (!fs.existsSync(flags['config-file'])) { | ||
| if (flags['config-file'] !== MIMIC_CONFIG_FILE) { | ||
| cmd.error(`Could not find ${flags['config-file']}`, { code: 'ConfigNotFound' }) | ||
| } | ||
|
|
||
| // If doesn't exist return the default with the flags the user added | ||
| return [{ ...DefaultFunctionConfig, ...flags }] | ||
| } | ||
|
|
||
| const fileContents = fs.readFileSync(flags['config-file'], 'utf8') | ||
| const rawConfig = yaml.load(fileContents) | ||
|
|
||
| if (!rawConfig || (typeof rawConfig === 'object' && Object.keys(rawConfig).length === 0)) { | ||
| cmd.error(`Invalid ${MIMIC_CONFIG_FILE} configuration: file is empty.`) | ||
| } | ||
|
|
||
| try { | ||
| let { functions } = MimicConfigSchema.parse(rawConfig) | ||
|
|
||
| if (flags.include && flags.include.length > 0) { | ||
| Functions.checkMissingFunctions(cmd, functions, flags.include) | ||
| functions = functions.filter((fn) => flags.include!.includes(fn.name)) | ||
| } | ||
|
|
||
| if (flags.exclude && flags.exclude.length > 0) { | ||
| Functions.checkMissingFunctions(cmd, functions, flags.exclude) | ||
| functions = functions.filter((fn) => !flags.exclude!.includes(fn.name)) | ||
| } | ||
|
|
||
| return functions | ||
| } catch (error) { | ||
| if (error instanceof z.ZodError) { | ||
| const errors = error.errors.map((e) => `${e.path.join('.')}: ${e.message}`).join('\n') | ||
| cmd.error(`Invalid ${MIMIC_CONFIG_FILE} configuration:\n${errors}`, { code: 'InvalidConfig' }) | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| private static checkMissingFunctions( | ||
| cmd: Command, | ||
| functions: FunctionConfig[], | ||
| filteredFunctionNames: string[] | ||
| ): void { | ||
| const functionNames = new Set(functions.map((fn) => fn.name)) | ||
| const missingFunctions = filteredFunctionNames.filter((name) => !functionNames.has(name)) | ||
| if (missingFunctions.length > 0) { | ||
| cmd.warn(`Functions not found in ${MIMIC_CONFIG_FILE}: ${missingFunctions.join(', ')}`) | ||
| } | ||
| } | ||
| } | ||
lgalende marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.