Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
root = true

[*]
indent_style = tab
indent_size = 4
tab_width = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{*.json,*.json.example,*.gyp,*.yml,*.yaml,*.workflow}]
indent_style = space
indent_size = 2

[{*.py,*.asm}]
indent_style = space

[*.py]
indent_size = 4

[*.asm]
indent_size = 8

[*.md]
trim_trailing_whitespace = false

# Ideal settings - some plugins might support these.
[*.js]
quote_type = single

[{*.c,*.cc,*.h,*.hh,*.cpp,*.hpp,*.m,*.mm,*.mpp,*.js,*.java,*.go,*.rs,*.php,*.ng,*.jsx,*.ts,*.d,*.cs,*.swift}]
curly_bracket_next_line = false
spaces_around_operators = true
spaces_around_brackets = outside
# close enough to 1TB
indent_brace_style = K&R
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
/dist
81 changes: 0 additions & 81 deletions index.js

This file was deleted.

36 changes: 27 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "now-bash",
"version": "2.0.0",
"description": "Now v2 Builder for Bash Serverless Functions",
"main": "index.js",
"main": "dist/index",
"types": "dist/index",
"author": "Nathan Rajlich <nate@zeit.co>",
"license": "MIT",
"homepage": "https://github.com/importpw/now-builder",
Expand All @@ -11,17 +12,34 @@
"url": "https://github.com/importpw/now-builder.git"
},
"files": [
"builder.sh",
"runtime.sh",
"bootstrap",
"index.js",
"package.json"
"dist"
],
"dependencies": {
"execa": "^1.0.0",
"snake-case": "^2.1.0"
"execa": "1",
"snake-case": "3"
},
"devDependencies": {
"@now/build-utils": "^1.3.6",
"@now/frameworks": "^0.0.7",
"@now/routing-utils": "^1.5.1",
"@types/node": "^12.12.17",
"@typescript-eslint/eslint-plugin": "1.6.0",
"@typescript-eslint/parser": "1.1.0",
"cpy-cli": "^2.0.0",
"eslint": "5.16.0",
"eslint-config-airbnb": "17.1.0",
"eslint-config-prettier": "4.1.0",
"eslint-import-resolver-typescript": "1.1.1",
"eslint-plugin-import": "2.16.0",
"eslint-plugin-jsx-a11y": "6.2.1",
"eslint-plugin-react": "7.12.4",
"rimraf": "^3.0.0",
"typescript": "^3.5.3"
},
"scripts": {
"test": "jest"
"prebuild": "rimraf dist",
"build": "tsc",
"postbuild": "cpy src '!**/*.ts' dist",
"prepublishOnly": "npm run build"
}
}
File renamed without changes.
12 changes: 6 additions & 6 deletions builder.sh → src/builder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ echo "Caching imports in \"$ENTRYPOINT\"…"
. "$DIST/$ENTRYPOINT"
echo "Done caching imports"

# Run user build script
if declare -f build > /dev/null; then
echo "Running \`build\` function in \"$ENTRYPOINT\"…"
build "$@"
fi

# Ensure the entrypoint defined a `handler` function
if ! declare -f handler > /dev/null; then
echo "ERROR: A \`handler\` function must be defined in \"$ENTRYPOINT\"!" >&2
exit 1
fi

# Run user build script
if declare -f build > /dev/null; then
echo "Running \`build\` function in \"$ENTRYPOINT\"…"
build "$@"
fi
88 changes: 88 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import execa from 'execa';
import { join } from 'path';
import { snakeCase } from 'snake-case';
import {
AnalyzeOptions,
BuildOptions,
Env,
glob,
download,
createLambda,
shouldServe
} from '@now/build-utils';

// From this list: https://import.pw/importpw/import/docs/config.md
const allowedConfigImports = new Set([
'CACHE',
'CURL_OPTS',
'DEBUG',
'RELOAD',
'SERVER'
]);

export const version = 3;

export { shouldServe };

export function analyze({ files, entrypoint }: AnalyzeOptions) {
return files[entrypoint].digest;
}

export async function build({
workPath,
files,
entrypoint,
meta,
config = {}
}: BuildOptions) {
const configEnv: Env = {};
const distPath = join(workPath, '.now', 'dist', entrypoint);
await download(files, workPath, meta);

for (const [key, val] of Object.entries(config)) {
const name = snakeCase(key).toUpperCase();
if (typeof val === 'string' && allowedConfigImports.has(name)) {
configEnv[`IMPORT_${name}`] = val;
}
}

if (config && config.import) {
for (const key of Object.keys(config.import)) {
const name = snakeCase(key).toUpperCase();
configEnv[`IMPORT_${name}`] = config.import[key];
}
}

const IMPORT_CACHE = `${distPath}/.import-cache`;
const env = {
...process.env,
...configEnv,
PATH: `${IMPORT_CACHE}/bin:${process.env.PATH}`,
IMPORT_CACHE,
DIST: distPath,
BUILDER: __dirname,
ENTRYPOINT: entrypoint
};

const builderPath = join(__dirname, 'builder.sh');

await execa(builderPath, [entrypoint], {
env,
cwd: workPath,
stdio: 'inherit'
});

const lambda = await createLambda({
files: await glob('**', distPath),
handler: entrypoint, // not actually used in `bootstrap`
runtime: 'provided',
environment: {
...configEnv,
SCRIPT_FILENAME: entrypoint
}
});

return {
output: lambda
};
}
File renamed without changes.
1 change: 1 addition & 0 deletions test/fixtures/01-cowsay/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.now
8 changes: 2 additions & 6 deletions test/fixtures/01-cowsay/now.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"public": true,
"version": 2,
"builds": [
{ "src": "index.sh", "use": "@now/bash" },
{ "src": "subdirectory/index.sh", "use": "@now/bash" }
],
"probes": [
{ "path": "/", "mustContain": "cow:RANDOMNESS_PLACEHOLDER" },
{ "path": "/subdirectory/", "mustContain": "yoda:RANDOMNESS_PLACEHOLDER" }
{ "src": "**/*.sh", "use": "now-bash" }
]
}
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"strict": true,
"module": "CommonJS",
"target": "es2015",
"esModuleInterop": true,
"lib": ["esnext"],
"outDir": "dist",
"sourceMap": true,
"declaration": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
Loading