-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(workspace-tools): Added the config-schema generator to create js…
…on schemas for Storm Config
- Loading branch information
1 parent
72d9e51
commit 4b66f3c
Showing
10 changed files
with
168 additions
and
4 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 |
---|---|---|
@@ -1,9 +1,16 @@ | ||
export * from "./src/base"; | ||
export * from "./src/executors/tsup-neutral/executor"; | ||
export * from "./src/executors/tsup-neutral/schema.d"; | ||
export * from "./src/executors/tsup-node/executor"; | ||
export * from "./src/executors/tsup-node/schema.d"; | ||
export * from "./src/executors/tsup/executor"; | ||
export * from "./src/executors/tsup/schema.d"; | ||
export * from "./src/generators/config-schema/generator"; | ||
export * from "./src/generators/config-schema/schema.d"; | ||
export * from "./src/generators/init/init"; | ||
export * from "./src/generators/init/schema.d"; | ||
export * from "./src/generators/node-library/generator"; | ||
export * from "./src/generators/node-library/schema.d"; | ||
export * from "./src/generators/preset/generator"; | ||
export * from "./src/generators/preset/schema.d"; | ||
export * from "./src/utils/versions"; | ||
export * from "./src/utils"; |
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
20 changes: 20 additions & 0 deletions
20
packages/workspace-tools/src/generators/config-schema/generator.spec.ts
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,20 @@ | ||
import { createTreeWithEmptyWorkspace } from "@nx/devkit/testing"; | ||
import { Tree, readProjectConfiguration } from "@nx/devkit"; | ||
|
||
import { configSchemaGenerator } from "./generator"; | ||
import { ConfigSchemaGeneratorSchema } from "./schema"; | ||
|
||
describe("config-schema generator", () => { | ||
let tree: Tree; | ||
const options: ConfigSchemaGeneratorSchema = { name: "test" }; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it("should run successfully", async () => { | ||
await configSchemaGenerator(tree, options); | ||
const config = readProjectConfiguration(tree, "test"); | ||
expect(config).toBeDefined(); | ||
}); | ||
}); |
89 changes: 89 additions & 0 deletions
89
packages/workspace-tools/src/generators/config-schema/generator.ts
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,89 @@ | ||
import { formatFiles, ProjectConfiguration, Tree, writeJson } from "@nx/devkit"; | ||
import { StormConfigSchema } from "@storm-software/config-tools/schema"; | ||
import { existsSync } from "fs"; | ||
import { join } from "path"; | ||
import * as z from "zod"; | ||
import { zodToJsonSchema } from "zod-to-json-schema"; | ||
import { withRunGenerator } from "../../base/base-generator"; | ||
import { getProjectConfigurations } from "../../utils/get-project-configurations"; | ||
import { getWorkspaceRoot } from "../../utils/get-workspace-root"; | ||
import { ConfigSchemaGeneratorSchema } from "./schema"; | ||
|
||
export type ModuleSchema = { | ||
name: string; | ||
schema: z.ZodObject<any>; | ||
}; | ||
|
||
export async function configSchemaGeneratorFn( | ||
tree: Tree, | ||
options: ConfigSchemaGeneratorSchema | ||
) { | ||
const schema = {}; | ||
const projectConfigurations = getProjectConfigurations< | ||
ProjectConfiguration & { config: any } | ||
>(); | ||
const workspaceRoot = getWorkspaceRoot(); | ||
|
||
const modules = await Promise.all( | ||
Object.keys(projectConfigurations).map(async key => { | ||
if (projectConfigurations[key]?.config) { | ||
const configPath = join( | ||
workspaceRoot, | ||
projectConfigurations[key].config | ||
); | ||
if (existsSync(configPath)) { | ||
const mod = await import(configPath); | ||
if (mod.default) { | ||
return { name: key, schema: mod.default } as ModuleSchema; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
}) | ||
); | ||
|
||
const workspaceSchema = z.object({ | ||
modules: z | ||
.object( | ||
modules | ||
.filter(module => !!module) | ||
.reduce( | ||
( | ||
ret: Record<string, z.ZodObject<any>>, | ||
module: ModuleSchema | null | ||
) => { | ||
if (module?.schema && !ret[module.name]) { | ||
ret[module.name] = module.schema; | ||
} | ||
|
||
return ret; | ||
}, | ||
{} | ||
) | ||
) | ||
.describe("Configuration of each used extension") | ||
}); | ||
|
||
const ModulesSchema = z | ||
.union([workspaceSchema, StormConfigSchema]) | ||
.describe( | ||
"The values set in the Storm config file. This file is expected to be named `storm.config.js` and be located in the root of the workspace" | ||
); | ||
|
||
writeJson( | ||
tree, | ||
options.outputFile | ||
? join(workspaceRoot, options.outputFile) | ||
: join(workspaceRoot, "storm.schema.json"), | ||
zodToJsonSchema(ModulesSchema, "StormConfig") | ||
); | ||
await formatFiles(tree); | ||
|
||
return null; | ||
} | ||
|
||
export default withRunGenerator<ConfigSchemaGeneratorSchema>( | ||
"Configuration Schema Creator", | ||
configSchemaGeneratorFn | ||
); |
3 changes: 3 additions & 0 deletions
3
packages/workspace-tools/src/generators/config-schema/schema.d.ts
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,3 @@ | ||
export interface ConfigSchemaGeneratorSchema { | ||
outputFile?: string; | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/workspace-tools/src/generators/config-schema/schema.json
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,20 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"$id": "config-schema", | ||
"title": "Configuration Schema Creator", | ||
"description": "Create a StormConfig JSON schema based on the workspace's project configurations", | ||
"type": "object", | ||
"properties": { | ||
"outputFile": { | ||
"type": "string", | ||
"description": "The file path where the schema json will be written (relative to the workspace root)", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
}, | ||
"x-prompt": "Where should the schema json be written (relative to the workspace root)?", | ||
"default": "{workspaceRoot}/storm.schema.json" | ||
} | ||
}, | ||
"required": ["outputFile"] | ||
} |
3 changes: 2 additions & 1 deletion
3
packages/workspace-tools/src/generators/node-library/schema.json
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.