Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Test Deno Module

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: extractions/setup-just@v1
- uses: actions/checkout@v2
- uses: denolib/setup-deno@v2
- name: Run tests
run: just test
13 changes: 2 additions & 11 deletions apex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,14 @@ const LEVEL =
(Deno.env.get("APEX_LOG")?.toUpperCase() as log.LevelName | undefined) ||
"INFO";

await log.setup({
handlers: {
console: new log.handlers.ConsoleHandler(LEVEL),
},
loggers: {
default: {
level: LEVEL,
handlers: ["console"],
},
},
});
await setupLogger(LEVEL);

import * as generate from "./src/commands/generate.ts";
import * as newCmd from "./src/commands/new.ts";
import * as init from "./src/commands/init.ts";
import * as list from "./src/commands/list.ts";
import * as watch from "./src/commands/watch.ts";
import { setupLogger } from "./src/utils.ts";

if (
Deno.args.length == 1 &&
Expand Down
7 changes: 7 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test:
deno fmt --check src/ test/
deno test --unstable -A test/init.test.ts

install:
deno install -f -A --unstable ./apex.ts

23 changes: 21 additions & 2 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import { Command } from "https://deno.land/x/cliffy@v0.25.5/command/mod.ts";

import { Variables } from "../config.ts";
import { initializeProject } from "../init.ts";
import { initializeProject, TemplateOptions } from "../init.ts";
import { templateCompletion, varOptions } from "./utils.ts";

export const command = new Command()
.complete("template", async () => await templateCompletion())
.arguments("<template:string>")
.option("-v, --var <item:string>", "define a template variable", varOptions)
.option(
"-p, --path <string>",
"specify a relative path to the template root",
{ default: "" },
)
.option(
"-b, --branch <string>",
"checkout branch before processing template",
{ default: "main" },
)
.description("Initialize a project using a template.")
.action(async (options, template: string) => {
const vars = (options || {}).var || ({} as Variables);
await initializeProject(false, ".", template, undefined, vars || {});
await initializeProject(
".",
template,
vars || {},
{
isNew: false,
path: options.path,
branch: options.branch,
},
);
});
24 changes: 22 additions & 2 deletions src/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
import { Command } from "https://deno.land/x/cliffy@v0.25.5/command/mod.ts";

import { Variables } from "../config.ts";
import { initializeProject } from "../init.ts";
import { initializeProject, TemplateOptions } from "../init.ts";
import { templateCompletion, varOptions } from "./utils.ts";

export const command = new Command()
.complete("template", async () => await templateCompletion())
.arguments("<template:string> <dir:string>")
.option("-v, --var <item:string>", "define a template variable", varOptions)
.option(
"-p, --path <string>",
"specify a relative path to the template root",
{ default: "" },
)
.option(
"-b, --branch <string>",
"checkout branch before processing template",
{ default: "main" },
)
.description("Create a new project directory using a template.")
.action(async (options, template: string, dir: string) => {
const vars = (options || {}).var || ({} as Variables);
await initializeProject(true, dir, template, undefined, vars || {});

await initializeProject(
dir,
template,
vars || {},
{
isNew: true,
path: options.path,
branch: options.branch,
},
);
});
2 changes: 1 addition & 1 deletion src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export const varOptions = {
};

export async function templateCompletion(): Promise<string[]> {
return (await templateList()).map((t) => t.name);
return (await templateList()).map((t) => t.name || "");
}
19 changes: 14 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,36 @@ export interface Command {
}

export interface Output {
file: string;
source: string;
path: string;
contents: Uint8Array;
mode?: number;
executable: boolean;
runAfter?: Command[];
}

export interface JsonOutput {
path: string;
contents: number[];
mode?: number;
executable: boolean;
runAfter?: Command[];
}

/// TEMPLATE FILES

export interface Template {
name: string;
name?: string;
description?: string;
variables?: Variable[];
specLocation?: string;
}

export interface Variable {
name: string;
message: string;
message?: string;
description?: string;
type?: "input" | "number" | "confirm";
prompt: string;
prompt?: string;
default?: string | number | boolean;
required: boolean;
loop: boolean;
Expand Down
11 changes: 8 additions & 3 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export async function processConfig(config: Configuration): Promise<Output[]> {
const context = new model.Context(visitorConfig, doc);
context.accept(context, visitor);
output.push({
file,
source: writer.string(),
path: file,
contents: new TextEncoder().encode(writer.string()),
executable: generatorConfig.executable || false,
runAfter: generatorConfig.runAfter,
});
Expand Down Expand Up @@ -88,7 +88,12 @@ if (!Deno.isatty(Deno.stdin.rid) && import.meta.main) {
const content = new TextDecoder().decode(stdinContent);
try {
const config = JSON.parse(content) as Configuration;
console.log(JSON.stringify(await processConfig(config)));
console.log(
JSON.stringify(
await processConfig(config),
(_, v) => v instanceof Uint8Array ? Array.from(v) : v,
),
);
} catch (e) {
console.error(e);
throw e;
Expand Down
Loading