Skip to content

Commit

Permalink
Got all extension creation commands working
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalacho-mit committed Dec 22, 2023
1 parent c633ed0 commit f8f29ab
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 35 deletions.
2 changes: 1 addition & 1 deletion extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion extensions/documentation/src/anatomy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions extensions/scripts/factories/customArgument.ts
Original file line number Diff line number Diff line change
@@ -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), "");
Expand Down
15 changes: 7 additions & 8 deletions extensions/scripts/factories/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -28,11 +26,12 @@ Object.values(templateByOperation)
.map(template => getPathToTemplate(template))
.forEach(filepath => assert(fs.existsSync(filepath)));

const { directory, operation } = processArgs<DirectoryArg & { operation: Operation }>(
{ ...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.");
Expand Down
23 changes: 11 additions & 12 deletions extensions/scripts/factories/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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 = <T>(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<DirectoryArg>(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;
4 changes: 2 additions & 2 deletions extensions/scripts/factories/package.ts
Original file line number Diff line number Diff line change
@@ -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) =>
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions extensions/scripts/factories/test.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions extensions/scripts/factories/translations.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions extensions/scripts/factories/ui.ts
Original file line number Diff line number Diff line change
@@ -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), "");
Expand Down
19 changes: 16 additions & 3 deletions scripts/options.ts
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -43,4 +43,17 @@ export const convertToFlags = (args: Record<string, string>) => Object.entries(a
* @param names
* @returns
*/
export const asFlags = (...names: string[]) => names.map(flagify);
export const asFlags = (...names: string[]) => names.map(flagify);

export type PositionalArg = [string, PositionalOptions];
type PositionalArgsToObject<T extends PositionalArg[]> = {
[P in T[number][0]]: InferredOptionType<Extract<T[number], [P, any]>[1]>;
};

export const parsePositionalArgs = <const T extends [string, PositionalOptions][]>(...args: T): PositionalArgsToObject<T> => {
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<T> & Arguments;
}

0 comments on commit f8f29ab

Please sign in to comment.