Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
call the buildDirectory function if the init flag is true
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Lynch committed Feb 12, 2021
1 parent e5f6ffc commit 780501f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ ARGUMENTS
OPTIONS
-h, --help show CLI help
-v, --version show CLI version
--multi-app
--init create the cdk directory before building the app and stack files
--multi-app create the stack files within sub directories as the project defines multiple apps
```

_See code: [src/commands/migrate.ts](https://github.com/guardian/cdk-cli/blob/v0.0.0/src/commands/migrate.ts)_
Expand All @@ -112,7 +113,8 @@ ARGUMENTS
OPTIONS
-h, --help show CLI help
-v, --version show CLI version
--multi-app
--init create the cdk directory before building the app and stack files
--multi-app create the stack files within sub directories as the project defines multiple apps
```

_See code: [src/commands/new.ts](https://github.com/guardian/cdk-cli/blob/v0.0.0/src/commands/new.ts)_
Expand Down
7 changes: 6 additions & 1 deletion src/commands/migrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("The MigrateCommand class", () => {
},
flags: {
"multi-app": false,
init: false,
},
};

Expand Down Expand Up @@ -49,7 +50,10 @@ describe("The MigrateCommand class", () => {

test("pulls outs computed values correctly if multiApp is true", () => {
expect(
MigrateCommand.getConfig({ ...args, flags: { "multi-app": true } })
MigrateCommand.getConfig({
...args,
flags: { "multi-app": true, init: false },
})
).toMatchObject({
cfnFile: "template.ts",
appPath: `/path/to/output/bin/app.ts`,
Expand All @@ -67,6 +71,7 @@ describe("The MigrateCommand class", () => {
},
flags: {
"multi-app": false,
init: false,
},
};

Expand Down
24 changes: 22 additions & 2 deletions src/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "../utils/args";
import { parse } from "../utils/cfn";
import { newAppImports, newTestImports } from "../utils/imports";
import { buildDirectory } from "../utils/init";
import { constructTest } from "../utils/snapshot";
import { constructStack } from "../utils/stack";
import type { Name } from "../utils/utils";
Expand All @@ -24,6 +25,7 @@ interface MigrateCommandConfig {
stackPath: string;
stackName: Name;
testPath: string;
init: boolean;
}

interface MigrateCommandArgs {
Expand All @@ -35,6 +37,7 @@ interface MigrateCommandArgs {

interface MigrateCommandFlags {
"multi-app": boolean;
init: boolean;
}

export class MigrateCommand extends Command {
Expand All @@ -44,7 +47,16 @@ export class MigrateCommand extends Command {
static flags = {
version: flags.version({ char: "v" }),
help: flags.help({ char: "h" }),
"multi-app": flags.boolean(),
"multi-app": flags.boolean({
default: false,
description:
"create the stack files within sub directories as the project defines multiple apps",
}),
init: flags.boolean({
default: false,
description:
"create the cdk directory before building the app and stack files",
}),
};

static args = [
Expand Down Expand Up @@ -106,6 +118,7 @@ export class MigrateCommand extends Command {
testPath: `${cdkDir}/lib/${
flags["multi-app"] ? `${kebabAppName}/` : ""
}${kebabStackName}.test.ts`,
init: flags["init"],
};

MigrateCommand.validateConfig(config);
Expand All @@ -116,7 +129,9 @@ export class MigrateCommand extends Command {
static validateConfig = (config: MigrateCommandConfig): void => {
// TODO: Do some better validation here to make sure that files and directories are what we expect them to be.
checkPathExists(config.cfnPath);
checkPathExists(config.cdkDir); // TODO: Add an option to init the CDK dir at the same time?
if (!config.init) {
checkPathExists(config.cdkDir);
}
checkPathDoesNotExist(config.appPath); // TODO: Update the app file if it already exists
checkPathDoesNotExist(config.stackPath);
};
Expand All @@ -126,6 +141,11 @@ export class MigrateCommand extends Command {

const config = MigrateCommand.getConfig(this.parse(MigrateCommand));

if (config.init) {
this.log("Creating CDK directory");
buildDirectory({ outputDir: config.cdkDir }, this);
}

this.log(`Converting template found at ${config.cfnPath}`);
this.log(
`New app ${config.appName.pascal} will be written to ${config.appPath}`
Expand Down
6 changes: 5 additions & 1 deletion src/commands/new.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("The NewCommand class", () => {
},
flags: {
"multi-app": false,
init: false,
},
};

Expand Down Expand Up @@ -46,7 +47,10 @@ describe("The NewCommand class", () => {

test("pulls outs computed values correctly if multiApp is true", () => {
expect(
NewCommand.getConfig({ ...args, flags: { "multi-app": true } })
NewCommand.getConfig({
...args,
flags: { "multi-app": true, init: false },
})
).toMatchObject({
appPath: `/path/to/output/bin/app.ts`,
stackPath: `/path/to/output/lib/app/stack-name.ts`,
Expand Down
23 changes: 21 additions & 2 deletions src/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
newStackImports,
newTestImports,
} from "../utils/imports";
import { buildDirectory } from "../utils/init";
import { cancellablePrompts } from "../utils/prompts";
import { constructTest } from "../utils/snapshot";
import type { StackTemplate } from "../utils/stack";
Expand All @@ -27,6 +28,7 @@ interface NewCommandConfig {
stackPath: string;
stackName: Name;
testPath: string;
init: boolean;
}

interface NewCommandArgs {
Expand All @@ -37,6 +39,7 @@ interface NewCommandArgs {

interface NewCommandFlags {
"multi-app": boolean;
init: boolean;
}

export class NewCommand extends Command {
Expand All @@ -45,7 +48,16 @@ export class NewCommand extends Command {
static flags = {
version: flags.version({ char: "v" }),
help: flags.help({ char: "h" }),
"multi-app": flags.boolean(),
"multi-app": flags.boolean({
default: false,
description:
"create the stack files within sub directories as the project defines multiple apps",
}),
init: flags.boolean({
default: false,
description:
"create the cdk directory before building the app and stack files",
}),
};

static args = [
Expand Down Expand Up @@ -102,6 +114,7 @@ export class NewCommand extends Command {
testPath: `${cdkDir}/lib/${
flags["multi-app"] ? `${kebabAppName}/` : ""
}${kebabStackName}.test.ts`,
init: flags["init"],
};

NewCommand.validateConfig(config);
Expand All @@ -110,7 +123,9 @@ export class NewCommand extends Command {
};

static validateConfig = (config: NewCommandConfig): void => {
checkPathExists(config.cdkDir); // TODO: Add an option to init the CDK dir at the same time?
if (!config.init) {
checkPathExists(config.cdkDir);
}
checkPathDoesNotExist(config.appPath); // TODO: Update the app file if it already exists
checkPathDoesNotExist(config.stackPath);
};
Expand All @@ -120,6 +135,10 @@ export class NewCommand extends Command {

const config = NewCommand.getConfig(this.parse(NewCommand));

if (config.init) {
buildDirectory({ outputDir: config.cdkDir }, this);
}

this.log(
`New app ${config.appName.pascal} will be written to ${config.appPath}`
);
Expand Down

0 comments on commit 780501f

Please sign in to comment.