diff --git a/package.json b/package.json index 6707e08..8ff206d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/build.ts b/src/build.ts index 5e10a88..ebbf242 100644 --- a/src/build.ts +++ b/src/build.ts @@ -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, diff --git a/src/build_cli.ts b/src/build_cli.ts index 41daa28..bfee570 100644 --- a/src/build_cli.ts +++ b/src/build_cli.ts @@ -8,7 +8,7 @@ if (typeof globalThis.require === "undefined") { } `; -esbuild.build({ +await esbuild.build({ entryPoints: ["./src/cli.ts"], bundle: true, outfile: "./build/cli.js", diff --git a/src/cli.ts b/src/cli.ts index 920628f..4d5ca87 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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 = [ { @@ -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); diff --git a/src/utils.ts b/src/utils.ts index 13b66e8..0c3fa0e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 { const res = await readdir(path, { withFileTypes: true }); @@ -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)}`;