From 6fe3d90a175f73c3a9ca442ffa3eb8259877bdc9 Mon Sep 17 00:00:00 2001 From: "nisch.codes" Date: Sun, 9 Mar 2025 20:26:18 +0100 Subject: [PATCH] feat: Add new cli-tool for creating and building webby projects --- src/bin.mts | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 19 +++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/bin.mts diff --git a/src/bin.mts b/src/bin.mts new file mode 100644 index 0000000..d01ca73 --- /dev/null +++ b/src/bin.mts @@ -0,0 +1,73 @@ +#!/usr/bin/env node +import type { WebbyAsyncOptions } from './index.js' +import { loadPackageJson } from 'package-json-from-dist' + +const { version } = loadPackageJson(import.meta.url) + +const runHelpForUsage = () => console.error('run `webby --help` for usage information') + +export const help = `webby version ${version} + + Usage: webby + Builds your website with the information from the webby.conf.js file. + + Options: + -h --help Display this usage info + --version Display version + ` + +import { parse, relative, resolve } from 'path' +const cwd = process.cwd() + +const main = async (...args: string[]) => { + if (process.env.__WEBBY_TESTING_BIN_FAIL__ === '1') { + throw new Error('simulated Webby failure') + } + + const opt: WebbyAsyncOptions = {} + const paths: string[] = [] + /* + let impl: ( + path: string | string[], + opt?: WebbyAsyncOptions, + ) => Promise = webby + */ + + for (const arg of args) { + if (arg === '-h' || arg === '--help') { + console.log(help) + return 0 + } else if (arg === '--version') { + console.log(version) + return 0 + } else { + console.error(`unknown option: ${arg}`) + runHelpForUsage() + return 1 + } + } + + if (!paths.length) { + console.error('webby: must provide a path to build') + runHelpForUsage() + return 1 + } + + return 0 +} + +main.help = help +main.version = version + +export default main + +if (process.env.__TESTING_WEBBY_BIN__ !== '1') { + const args = process.argv.slice(2) + main(...args).then( + code => process.exit(code), + er => { + console.error(er) + process.exit(1) + }, + ) +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 4d15593..7a87a62 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,18 @@ -console.log("This is Webby - A small static website builder written in TypeScript for learning purposes.") \ No newline at end of file +import { Dirent, Stats } from 'fs' +import { GlobOptions } from 'glob' + +export interface WebbyAsyncOptions { + preserveRoot?: boolean + tmp?: string + maxRetries?: number + retryDelay?: number + backoff?: number + maxBackoff?: number + signal?: AbortSignal + glob?: boolean | GlobOptions + filter?: + | ((path: string, ent: Dirent | Stats) => boolean) + | ((path: string, ent: Dirent | Stats) => Promise) +} + +//console.log("This is Webby - A small static website builder written in TypeScript for learning purposes.") \ No newline at end of file