Skip to content

Commit

Permalink
Merge pull request #8 from kwameopareasiedu/do-update-refactor
Browse files Browse the repository at this point in the history
Do update refactor
  • Loading branch information
kwameopareasiedu authored Jun 5, 2024
2 parents fefd874 + 74a82c0 commit ade2523
Show file tree
Hide file tree
Showing 12 changed files with 838 additions and 743 deletions.
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ Owing to the above issues, `docts` CLI has the following objectives:

1. [x] Create a **Typescript** serverless project with a modified file structure
2. [x] Add/Remove functions to/from your serverless project and automatically
update the `project.yml`.
update the `project.yml`.
3. [x] Install dependencies in the project root instead of function roots
4. [x] In the build step, traverse through each function's `import` statements,
building a dependency graph. From this graph, automatically pick out the
function's dependencies and save in the function's `package.json`
building a dependency graph. From this graph, automatically pick out the
function's dependencies and save in the function's `package.json`
5. [x] Build `packages` from `src` and generate `package.json` with correct
dependencies for each package
dependencies for each package

## Installation

Expand Down Expand Up @@ -247,15 +247,23 @@ deployment. Any and all contributions from the community are greatly welcome.

## Change Log

- `1.1.1 (Current)`
- `1.2.0 (Current)`

- Fixed path of default function from ~~`/src/sample/hello/hello.ts`~~
to `/src/sample/hello/index.ts`
- Added `http` object to `DoFunctionArgs`
interface ([Docs](https://docs.digitalocean.com/products/functions/reference/parameters-responses/#parsed-web-events))
- Deprecated `__ow_*` fields in `DoFunctionArgs` interface
- Updated nodejs runtime in `project.yml` template to '18' instead of '
default' (14)

- `1.1.1`

- Fixed path of default function from ~~`/src/sample/hello/hello.ts`~~
to `/src/sample/hello/index.ts`

- `1.1.0`

- Added `--include-dependencies`, `-d` switch to `build` command. This is a
list of dependencies to include in the bundle instead of marking them as
external.
- Added `--include-dependencies`, `-d` switch to `build` command. This is a
list of dependencies to include in the bundle instead of marking them as
external.

E.g. `docts build --include-dependencies dayjs @acme/core`
E.g. `docts build --include-dependencies dayjs @acme/core`
11 changes: 3 additions & 8 deletions bin/build-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ export default async function buildProject(root, includedPackages = []) {
// include the @rollup/plugin-node-resolve
// and @rollup/plugin-commonjs plugins
if (includedPackages.length > 0) {
plugins.push(nodeResolve({
resolveOnly: includedPackages
}), commonJs());
plugins.push(nodeResolve({ resolveOnly: includedPackages }), commonJs());
}
const build = await rollup({
input: fnSrcIndex,
plugins
});
const build = await rollup({ input: fnSrcIndex, plugins });
const { output: buildOutput } = await build.generate({});
let dependencies = {};
for (const chunk of buildOutput) {
Expand Down Expand Up @@ -78,5 +73,5 @@ export default async function buildProject(root, includedPackages = []) {
console.warn(`skipping '${relative(srcDir, fnSrcDir)}' due to missing index.ts!`);
}
}
console.log("\nBuilt 'src' into 'packages' dir. You can deploy with doctl!");
console.log("\nBuilt 'packages' dir successfully! Ready to deploy with doctl!");
}
2 changes: 1 addition & 1 deletion bin/create-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default async function createFunction(root, fnPath) {
name: fnName,
binary: false,
main: "",
runtime: "nodejs:default",
runtime: "nodejs:18",
web: true,
parameters: {},
environment: {},
Expand Down
9 changes: 4 additions & 5 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#!/usr/bin/env node
import { Command, program as docts } from "commander";
import { Command } from "commander";
import inquirer from "inquirer";
import init from "./init.js";
import createFunction from "./create-function.js";
import { polyfillGlobals, scanProject } from "./utils.js";
import removeFunction from "./remove-function.js";
import buildProject from "./build-project.js";
polyfillGlobals();
docts
.name("docts")
.description("Enhances the development experience of DigitalOcean 'doctl serverless' when working with Typescript function projects")
.version("0.1.0");
const docts = new Command("docts");
docts.description("Enhances the development experience of DigitalOcean 'doctl serverless' when working with Typescript function projects");
docts.version("1.2.0");
docts
.command("init")
.description("Initialize a new Typescript doctl function project")
Expand Down
48 changes: 24 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docts-cli",
"version": "1.1.1",
"version": "1.2.0",
"description": "A CLI library which enhances the development experience of DigitalOcean 'doctl serverless' when working with Typescript function projects",
"author": "Kwame Opare Asiedu",
"homepage": "https://github.com/kwameopareasiedu/docts#readme",
Expand All @@ -24,33 +24,33 @@
},
"license": "MIT",
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/ejs": "^3.1.1",
"@types/inquirer": "^9.0.3",
"@types/mocha": "^10.0.0",
"@types/node": "^18.11.17",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"@typescript-eslint/parser": "^5.46.1",
"chai": "^4.3.6",
"eslint": "^8.30.0",
"mocha": "^10.2.0",
"prettier": "2.8.1",
"@types/chai": "^4.3.16",
"@types/ejs": "^3.1.5",
"@types/inquirer": "^9.0.7",
"@types/mocha": "^10.0.6",
"@types/node": "^20.14.1",
"@typescript-eslint/eslint-plugin": "^7.12.0",
"@typescript-eslint/parser": "^7.12.0",
"chai": "^5.1.1",
"eslint": "8.57.0",
"mocha": "^10.4.0",
"prettier": "3.3.0",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.1",
"tslib": "^2.4.0",
"typescript": "^4.8.4"
"ts-node": "^10.9.2",
"tslib": "^2.6.3",
"typescript": "^5.4.5"
},
"dependencies": {
"@rollup/plugin-commonjs": "^23.0.7",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^10.0.1",
"commander": "^9.4.1",
"ejs": "^3.1.8",
"inquirer": "9.1.4",
"rollup": "^3.7.5",
"yaml": "^2.1.3"
"@rollup/plugin-commonjs": "^25.0.8",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"commander": "^12.1.0",
"ejs": "^3.1.10",
"inquirer": "9.2.23",
"rollup": "^4.18.0",
"yaml": "^2.4.3"
},
"engines": {
"node": "^18.7.0"
"node": "^18 || ^20"
}
}
13 changes: 5 additions & 8 deletions src/build-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,12 @@ export default async function buildProject(
// and @rollup/plugin-commonjs plugins
if (includedPackages.length > 0) {
plugins.push(
nodeResolve({
resolveOnly: includedPackages
}),
nodeResolve({ resolveOnly: includedPackages }),
commonJs()
);
}

const build = await rollup({
input: fnSrcIndex,
plugins
});
const build = await rollup({ input: fnSrcIndex, plugins });

const { output: buildOutput } = await build.generate({});

Expand Down Expand Up @@ -110,5 +105,7 @@ export default async function buildProject(
}
}

console.log("\nBuilt 'src' into 'packages' dir. You can deploy with doctl!");
console.log(
"\nBuilt 'packages' dir successfully! Ready to deploy with doctl!"
);
}
2 changes: 1 addition & 1 deletion src/create-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default async function createFunction(root: string, fnPath: string) {
name: fnName,
binary: false,
main: "",
runtime: "nodejs:default",
runtime: "nodejs:18",
web: true,
parameters: {},
environment: {},
Expand Down
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import { Command, program as docts } from "commander";
import { Command } from "commander";
import inquirer from "inquirer";
import init from "./init.js";
import createFunction from "./create-function.js";
Expand All @@ -9,12 +9,11 @@ import buildProject from "./build-project.js";

polyfillGlobals();

docts
.name("docts")
.description(
"Enhances the development experience of DigitalOcean 'doctl serverless' when working with Typescript function projects"
)
.version("0.1.0");
const docts = new Command("docts");
docts.description(
"Enhances the development experience of DigitalOcean 'doctl serverless' when working with Typescript function projects"
);
docts.version("1.2.0");

docts
.command("init")
Expand Down
4 changes: 2 additions & 2 deletions templates/project/project.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ packages:
- name: hello
binary: false
main: ''
runtime: 'nodejs:default'
runtime: 'nodejs:18'
web: true
parameters: {}
environment: {}
annotations: {}
limits: {}
limits: {}
4 changes: 3 additions & 1 deletion templates/project/tsconfig.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
"strict": true,
"skipLibCheck": true,
"typeRoots": ["./types"]
}
},
"include": ["./src", "./types"],
"exclude": ["./node_modules", "packages"]
}
52 changes: 35 additions & 17 deletions templates/project/types/global.d.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
export {};
interface DoFunctionArgs {
/** @deprecated Use http.headers */
__ow_headers?: Record<string, any>;
/** @deprecated Use http.path */
__ow_path?: string;
/** @deprecated Use http.method */
__ow_method?: "get" | "post" | "put" | "patch" | "delete" | "options";
/** @deprecated Get body data from root */
__ow_body?: string | Record<string, any>;
/** @deprecated Get query params from root */
__ow_query?: string;

declare global {
interface DoFunctionArgs {
__ow_headers?: Record<string, any>;
__ow_path?: string;
__ow_method?: "get" | "post" | "put" | "patch" | "delete" | "options";
__ow_body?: string | Record<string, any>;
__ow_query?: string;
http: {
path: string;
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
headers: {
accept: string;
"accept-encoding": string;
"content-type": string;
host: string;
"user-agent": string;
"x-forwarded-for": string;
"x-forwarded-proto": string;
"x-request-id": string;
[k: string]: any;
};
};

[k: string]: any;
}
[k: string]: any;
}

interface DoFunctionReturn {
body?: any;
headers?: Record<string, any>;
}
interface DoFunctionReturn {
body?: any;
statusCode?: number;
headers?: Record<string, any>;
}

type DoFunction = (
args: DoFunctionArgs
) => Promise<DoFunctionReturn | undefined>;
interface DoFunction {
(args: DoFunctionArgs): Promise<DoFunctionReturn | undefined>
}
Loading

0 comments on commit ade2523

Please sign in to comment.