Skip to content

Commit 6a56cef

Browse files
authored
Refactor to TypeScript (#5)
* Refactor to TypeScript * Don't cast to `any` * Move other files to `src` dir * Check for `handler` function before running `build` * Pin to `execa@1` for Node 8 compat * Change output files to `.now` dir * Fix test fixture
1 parent 8adf25d commit 6a56cef

File tree

12 files changed

+2589
-151
lines changed

12 files changed

+2589
-151
lines changed

β€Ž.editorconfigβ€Ž

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
tab_width = 4
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[{*.json,*.json.example,*.gyp,*.yml,*.yaml,*.workflow}]
13+
indent_style = space
14+
indent_size = 2
15+
16+
[{*.py,*.asm}]
17+
indent_style = space
18+
19+
[*.py]
20+
indent_size = 4
21+
22+
[*.asm]
23+
indent_size = 8
24+
25+
[*.md]
26+
trim_trailing_whitespace = false
27+
28+
# Ideal settings - some plugins might support these.
29+
[*.js]
30+
quote_type = single
31+
32+
[{*.c,*.cc,*.h,*.hh,*.cpp,*.hpp,*.m,*.mm,*.mpp,*.js,*.java,*.go,*.rs,*.php,*.ng,*.jsx,*.ts,*.d,*.cs,*.swift}]
33+
curly_bracket_next_line = false
34+
spaces_around_operators = true
35+
spaces_around_brackets = outside
36+
# close enough to 1TB
37+
indent_brace_style = K&R

β€Ž.gitignoreβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/node_modules
2+
/dist

β€Žindex.jsβ€Ž

Lines changed: 0 additions & 81 deletions
This file was deleted.

β€Žpackage.jsonβ€Ž

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "now-bash",
33
"version": "2.0.0",
44
"description": "Now v2 Builder for Bash Serverless Functions",
5-
"main": "index.js",
5+
"main": "dist/index",
6+
"types": "dist/index",
67
"author": "Nathan Rajlich <[email protected]>",
78
"license": "MIT",
89
"homepage": "https://github.com/importpw/now-builder",
@@ -11,17 +12,34 @@
1112
"url": "https://github.com/importpw/now-builder.git"
1213
},
1314
"files": [
14-
"builder.sh",
15-
"runtime.sh",
16-
"bootstrap",
17-
"index.js",
18-
"package.json"
15+
"dist"
1916
],
2017
"dependencies": {
21-
"execa": "^1.0.0",
22-
"snake-case": "^2.1.0"
18+
"execa": "1",
19+
"snake-case": "3"
20+
},
21+
"devDependencies": {
22+
"@now/build-utils": "^1.3.6",
23+
"@now/frameworks": "^0.0.7",
24+
"@now/routing-utils": "^1.5.1",
25+
"@types/node": "^12.12.17",
26+
"@typescript-eslint/eslint-plugin": "1.6.0",
27+
"@typescript-eslint/parser": "1.1.0",
28+
"cpy-cli": "^2.0.0",
29+
"eslint": "5.16.0",
30+
"eslint-config-airbnb": "17.1.0",
31+
"eslint-config-prettier": "4.1.0",
32+
"eslint-import-resolver-typescript": "1.1.1",
33+
"eslint-plugin-import": "2.16.0",
34+
"eslint-plugin-jsx-a11y": "6.2.1",
35+
"eslint-plugin-react": "7.12.4",
36+
"rimraf": "^3.0.0",
37+
"typescript": "^3.5.3"
2338
},
2439
"scripts": {
25-
"test": "jest"
40+
"prebuild": "rimraf dist",
41+
"build": "tsc",
42+
"postbuild": "cpy src '!**/*.ts' dist",
43+
"prepublishOnly": "npm run build"
2644
}
2745
}
File renamed without changes.

β€Žbuilder.shβ€Ž renamed to β€Žsrc/builder.shβ€Ž

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ echo "Caching imports in \"$ENTRYPOINT\"…"
2727
. "$DIST/$ENTRYPOINT"
2828
echo "Done caching imports"
2929

30-
# Run user build script
31-
if declare -f build > /dev/null; then
32-
echo "Running \`build\` function in \"$ENTRYPOINT\"…"
33-
build "$@"
34-
fi
35-
3630
# Ensure the entrypoint defined a `handler` function
3731
if ! declare -f handler > /dev/null; then
3832
echo "ERROR: A \`handler\` function must be defined in \"$ENTRYPOINT\"!" >&2
3933
exit 1
4034
fi
35+
36+
# Run user build script
37+
if declare -f build > /dev/null; then
38+
echo "Running \`build\` function in \"$ENTRYPOINT\"…"
39+
build "$@"
40+
fi

β€Žsrc/index.tsβ€Ž

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import execa from 'execa';
2+
import { join } from 'path';
3+
import { snakeCase } from 'snake-case';
4+
import {
5+
AnalyzeOptions,
6+
BuildOptions,
7+
Env,
8+
glob,
9+
download,
10+
createLambda,
11+
shouldServe
12+
} from '@now/build-utils';
13+
14+
// From this list: https://import.pw/importpw/import/docs/config.md
15+
const allowedConfigImports = new Set([
16+
'CACHE',
17+
'CURL_OPTS',
18+
'DEBUG',
19+
'RELOAD',
20+
'SERVER'
21+
]);
22+
23+
export const version = 3;
24+
25+
export { shouldServe };
26+
27+
export function analyze({ files, entrypoint }: AnalyzeOptions) {
28+
return files[entrypoint].digest;
29+
}
30+
31+
export async function build({
32+
workPath,
33+
files,
34+
entrypoint,
35+
meta,
36+
config = {}
37+
}: BuildOptions) {
38+
const configEnv: Env = {};
39+
const distPath = join(workPath, '.now', 'dist', entrypoint);
40+
await download(files, workPath, meta);
41+
42+
for (const [key, val] of Object.entries(config)) {
43+
const name = snakeCase(key).toUpperCase();
44+
if (typeof val === 'string' && allowedConfigImports.has(name)) {
45+
configEnv[`IMPORT_${name}`] = val;
46+
}
47+
}
48+
49+
if (config && config.import) {
50+
for (const key of Object.keys(config.import)) {
51+
const name = snakeCase(key).toUpperCase();
52+
configEnv[`IMPORT_${name}`] = config.import[key];
53+
}
54+
}
55+
56+
const IMPORT_CACHE = `${distPath}/.import-cache`;
57+
const env = {
58+
...process.env,
59+
...configEnv,
60+
PATH: `${IMPORT_CACHE}/bin:${process.env.PATH}`,
61+
IMPORT_CACHE,
62+
DIST: distPath,
63+
BUILDER: __dirname,
64+
ENTRYPOINT: entrypoint
65+
};
66+
67+
const builderPath = join(__dirname, 'builder.sh');
68+
69+
await execa(builderPath, [entrypoint], {
70+
env,
71+
cwd: workPath,
72+
stdio: 'inherit'
73+
});
74+
75+
const lambda = await createLambda({
76+
files: await glob('**', distPath),
77+
handler: entrypoint, // not actually used in `bootstrap`
78+
runtime: 'provided',
79+
environment: {
80+
...configEnv,
81+
SCRIPT_FILENAME: entrypoint
82+
}
83+
});
84+
85+
return {
86+
output: lambda
87+
};
88+
}
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.now
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
{
2+
"public": true,
23
"version": 2,
34
"builds": [
4-
{ "src": "index.sh", "use": "@now/bash" },
5-
{ "src": "subdirectory/index.sh", "use": "@now/bash" }
6-
],
7-
"probes": [
8-
{ "path": "/", "mustContain": "cow:RANDOMNESS_PLACEHOLDER" },
9-
{ "path": "/subdirectory/", "mustContain": "yoda:RANDOMNESS_PLACEHOLDER" }
5+
{ "src": "**/*.sh", "use": "now-bash" }
106
]
117
}

0 commit comments

Comments
Β (0)