From f8f29ab8f354a283dc8159f3802194de12bc96cd Mon Sep 17 00:00:00 2001 From: "Github Action (authored by pmalacho-mit)" Date: Fri, 22 Dec 2023 10:36:09 -0500 Subject: [PATCH] Got all extension creation commands working --- extensions/README.md | 2 +- .../documentation/src/anatomy/README.md | 2 +- .../scripts/factories/customArgument.ts | 4 ++-- extensions/scripts/factories/extension.ts | 15 ++++++------ extensions/scripts/factories/index.ts | 23 +++++++++---------- extensions/scripts/factories/package.ts | 4 ++-- extensions/scripts/factories/test.ts | 4 ++-- extensions/scripts/factories/translations.ts | 4 ++-- extensions/scripts/factories/ui.ts | 4 ++-- scripts/options.ts | 19 ++++++++++++--- 10 files changed, 46 insertions(+), 35 deletions(-) diff --git a/extensions/README.md b/extensions/README.md index 6e1c97b4e..1efe2f558 100644 --- a/extensions/README.md +++ b/extensions/README.md @@ -62,7 +62,7 @@ export default class ExampleExtension extends extension({ name: "Example" }) { ``` - Instead of running the following from the root of the project every time: ``` - npm run dev --include myExtension + npm run dev -- --include myExtension ``` - Inspect the `package.json` file to see all augmented scripts. diff --git a/extensions/documentation/src/anatomy/README.md b/extensions/documentation/src/anatomy/README.md index 8b1c488b9..d08b3377c 100644 --- a/extensions/documentation/src/anatomy/README.md +++ b/extensions/documentation/src/anatomy/README.md @@ -28,7 +28,7 @@ Below are the files you should always find within an extension's directory: ``` - Instead of running the following from the root of the project every time: ``` - npm run dev --include myExtension + npm run dev -- --include myExtension ``` - Inspect the `package.json` file to see all augmented scripts. diff --git a/extensions/scripts/factories/customArgument.ts b/extensions/scripts/factories/customArgument.ts index 3a464971d..0e02efbbf 100644 --- a/extensions/scripts/factories/customArgument.ts +++ b/extensions/scripts/factories/customArgument.ts @@ -1,8 +1,8 @@ import chalk from "chalk"; import path from "path"; -import { copyTemplateToDestinationAndIncrementIfExists, processDirectoryArg } from "."; +import { copyTemplateToDestinationAndIncrementIfExists, getDirectoryArg } from "."; -const directory = processDirectoryArg(); +const directory = getDirectoryArg(); const destination = copyTemplateToDestinationAndIncrementIfExists(directory, "CustomArgument.svelte"); const name = path.basename(destination).replace(path.extname(destination), ""); diff --git a/extensions/scripts/factories/extension.ts b/extensions/scripts/factories/extension.ts index 1f8d4106e..d600c0229 100644 --- a/extensions/scripts/factories/extension.ts +++ b/extensions/scripts/factories/extension.ts @@ -2,11 +2,9 @@ import path from "path"; import assert from "assert"; import fs from "fs"; import chalk from "chalk"; -import { processArgs } from "$root/scripts/processArgs"; -import { UnionToTuple, ValueOf } from "$common"; -import { DirectoryArg, directoryDefault, directoryFlag, getPathToExtension, getPathToTemplate } from "."; - -const alreadyExists = (directory: string) => fs.existsSync(getPathToExtension(directory, false)); +import { ValueOf } from "$common"; +import { getPathToExtension, getPathToTemplate, directoryArg } from "."; +import { parsePositionalArgs } from "$root/scripts/options"; const OperationMap = { Default: "default", @@ -28,11 +26,12 @@ Object.values(templateByOperation) .map(template => getPathToTemplate(template)) .forEach(filepath => assert(fs.existsSync(filepath))); -const { directory, operation } = processArgs( - { ...directoryFlag, operation: "op" }, - { ...directoryDefault, operation: OperationMap.Default } +const { directory, operation } = parsePositionalArgs(directoryArg, + ["operation", { type: "string", default: OperationMap.Default, choices: operations }] ); +const alreadyExists = (directory: string) => fs.existsSync(getPathToExtension(directory, false)); + const error = (msg: string) => { throw new Error(chalk.redBright(msg)) }; if (!directory) error("A directory must be provided in order to create an extension."); diff --git a/extensions/scripts/factories/index.ts b/extensions/scripts/factories/index.ts index a116504aa..b361f8547 100644 --- a/extensions/scripts/factories/index.ts +++ b/extensions/scripts/factories/index.ts @@ -1,8 +1,10 @@ import path from "path"; import fs from "fs"; +import yargs, { type PositionalOptions, type InferredOptionType, choices } from 'yargs'; +import { hideBin } from 'yargs/helpers' import { extensionsSrc, templatesDirectory } from "scripts/utils/fileSystem"; import chalk from "chalk"; -import { processArgs } from "$root/scripts/processArgs"; +import { PositionalArg, parsePositionalArgs } from "$root/scripts/options"; export const getPathToTemplate = (name: string) => { const location = path.join(templatesDirectory, name); @@ -50,17 +52,14 @@ export const copyTemplateToDestinationAndIncrementIfExists = (extensionDirectory return destination; } -export type DirectoryArg = { - directory: string; -} +export const error = (msg: string) => { throw new Error(chalk.redBright(msg)) }; -export const directoryFlag: DirectoryArg = { directory: "dir" }; -export const directoryDefault: DirectoryArg = { directory: null }; +export const checkPositionalArgs = (argv: T) => { }; -export const error = (msg: string) => { throw new Error(chalk.redBright(msg)) }; +export const directoryArg = ["directory", { + type: "string", + demandOption: true, + description: "The name of the extension directory" +}] as const satisfies PositionalArg; -export const processDirectoryArg = () => { - const { directory } = processArgs(directoryFlag, directoryDefault); - if (!directory) error("An extension directory must be provided in order to add a ui to an extension."); - return directory; -} +export const getDirectoryArg = () => parsePositionalArgs(directoryArg).directory; \ No newline at end of file diff --git a/extensions/scripts/factories/package.ts b/extensions/scripts/factories/package.ts index 6f7c3f0d0..23ba0b9bf 100644 --- a/extensions/scripts/factories/package.ts +++ b/extensions/scripts/factories/package.ts @@ -1,6 +1,6 @@ import fs from "fs"; import chalk from "chalk"; -import { processDirectoryArg, copyTemplateToDestination } from "."; +import { copyTemplateToDestination, getDirectoryArg, } from "."; import { splitOnCapitals } from "$common"; const convertToPackageName = (name: string) => @@ -18,7 +18,7 @@ const fillInPackageDetails = (location: string, name: string) => { fs.writeFileSync(location, text, { encoding }); } -const directory = processDirectoryArg(); +const directory = getDirectoryArg(); const file = copyTemplateToDestination(directory, "package.json"); fillInPackageDetails(file, directory); diff --git a/extensions/scripts/factories/test.ts b/extensions/scripts/factories/test.ts index b1499a281..fac5579c8 100644 --- a/extensions/scripts/factories/test.ts +++ b/extensions/scripts/factories/test.ts @@ -1,7 +1,7 @@ import chalk from "chalk"; -import { processDirectoryArg, copyTemplateToDestination } from "."; +import { copyTemplateToDestination, getDirectoryArg } from "."; -const directory = processDirectoryArg(); +const directory = getDirectoryArg(); const destination = copyTemplateToDestination(directory, "index.test.ts"); const msg = [ diff --git a/extensions/scripts/factories/translations.ts b/extensions/scripts/factories/translations.ts index d49f42bc6..5abae3c1a 100644 --- a/extensions/scripts/factories/translations.ts +++ b/extensions/scripts/factories/translations.ts @@ -1,7 +1,7 @@ import chalk from "chalk"; -import { processDirectoryArg, copyTemplateToDestination } from "."; +import { copyTemplateToDestination, getDirectoryArg } from "."; -const directory = processDirectoryArg(); +const directory = getDirectoryArg(); const destination = copyTemplateToDestination(directory, "translations.ts"); const msg = [ diff --git a/extensions/scripts/factories/ui.ts b/extensions/scripts/factories/ui.ts index 390df682d..3cd00325f 100644 --- a/extensions/scripts/factories/ui.ts +++ b/extensions/scripts/factories/ui.ts @@ -1,8 +1,8 @@ import chalk from "chalk"; import path from "path"; -import { copyTemplateToDestinationAndIncrementIfExists, processDirectoryArg } from "."; +import { copyTemplateToDestinationAndIncrementIfExists, getDirectoryArg } from "."; -const directory = processDirectoryArg(); +const directory = getDirectoryArg(); const destination = copyTemplateToDestinationAndIncrementIfExists(directory, "UI.svelte"); const name = path.basename(destination).replace(path.extname(destination), ""); diff --git a/scripts/options.ts b/scripts/options.ts index 6ec7f5840..15f51cd88 100644 --- a/scripts/options.ts +++ b/scripts/options.ts @@ -1,8 +1,8 @@ -import yargs from 'yargs'; +import yargs, { type PositionalOptions, type InferredOptionType, Arguments } from 'yargs'; import { hideBin } from 'yargs/helpers' /** - * Parses command line arguments + * Parses command line arguments (for the build script) * @param argv `process.argv` */ export default (argv: string[]) => yargs(hideBin(argv)) @@ -43,4 +43,17 @@ export const convertToFlags = (args: Record) => Object.entries(a * @param names * @returns */ -export const asFlags = (...names: string[]) => names.map(flagify); \ No newline at end of file +export const asFlags = (...names: string[]) => names.map(flagify); + +export type PositionalArg = [string, PositionalOptions]; +type PositionalArgsToObject = { + [P in T[number][0]]: InferredOptionType[1]>; +}; + +export const parsePositionalArgs = (...args: T): PositionalArgsToObject => { + const commandString = `$0 ${args.map(([name, options]) => options.default ? `[${name}]` : `<${name}>`).join(" ")}`; + const options = yargs(hideBin(process.argv)).command(commandString, "", yargs => { + args.reduce((acc, [name, options]) => acc.positional(name, options), yargs); + }).parseSync(); + return options as any as PositionalArgsToObject & Arguments; +} \ No newline at end of file