From 6a026ce1a198576e114a790dc723765b6fe9647e Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Fri, 2 Feb 2024 23:23:24 +0200 Subject: [PATCH] chore: version output + docs update --- src/cmd.ts | 16 ++++++++++++++-- src/index.ts | 1 + src/scaffold.ts | 1 + src/types.ts | 36 ++++++++++++++++++++++++++---------- tests/config.test.ts | 1 + 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/cmd.ts b/src/cmd.ts index d81c990..a158909 100644 --- a/src/cmd.ts +++ b/src/cmd.ts @@ -8,19 +8,26 @@ import { Scaffold } from "./scaffold" import path from "node:path" import fs from "node:fs/promises" import { parseAppendData, parseConfigFile } from "./config" +import { log } from "./logger" export async function parseCliArgs(args = process.argv.slice(2)) { const isProjectRoot = Boolean(await fs.stat(path.join(__dirname, "package.json")).catch(() => false)) const pkgFile = await fs.readFile(path.resolve(__dirname, isProjectRoot ? "." : "..", "package.json")) const pkg = JSON.parse(pkgFile.toString()) + const isVersionFlag = args.includes("--version") || args.includes("-v") const isConfigProvided = - args.includes("--config") || args.includes("-c") || args.includes("--git") || args.includes("-g") + args.includes("--config") || args.includes("-c") || args.includes("--git") || args.includes("-g") || isVersionFlag return massarg({ name: pkg.name, description: pkg.description, }) .main(async (config) => { + if (config.version) { + console.log(pkg.version) + return + } + log(config, LogLevel.info, `Simple Scaffold v${pkg.version}`) const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`) const parsed = await parseConfigFile(config, tmpPath) try { @@ -36,7 +43,7 @@ export async function parseCliArgs(args = process.argv.slice(2)) { "Name to be passed to the generated files. `{{name}}` and other data parameters inside " + "contents and file names will be replaced accordingly. You may omit the `--name` or `-n` for this specific option.", isDefault: true, - required: true, + required: !isVersionFlag, }) .option({ name: "config", @@ -136,6 +143,11 @@ export async function parseCliArgs(args = process.argv.slice(2)) { "Don't emit files. This is good for testing your scaffolds and making sure they " + "don't fail, without having to write actual file contents or create directories.", }) + .flag({ + name: "version", + aliases: ["v"], + description: "Display version.", + }) .example({ description: "Usage with config file", input: "simple-scaffold -c scaffold.cmd.js --key component", diff --git a/src/index.ts b/src/index.ts index cf8b4b8..700aed8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export * from "./scaffold" export * from "./types" import Scaffold from "./scaffold" + export default Scaffold diff --git a/src/scaffold.ts b/src/scaffold.ts index c175d74..67e0538 100644 --- a/src/scaffold.ts +++ b/src/scaffold.ts @@ -128,6 +128,7 @@ Scaffold.fromConfig = async function ( subdir: false, quiet: false, config: pathOrUrl, + version: false, ...config, } const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`) diff --git a/src/types.ts b/src/types.ts index d46781c..450991b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -257,6 +257,18 @@ export type DefaultHelpers = CaseHelpers | DateHelpers */ export type Helper = HelperDelegate +/** + * The amount of information to log when generating scaffold. + * When not `none`, the selected level will be the lowest level included. + * + * For example, level `info` will include `info`, `warning` and `error`, but not `debug`; and `warning` will only + * show `warning` and `error`, but not `info` or `debug`. + * + * @default `info` + * + * @category Logging (const) + */ + export const LogLevel = { /** Silent output */ none: "none", @@ -276,17 +288,14 @@ export const LogLevel = { /** * The amount of information to log when generating scaffold. - * When not `None`, the selected level will be the lowest level included. + * When not `none`, the selected level will be the lowest level included. * - * For example, level `Info` (2) will include `Info`, `Warning` and `Error`, but not `Debug`; and `Warning` will only - * show `Warning` and `Error`. + * For example, level `info` will include `info`, `warning` and `error`, but not `debug`; and `warning` will only + * show `warning` and `error`, but not `info` or `debug`. * - * You may use either the number or the name of the level. - * For example, `2` or `info` are both valid. + * @default `info` * - * @default `2 (info)` - * - * @category Logging + * @category Logging (type) */ export type LogLevel = (typeof LogLevel)[keyof typeof LogLevel] @@ -324,7 +333,7 @@ export type FileResponse = T | FileResponseHandler * The Scaffold config for CLI * Contains less and more specific options than {@link ScaffoldConfig} */ -export interface ScaffoldCmdConfig { +export type ScaffoldCmdConfig = { /** The name of the scaffold template to use. */ name: string /** The templates to use for generation */ @@ -357,6 +366,8 @@ export interface ScaffoldCmdConfig { key?: string /** The git repository to use to fetch the config file */ git?: string + /** Display version */ + version: boolean } /** @@ -369,14 +380,19 @@ export interface ScaffoldCmdConfig { * When no template key is provided to the scaffold command, the "default" template is used. * * @see {@link ScaffoldConfig} + * + * @category Config */ export type ScaffoldConfigMap = Record -/** The scaffold config file is either: +/** + * The scaffold config file is either: * - A {@link ScaffoldConfigMap} object * - A function that returns a {@link ScaffoldConfigMap} object * - A promise that resolves to a {@link ScaffoldConfigMap} object * - A function that returns a promise that resolves to a {@link ScaffoldConfigMap} object + * + * @category Config */ export type ScaffoldConfigFile = AsyncResolver diff --git a/tests/config.test.ts b/tests/config.test.ts index 9c1ff7e..1b213ed 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -30,6 +30,7 @@ const blankCliConf: ScaffoldCmdConfig = { subdir: false, dryRun: false, quiet: false, + version: false, } const blankConfig: ScaffoldCmdConfig = {