From 780501fb3e59a1301314a7f700ea5313f67a234d Mon Sep 17 00:00:00 2001 From: Jamie Lynch Date: Fri, 29 Jan 2021 15:34:03 +0000 Subject: [PATCH] call the buildDirectory function if the init flag is true --- README.md | 6 ++++-- src/commands/migrate.test.ts | 7 ++++++- src/commands/migrate.ts | 24 ++++++++++++++++++++++-- src/commands/new.test.ts | 6 +++++- src/commands/new.ts | 23 +++++++++++++++++++++-- 5 files changed, 58 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 05e781a..357472c 100644 --- a/README.md +++ b/README.md @@ -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)_ @@ -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)_ diff --git a/src/commands/migrate.test.ts b/src/commands/migrate.test.ts index 1f6deab..38f30c2 100644 --- a/src/commands/migrate.test.ts +++ b/src/commands/migrate.test.ts @@ -19,6 +19,7 @@ describe("The MigrateCommand class", () => { }, flags: { "multi-app": false, + init: false, }, }; @@ -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`, @@ -67,6 +71,7 @@ describe("The MigrateCommand class", () => { }, flags: { "multi-app": false, + init: false, }, }; diff --git a/src/commands/migrate.ts b/src/commands/migrate.ts index 0aa7ca0..6e3d532 100644 --- a/src/commands/migrate.ts +++ b/src/commands/migrate.ts @@ -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"; @@ -24,6 +25,7 @@ interface MigrateCommandConfig { stackPath: string; stackName: Name; testPath: string; + init: boolean; } interface MigrateCommandArgs { @@ -35,6 +37,7 @@ interface MigrateCommandArgs { interface MigrateCommandFlags { "multi-app": boolean; + init: boolean; } export class MigrateCommand extends Command { @@ -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 = [ @@ -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); @@ -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); }; @@ -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}` diff --git a/src/commands/new.test.ts b/src/commands/new.test.ts index bc078b9..bdd992d 100644 --- a/src/commands/new.test.ts +++ b/src/commands/new.test.ts @@ -18,6 +18,7 @@ describe("The NewCommand class", () => { }, flags: { "multi-app": false, + init: false, }, }; @@ -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`, diff --git a/src/commands/new.ts b/src/commands/new.ts index d82da9b..9c4a0f9 100644 --- a/src/commands/new.ts +++ b/src/commands/new.ts @@ -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"; @@ -27,6 +28,7 @@ interface NewCommandConfig { stackPath: string; stackName: Name; testPath: string; + init: boolean; } interface NewCommandArgs { @@ -37,6 +39,7 @@ interface NewCommandArgs { interface NewCommandFlags { "multi-app": boolean; + init: boolean; } export class NewCommand extends Command { @@ -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 = [ @@ -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); @@ -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); }; @@ -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}` );