Skip to content

Commit

Permalink
Merge pull request #151 from tommy351/import-option-2
Browse files Browse the repository at this point in the history
Add `import` option
  • Loading branch information
tommy351 authored Nov 1, 2024
2 parents e644798 + 096411e commit 044798d
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-trains-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kosko/cli": minor
---

Add `--import` option to `generate` and `validate` command.
5 changes: 5 additions & 0 deletions .changeset/tasty-eggs-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kosko/config": minor
---

Add `import` option.
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ Options:
--bail Stop immediately when an error occurred. [boolean]
-s, --set Set values on the command line KEY=VAL (can be used multiple
times) [array] [default: []]
-r, --require Require modules. Modules set in config file will also be
required. [array]
-r, --require Require CommonJS modules. Modules set in config file will also
be required. [array]
--loader Module loader. Loaders set in config file will also be loaded.
[array]
--import Preload ES modules at startup. Modules set in config file are
also imported. [array]
-o, --output Output format
[string] [choices: "yaml", "json"] [default: "yaml"]
--validate Validate components [boolean] [default: true]
Expand Down Expand Up @@ -240,10 +242,12 @@ Options:
--bail Stop immediately when an error occurred. [boolean]
-s, --set Set values on the command line KEY=VAL (can be used multiple
times) [array] [default: []]
-r, --require Require modules. Modules set in config file will also be
required. [array]
-r, --require Require CommonJS modules. Modules set in config file will also
be required. [array]
--loader Module loader. Loaders set in config file will also be loaded.
[array]
--import Preload ES modules at startup. Modules set in config file are
also imported. [array]
-o, --output Output format
[string] [choices: "yaml", "json"] [default: "yaml"]
--validate Validate components [boolean] [default: true]
Expand Down Expand Up @@ -327,10 +331,12 @@ Options:
--bail Stop immediately when an error occurred. [boolean]
-s, --set Set values on the command line KEY=VAL (can be used multiple
times) [array] [default: []]
-r, --require Require modules. Modules set in config file will also be
required. [array]
-r, --require Require CommonJS modules. Modules set in config file will also
be required. [array]
--loader Module loader. Loaders set in config file will also be loaded.
[array]
--import Preload ES modules at startup. Modules set in config file are
also imported. [array]
-o, --output Output format
[string] [choices: "yaml", "json"] [default: "yaml"]
--validate Validate components [boolean] [default: true]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should load ES modules specified in args 1`] = `
"apiVersion: v1
kind: Pod
metadata:
name: test-pod"
`;

exports[`should load ES modules specified in config 1`] = `
"apiVersion: v1
kind: Pod
metadata:
name: test-pod"
`;

exports[`should load loaders specified in args 1`] = `
"apiVersion: v1
kind: Pod
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/integration/generate-ts-mjs/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,23 @@ test("should load loaders specified in config", async () => {
);
expect(result.stdout).toMatchSnapshot();
});

test("should load ES modules specified in args", async () => {
const result = await runNodeCLI(
[
"generate",
"--import",
`data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));`
],
{ cwd: testDir }
);
expect(result.stdout).toMatchSnapshot();
});

test("should load ES modules specified in config", async () => {
const result = await runNodeCLI(
["generate", "--config", "kosko-import.toml"],
{ cwd: testDir }
);
expect(result.stdout).toMatchSnapshot();
});
3 changes: 3 additions & 0 deletions packages/cli/integration/generate-ts-mjs/kosko-import.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
components = ["*"]
loaders = ['data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));']
extensions = ["ts"]
8 changes: 7 additions & 1 deletion packages/cli/src/commands/generate/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ export function generateBuilder(
type: "string",
array: true,
describe:
"Require modules. Modules set in config file will also be required.",
"Require CommonJS modules. Modules set in config file will also be required.",
alias: "r"
})
.option("loader", {
type: "string",
array: true,
describe:
"Module loader. Loaders set in config file will also be loaded."
})
.option("import", {
type: "string",
array: true,
describe:
"Preload ES modules at startup. Modules set in config file are also imported."
});
}

Expand Down
13 changes: 8 additions & 5 deletions packages/cli/src/commands/generate/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ export async function loadConfig(args: BaseGenerateArguments) {
const envs = [base.baseEnvironment, args.env].filter(
(env): env is string => typeof env === "string"
);
const { components, require, loaders, plugins } = getConfig(base, envs);
const envConfig = getConfig(base, envs);
const config = {
...base,
plugins,
components: args.components?.length ? args.components : components,
require: [...require, ...(args.require || [])],
loaders: [...loaders, ...(args.loader || [])],
plugins: envConfig.plugins,
components: args.components?.length
? args.components
: envConfig.components,
require: [...envConfig.require, ...(args.require || [])],
loaders: [...envConfig.loaders, ...(args.loader || [])],
import: [...envConfig.import, ...(args.import || [])],
bail: args.bail ?? base.bail
};

Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/generate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface BaseGenerateArguments extends GlobalArguments {
validate?: boolean;
set?: SetOption[];
loader?: string[];
import?: string[];
config?: string;
bail?: boolean;
}
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/commands/generate/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export interface WorkerOptions {
export async function handler(options: WorkerOptions) {
const { printFormat, args, config, ignoreLoaders } = options;

if (BUILD_TARGET === "node" && !ignoreLoaders && config.loaders.length) {
if (
BUILD_TARGET === "node" &&
!ignoreLoaders &&
(config.loaders.length || config.import.length)
) {
await runWithLoaders(options);
return;
}
Expand Down Expand Up @@ -90,6 +94,8 @@ async function runWithLoaders(options: WorkerOptions) {
...execArgv,
// ESM loaders
...options.config.loaders.flatMap((loader) => ["--loader", loader]),
// ESM import
...options.config.import.flatMap((imp) => ["--import", imp]),
// Entry file
join(fileURLToPath(import.meta.url), "../worker-bin." + TARGET_SUFFIX)
],
Expand Down
35 changes: 24 additions & 11 deletions packages/config/src/__tests__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ describe("getConfig", () => {
components: [],
require: [],
loaders: [],
plugins: []
plugins: [],
import: []
});
});

Expand All @@ -61,7 +62,8 @@ describe("getConfig", () => {
components: ["foo"],
require: ["bar"],
loaders: ["baz"],
plugins: [{ name: "a" }]
plugins: [{ name: "a" }],
import: ["qux"]
};

expect(getConfig(input, "dev")).toEqual(input);
Expand All @@ -72,7 +74,8 @@ describe("getConfig", () => {
components: ["foo"],
require: ["bar"],
loaders: ["baz"],
plugins: [{ name: "a" }]
plugins: [{ name: "a" }],
import: ["qux"]
};
const input = {
...globalConf,
Expand All @@ -81,7 +84,8 @@ describe("getConfig", () => {
components: ["aaa"],
require: ["bbb"],
loaders: ["ccc"],
plugins: [{ name: "b" }]
plugins: [{ name: "b" }],
import: ["ddd"]
}
}
};
Expand All @@ -95,12 +99,14 @@ describe("getConfig", () => {
require: ["bar"],
loaders: ["baz"],
plugins: [{ name: "a" }],
import: ["qux"],
environments: {
dev: {
components: ["aaa"],
require: ["bbb"],
loaders: ["ccc"],
plugins: [{ name: "b" }]
plugins: [{ name: "b" }],
import: ["ddd"]
}
}
};
Expand All @@ -109,7 +115,8 @@ describe("getConfig", () => {
components: ["foo", "aaa"],
require: ["bar", "bbb"],
loaders: ["baz", "ccc"],
plugins: [{ name: "a" }, { name: "b" }]
plugins: [{ name: "a" }, { name: "b" }],
import: ["qux", "ddd"]
});
});

Expand All @@ -119,18 +126,21 @@ describe("getConfig", () => {
require: ["bar"],
loaders: ["baz"],
plugins: [{ name: "a" }],
import: ["qux"],
environments: {
a: {
components: ["aa"],
require: ["ab"],
loaders: ["ac"],
plugins: [{ name: "ad" }]
plugins: [{ name: "ad" }],
import: ["ae"]
},
c: {
components: ["ca"],
require: ["cb"],
loaders: ["cc"],
plugins: [{ name: "cd" }]
plugins: [{ name: "cd" }],
import: ["ce"]
}
}
};
Expand All @@ -139,7 +149,8 @@ describe("getConfig", () => {
components: ["foo", "aa", "ca"],
require: ["bar", "ab", "cb"],
loaders: ["baz", "ac", "cc"],
plugins: [{ name: "a" }, { name: "ad" }, { name: "cd" }]
plugins: [{ name: "a" }, { name: "ad" }, { name: "cd" }],
import: ["qux", "ae", "ce"]
});
});

Expand All @@ -153,7 +164,8 @@ describe("getConfig", () => {
components: ["foo"],
loaders: [],
require: [],
plugins: [{ name: "a", config: { foo: "bar" } }]
plugins: [{ name: "a", config: { foo: "bar" } }],
import: []
});
});

Expand All @@ -173,7 +185,8 @@ describe("getConfig", () => {
plugins: [
{ name: "a", config: { a: 1 } },
{ name: "a", config: { a: 2 } }
]
],
import: []
});
});
});
3 changes: 2 additions & 1 deletion packages/config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function getConfig(
...envConfigs.map((e) => e.components)
),
loaders: flatten(config.loaders, ...envConfigs.map((e) => e.loaders)),
plugins: flatten(config.plugins, ...envConfigs.map((e) => e.plugins))
plugins: flatten(config.plugins, ...envConfigs.map((e) => e.plugins)),
import: flatten(config.import, ...envConfigs.map((e) => e.import))
};
}
4 changes: 3 additions & 1 deletion packages/config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ export interface EnvironmentConfig {
components?: string[];
loaders?: string[];
plugins?: PluginConfig[];
import?: string[];
}

export const environmentConfigSchema = object({
require: optional(array(string())),
components: optional(array(string())),
loaders: optional(array(string())),
plugins: optional(array(pluginConfigSchema))
plugins: optional(array(pluginConfigSchema)),
import: optional(array(string()))
});

/**
Expand Down

0 comments on commit 044798d

Please sign in to comment.