Skip to content

Commit

Permalink
Add open option to cli and make CLI use strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Bardadym committed Dec 9, 2023
1 parent e1b03a0 commit b410872
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { promises as fs } from "fs";
import path from "path";

import opn from "open";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

Expand Down Expand Up @@ -34,6 +35,12 @@ const argv = yargs(hideBin(process.argv))
type: "boolean",
default: false,
})
.option("open", {
describe: "Open generated tempate in default user agent",
type: "boolean",
default: false,
})
.strict()
.help()
.parseSync();

Expand All @@ -44,9 +51,10 @@ interface CliArgs {
title: string;
template: TemplateType;
sourcemap: boolean;
open: boolean;
}

const runForPluginJson = async ({ title, template, filename }: CliArgs, files: string[]) => {
const runForPluginJson = async ({ title, template, filename, open }: CliArgs, files: string[]) => {
if (files.length === 0) {
throw new Error("Empty file list");
}
Expand All @@ -57,7 +65,7 @@ const runForPluginJson = async ({ title, template, filename }: CliArgs, files: s
const data = JSON.parse(textContent) as VisualizerData;

return { file, data };
})
}),
);

const tree: ModuleTree = {
Expand All @@ -70,7 +78,7 @@ const runForPluginJson = async ({ title, template, filename }: CliArgs, files: s
for (const { file, data } of fileContents) {
if (data.version !== version) {
warn(
`Version in ${file} is not supported (${data.version}). Current version ${version}. Skipping...`
`Version in ${file} is not supported (${data.version}). Current version ${version}. Skipping...`,
);
continue;
}
Expand Down Expand Up @@ -106,6 +114,10 @@ const runForPluginJson = async ({ title, template, filename }: CliArgs, files: s
// ignore
}
await fs.writeFile(filename, fileContent);

if (open) {
await opn(filename);
}
};

runForPluginJson(argv, listOfFiles as string[]).catch((err: Error) => {
Expand Down

0 comments on commit b410872

Please sign in to comment.