mirror of
https://github.com/chenasraf/simple-scaffold.git
synced 2026-05-17 17:28:09 +00:00
chore: version output + docs update
This commit is contained in:
16
src/cmd.ts
16
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<ScaffoldCmdConfig>({
|
||||
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",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./scaffold"
|
||||
export * from "./types"
|
||||
import Scaffold from "./scaffold"
|
||||
|
||||
export default Scaffold
|
||||
|
||||
@@ -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()}`)
|
||||
|
||||
36
src/types.ts
36
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> = T | FileResponseHandler<T>
|
||||
* 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<string, ScaffoldConfig>
|
||||
|
||||
/** 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<ScaffoldCmdConfig, ScaffoldConfigMap>
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ const blankCliConf: ScaffoldCmdConfig = {
|
||||
subdir: false,
|
||||
dryRun: false,
|
||||
quiet: false,
|
||||
version: false,
|
||||
}
|
||||
|
||||
const blankConfig: ScaffoldCmdConfig = {
|
||||
|
||||
Reference in New Issue
Block a user