Skip to content

Commit

Permalink
Merge pull request #146 from sure-thing/types
Browse files Browse the repository at this point in the history
Types
  • Loading branch information
estrattonbailey committed Dec 5, 2021
2 parents a81c37e + a7d0316 commit 8007be4
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-ghosts-smash.md
@@ -0,0 +1,5 @@
---
'presta': minor
---

Update file exports and type generation for easier maintenance
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: pnpm - config
run: pnpm config set store-dir $PNPM_CACHE_FOLDER
- name: pnpm - output bin stubs
run: echo "#! /usr/bin/env node" > ./packages/presta/cli.js
run: mkdir ./packages/presta/dist && echo "#! /usr/bin/env node" > ./packages/presta/dist/cli.js
- name: pnpm - install
run: pnpm install

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: pnpm - config
run: pnpm config set store-dir $PNPM_CACHE_FOLDER
- name: pnpm - output bin stubs
run: echo "#! /usr/bin/env node" > ./packages/presta/cli.js
run: mkdir ./packages/presta/dist && echo "#! /usr/bin/env node" > ./packages/presta/dist/cli.js
- name: pnpm - install
run: pnpm install

Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/package.json
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"@presta/html": "workspace:^0.1.5",
"presta": "workspace:^0.41.0"
"presta": "workspace:^0.41.1"
},
"devDependencies": {
"@tsconfig/node14": "^1.0.1",
Expand Down
16 changes: 8 additions & 8 deletions package.json
Expand Up @@ -9,20 +9,20 @@
"pnpm": ">=5.3.0"
},
"devDependencies": {
"@changesets/changelog-github": "^0.4.1",
"@changesets/cli": "^2.17.0",
"@changesets/changelog-github": "^0.4.2",
"@changesets/cli": "^2.18.1",
"@commitlint/cli": "^12.1.4",
"@commitlint/config-conventional": "^12.1.4",
"@tsconfig/node12": "^1.0.9",
"@types/tap": "^15.0.5",
"esbuild": "^0.12.29",
"esbuild-register": "^2.6.0",
"husky": "^7.0.2",
"is-ci": "^3.0.0",
"prettier": "^2.4.1",
"tap": "^15.0.10",
"ts-node": "^10.2.1",
"typescript": "^4.4.3"
"husky": "^7.0.4",
"is-ci": "^3.0.1",
"prettier": "^2.5.1",
"tap": "^15.1.5",
"ts-node": "^10.4.0",
"typescript": "^4.5.2"
},
"scripts": {
"prepare": "is-ci || pnpx husky install",
Expand Down
2 changes: 1 addition & 1 deletion packages/presta/.gitignore
@@ -1,2 +1,2 @@
node_modules
*.js
dist
9 changes: 0 additions & 9 deletions packages/presta/index.d.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/presta/lib/index.ts
Expand Up @@ -3,7 +3,8 @@
*/
import * as log from './log'

export const logger = log
export { Env } from './constants'
export * from './types'
export const logger = log
export { wrapHandler } from './wrapHandler'
export { getCurrentPrestaInstance } from './currentPrestaInstance'
43 changes: 18 additions & 25 deletions packages/presta/lib/serve.ts
Expand Up @@ -17,17 +17,19 @@ import { createLiveReloadScript } from './liveReloadScript'
import { AWS, Presta } from './types'
import { normalizeResponse } from './normalizeResponse'

function resolveHTML(dir: string, url: string) {
let file = path.join(dir, url)

// if no extension, it's probably intended to be an HTML file
if (!path.extname(url)) {
try {
return fs.readFileSync(path.join(dir, url, 'index.html'), 'utf8')
} catch (e) {}
}
export function getLambda(url: string, manifest: { [route: string]: string }): { handler: AWS['Handler'] } {
const routes = Object.keys(manifest)
const lambdaFilepath = routes
.map((route) => ({
matcher: toRegExp(route),
route,
}))
.filter(({ matcher }) => {
return matcher.pattern.test(url.split('?')[0])
})
.map(({ route }) => manifest[route])[0]

return fs.readFileSync(file, 'utf8')
return lambdaFilepath ? require(lambdaFilepath) : undefined
}

export function createServerHandler({ port, config }: { port: number; config: Presta }) {
Expand Down Expand Up @@ -61,33 +63,24 @@ export function createServerHandler({ port, config }: { port: number; config: Pr
const accept = event.headers.Accept || event.headers.accept
const acceptsJson = accept && accept.includes('json')

/*
* No asset file, no static file, try dynamic
*/
try {
/*
* No asset file, no static file, try dynamic
*/
delete require.cache[config.functionsManifest]
const manifest = require(config.functionsManifest)
const routes = Object.keys(manifest)
const lambdaFilepath = routes
.map((route) => ({
matcher: toRegExp(route),
route,
}))
.filter(({ matcher }) => {
return matcher.pattern.test(url.split('?')[0])
})
.map(({ route }) => manifest[route])[0]
const lambda = getLambda(url, manifest)

/**
* If we have a serverless function, delegate to it, otherwise 404
*/
if (lambdaFilepath) {
if (lambda) {
logger.debug({
label: 'debug',
message: `attempting to render lambda for ${url}`,
})

const { handler }: { handler: AWS['Handler'] } = require(lambdaFilepath)
const { handler }: { handler: AWS['Handler'] } = lambda
let response: AWS['HandlerResponse']

try {
Expand Down
14 changes: 9 additions & 5 deletions packages/presta/package.json
Expand Up @@ -2,13 +2,16 @@
"name": "presta",
"version": "0.41.1",
"description": "Hyper minimal framework for the modern web.",
"main": "index.js",
"types": "index.d.ts",
"main": "./dist/index.js",
"types": "./dist/lib/index.d.ts",
"bin": {
"presta": "cli.js"
"presta": "dist/cli.js"
},
"files": [
"dist"
],
"scripts": {
"build": "node scripts/build",
"build": "node scripts/build && pnpx tsc --emitDeclarationOnly",
"cloc": "pnpx cloc lib/*.ts",
"typecheck": "pnpx tsc --noEmit"
},
Expand Down Expand Up @@ -54,6 +57,7 @@
"@types/sade": "^1.7.3",
"@types/statuses": "^2.0.0",
"proxyquire": "^2.1.3",
"supertest-fetch": "^1.4.3"
"supertest-fetch": "^1.4.3",
"typescript": "^4.5.2"
}
}
2 changes: 1 addition & 1 deletion packages/presta/scripts/build.js
Expand Up @@ -4,7 +4,7 @@ const pkg = require('../package.json')

require('esbuild').buildSync({
entryPoints: ['lib/cli.ts', 'lib/index.ts'],
outdir: path.join(__dirname, '..'),
outdir: path.join(__dirname, '../dist'),
bundle: true,
minify: true,
platform: 'node',
Expand Down
5 changes: 3 additions & 2 deletions packages/presta/tsconfig.json
@@ -1,9 +1,10 @@
{
"extends": "@tsconfig/node12/tsconfig.json",
"include": ["lib/*.ts", "index.d.ts"],
"include": ["lib/index.ts", "lib/cli.ts"],
"exclude": ["node_modules"],
"compilerOptions": {
"declaration": true,
"declarationDir": "dist",
"declarationDir": "./dist",
"resolveJsonModule": true
}
}

0 comments on commit 8007be4

Please sign in to comment.