Skip to content

Commit

Permalink
Merge pull request #1 from atmelmicro/david/create-cli
Browse files Browse the repository at this point in the history
Create CLI
  • Loading branch information
atmelmicro authored Feb 23, 2024
2 parents cc2d927 + b5dfc7b commit 84ecc1d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "@atmelmicro/lack",
"version": "0.1.3",
"version": "0.1.4",
"dependencies": {
"@hono/node-server": "^1.8.0",
"aws-cdk-lib": "^2.127.0",
Expand Down
3 changes: 2 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export async function generateLambda() {
export async function build() {
const entries = await getAllEntryPoint("./app");
// clear out folder
await rm("./out/", { recursive: true }).catch();
await rm("./out/", { recursive: true }).catch(() => {});
await mkdir("./out");

await esbuild.build({
entryPoints: entries,
Expand Down
2 changes: 1 addition & 1 deletion src/build_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (typeof globalThis.require === "undefined") {
}
`;

esbuild.build({
await esbuild.build({
entryPoints: ["./src/cli.ts"],
bundle: true,
outfile: "./build/cli.js",
Expand Down
55 changes: 55 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { bundleAll } from "./build";
import { deploy as cdkDeploy } from "./deploy";
import { argv } from "node:process";
import { startDev } from "./dev";
import { spawn } from "node:child_process";
import { writeFile, mkdir } from "node:fs/promises";

const commands = [
{
Expand All @@ -23,8 +25,61 @@ const commands = [
props: [],
fn: startDev,
},
{
title: "create",
props: [],
fn: newApp,
},
];

const sampleConfig = `export const name = "example-app"
export const llrt = "./llrt.zip"
`;

const sampleGitignore = `./llrt.zip
out/
node_modules/`;

async function newApp() {
const log = (text: string) => {
console.log(color(" Create ", "Gray"), text);
};

log("creating new lack app");

log("downloading llrt");
const repoRes = await fetch(
"https://api.github.com/repos/awslabs/llrt/releases/latest"
);
const data: {
assets: {
browser_download_url: string;
name: string;
}[];
} = await repoRes.json();

const correctVersion = data.assets.find(
(x) => x.name === "llrt-lambda-arm64.zip"
);
if (!correctVersion) {
error(
"Couldn't download the correct LLRT version, please set up Lack manually"
);
return;
}

const llrtBin = await fetch(correctVersion.browser_download_url);
const blob = await llrtBin.blob();
await writeFile("./llrt.zip", Buffer.from(await blob.arrayBuffer()));
log("downloaded llrt");
log("generating config and gitignore");
await writeFile(".gitignore", sampleGitignore);
await writeFile("lack.config.js", sampleConfig);

await mkdir("./app");
log("done");
}

async function deploy() {
const log = (text: string) => {
console.log(color(" Deploy ", "Green"), text);
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readdir } from "fs/promises";
import { extname } from "path";
import { extname, resolve } from "path";

export async function getAllEntryPoint(path: string): Promise<string[]> {
const res = await readdir(path, { withFileTypes: true });
Expand Down Expand Up @@ -83,7 +83,7 @@ export async function getAllRoutes() {
return (
await Promise.all(
entries.map((file) =>
import(file).then((functions) =>
import("file://" + resolve(file)).then((functions) =>
Object.keys(functions).map((functionName) => {
const path = file.slice(base.length, -extname(file).length);
const fullPath = `${path}/${removeMethod(functionName)}`;
Expand Down

0 comments on commit 84ecc1d

Please sign in to comment.