Skip to content

Commit

Permalink
Merge pull request #20 from pipelinit/feat/improve-cli
Browse files Browse the repository at this point in the history
Improve CLI
  • Loading branch information
oesgalha authored Aug 30, 2021
2 parents f2f73ad + 4bdb4be commit e3035f1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
43 changes: 34 additions & 9 deletions src/cli/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,47 @@ import { GlobalOptions } from "../types.ts";
import { outputErrors } from "../../plugin/errors.ts";
import { context } from "../../plugin/mod.ts";
import { ensureFile } from "../../../deps.ts";
import { errorCodes } from "../errors.ts";
import { config } from "../../config/mod.ts";

type DefaultOptions = GlobalOptions;

const WARN_NOTHING_DETECTED = `
Check the available stacks at the project README:
https://github.com/pipelinit/pipelinit-cli#support-overview
If your project has one of the available stacks and it
wasn't detected by pipelinit, this is probably a bug. Please
run pipelinit again with the --debug flag and open an issue here:
https://github.com/pipelinit/pipelinit-cli/issues/new
If you didn't see your stack there and wish it to be included,
start a discussion proposing the new stack here:
https://github.com/pipelinit/pipelinit-cli/discussions/new
`;

export default async function (opts: DefaultOptions): Promise<void> {
await prelude(opts);
const logger = context.getLogger("main");
const detected = await introspect();
const platform = "github";
const files = await platformWriters[platform](
context,
renderTemplates(platform, detected),
);
for (const { path, content } of files) {
logger.info(`Writing ${path}`);
await ensureFile(path);
await Deno.writeTextFile(path, content);

if (Object.keys(detected).length === 0) {
logger.error(WARN_NOTHING_DETECTED);
Deno.exit(errorCodes.NO_STACK_DETECTED);
}

const platforms = config.platforms!;
for (const platform of platforms) {
const files = await platformWriters[platform](
context,
renderTemplates(platform, detected),
);
for (const { path, content } of files) {
logger.info(`Writing ${path}`);
await ensureFile(path);
await Deno.writeTextFile(path, content);
}
}

outputErrors();
}
3 changes: 3 additions & 0 deletions src/cli/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const errorCodes = {
NO_STACK_DETECTED: 2,
};
17 changes: 17 additions & 0 deletions src/cli/prelude/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import { Checkbox, log } from "../../../deps.ts";
import { config } from "../../config/mod.ts";
import { isPlatform } from "../../platform/mod.ts";

// TODO
// The handling of some specific key presses on Windows doesn't
// work correclty in Deno. As a workaround, the checkbox prompt
// uses "u" and "d" to navigate across options. This hint informs
// that to the user. We should remove this hint when the expected
// behavior (up and down arrow keys) are available.
//
// Keep an eye on those issues:
// https://github.com/c4spar/deno-cliffy/issues/272
// https://github.com/denoland/deno/issues/5945
const NAV_HINT = Deno.build.os === "windows"
? "Press <space> to select, use 'u' and 'd' keys to navigate"
: "Press <space> to select, use arrow keys to navigate";

export async function configure() {
const logger = log.getLogger("main");
// Load the project config if it exists
Expand All @@ -15,9 +29,12 @@ export async function configure() {
logger.info("Configure this project");
const ciPlatforms = (await Checkbox.prompt({
message: "Which platforms does this project use?",
minOptions: 1, // The user must select at least one
hint: NAV_HINT,
options: [
{ name: "GitHub Actions", value: "github", checked: true },
{ name: "GitLab CI (coming soon)", value: "gitlab", disabled: true },
{ name: "Travis CI (coming soon)", value: "travis", disabled: true },
],
})).filter(isPlatform);

Expand Down
6 changes: 5 additions & 1 deletion src/stack/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export async function introspect() {
logger.info("Detecting stack...");
const stack = await detected();
const stackNames = stack.map((t) => t.name).sort().join(", ");
logger.info(`Detected stack: ${stackNames}`);
if (stackNames.length === 0) {
logger.warning("No stack detected!");
} else {
logger.info(`Detected stack: ${stackNames}`);
}

const introspected = (await Promise.all(
stack.map<Promise<Maybe<ProjectData>>>((introspector) =>
Expand Down

0 comments on commit e3035f1

Please sign in to comment.