Comparing version 1.0.18 to 1.0.19
{ | ||
"name": "fambda", | ||
"version": "1.0.18", | ||
"version": "1.0.19", | ||
"packageManager": "yarn@4.3.0", | ||
@@ -5,0 +5,0 @@ "scripts": {}, |
150
src/index.ts
@@ -1,12 +0,17 @@ | ||
import * as zlib from "zlib"; | ||
import { existsSync, readFileSync, stat, statSync, writeFileSync } from "fs"; | ||
import { dirname, normalize } from "path"; | ||
import { readFileSync } from "fs"; | ||
import { normalize } from "path"; | ||
import esbuild from "esbuild"; | ||
import fetch, { Response } from "node-fetch"; | ||
import { getFunction } from "./getFunction"; | ||
import { getPrompt } from "./getPrompt"; | ||
import { createHash } from "crypto"; | ||
import { | ||
getDeployInfoPath, | ||
getHumanReadableBytes, | ||
getScriptPath, | ||
getStringHash, | ||
hasExport, | ||
put, | ||
readJSON, | ||
writeJSON, | ||
} from "./utils"; | ||
const host = | ||
"https://sf5yrb4fnvqb3mgdcxvmobwr4q0mhyku.lambda-url.us-east-1.on.aws"; | ||
const host = "https://api.fambda.com"; | ||
@@ -43,4 +48,2 @@ type DeployResponse = { | ||
console.log({ deployInfoPath }); | ||
// If there is no project name, prompt the user for one | ||
@@ -147,109 +150,2 @@ if (!deployInfo.project) { | ||
function readJSON<T = any>(path: string): T | undefined { | ||
try { | ||
return JSON.parse(readFileSync(path, "utf8")); | ||
} catch (error) {} | ||
} | ||
function writeJSON(path: string, data: any): void { | ||
writeFileSync(path, JSON.stringify(data, null, 2)); | ||
} | ||
function getParentDirectory(path: string): string { | ||
const parent = dirname(path); | ||
if (parent === path) return path; | ||
return getParentDirectory(parent); | ||
} | ||
function getParentDirectoryWith( | ||
path: string, | ||
f: (path: string) => boolean | ||
): string | undefined { | ||
const parent = getParentDirectory(path); | ||
if (parent === path) return undefined; | ||
if (f(parent)) return parent; | ||
return getParentDirectoryWith(parent, f); | ||
} | ||
function getScriptPath(path: string): undefined | string { | ||
// If the path is a file and it has a .js extension, return the path | ||
if (existsSync(path) && (path.endsWith(".js") || path.endsWith(".ts"))) { | ||
return path; | ||
} | ||
// If the path is a directory, check if there is an index.ts or index.js file | ||
const jsPath = `${path}/index.js`; | ||
if (existsSync(jsPath)) { | ||
return jsPath; | ||
} | ||
const tsPath = `${path}/index.ts`; | ||
if (existsSync(tsPath)) { | ||
return tsPath; | ||
} | ||
return undefined; | ||
} | ||
function getDeployInfoPath(path: string): string { | ||
path = normalize(path); | ||
if (!statSync(path).isDirectory()) { | ||
path = dirname(path); | ||
} | ||
const deployFileName = ".deploy.json"; | ||
// See if there is a parent directory with a deploy.json file | ||
const parentDeployPath = getParentDirectoryWith(path, (parent) => { | ||
return existsSync(`${parent}/${deployFileName}`); | ||
}); | ||
if (parentDeployPath) { | ||
return `${parentDeployPath}/${deployFileName}`; | ||
} | ||
// See if there is a parent directory with a package.json file | ||
const parentPackagePath = getParentDirectoryWith(path, (parent) => { | ||
return existsSync(`${parent}/package.json`); | ||
}); | ||
if (parentPackagePath) { | ||
return `${parentPackagePath}/${deployFileName}`; | ||
} | ||
return `${path}/${deployFileName}`; | ||
} | ||
async function put(url: string, body: any, gzip = true): Promise<Response> { | ||
// Convert the request body to a JSON string | ||
const jsonString = JSON.stringify(body); | ||
// Send the gzipped request | ||
const response = await fetch(url, { | ||
method: "PUT", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Content-Encoding": gzip ? "gzip" : "identity", | ||
}, | ||
body: gzip ? zlib.gzipSync(jsonString) : jsonString, | ||
}); | ||
return response; | ||
} | ||
/** | ||
* Check if a script has handler named "handler" | ||
*/ | ||
function hasExport(script: string, name: string): boolean { | ||
const f = getFunction(script); | ||
try { | ||
return f[name] !== undefined; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
async function transpile( | ||
@@ -281,21 +177,1 @@ path: string, | ||
} | ||
function getHumanReadableBytes(bytes: number): string { | ||
if (bytes === 0) return "0 B"; | ||
const k = 1024; | ||
const dm = decimals(bytes); | ||
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | ||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]; | ||
} | ||
function decimals(bytes: number): number { | ||
return bytes % 100 >= 10 ? 2 : 0; | ||
} | ||
function getStringHash(str: string): string { | ||
const hash = createHash("sha256"); | ||
hash.update(str); | ||
return hash.digest("hex"); | ||
} |
Sorry, the diff of this file is too big to display
277526
8
6984
1
5