Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-badgers-wish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"scaffolder-toolkit": minor
---

feat(config): Implement schema validation when loading configuration
2 changes: 1 addition & 1 deletion packages/devkit/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ This document tracks all planned and completed tasks for the Dev Kit project.
- [x] ** Enhance for organization Purpose **: Add new language `Typescript(ts)` with same code as javascript(js), also support for nodejs(node) template name for those who prefer it than the programming language name
- [x] **Testing**: Stabilize the integration test of the `new` command
- [x] Make sure to clean up if the `dk new` command fail
- [ ] Add a configuration validation step when updating the config file to ensure all required fields are present and correctly formatted.
- [x] Add a configuration validation step when updating the config file to ensure all required fields are present and correctly formatted.
- [ ] **Dynamic Help Text**: Programmatically generate help text for options with constrained values (e.g., `--cache-strategy`) to ensure it's always up to date.

#### Multi-Language Support
Expand Down
4 changes: 2 additions & 2 deletions packages/devkit/__tests__/integrations/config/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("dk config", () => {
it("should throw an error if no config file is found for setting", async () => {
const { all, exitCode } = await execute(
"bun",
[CLI_PATH, "conf", "--set", "lang", "en"],
[CLI_PATH, "config", "--set", "lang", "en"],
{ all: true, reject: false },
);

Expand Down Expand Up @@ -196,7 +196,7 @@ describe("dk config", () => {

expect(exitCode).toBe(0);
expect(all).toContain(
"Clé de configuration 'non_existent_key' non trouvée.",
"Clé de configuration 'non_existent_key' introuvable",
);
});
});
Expand Down
180 changes: 158 additions & 22 deletions packages/devkit/__tests__/integrations/config/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ let globalConfigDir: string;

const localConfig: CliConfig = {
...defaultCliConfig,
settings: {
...defaultCliConfig.settings,
language: "en",
},
templates: {
javascript: {
templates: {
Expand All @@ -43,7 +47,7 @@ const localConfig: CliConfig = {
},
},
},
node: {
nodejs: {
templates: {
"node-api": {
description: "A Node.js API boilerplate",
Expand All @@ -57,19 +61,51 @@ const localConfig: CliConfig = {

const globalConfig: CliConfig = {
...defaultCliConfig,
settings: {
...defaultCliConfig.settings,
language: "fr",
},
templates: {
python: {
typescript: {
templates: {
django: {
description: "A Django template",
location: "https://github.com/django/django",
alias: "dj",
"ts-lib": {
description: "A TypeScript library template",
location: "https://github.com/ts-lib-template",
alias: "tl",
},
},
},
},
};

const invalidLocalConfigMissingLocation: Partial<CliConfig> = {
...localConfig,
templates: {
javascript: {
templates: {
"bad-template": {
description: "Missing Location",
alias: "bt",
packageManager: "npm",
} as any,
},
} as any,
},
} as any;

const invalidGlobalConfigMalformedSetting: Partial<CliConfig> = {
...globalConfig,
settings: {
...globalConfig.settings,
defaultPackageManager: "invalid-package-manager-alias" as any,
},
};

const invalidLocalConfigMissingRequiredSettings: Partial<CliConfig> = {
...localConfig,
settings: {} as any,
} as any;

describe("dk config list", () => {
beforeAll(() => {
vi.unmock("#utils/shell.js");
Expand Down Expand Up @@ -112,8 +148,8 @@ describe("dk config list", () => {
expect(exitCode).toBe(0);
expect(all).toContain("Using local configuration.");
expect(all).toContain("Javascript");
expect(all).toContain("Node");
expect(all).not.toContain("Python");
expect(all).toContain("Nodejs");
expect(all).not.toContain("Typescript");
});

describe("Include defaults option", () => {
Expand All @@ -133,7 +169,7 @@ describe("dk config list", () => {
);

expect(exitCode).toBe(0);
expect(all).toContain("Available Templates:");
expect(all).toContain("Modèles disponibles :");
expect(all).toContain("remix");
});

Expand All @@ -153,8 +189,14 @@ describe("dk config list", () => {
);

expect(exitCode).toBe(0);
expect(all).toContain(
"Using global configuration.(including default templates)",
);
expect(all).toContain("Available Templates:");
expect(all).toContain("remix");
expect(all).toContain("Typescript");
expect(all).toContain("Javascript");
expect(all).toContain("Nodejs");
});

it("should use both the `default` config and the local config if exists and `--include-defaults` is used", async () => {
Expand All @@ -176,7 +218,7 @@ describe("dk config list", () => {
expect(all).toContain("Available Templates:");
expect(all).toContain("Javascript");
expect(all).toContain("remix");
expect(all).toContain("Node");
expect(all).toContain("Nodejs");
expect(all).toContain("node-api");
});

Expand All @@ -202,8 +244,8 @@ describe("dk config list", () => {
expect(exitCode).toBe(0);
expect(all).toContain("Using local and global configurations.");
expect(all).toContain("Javascript");
expect(all).toContain("Node");
expect(all).toContain("Python");
expect(all).toContain("Nodejs");
expect(all).toContain("Typescript");
});

it("should use both the `default` config and the global config if exists and `--include-defaults` is used", async () => {
Expand All @@ -222,11 +264,11 @@ describe("dk config list", () => {
);

expect(exitCode).toBe(0);
expect(all).toContain("Available Templates:");
expect(all).toContain("Modèles disponibles :");
expect(all).toContain("Javascript");
expect(all).toContain("remix");
expect(all).toContain("Python");
expect(all).toContain("django");
expect(all).toContain("Typescript");
expect(all).toContain("ts-lib");
expect(all).not.toContain("node-api");
});
});
Expand All @@ -249,9 +291,9 @@ describe("dk config list", () => {

expect(exitCode).toBe(0);
expect(all).toContain("Using global configuration.");
expect(all).toContain("Python");
expect(all).toContain("Typescript");
expect(all).not.toContain("Javascript");
expect(all).not.toContain("Node");
expect(all).not.toContain("Nodejs");
});

it("should show an error when --global is used and no global config exists", async () => {
Expand All @@ -274,7 +316,11 @@ describe("dk config list", () => {
});

it("should handle a config file with an empty templates section", async () => {
const emptyConfig = { ...localConfig, templates: {} };
const emptyConfig = {
...localConfig,
templates: {},
settings: { ...localConfig.settings },
} as CliConfig;
await fs.writeJson(path.join(tempDir, LOCAL_CONFIG_FILE_NAME), emptyConfig);
const { all, exitCode } = await execute(
"bun",
Expand All @@ -288,16 +334,20 @@ describe("dk config list", () => {
expect(exitCode).toBe(0);
expect(all).toContain("No templates found in the configuration file.");
expect(all).not.toContain("JAVASCRIPT");
expect(all).not.toContain("NODE");
expect(all).not.toContain("PYTHON");
expect(all).not.toContain("NODEJS");
expect(all).not.toContain("TYPESCRIPT");
});

it("should handle both local and global configs being empty", async () => {
await fs.writeJson(path.join(tempDir, LOCAL_CONFIG_FILE_NAME), {
...localConfig,
templates: {},
settings: { ...localConfig.settings },
});
await fs.writeJson(path.join(globalConfigDir, GLOBAL_CONFIG_FILE_NAME), {
...globalConfig,
templates: {},
settings: { ...globalConfig.settings },
});

const { all, exitCode } = await execute(
Expand All @@ -312,7 +362,93 @@ describe("dk config list", () => {
expect(exitCode).toBe(0);
expect(all).toContain("No templates found in the configuration file.");
expect(all).not.toContain("Javascript");
expect(all).not.toContain("Node");
expect(all).not.toContain("Python");
expect(all).not.toContain("Nodejs");
expect(all).not.toContain("Typescript");
});

describe("Configuration Validation Failures", () => {
const VALIDATION_ERROR_MESSAGE = "Configuration validation failed.";
const TEMPLATE_ERROR_FRAGMENT = "is missing required field: 'location'";
const SETTINGS_PM_ERROR_FRAGMENT =
"The value for setting 'defaultPackageManager' is invalid";
const SETTINGS_MISSING_ERROR_FRAGMENT =
"The value for setting 'defaultPackageManager' is invalid or missing.";

it("should fail and exit if local config is invalid (missing required template field)", async () => {
await fs.writeJson(
path.join(tempDir, LOCAL_CONFIG_FILE_NAME),
invalidLocalConfigMissingLocation,
);
await fs.writeJson(
path.join(globalConfigDir, GLOBAL_CONFIG_FILE_NAME),
globalConfig,
);

const { all, exitCode } = await execute(
"bun",
[CLI_PATH, "config", "list"],
{
all: true,
env: { HOME: globalConfigDir },
reject: false,
},
);

expect(exitCode).toBe(1);
expect(all).toContain(VALIDATION_ERROR_MESSAGE);
expect(all).toContain(TEMPLATE_ERROR_FRAGMENT);
});

it("should fail and exit if global config is invalid (malformed settings field) when using --all", async () => {
await fs.writeJson(
path.join(tempDir, LOCAL_CONFIG_FILE_NAME),
localConfig,
);
await fs.writeJson(
path.join(globalConfigDir, GLOBAL_CONFIG_FILE_NAME),
invalidGlobalConfigMalformedSetting,
);

const { all, exitCode } = await execute(
"bun",
[CLI_PATH, "config", "list", "--all"],
{
all: true,
env: { HOME: globalConfigDir },
reject: false,
},
);

expect(exitCode).toBe(1);
expect(all).toContain(VALIDATION_ERROR_MESSAGE);
expect(all).toContain(SETTINGS_PM_ERROR_FRAGMENT);
});

it("should fail and exit if local config has empty/missing required settings fields", async () => {
await fs.writeJson(
path.join(tempDir, LOCAL_CONFIG_FILE_NAME),
invalidLocalConfigMissingRequiredSettings,
);

const { all, exitCode } = await execute(
"bun",
[CLI_PATH, "config", "list"],
{
all: true,
env: { HOME: globalConfigDir },
reject: false,
},
);

expect(exitCode).toBe(1);
expect(all).toContain(VALIDATION_ERROR_MESSAGE);
expect(all).toContain(SETTINGS_MISSING_ERROR_FRAGMENT);
expect(all).toContain(
"The value for setting 'cacheStrategy' is invalid or missing.",
);
expect(all).toContain(
"The value for setting 'language' is invalid or missing.",
);
});
});
});
Loading