Skip to content
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

feat: Add import option #144

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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/friendly-walls-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kosko/require": minor
---

Remove `ESM_IMPORT_DISABLED` environment variable.
5 changes: 5 additions & 0 deletions .changeset/funny-chefs-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kosko/require": minor
---

Handle `ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG` errors.
5 changes: 5 additions & 0 deletions .changeset/red-forks-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kosko/require": minor
---

Use import attributes when importing JSON files.
5 changes: 5 additions & 0 deletions .changeset/serious-clouds-march.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.
26 changes: 26 additions & 0 deletions .changeset/shaggy-turkeys-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
"@kosko/template-deployed-service": minor
"@kosko/template-environment": minor
"@kosko/common-utils": minor
"create-kosko": minor
"@kosko/jest-preset": minor
"@kosko/plugin-set-metadata": minor
"@kosko/exec-utils": minor
"@kosko/cli-utils": minor
"@kosko/kustomize": minor
"@kosko/generate": minor
"@kosko/template": minor
"@kosko/migrate": minor
"@kosko/require": minor
"@kosko/config": minor
"@kosko/plugin": minor
"kosko": minor
"@kosko/helm": minor
"@kosko/yaml": minor
"@kosko/cli": minor
"@kosko/env": minor
"@kosko/log": minor
"@kosko/plugin-lint": minor
---

The minimal supported Node.js version is 18.20.0, 20.10.0, 21 and above because import assertion is replaced by [import attributes](https://nodejs.org/docs/latest-v22.x/api/esm.html#import-attributes) now.
5 changes: 5 additions & 0 deletions .changeset/slimy-clocks-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kosko/config": minor
---

Add `import` option.
3 changes: 2 additions & 1 deletion internal/build-scripts/bin/build-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const typesPath = args.types ? join(cwd, args.types) : fullDistPath;
* @param {{
* suffixes?: readonly string[];
* output: string;
* format: import("rollup").ModuleFormat;
* format: 'cjs' | 'esm';
* importMetaUrlShim?: boolean;
* target: 'browser' | 'node';
* reuse?: string;
Expand Down Expand Up @@ -87,6 +87,7 @@ export const BUILD_PROD = true;
export const BUILD_TARGET = ${JSON.stringify(options.target)};
export const BUILD_FORMAT = ${JSON.stringify(options.format)};
export const TARGET_SUFFIX = ${JSON.stringify(options.output)};
export const ESM_IMPORT_DISABLED = false;
`
}),
replace({
Expand Down
1 change: 1 addition & 0 deletions internal/build-scripts/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ exports.BUILD_PROD = false;
exports.BUILD_TARGET = "node";
exports.BUILD_FORMAT = "cjs";
exports.TARGET_SUFFIX = ".js";
exports.ESM_IMPORT_DISABLED = !!process.env.ESM_IMPORT_DISABLED;
2 changes: 1 addition & 1 deletion internal/build-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-swc": "^0.3.0",
"@rollup/plugin-virtual": "^3.0.2",
"@swc/core": "^1.4.8",
"@swc/core": "^1.5.25",
"@swc/helpers": "^0.5.6",
"execa": "^5.1.1",
"globby": "^11.0.2",
Expand Down
5 changes: 2 additions & 3 deletions internal/build-scripts/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ModuleFormat } from "rollup";

export const BUILD_PROD: boolean;
export const BUILD_TARGET: "browser" | "node";
export const BUILD_FORMAT: ModuleFormat;
export const BUILD_FORMAT: "cjs" | "esm";
export const TARGET_SUFFIX: string;
export const ESM_IMPORT_DISABLED: boolean;
2 changes: 1 addition & 1 deletion internal/jest-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"@kosko/jest-serializer-path": "workspace:^",
"@swc/core": "^1.4.8",
"@swc/core": "^1.5.25",
"@swc/helpers": "^0.5.6",
"@swc/jest": "^0.2.36",
"jest-environment-node": "^29.7.0",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"*.{js,ts,jsx,tsx,cjs,mjs,cts,mts}": "eslint --fix"
},
"engines": {
"node": ">=18"
"node": "^18.20.0 || ^20.10.0 || >= 21"
},
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
Expand Down Expand Up @@ -56,7 +56,7 @@
"jest": "^29.3.1",
"jest-extended": "^4.0.2",
"jest-puppeteer": "^10.0.0",
"kubernetes-models": "^4.1.0",
"kubernetes-models": "^4.4.0",
"lint-staged": "^13.0.3",
"npm-run-all": "^4.1.5",
"prettier": "^3.2.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"access": "public"
},
"engines": {
"node": ">=18"
"node": "^18.20.0 || ^20.10.0 || >= 21"
},
"dependencies": {
"@kosko/log": "workspace:^"
Expand Down
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
-r, --require Require CommonJS modules. Modules set in config file are also
required. [array]
--loader Module loader. Loaders set in config file will also be loaded.
--loader Module loader. Loaders set in config file are also 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
-r, --require Require CommonJS modules. Modules set in config file are also
required. [array]
--loader Module loader. Loaders set in config file will also be loaded.
--loader Module loader. Loaders set in config file are also 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
-r, --require Require CommonJS modules. Modules set in config file are also
required. [array]
--loader Module loader. Loaders set in config file will also be loaded.
--loader Module loader. Loaders set in config file are also 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
Expand Up @@ -4,7 +4,7 @@ exports[`should print the error 1`] = `
"
components/nginx.ts - v1/Pod test-pod

✖ /spec must have required property 'containers'
✖ /spec must have required property containers

error - Found 1 error in total
error - Generate failed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ exports[`when bail is not set should print the error 1`] = `
"
components/foo.js - v1/Pod foo [0]

✖ /spec must have required property 'containers'
✖ /spec must have required property containers

components/nginx.js - apps/v1/Deployment nginx [0]

✖ /spec must have required property 'selector'
✖ /spec must have required property selector

✖ /spec must have required property 'template'
✖ /spec must have required property template

components/nginx.js - v1/Service nginx [1]

Expand All @@ -26,11 +26,11 @@ exports[`when bail is true should print the error 1`] = `
"
components/foo.js - v1/Pod foo [0]

✖ /spec must have required property 'containers'
✖ /spec must have required property containers

components/nginx.js - apps/v1/Deployment nginx [0]

✖ /spec must have required property 'selector'
✖ /spec must have required property selector

error - Found 2 errors in total
error - Generate failed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ kind: Pod
metadata:
name: test-pod"
`;

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

exports[`should use import specified in config 1`] = `
"apiVersion: v1
kind: Pod
metadata:
name: test-pod"
`;
20 changes: 17 additions & 3 deletions packages/cli/integration/generate-ts-mjs/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@ test("should load loaders specified in args", async () => {
test("should load loaders specified in config", async () => {
const result = await runNodeCLI(
["generate", "--config", "kosko-loader.toml"],
{
cwd: testDir
}
{ cwd: testDir }
);
expect(result.stdout).toMatchSnapshot();
});

test("should use import specified in args", async () => {
const result = await runNodeCLI(
["generate", "--import", "@swc-node/register/esm-register"],
{ cwd: testDir, stderr: "inherit" }
);
expect(result.stdout).toMatchSnapshot();
});

test("should use import specified in config", async () => {
const result = await runNodeCLI(
["generate", "--config", "kosko-import.toml"],
{ cwd: testDir, stderr: "inherit" }
);
expect(result.stdout).toMatchSnapshot();
});
6 changes: 5 additions & 1 deletion packages/cli/integration/generate-ts-mjs/components/nginx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Pod } from "kubernetes-models/v1/Pod";
import env from "@kosko/env";

const params = await env.global();

export default new Pod({
metadata: {
name: "test-pod"
name: "test-pod",
annotations: params
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
env: "dev"
};
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 = ["*"]
import = ["@swc-node/register/esm-register"]
extensions = ["ts"]
4 changes: 2 additions & 2 deletions packages/cli/integration/generate-ts-mjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
"module": "nodenext",
"moduleResolution": "nodenext",
"esModuleInterop": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports[`when validate is not set should print the error 1`] = `
"
components/foo.js - v1/Pod foo

✖ /spec must have required property 'containers'
✖ /spec must have required property containers

error - Found 1 error in total
error - Generate failed"
Expand All @@ -22,7 +22,7 @@ exports[`when validate is true should print the error 1`] = `
"
components/foo.js - v1/Pod foo

✖ /spec must have required property 'containers'
✖ /spec must have required property containers

error - Found 1 error in total
error - Generate failed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`should print the error 1`] = `
"
components/foo.js - v1/Pod foo

✖ /spec must have required property 'containers'
✖ /spec must have required property containers

error - Found 1 error in total
error - Generate failed"
Expand Down
9 changes: 6 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"access": "public"
},
"engines": {
"node": ">=18"
"node": "^18.20.0 || ^20.10.0 || >= 21"
},
"dependencies": {
"@kosko/cli-utils": "workspace:^",
Expand Down Expand Up @@ -73,16 +73,19 @@
"@kosko/jest-preset": "workspace:^",
"@kosko/plugin": "workspace:^",
"@kosko/test-utils": "workspace:^",
"@swc-node/register": "^1.9.2",
"@swc/core": "^1.5.25",
"@types/bl": "^5.0.2",
"@types/exit": "^0.1.31",
"@types/jsonpath": "^0.2.0",
"@types/yargs": "^17.0.12",
"bl": "^5.0.0",
"execa": "^5.1.1",
"fast-glob": "^3.2.12",
"kubernetes-models": "^4.1.0",
"kubernetes-models": "^4.4.0",
"pkg-dir": "^5.0.0",
"replace-string": "^3.1.0"
"replace-string": "^3.1.0",
"ts-node": "^10.9.2"
},
"jest": {
"preset": "@kosko/jest-preset"
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/generate/__tests__/command.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="jest-extended" />
import toml from "@iarna/toml";
import { Config } from "@kosko/config";
import { Environment } from "@kosko/env";
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/src/commands/generate/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ 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 are also required.",
alias: "r"
})
.option("loader", {
type: "string",
array: true,
describe: "Module loader. Loaders set in config file are also loaded."
})
.option("import", {
type: "string",
array: true,
describe:
"Module loader. Loaders set in config file will also be loaded."
"Preload ES modules at startup. Modules set in config file are also imported."
});
}

Expand Down
Loading