Skip to content

Commit

Permalink
feat(core): finalize core package
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Aug 19, 2022
1 parent 37b4e95 commit a832d13
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 34 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"build": "cross-env NODE_ENV=production lerna run build",
"format": "prettier --write .",
"lint": "prettier --check . && eslint .",
"test": "jest",
"test": "NODE_OPTIONS=--experimental-vm-modules jest && lerna run test",
"release": "npm run build && lerna publish --conventional-commits"
},
"engines": {
Expand Down
6 changes: 1 addition & 5 deletions packages/core/.swcrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"env": {
"targets": {
"node": "16"
}
},
"jsc": {
"target": "es2021",
"parser": {
"syntax": "typescript"
}
Expand Down
26 changes: 26 additions & 0 deletions packages/core/integration/run-upload.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { join } = require("node:path");

/**
* Run upload command.
* @param {typeof import('../src/index').upload} implementation
* @param {string} name
*/
const runUpload = async (implementation, name) => {
try {
const result = await implementation({
cwd: join(__dirname, "../__fixtures__/screenshots"),
token: process.env.ARGOS_TOKEN,
name,
});
console.log(result);
} catch (err) {
if (err.response) {
console.error("Status: %s", err.response.status);
if (err.response?.data?.message) {
console.error(err.response?.data?.message);
}
}
process.exit(1);
}
};
exports.runUpload = runUpload;
4 changes: 4 additions & 0 deletions packages/core/integration/upload.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { upload } = require("@argos-ci/core");
const { runUpload } = require("./run-upload.cjs");

runUpload(upload, "@argos/core--cjs");
4 changes: 4 additions & 0 deletions packages/core/integration/upload.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { upload } from "@argos-ci/core";
import { runUpload } from "./run-upload.cjs";

runUpload(upload, "@argos/core--mjs");
1 change: 1 addition & 0 deletions packages/core/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
transform: {
"^.+\\.(t|j)sx?$": ["@swc/jest"],
},
extensionsToTreatAsEsm: [".ts"],
};
8 changes: 6 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"version": "0.0.0",
"scripts": {
"prebuild": "rm -rf dist",
"build": "rollup -c"
"build": "rollup -c",
"test": "node ./integrations/upload.cjs && node ./integrations/upload.mjs"
},
"type": "module",
"main": "./dist/index.cjs",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
"default": "./dist/index.cjs"
},
"./package.json": "./package.json"
},
Expand Down Expand Up @@ -40,6 +43,7 @@
"dependencies": {
"axios": "^0.27.2",
"convict": "^6.2.3",
"debug": "^4.3.4",
"env-ci": "^7.3.0",
"fast-glob": "^3.2.11",
"sharp": "^0.30.7",
Expand Down
14 changes: 12 additions & 2 deletions packages/core/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import dts from "rollup-plugin-dts";
import swc from "rollup-plugin-swc3";

const bundle = (config) => ({
...config,
input: "src/index.ts",
external: (id) => !/^[./]/.test(id),
external: (id) => {
return id === "./index.mjs" || !/^[./]/.test(id);
},
...config,
});

export default [
Expand All @@ -15,6 +17,14 @@ export default [
},
plugins: [swc()],
}),
bundle({
input: "src/index.cjs.ts",
output: {
file: `dist/index.cjs`,
format: "es",
},
plugins: [swc()],
}),
bundle({
plugins: [dts()],
output: {
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ export const createArgosApiClient = (
});
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.log(error.response?.data);
}
throw error;
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const mustBeArgosToken = (value: any) => {
const schema = {
apiBaseUrl: {
env: "ARGOS_API_BASE_URL",
default: "https://api.argos-ci.com",
default: "https://api.argos-ci.com/v2/",
format: mustBeApiBaseUrl,
},
commit: {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import createDebug from "debug";

export const debug = createDebug("@argos-ci/core");
24 changes: 9 additions & 15 deletions packages/core/src/discovery.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
import path from "path";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { discoverScreenshots } from "./discovery";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

describe("#discoverScreenshots", () => {
it("finds all images", async () => {
const screenshots = await discoverScreenshots(["**/*.{png,jpg,jpeg}"], {
cwd: path.join(__dirname, "../__fixtures__/screenshots"),
cwd: join(__dirname, "../__fixtures__/screenshots"),
});
expect(screenshots).toEqual([
{
name: "penelope.jpg",
path: path.resolve(
__dirname,
"../__fixtures__/screenshots/penelope.jpg"
),
path: join(__dirname, "../__fixtures__/screenshots/penelope.jpg"),
},
{
name: "nested/alicia.jpg",
path: path.resolve(
__dirname,
"../__fixtures__/screenshots/nested/alicia.jpg"
),
path: join(__dirname, "../__fixtures__/screenshots/nested/alicia.jpg"),
},
]);
});

it("ignores files using `ignore` option", async () => {
const screenshots = await discoverScreenshots(["**/*.{png,jpg,jpeg}"], {
cwd: path.join(__dirname, "../__fixtures__/screenshots"),
cwd: join(__dirname, "../__fixtures__/screenshots"),
ignore: ["**/alicia.jpg"],
});
expect(screenshots).toEqual([
{
name: "penelope.jpg",
path: path.resolve(
__dirname,
"../__fixtures__/screenshots/penelope.jpg"
),
path: join(__dirname, "../__fixtures__/screenshots/penelope.jpg"),
},
]);
});
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/hashing.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { hashFile } from "./hashing";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

describe("#hashFile", () => {
it("hashes file", async () => {
const hash = await hashFile(
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/index.cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { UploadParameters } from "./index";

exports.upload = async (params: UploadParameters) => {
// @ts-ignore
const { upload } = await import("./index.mjs");
return upload(params);
};
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { upload } from "./upload";
export * from "./upload";
5 changes: 4 additions & 1 deletion packages/core/src/optimize.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { join } from "node:path";
import { stat } from "fs/promises";
import { fileURLToPath } from "node:url";
import { stat } from "node:fs/promises";
import { optimizeScreenshot } from "./optimize";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

const exists = async (filepath: string) => {
try {
await stat(filepath);
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/s3.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { setupJest } from "../mocks/server";
import { upload } from "./s3";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

setupJest();

describe("#upload", () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/upload.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { upload } from "./upload";
import { setupJest } from "../mocks/server";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

setupJest();

describe("#upload", () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { optimizeScreenshot } from "./optimize";
import { hashFile } from "./hashing";
import { createArgosApiClient } from "./api-client";
import { upload as uploadToS3 } from "./s3";
import { debug } from "./debug";

interface UploadParameters {
export interface UploadParameters {
files?: string[];
cwd?: string;
ignore?: string[];
Expand Down Expand Up @@ -85,6 +86,7 @@ export const upload = async (params: UploadParameters) => {
});

// Create build
debug("Creating build");
const result = await apiClient.createBuild({
commit: config.commit,
branch: config.branch,
Expand All @@ -94,7 +96,10 @@ export const upload = async (params: UploadParameters) => {
screenshotKeys: screenshots.map((screenshot) => screenshot.hash),
});

debug("Got screenshots", result);

// Upload screenshots
debug("Uploading screenshots");
await Promise.all(
result.screenshots.map(async ({ key, putUrl }) => {
const screenshot = screenshots.find((s) => s.hash === key);
Expand All @@ -106,6 +111,7 @@ export const upload = async (params: UploadParameters) => {
);

// Update build
debug("Updating build");
await apiClient.updateBuild({
buildId: result.build.id,
screenshots: screenshots.map((screenshot) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"include": ["./src", "./typings"],
"exclude": ["node_modules/**"],
"compilerOptions": {
"module": "Node16",
"module": "ESNext",
"moduleResolution": "node",
"target": "ES2022",
"target": "ES2021",
"importHelpers": true,
"declaration": true,
"sourceMap": true,
Expand Down

0 comments on commit a832d13

Please sign in to comment.