Skip to content

Commit

Permalink
feat(create-kosko): Introduce package
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Feb 15, 2024
1 parent 62a2159 commit 24661f9
Show file tree
Hide file tree
Showing 57 changed files with 243 additions and 65 deletions.
6 changes: 6 additions & 0 deletions .changeset/tough-carrots-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"create-kosko": minor
"@kosko/cli-utils": minor
---

First release.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@
}
},
{
"files": ["packages/cli/templates/**/*"],
"files": ["packages/create-kosko/templates/**/*"],
"rules": {
"node/no-extraneous-require": "off",
"node/no-extraneous-import": "off",
"node/no-unpublished-require": "off",
"node/no-unpublished-import": "off",
"node/no-missing-import": "off"
"node/no-missing-import": "off",
"node/no-missing-require": "off"
}
},
{
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
save-workspace-protocol = rolling
auto-install-peers = false
public-hoist-pattern[] = "yargs"
4 changes: 4 additions & 0 deletions packages/cli-utils/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"extends": "../../api-extractor.json"
}
58 changes: 58 additions & 0 deletions packages/cli-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@kosko/cli-utils",
"version": "0.0.0",
"description": "CLI utilities used in Kosko.",
"homepage": "https://kosko.dev",
"repository": "https://github.com/tommy351/kosko/tree/master/packages/cli-utils",
"author": "Tommy Chen <[email protected]>",
"license": "MIT",
"main": "dist/index.node.cjs",
"module": "dist/index.node.mjs",
"types": "dist/types.d.ts",
"sideEffects": false,
"scripts": {
"build": "build-scripts"
},
"exports": {
"node": {
"import": {
"types": "./dist/types.d.mts",
"default": "./dist/index.node.mjs"
},
"types": "./dist/types.d.ts",
"require": "./dist/index.node.cjs"
},
"import": {
"types": "./dist/types.d.mts",
"default": "./dist/index.base.mjs"
},
"types": "./dist/types.d.ts",
"require": "./dist/index.node.cjs"
},
"files": [
"/dist/"
],
"keywords": [
"kosko"
],
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=18"
},
"dependencies": {
"@kosko/log": "workspace:^"
},
"devDependencies": {
"@kosko/build-scripts": "workspace:^",
"@kosko/jest-preset": "workspace:^",
"@types/yargs": "^17.0.12"
},
"peerDependencies": {
"yargs": "^17.0.0"
},
"jest": {
"preset": "@kosko/jest-preset"
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// eslint-disable-next-line node/no-missing-import
import { Argv, CommandModule } from "yargs";

/**
* @public
*/
export interface RootArguments {
cwd: string;
"log-level": string;
silent: boolean;
}

/**
* @public
*/
export type Command<T> = CommandModule<RootArguments, T>;

/**
* @public
*/
export function parse(input: Argv, argv: readonly string[]): Promise<void> {
return new Promise((resolve, reject) => {
input.parse(argv, {}, (err, args, output) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logger, { LogLevel } from "@kosko/log";
import { exit } from "node:process";

/**
* @public
*/
export class CLIError extends Error {
public readonly output?: string;
public readonly code?: number;
Expand All @@ -18,7 +21,7 @@ export class CLIError extends Error {
CLIError.prototype.name = "CLIError";

/**
* Handles errors thrown by {@link run} function.
* Handles CLI errors.
*
* @remarks
* This function prints error message to logger and terminates current process
Expand Down
4 changes: 4 additions & 0 deletions packages/cli-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { type RootArguments, type Command, parse } from "./command";
export { CLIError, handleError } from "./error";
export { setupLogger } from "./logger";
export { createRootCommand } from "./root";
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import logger, {
} from "@kosko/log";
import { RootArguments } from "./command";

/**
* @public
*/
export function setupLogger(args: Pick<RootArguments, "silent" | "log-level">) {
if (args.silent) {
logger.setWriter(new SilentLogWriter());
Expand Down
37 changes: 37 additions & 0 deletions packages/cli-utils/src/root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { isAbsolute, resolve } from "node:path";
// eslint-disable-next-line node/no-missing-import
import yargs from "yargs";
import { cwd } from "node:process";
import { setupLogger } from "./logger";

/**
* @public
*/
export function createRootCommand(args?: readonly string[]) {
return yargs(args)
.exitProcess(false)
.option("cwd", {
type: "string",
describe: "Path of working directory",
global: true,
default: cwd(),
defaultDescription: "CWD",
coerce(arg) {
return isAbsolute(arg) ? arg : resolve(arg);
}
})
.option("log-level", {
type: "string",
describe: "Set log level",
global: true,
default: "info"
})
.option("silent", {
type: "boolean",
describe: "Disable log output",
global: true,
default: false
})
.group(["cwd", "log-level", "silent", "help", "version"], "Global Options:")
.middleware(setupLogger);
}
3 changes: 3 additions & 0 deletions packages/cli-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.build.json"
}
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"dependencies": {
"@kosko/aggregate-error": "workspace:^",
"@kosko/cli-utils": "workspace:^",
"@kosko/common-utils": "workspace:^",
"@kosko/config": "workspace:^",
"@kosko/exec-utils": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/generate/__tests__/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BufferList from "bl";
import AggregateError from "@kosko/aggregate-error";
import { GenerateError, ResolveError } from "@kosko/generate";
import { join, normalize } from "node:path";
import { CLIError } from "../../../cli/error";
import { CLIError } from "@kosko/cli-utils";

let stderr: BufferList;

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/generate/command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PrintFormat } from "@kosko/generate";
import { Argv } from "yargs";
import { Command, RootArguments } from "../../cli/command";
import type { Argv } from "yargs";
import type { Command, RootArguments } from "@kosko/cli-utils";
import { loadConfig } from "./config";
import { SetOption, parseSetOptions } from "./set-option";
import { BaseGenerateArguments, GenerateArguments } from "./types";
import type { BaseGenerateArguments, GenerateArguments } from "./types";
import { handler } from "./worker";

/* istanbul ignore next */
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/generate/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
loadConfig as loadConfigFile
} from "@kosko/config";
import { resolve } from "node:path";
import { CLIError } from "../../cli/error";
import { CLIError } from "@kosko/cli-utils";
import { BaseGenerateArguments } from "./types";

export async function loadConfig(args: BaseGenerateArguments) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/generate/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GenerateError, ResolveError } from "@kosko/generate";
import cleanStack from "clean-stack";
import extractStack from "extract-stack";
import pc from "picocolors";
import { CLIError } from "../../cli/error";
import { CLIError } from "@kosko/cli-utils";
import stringify from "fast-safe-stringify";
import { isRecord } from "@kosko/common-utils";
import { stderr } from "node:process";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/generate/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { importPath } from "@kosko/require";
import logger, { LogLevel } from "@kosko/log";
import resolveFrom from "resolve-from";
import { isRecord } from "@kosko/common-utils";
import { CLIError } from "../../cli/error";
import { CLIError } from "@kosko/cli-utils";
import pc from "picocolors";

type UnknownPluginFactory = (ctx: PluginContext) => unknown;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/generate/set-option.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Reducer } from "@kosko/env";
import jp from "jsonpath";
import { CLIError } from "../../cli/error";
import { CLIError } from "@kosko/cli-utils";

/**
* Contains a value to override a variable of the specified component
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/generate/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrintFormat } from "@kosko/generate";
import { RootArguments } from "../../cli/command";
import type { RootArguments } from "@kosko/cli-utils";
import { SetOption } from "./set-option";

export interface BaseGenerateArguments extends RootArguments {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/generate/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { spawn, SpawnError } from "@kosko/exec-utils";
import { generate, GenerateOptions, print, PrintFormat } from "@kosko/generate";
import { join } from "node:path";
import stringify from "fast-safe-stringify";
import { CLIError } from "../../cli/error";
import { CLIError } from "@kosko/cli-utils";
import { setupEnv } from "./env";
import { handleGenerateError } from "./error";
import { BaseGenerateArguments } from "./types";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MigrateFormat, migrateString } from "@kosko/migrate";
import { readdir, readFile, stat } from "node:fs/promises";
import getStdin from "get-stdin";
import { join, resolve } from "node:path";
import { Command, RootArguments } from "../cli/command";
import type { Command, RootArguments } from "@kosko/cli-utils";
import { print } from "../cli/print";
import logger, { LogLevel } from "@kosko/log";
import { toArray } from "@kosko/common-utils";
Expand Down
34 changes: 2 additions & 32 deletions packages/cli/src/commands/root.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
import { isAbsolute, resolve } from "node:path";
import yargs from "yargs";
import { createRootCommand as createRoot } from "@kosko/cli-utils";
import { generateCmd } from "./generate/command";
import { initCmd } from "./init/command";
import { validateCmd } from "./validate";
import { migrateCmd } from "./migrate";
import { setupLogger } from "../cli/logger";
import { cwd } from "node:process";
import { version } from "../../package.json";

export function createRootCommand(args: readonly string[]) {
return yargs(args)
return createRoot(args)
.scriptName("kosko")
.version(version)
.exitProcess(false)
.option("cwd", {
type: "string",
describe: "Path of working directory",
global: true,
default: cwd(),
defaultDescription: "CWD",
coerce(arg) {
return isAbsolute(arg) ? arg : resolve(arg);
}
})
.option("log-level", {
type: "string",
describe: "Set log level",
global: true,
default: "info"
})
.option("silent", {
type: "boolean",
describe: "Disable log output",
global: true,
default: false
})
.group(["cwd", "log-level", "silent", "help", "version"], "Global Options:")
.middleware(setupLogger)
.command(initCmd)
.command(generateCmd)
.command(validateCmd)
.command(migrateCmd)
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/validate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Command } from "../cli/command";
import type { Command } from "@kosko/cli-utils";
import logger, { LogLevel } from "@kosko/log";
import { handler } from "./generate/worker";
import { BaseGenerateArguments } from "./generate/types";
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* @packageDocumentation
*/

import { parse } from "./cli/command";
import { parse } from "@kosko/cli-utils";
import { createRootCommand } from "./commands/root";
import { argv } from "node:process";

export { handleError } from "./cli/error";
export { handleError } from "@kosko/cli-utils";

/**
* Runs CLI with the given arguments.
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/worker-bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import getStdin from "get-stdin";
import { handleError } from "./cli/error";
import { setupLogger } from "./cli/logger";
import { handleError, setupLogger } from "@kosko/cli-utils";
import { handler, type WorkerOptions } from "./commands/generate/worker";

(async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/create-kosko/api-extractor.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"extends": "../../api-extractor.json"
"extends": "../../api-extractor.json",
"mainEntryPointFilePath": "<projectFolder>/dist/src/index.d.ts"
}
Empty file modified packages/create-kosko/bin/create-kosko.js
100644 → 100755
Empty file.
16 changes: 13 additions & 3 deletions packages/create-kosko/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"require": "./dist/index.node.cjs"
},
"files": [
"/dist/"
"/bin/",
"/dist/",
"/templates/"
],
"keywords": [
"kosko"
Expand All @@ -43,12 +45,20 @@
"node": ">=18"
},
"dependencies": {
"@kosko/cli-utils": "workspace:^",
"@kosko/common-utils": "workspace:^",
"@kosko/log": "workspace:^"
"@kosko/exec-utils": "workspace:^",
"@kosko/log": "workspace:^",
"@kosko/require": "workspace:^",
"fast-safe-stringify": "^2.1.1",
"picocolors": "^1.0.0",
"yargs": "^17.5.1"
},
"devDependencies": {
"@kosko/build-scripts": "workspace:^",
"@kosko/jest-preset": "workspace:^"
"@kosko/jest-preset": "workspace:^",
"@kosko/test-utils": "workspace:^",
"fast-glob": "^3.2.12"
},
"jest": {
"preset": "@kosko/jest-preset"
Expand Down
Loading

0 comments on commit 24661f9

Please sign in to comment.