Skip to content

Commit

Permalink
fix(cli): speed up shell autocompletions
Browse files Browse the repository at this point in the history
Speed up shell autocompletions by pulling actual handler code into
a separate entrypoint which is only required when it is going to be
invoked. When testing this speed up shell completions by 6x (from
1.2s to 0.2s [locally on OS X]). The main reason seemed to be e.g.
the React package required by e.g. ink which is deffered into a
separate bundle / entrypoint now. Also `inquirer` seems to have been
quite a burden (used in `init`).
  • Loading branch information
ansgarm committed Jan 19, 2022
1 parent 51be30b commit 106221b
Show file tree
Hide file tree
Showing 18 changed files with 397 additions and 334 deletions.
83 changes: 5 additions & 78 deletions packages/cdktf-cli/bin/cmds/convert.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,5 @@
import yargs from "yargs";
import { convert } from "@cdktf/hcl2cdk";
import { displayVersionMessage } from "./helper/version-check";
import { sendTelemetry } from "../../lib/checkpoint";
import { Errors } from "../../lib/errors";
import * as fs from "fs-extra";
import * as path from "path";
import {
readSchema,
ConstructsMakerProviderTarget,
LANGUAGES,
config,
} from "@cdktf/provider-generator";

function readStreamAsString(stream: typeof process.stdin): Promise<string> {
return new Promise((ok, ko) => {
if (stream.isTTY) {
ko(
"No stdin was passed, please use it like this: cat main.tf | cdktf convert > imported.ts"
);
} else {
let string = "";
stream.on("data", (data) => (string += data.toString()));

stream.on("close", () => ok(string));
stream.on("error", (err) => ko(err));
}
});
}

function findFileAboveCwd(
file: string,
rootPath = process.cwd()
): string | null {
const fullPath = path.resolve(rootPath, file);
if (fs.existsSync(fullPath)) {
return fullPath;
}

const parentDir = path.resolve(rootPath, "..");
if (fs.existsSync(parentDir) && parentDir !== rootPath) {
return findFileAboveCwd(file, parentDir);
}

return null;
}
import { requireHandlers } from "./helper/utilities";

class Command implements yargs.CommandModule {
public readonly command = "convert [OPTIONS]";
Expand Down Expand Up @@ -76,39 +32,10 @@ class Command implements yargs.CommandModule {
})
.showHelpOnFail(true);

public async handler({ language }: any) {
await displayVersionMessage();

const providerRequirements: string[] = yargs.argv.provider as string[];
const cdktfJsonPath = findFileAboveCwd("cdktf.json");
if (cdktfJsonPath) {
const cdktfJson = await fs.readJson(cdktfJsonPath);
providerRequirements.push(...cdktfJson.terraformProviders);
}
// Get all the provider schemas
const { providerSchema } = await readSchema(
providerRequirements.map((spec) =>
ConstructsMakerProviderTarget.from(
new config.TerraformProviderConstraint(spec),
LANGUAGES[0]
)
)
);

const input = await readStreamAsString(process.stdin);
let output;
try {
const { all, stats } = await convert(input, {
language,
providerSchema,
});
output = all;
await sendTelemetry("convert", { ...stats, error: false });
} catch (err) {
throw Errors.Internal("convert", err.message, { language });
}

console.log(output);
public async handler(argv: any) {
// deferred require to keep cdktf-cli package small (e.g. for fast shell completions)
const api = requireHandlers();
api.convert(argv);
}
}

Expand Down
26 changes: 4 additions & 22 deletions packages/cdktf-cli/bin/cmds/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import * as yargs from "yargs";
import React from "react";
import { Deploy } from "./ui/deploy";
import { config as cfg } from "@cdktf/provider-generator";
import { renderInk } from "./helper/render-ink";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";
import { checkEnvironment } from "./helper/check-environment";
import { requireHandlers } from "./helper/utilities";

const config = cfg.readConfigSync();

Expand Down Expand Up @@ -41,22 +36,9 @@ class Command implements yargs.CommandModule {
.showHelpOnFail(true);

public async handler(argv: any) {
throwIfNotProjectDirectory("deploy");
await displayVersionMessage();
await checkEnvironment("deploy");
const command = argv.app;
const outdir = argv.output;
const autoApprove = argv.autoApprove;
const stack = argv.stack;

await renderInk(
React.createElement(Deploy, {
targetDir: outdir,
targetStack: stack,
synthCommand: command,
autoApprove,
})
);
// deferred require to keep cdktf-cli package small (e.g. for fast shell completions)
const api = requireHandlers();
api.deploy(argv);
}
}

Expand Down
26 changes: 4 additions & 22 deletions packages/cdktf-cli/bin/cmds/destroy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import * as yargs from "yargs";
import React from "react";
import { Destroy } from "./ui/destroy";
import { config as cfg } from "@cdktf/provider-generator";
import { renderInk } from "./helper/render-ink";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";
import { checkEnvironment } from "./helper/check-environment";
import { requireHandlers } from "./helper/utilities";

const config = cfg.readConfigSync();

Expand Down Expand Up @@ -40,22 +35,9 @@ class Command implements yargs.CommandModule {
.showHelpOnFail(true);

public async handler(argv: any) {
throwIfNotProjectDirectory("destroy");
await displayVersionMessage();
await checkEnvironment("destroy");
const command = argv.app;
const outdir = argv.output;
const autoApprove = argv.autoApprove;
const stack = argv.stack;

await renderInk(
React.createElement(Destroy, {
targetDir: outdir,
targetStack: stack,
synthCommand: command,
autoApprove,
})
);
// deferred require to keep cdktf-cli package small (e.g. for fast shell completions)
const api = requireHandlers();
api.destroy(argv);
}
}

Expand Down
24 changes: 4 additions & 20 deletions packages/cdktf-cli/bin/cmds/diff.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import yargs from "yargs";
import React from "react";
import { Diff } from "./ui/diff";
import { config as cfg } from "@cdktf/provider-generator";
import { renderInk } from "./helper/render-ink";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";
import { checkEnvironment } from "./helper/check-environment";
import { requireHandlers } from "./helper/utilities";

const config = cfg.readConfigSync();

Expand Down Expand Up @@ -36,20 +31,9 @@ class Command implements yargs.CommandModule {
.showHelpOnFail(true);

public async handler(argv: any) {
throwIfNotProjectDirectory("diff");
await displayVersionMessage();
await checkEnvironment("diff");
const command = argv.app;
const outdir = argv.output;
const stack = argv.stack;

await renderInk(
React.createElement(Diff, {
targetDir: outdir,
targetStack: stack,
synthCommand: command,
})
);
// deferred require to keep cdktf-cli package small (e.g. for fast shell completions)
const api = requireHandlers();
api.diff(argv);
}
}

Expand Down
45 changes: 5 additions & 40 deletions packages/cdktf-cli/bin/cmds/get.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import yargs from "yargs";
import React from "react";
import { Language, LANGUAGES, config as cfg } from "@cdktf/provider-generator";
import { Get } from "./ui/get";
import { renderInk } from "./helper/render-ink";
import { displayVersionMessage } from "./helper/version-check";
import { throwIfNotProjectDirectory } from "./helper/check-directory";
import { checkEnvironment } from "./helper/check-environment";

import { LANGUAGES, config as cfg } from "@cdktf/provider-generator";
import { requireHandlers } from "./helper/utilities";
const config = cfg.readConfigSync();

interface Arguments {
output: string;
language: Language;
}

class Command implements yargs.CommandModule {
public readonly command = "get [OPTIONS]";
public readonly describe =
Expand All @@ -38,33 +27,9 @@ class Command implements yargs.CommandModule {
});

public async handler(argv: any) {
throwIfNotProjectDirectory("get");
await displayVersionMessage();
await checkEnvironment("get");
const args = argv as Arguments;
const providers = config.terraformProviders ?? [];
const modules = config.terraformModules ?? [];
const { output, language } = args;

const constraints: cfg.TerraformDependencyConstraint[] = [
...providers,
...modules,
];

if (constraints.length === 0) {
console.error(
`ERROR: Please specify providers or modules in "cdktf.json" config file`
);
process.exit(1);
}

await renderInk(
React.createElement(Get, {
codeMakerOutput: output,
language: language,
constraints,
})
);
// deferred require to keep cdktf-cli package small (e.g. for fast shell completions)
const api = requireHandlers();
api.get(argv);
}
}

Expand Down
Loading

0 comments on commit 106221b

Please sign in to comment.