Skip to content

Commit 2955017

Browse files
committed
Added convenience CLI flags
1 parent 796b757 commit 2955017

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

solves/src/arg-parse.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ function ensureStr(obj: any, argName: string): string {
1919
/********************************************************************/
2020

2121
export interface ArgValues {
22-
specFilePath: string;
23-
sourceOutputDirPath: string;
24-
appOutputDirPath: string;
22+
specFilePath: string;
23+
sourceOutputDirPath: string;
24+
appOutputDirPath: string;
25+
26+
allowOverwriteOutput: boolean;
27+
startDevServer: boolean;
2528
}
2629

2730
const program = new Command();
@@ -46,6 +49,16 @@ program.option(
4649
"../../generated-web-app", // TODO: How to make this relative to invocation?
4750
);
4851

52+
program.option(
53+
"-f, --force",
54+
"Allows overwriting of output directories (--source-out and --app-out).",
55+
);
56+
57+
program.option(
58+
"--start-dev-server",
59+
"Instead of emitting a generated static web app (via. -o/--app-out), start the Webpack dev server. Using --start-dev-server will cause -o/--app-out to be ignored.",
60+
);
61+
4962
program.parse();
5063

5164
const rawOptions = program.opts();
@@ -54,6 +67,9 @@ const typedOptions: ArgValues = {
5467
specFilePath: path.join(cwd, ensureStr(rawOptions["spec"], "spec")),
5568
sourceOutputDirPath: path.join(cwd, ensureStr(rawOptions["sourceOut"], "source-out")),
5669
appOutputDirPath: path.join(cwd, ensureStr(rawOptions["appOut"], "app-out")),
70+
71+
allowOverwriteOutput: (rawOptions["force"] === true),
72+
startDevServer: (rawOptions["startDevServer"] === true),
5773
};
5874

5975
// Some really crude validation for this prototype, for safety

solves/src/code-generator/index.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* License: GNU Affero General Public License v3 (AGPL-3.0)
55
*/
66

7+
//import fs from "fs";
8+
//import child_process from "child_process";
9+
710
import {execute} from "@yarnpkg/shell";
811

912
import {type ArgValues} from "../arg-parse";
@@ -12,15 +15,41 @@ import {copyDirAndApplyTemplate} from "./base-template";
1215

1316
const BASE_TEMPLATE_PATH = "./base-template";
1417

15-
export function generateSource(specValues: SpecValues, cliArgs: ArgValues) {
18+
export async function generateSource(specValues: SpecValues, cliArgs: ArgValues) {
19+
if (cliArgs.allowOverwriteOutput) {
20+
// Weird bug in yarn that causes this to fail.
21+
// See <https://github.com/yarnpkg/berry/issues/1818>
22+
//fs.rmSync(String(cliArgs.sourceOutputDirPath), {force: true, recursive: true});
23+
// We'll just use a shell command for now
24+
await execute(`rm -rf ${cliArgs.sourceOutputDirPath}`);
25+
// TODO: Fix this
26+
}
27+
1628
copyDirAndApplyTemplate(BASE_TEMPLATE_PATH, cliArgs.sourceOutputDirPath, {
1729
...specValues,
1830
});
1931
// TODO: Sanitize to prevent command injection
20-
execute(
32+
33+
await execute(
2134
`cd ${cliArgs.sourceOutputDirPath}`
2235
+ " && yarn set version stable && yarn install"
23-
+ ` && yarn build --output-path ${cliArgs.appOutputDirPath}`
2436
);
37+
38+
if (cliArgs.startDevServer) {
39+
await execute(
40+
`cd ${cliArgs.sourceOutputDirPath}`
41+
+ " && yarn start"
42+
);
43+
} else {
44+
if (cliArgs.allowOverwriteOutput) {
45+
//fs.rmSync(String(cliArgs.appOutputDirPath), {force: true, recursive: true});
46+
await execute(`rm -rf ${cliArgs.appOutputDirPath}`);
47+
// TODO: Fix this
48+
}
49+
await execute(
50+
`cd ${cliArgs.sourceOutputDirPath}`
51+
+ ` && yarn build --output-path ${cliArgs.appOutputDirPath}`
52+
);
53+
}
2554
}
2655

solves/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ const cliArgs = getCLIArgs();
1313
const specValues = getSpecValues(cliArgs.specFilePath);
1414
console.log(specValues);
1515

16-
generateSource(specValues, cliArgs);
16+
generateSource(specValues, cliArgs)
17+
.then()
18+
.catch(err => {
19+
console.log(err);
20+
});
1721

0 commit comments

Comments
 (0)