|
| 1 | +import { NoticeSchema, ProjectSchema, TemplateSchema, VariableSchema } from '@codifycli/schemas'; |
| 2 | + |
| 3 | +type JsonSchema = Record<string, unknown>; |
| 4 | + |
| 5 | +/** An example shown on the resource page of the resource library (same shape as ExampleConfig in plugin-core) */ |
| 6 | +export type CoreExampleConfig = { |
| 7 | + title: string; |
| 8 | + description: string; |
| 9 | + configs: Array<Record<string, unknown>>; |
| 10 | +}; |
| 11 | + |
| 12 | +/** |
| 13 | + * A core config block: handled by the CLI itself and never sent to a plugin. Uploaded to the resource library like the |
| 14 | + * resources of the default plugin so the dashboard editor and the agent can find, validate and complete them. |
| 15 | + */ |
| 16 | +export type CoreResource = { |
| 17 | + type: string; |
| 18 | + schema: JsonSchema; |
| 19 | + allowMultiple: boolean; |
| 20 | + defaultConfig: Record<string, unknown>; |
| 21 | + exampleConfigs: { example1: CoreExampleConfig; example2?: CoreExampleConfig }; |
| 22 | +}; |
| 23 | + |
| 24 | +export const CORE_RESOURCES: CoreResource[] = [ |
| 25 | + { |
| 26 | + type: 'project', |
| 27 | + schema: ProjectSchema, |
| 28 | + allowMultiple: false, |
| 29 | + defaultConfig: {}, |
| 30 | + exampleConfigs: { |
| 31 | + example1: { |
| 32 | + title: 'Project with a description', |
| 33 | + description: 'Describes what the config sets up and the minimum Codify version it needs.', |
| 34 | + configs: [ |
| 35 | + { type: 'project', description: 'My React development machine', version: '1.3.0' }, |
| 36 | + { type: 'template', source: '@codify/react-essentials' }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + type: 'template', |
| 43 | + schema: TemplateSchema, |
| 44 | + allowMultiple: true, |
| 45 | + defaultConfig: { source: '<Replace me here!>' }, |
| 46 | + exampleConfigs: { |
| 47 | + example1: { |
| 48 | + title: 'Include a template', |
| 49 | + description: 'Adds every resource of a published template to the config, in place, without copying them.', |
| 50 | + configs: [ |
| 51 | + { type: 'template', source: '@codify/react-essentials' }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + example2: { |
| 55 | + title: 'Template with variables and conditions', |
| 56 | + description: 'Passes values to the variables of a template and only includes another template on macOS when a variable matches.', |
| 57 | + configs: [ |
| 58 | + { type: 'variable', name: 'needsNode', valueType: 'boolean', label: 'Do you also want Node.js?', default: true }, |
| 59 | + { type: 'template', name: 'website', source: '@codify/basic-website', variables: { siteType: 'blog', projectDirectory: '~/projects/blog' } }, |
| 60 | + { type: 'template', source: '@codify/nodejs', os: ['macOS'], when: { needsNode: true } }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + type: 'variable', |
| 67 | + schema: VariableSchema, |
| 68 | + allowMultiple: true, |
| 69 | + defaultConfig: { name: '<Replace me here!>', valueType: 'text' }, |
| 70 | + exampleConfigs: { |
| 71 | + example1: { |
| 72 | + title: 'Text variable used in a resource', |
| 73 | + description: 'Asks where to clone a project when the config runs and uses the answer as a parameter.', |
| 74 | + configs: [ |
| 75 | + { type: 'variable', name: 'projectDirectory', label: 'Where should the project be cloned?', default: '~/projects/my-app' }, |
| 76 | + { type: 'git-repository', repository: 'https://github.com/satnaing/astro-paper.git', directory: '${{ projectDirectory }}' }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + example2: { |
| 80 | + title: 'Choice that picks the resources', |
| 81 | + description: 'Asks which tools to install and only includes the resources whose when condition matches the answer.', |
| 82 | + configs: [ |
| 83 | + { |
| 84 | + type: 'variable', |
| 85 | + name: 'packageManager', |
| 86 | + valueType: 'enum', |
| 87 | + label: 'Which package manager do you use?', |
| 88 | + options: ['npm', 'pnpm'], |
| 89 | + default: 'pnpm', |
| 90 | + }, |
| 91 | + { type: 'nvm', nodeVersions: ['24'], global: '24' }, |
| 92 | + { type: 'pnpm', when: { packageManager: 'pnpm' }, dependsOn: ['nvm'] }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + type: 'notice', |
| 99 | + schema: NoticeSchema, |
| 100 | + allowMultiple: true, |
| 101 | + defaultConfig: { message: '<Replace me here!>' }, |
| 102 | + exampleConfigs: { |
| 103 | + example1: { |
| 104 | + title: 'Getting started steps', |
| 105 | + description: 'Shows the user how to start their project once everything is installed.', |
| 106 | + configs: [ |
| 107 | + { type: 'variable', name: 'projectDirectory', label: 'Where should the project be cloned?', default: '~/projects/blog' }, |
| 108 | + { type: 'template', name: 'website', source: '@codify/basic-website', variables: { siteType: 'blog', projectDirectory: '${{ projectDirectory }}' } }, |
| 109 | + { |
| 110 | + type: 'notice', |
| 111 | + title: 'Start your blog', |
| 112 | + level: 'success', |
| 113 | + message: '1. `cd ${{ projectDirectory }}`\n2. Run `pnpm dev`\n3. Open http://localhost:4321', |
| 114 | + dependsOn: ['template.website'], |
| 115 | + }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + example2: { |
| 119 | + title: 'Warning before applying', |
| 120 | + description: 'Warns the user with the plan, before they confirm, and reminds them on every run.', |
| 121 | + configs: [ |
| 122 | + { type: 'nvm', nodeVersions: ['24'], global: '24' }, |
| 123 | + { |
| 124 | + type: 'notice', |
| 125 | + stage: 'plan', |
| 126 | + level: 'warning', |
| 127 | + title: 'Node versions are managed by nvm', |
| 128 | + message: 'Uninstall any Node.js installed with Homebrew or the official installer first, or `node` may not be the version nvm selects.', |
| 129 | + }, |
| 130 | + { type: 'notice', show: 'always', message: 'Run `nvm use` in a project to switch to the Node version in its `.nvmrc`.' }, |
| 131 | + ], |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | +]; |
| 136 | + |
| 137 | +/** The schema as it is stored in the resource library: the block of the schemastore file (no $schema, $id or title) */ |
| 138 | +export function toRegistrySchema(schema: JsonSchema): JsonSchema { |
| 139 | + const { $id: _id, $schema: _schema, title: _title, ...rest } = structuredClone(schema); |
| 140 | + return rest; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * The type stored with a parameter in the resource library (registry_resource_parameters.type, required). A property |
| 145 | + * with several types (anyOf / oneOf, or a list of types) uses the first one. Ex: the default of a variable can be text, |
| 146 | + * a number, a boolean or a list, and is stored as string. |
| 147 | + */ |
| 148 | +export function parameterType(property: JsonSchema): string { |
| 149 | + if (typeof property.type === 'string') { |
| 150 | + return property.type; |
| 151 | + } |
| 152 | + |
| 153 | + if (Array.isArray(property.type) && typeof property.type[0] === 'string') { |
| 154 | + return property.type[0]; |
| 155 | + } |
| 156 | + |
| 157 | + const branches = (property.anyOf ?? property.oneOf) as JsonSchema[] | undefined; |
| 158 | + const first = branches?.find((b) => b.type !== undefined); |
| 159 | + return first ? parameterType(first) : 'string'; |
| 160 | +} |
0 commit comments