Skip to content

Commit

Permalink
feat: add build function
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed Mar 4, 2024
1 parent c401d69 commit da92f61
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/zenith.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
- name: Run Dagger Pipelines
run: |
dagger call test --src .
dagger call build --src . --compile --outfile example --entrypoints index.ts
dagger call build --src . --outfile index.js --entrypoints index.ts
working-directory: example
env:
DAGGER_CLOUD_TOKEN: ${{ secrets.DAGGER_CLOUD_TOKEN }}
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ dagger install github.com/fluent-ci-templates/bun-pipeline@main
Call a function from the module:

```bash
dagger call build --src . \
--compile \
--outfile example \
--entrypoints index.ts

dagger call test --src .

dagger call run --command build --src .
```

Expand All @@ -63,23 +69,40 @@ dagger call run --command build --src .

## ✨ Jobs

| Job | Description |
| ------ | ------------------- |
| run | Run a command |
| test | Run the tests |
| Job | Description |
| ------ | -------------------------------------- |
| build | Transpile and bundle one or more files |
| run | Run a command |
| test | Run the tests |

```typescript
test(
build(
src: string | Directory | undefined = ".",
bunVersion: string = "latest"
): Promise<string>
entrypoints: string[] = ["index.ts"],
outfile?: string,
bunVersion: string = "latest",
target?: string,
compile: boolean = false,
outdir?: string,
sourcemap?: string,
minify: boolean = false,
minifySyntax: boolean = false,
minifyWhitespace: boolean = false,
minifyIdentifiers: boolean = false,
splitting: boolean = false
): Promise<Directory | string>

run(
command: string,
src: string | Directory | undefined = ".",
bunVersion: string = "latest"
): Promise<string>

test(
src: string | Directory | undefined = ".",
bunVersion: string = "latest"
): Promise<string>

```

## 👨‍💻 Programmatic usage
Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fluentci/bun",
"version": "0.6.12",
"version": "0.6.13",
"exports": "./mod.ts",
"importMap": "import_map.json",
"tasks": {
Expand Down Expand Up @@ -29,4 +29,4 @@
"gen/"
]
}
}
}
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { assertEquals } from "jsr:@std/testing@0.218.2/asserts";

export type { DirectoryID, SecretID } from "./sdk/client.gen.ts";
export { Directory, Secret, dag } from "./sdk/client.gen.ts";
export { File, Directory, Secret, dag } from "./sdk/client.gen.ts";
export { brightGreen } from "jsr:@std/fmt@0.218.2/colors";
export { stringifyTree } from "npm:stringify-tree@1.1.1";
import { gql } from "npm:graphql-request@6.1.0";
Expand Down
5 changes: 4 additions & 1 deletion example/.fluentci/deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { assertEquals } from "jsr:@std/testing@0.218.2/asserts";

export type { DirectoryID, SecretID } from "./sdk/client.gen.ts";
export { Directory, Secret, dag } from "./sdk/client.gen.ts";
export { File, Directory, Secret, dag } from "./sdk/client.gen.ts";
export { brightGreen } from "jsr:@std/fmt@0.218.2/colors";
export { stringifyTree } from "npm:stringify-tree@1.1.1";
import { gql } from "npm:graphql-request@6.1.0";
Expand All @@ -14,6 +14,9 @@ const snakeCase = _.default.snakeCase;
const camelCase = _.default.camelCase;
export { snakeCase, camelCase };

import * as env from "jsr:@tsirysndr/env-js@0.1.2";
export { env };

export { ClientError, GraphQLClient } from "npm:graphql-request@6.1.0";
export {
DaggerSDKError,
Expand Down
4 changes: 2 additions & 2 deletions example/.fluentci/src/dagger/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pipeline from "./pipeline.ts";
import { test, run, jobDescriptions } from "./jobs.ts";
import { build, test, run, jobDescriptions } from "./jobs.ts";

export { pipeline, test, run, jobDescriptions };
export { pipeline, build, test, run, jobDescriptions };
123 changes: 114 additions & 9 deletions example/.fluentci/src/dagger/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
* @description Provides a set of functions for Bun projects
*/

import { Directory, dag } from "../../deps.ts";
import { Directory, dag, env } from "../../deps.ts";
import { getDirectory } from "./lib.ts";

export enum Job {
test = "test",
build = "build",
run = "run",
}

const NODE_VERSION = Deno.env.get("NODE_VERSION") || "18.16.1";
const NODE_VERSION = env.get("NODE_VERSION") || "18.16.1";

export const exclude = [".git", ".devbox", "node_modules", ".fluentci"];

/**
* Run tests
*
* @function
* @description Run tests
* @param {string | Directory | undefined} src
Expand All @@ -24,9 +27,9 @@ export const exclude = [".git", ".devbox", "node_modules", ".fluentci"];
*/
export async function test(
src: string | Directory | undefined = ".",
bunVersion = "latest"
bunVersion: string = "latest"
): Promise<string> {
const BUN_VERSION = Deno.env.get("BUN_VERSION") || bunVersion;
const BUN_VERSION = env.get("BUN_VERSION") || bunVersion;
const context = await getDirectory(src);
const ctr = dag
.pipeline(Job.test)
Expand All @@ -42,10 +45,12 @@ export async function test(

const stdout = await ctr.stdout();
const stderr = await ctr.stderr();
return stdout + '\n' + stderr;
return stdout + "\n" + stderr;
}

/**
* Run commands
*
* @function
* @description Run commands
* @param {string} command
Expand All @@ -56,9 +61,9 @@ export async function test(
export async function run(
command: string,
src: string | Directory | undefined = ".",
bunVersion = "latest"
bunVersion: string = "latest"
): Promise<string> {
const BUN_VERSION = Deno.env.get("BUN_VERSION") || bunVersion;
const BUN_VERSION = env.get("BUN_VERSION") || bunVersion;
const context = await getDirectory(src);
let ctr = dag
.pipeline(Job.run)
Expand All @@ -85,19 +90,119 @@ export async function run(
await ctr.directory("/app/build").export("./build");
}

return ctr.stdout();
const stdout = await ctr.stdout();
const stderr = await ctr.stderr();
return stdout + "\n" + stderr;
}

/**
* Transpile and bundle one or more files
*
* @function
* @description Transpile and bundle one or more files
*/
export async function build(
src: string | Directory | undefined = ".",
entrypoints: string[] = ["index.ts"],
outfile?: string,
bunVersion: string = "latest",
target?: string,
compile: boolean = false,
outdir?: string,
sourcemap?: string,
minify: boolean = false,
minifySyntax: boolean = false,
minifyWhitespace: boolean = false,
minifyIdentifiers: boolean = false,
splitting: boolean = false
): Promise<Directory | string> {
const args: string[] = [];
if (compile) {
args.push("--compile");
}
if (target) {
args.push("--target", target);
}
if (outdir) {
args.push("--outdir", outdir);
}
if (outfile) {
args.push("--outfile", outfile);
}
if (sourcemap) {
args.push("--sourcemap", sourcemap);
}
if (minify) {
args.push("--minify");
}
if (minifySyntax) {
args.push("--minify-syntax");
}
if (minifyWhitespace) {
args.push("--minify-whitespace");
}
if (minifyIdentifiers) {
args.push("--minify-identifiers");
}
if (splitting) {
args.push("--splitting");
}

const BUN_VERSION = env.get("BUN_VERSION") || bunVersion;
const context = await getDirectory(src);
const ctr = dag
.pipeline(Job.build)
.container()
.from("pkgxdev/pkgx:latest")
.withExec(["apt-get", "update"])
.withExec(["apt-get", "install", "-y", "ca-certificates"])
.withExec(["pkgx", "install", `node@${NODE_VERSION}`, `bun@${BUN_VERSION}`])
.withDirectory("/app", context, { exclude })
.withWorkdir("/app")
.withExec(["bun", "install"])
.withExec(["bun", "build", ...entrypoints, ...args])
.withExec(["mkdir", "-p", "/app/dist"])
.withExec([
"sh",
"-c",
`[ -f ${outfile} ] && cp ${outfile} /app/dist ; exit 0`,
]);

await ctr.stdout();

await ctr
.directory(`/app/${outdir || "dist"}`)
.export(`./${outdir || "dist"}`);
return ctr.directory(`/app/${outdir || "dist"}`).id();
}

export type JobExec =
| ((src?: string, bunVersion?: string) => Promise<string>)
| ((command: string, src?: string, bunVersion?: string) => Promise<string>);
| ((command: string, src?: string, bunVersion?: string) => Promise<string>)
| ((
src: string | Directory | undefined,
entrypoints: string[],
outfile?: string,
bunVersion?: string,
target?: string,
compile?: boolean,
outdir?: string,
sourcemap?: string,
minify?: boolean,
minifySyntax?: boolean,
minifyWhitespace?: boolean,
minifyIdentifiers?: boolean,
splitting?: boolean
) => Promise<Directory | string>);

export const runnableJobs: Record<Job, JobExec> = {
[Job.test]: test,
[Job.build]: build,
[Job.run]: run,
};

export const jobDescriptions: Record<Job, string> = {
[Job.test]: "Run tests",
[Job.build]: "Transpile and bundle one or more files",
[Job.run]: "Run a command",
};
4 changes: 2 additions & 2 deletions src/dagger/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pipeline from "./pipeline.ts";
import { test, run, jobDescriptions } from "./jobs.ts";
import { build, test, run, jobDescriptions } from "./jobs.ts";

export { pipeline, test, run, jobDescriptions };
export { pipeline, build, test, run, jobDescriptions };
Loading

0 comments on commit da92f61

Please sign in to comment.