Skip to content

Commit 35b45ac

Browse files
committed
fix: build api
1 parent 6725e72 commit 35b45ac

File tree

18 files changed

+810
-116
lines changed

18 files changed

+810
-116
lines changed

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@types/fs-extra": "^9.0.13",
3434
"@types/node": "^18.11.17",
3535
"@types/yargs": "^17.0.17",
36+
"ava": "^5.2.0",
3637
"rimraf": "^3.0.2",
3738
"rollup": "^3.7.5",
3839
"rollup-plugin-auto-external": "^2.0.0",
@@ -47,7 +48,8 @@
4748
"start": "yarn run cli",
4849
"cli": "ts-node --esm --experimental-specifier-resolution=node src/index.ts",
4950
"build": "rimraf pkg && rollup --config rollup.config.mjs",
50-
"release": "yarn build && semantic-release"
51+
"release": "yarn build && semantic-release",
52+
"test": "ava"
5153
},
5254
"release": {
5355
"branches": "main",
@@ -65,5 +67,14 @@
6567
},
6668
"engines": {
6769
"node": ">=16"
70+
},
71+
"ava": {
72+
"extensions": {
73+
"ts": "module"
74+
},
75+
"nodeArguments": [
76+
"--loader=ts-node/esm",
77+
"--experimental-specifier-resolution=node"
78+
]
6879
}
6980
}

src/api/FaableApi.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ function handleError(message?: string) {
2121
const method = descriptor.value;
2222
descriptor.value = async function (...args: any) {
2323
try {
24-
const res = await method.bind(this).apply(target, args);
25-
return res.data;
24+
return await method.bind(this).apply(target, args);
2625
} catch (error) {
2726
const e: AxiosError<{ message: string }> = error;
2827
if (e.isAxiosError) {
2928
const res = e.response;
3029
if (res) {
31-
throw new Error(`API Error ${res.status}: ${res?.data.message}`);
30+
throw new Error(
31+
`FaableApi ${e.config.url} ${res.status}: ${res?.data.message}`
32+
);
3233
} else {
33-
throw new Error(`API Error:${e.message}`);
34+
throw new Error(`FaableApi ${e.message}`);
3435
}
3536
}
3637
throw error;

src/api/context.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { FaableApi } from "./FaableApi";
2-
import { authenticateOAuthApp } from "./authenticateOAuthApp";
2+
import { apikey } from "./strategies/apikey";
3+
import { authenticateOAuthApp } from "./strategies/authenticateOAuthApp";
4+
35
export const context = async () => {
4-
const authStrategy = authenticateOAuthApp;
56
const { config } = await import("./userdir_config");
67
return {
7-
api: FaableApi.create({ authStrategy, auth: config }),
8+
api: FaableApi.create({ authStrategy: apikey, auth: config }),
89
};
910
};

src/api/strategies/apikey.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import axios from "axios";
2+
import { AuthStrategyBuilder } from "../types";
3+
4+
interface ApikeyConfig {
5+
apikey: string;
6+
}
7+
8+
export const apikey: AuthStrategyBuilder<Partial<ApikeyConfig>> = (config) => {
9+
const { apikey } = config;
10+
if (!apikey) {
11+
throw new Error("Missing apikey.");
12+
}
13+
return {
14+
headers: async () => {
15+
return {
16+
Authorization: `Basic ${Buffer.from(`${apikey}:`).toString("base64")}`,
17+
};
18+
},
19+
};
20+
};

src/api/authenticateOAuthApp.ts renamed to src/api/strategies/authenticateOAuthApp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "axios";
2-
import { AuthStrategyBuilder } from "./types";
2+
import { AuthStrategyBuilder } from "../types";
33

44
interface ClientCredentials {
55
clientId: string;

src/api/userdir_config.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const faable_home = path.join(os.homedir(), ".faable");
66
export const config: {
77
clientId?: string;
88
clientSecret?: string;
9+
apikey?: string;
910
} = {};
1011

1112
const credentials_path = path.join(faable_home, "credentials");
@@ -15,9 +16,9 @@ if (fs.existsSync(credentials_path)) {
1516
// console.log(creds);
1617
config.clientId = creds.clientId;
1718
config.clientSecret = creds.clientSecret;
19+
config.apikey = creds.apikey;
1820
}
1921

20-
if (process.env.FAABLE_CLIENT_ID && process.env.FAABLE_CLIENT_SECRET) {
21-
config.clientId = process.env.FAABLE_CLIENT_ID;
22-
config.clientSecret = process.env.FAABLE_CLIENT_SECRET;
23-
}
22+
config.clientId = process.env.FAABLE_CLIENT_ID || config.clientId;
23+
config.clientSecret = process.env.FAABLE_CLIENT_SECRET || config.clientSecret;
24+
config.apikey = process.env.FAABLE_APIKEY || config.apikey;

src/commands/configure/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@ export const configure: CommandModule<{}, Options> = {
3737
const response = await prompts([
3838
{
3939
type: "text",
40-
name: "clientId",
41-
message: "What is your Faable clientId?",
42-
},
43-
{
44-
type: "text",
45-
name: "clientSecret",
46-
message: "What is your Faable clientSecret?",
40+
name: "apikey",
41+
message: "What is your Faable ApiKey?",
4742
},
4843
]);
4944
await fs.ensureDir(faable_home);

src/commands/deploy/analyze_package.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import { PackageJson } from "type-fest";
55

66
interface AnalyzePackage {
77
workdir: string;
8+
build_script?: string;
89
}
910

10-
export const analyze_package = async ({ workdir }: AnalyzePackage) => {
11+
export const analyze_package = async (params: AnalyzePackage) => {
12+
const workdir = params.workdir;
13+
1114
const package_file = path.join(path.resolve(workdir), "package.json");
1215
log.info(`Loading config from package.json...`);
1316
const pkg: PackageJson = await fs.readJSON(package_file);
@@ -18,15 +21,13 @@ export const analyze_package = async ({ workdir }: AnalyzePackage) => {
1821
}
1922

2023
// Check if build is required to run
21-
let hasBuild = false;
22-
if (pkg?.scripts?.build) {
23-
log.info(`Build step: ${pkg?.scripts?.build}`);
24-
hasBuild = true;
25-
} else {
24+
const build_script = params.build_script || "build";
25+
let build = pkg?.scripts[build_script];
26+
if (!build) {
2627
log.info(`No build script found`);
2728
}
2829

2930
return {
30-
hasBuild,
31+
build_script,
3132
};
3233
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { FaableApp } from "../../api/FaableApi";
2+
import { log } from "../../log";
3+
import { cmd } from "./cmd";
4+
5+
interface BuildProjectArgs {
6+
/**App we are building */
7+
app: FaableApp;
8+
/**build script*/
9+
build_script?: string;
10+
cwd?: string;
11+
}
12+
13+
export const build_project = async (args: BuildProjectArgs) => {
14+
const cwd = args?.cwd || process.cwd();
15+
const build_script = args?.build_script || "build";
16+
const app = args.app;
17+
log.info(`⚡️ Running build script [${build_script}]...`);
18+
const timeout = 1000 * 60 * 10; // 10 minute timeout
19+
await cmd("yarn", ["run", build_script], {
20+
timeout,
21+
cwd,
22+
enableOutput: true,
23+
});
24+
};
Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs from "fs-extra";
44
import Handlebars from "handlebars";
55
import * as path from "path";
66
import { fileURLToPath } from "url";
7+
import { FaableApp } from "../../api/FaableApi";
78

89
const __filename = fileURLToPath(import.meta.url);
910
const __dirname = path.dirname(__filename);
@@ -27,28 +28,37 @@ const docker_template = Handlebars.compile(dockerfile);
2728
const entrypoint_template = Handlebars.compile(entrypoint);
2829

2930
interface BuildConfig {
30-
app_name: string;
31+
app: FaableApp;
3132
workdir: string;
3233
template_context: {
3334
from: string;
34-
build_script: string;
3535
start_script: string;
3636
};
3737
}
3838

39-
export const build_docker = async (props: BuildConfig) => {
40-
const { app_name, workdir, template_context } = props;
39+
export const bundle_docker = async (props: BuildConfig) => {
40+
const { app, workdir, template_context } = props;
4141

4242
const entrypoint_custom = entrypoint_template(template_context);
4343
const dockerfile = docker_template({
4444
...template_context,
4545
entry_script: entrypoint_custom,
4646
});
47-
// console.log(dockerfile);
4847

49-
await cmd(
50-
"/bin/bash",
51-
["-c", `docker build -t app ${workdir} -f-<<EOF\n${dockerfile}\nEOF`],
52-
{ enableOutput: true }
53-
);
48+
log.info(`📦 Packaging inside a docker image`);
49+
const tagname = app.id;
50+
51+
const timeout = 10 * 60 * 1000; // 10 minute timeout
52+
const command = [
53+
"-c",
54+
`docker build -t ${tagname} ${workdir} -f -<<EOF\n${dockerfile}\nEOF`,
55+
];
56+
console.log(command.join(" "));
57+
await cmd("/bin/bash", command, { timeout, enableOutput: true });
58+
59+
log.info(`⚙️ Image ready [tag:${tagname}]`);
60+
61+
return {
62+
tagname,
63+
};
5464
};

0 commit comments

Comments
 (0)