diff --git a/package-lock.json b/package-lock.json index ca1c611..33cbdcf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27699,6 +27699,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", @@ -28469,6 +28470,7 @@ "@types/convict": "^6.1.1", "axios": "^0.27.2", "convict": "^6.2.3", + "debug": "^4.3.4", "env-ci": "^7.3.0", "fast-glob": "^3.2.11", "msw": "^0.44.2", diff --git a/package.json b/package.json index f59da2e..5938082 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/packages/core/.swcrc b/packages/core/.swcrc index 7bea9e0..cfb1703 100644 --- a/packages/core/.swcrc +++ b/packages/core/.swcrc @@ -1,11 +1,7 @@ { "$schema": "https://json.schemastore.org/swcrc", - "env": { - "targets": { - "node": "16" - } - }, "jsc": { + "target": "es2021", "parser": { "syntax": "typescript" } diff --git a/packages/core/integration/run-upload.cjs b/packages/core/integration/run-upload.cjs new file mode 100644 index 0000000..afef731 --- /dev/null +++ b/packages/core/integration/run-upload.cjs @@ -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; diff --git a/packages/core/integration/upload.cjs b/packages/core/integration/upload.cjs new file mode 100644 index 0000000..9713557 --- /dev/null +++ b/packages/core/integration/upload.cjs @@ -0,0 +1,4 @@ +const { upload } = require("@argos-ci/core"); +const { runUpload } = require("./run-upload.cjs"); + +runUpload(upload, "@argos/core--cjs"); diff --git a/packages/core/integration/upload.mjs b/packages/core/integration/upload.mjs new file mode 100644 index 0000000..d517a86 --- /dev/null +++ b/packages/core/integration/upload.mjs @@ -0,0 +1,4 @@ +import { upload } from "@argos-ci/core"; +import { runUpload } from "./run-upload.cjs"; + +runUpload(upload, "@argos/core--mjs"); diff --git a/packages/core/jest.config.cjs b/packages/core/jest.config.cjs index c39d5a3..381f402 100644 --- a/packages/core/jest.config.cjs +++ b/packages/core/jest.config.cjs @@ -2,4 +2,5 @@ module.exports = { transform: { "^.+\\.(t|j)sx?$": ["@swc/jest"], }, + extensionsToTreatAsEsm: [".ts"], }; diff --git a/packages/core/package.json b/packages/core/package.json index 442b237..7b30063 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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" }, @@ -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", diff --git a/packages/core/rollup.config.js b/packages/core/rollup.config.js index 7a65b32..572ec1e 100644 --- a/packages/core/rollup.config.js +++ b/packages/core/rollup.config.js @@ -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 [ @@ -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: { diff --git a/packages/core/src/api-client.ts b/packages/core/src/api-client.ts index 936452f..4661cee 100644 --- a/packages/core/src/api-client.ts +++ b/packages/core/src/api-client.ts @@ -69,9 +69,6 @@ export const createArgosApiClient = ( }); return response.data; } catch (error) { - if (axios.isAxiosError(error)) { - console.log(error.response?.data); - } throw error; } }; diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 9af01f7..8d8d3f0 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -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: { diff --git a/packages/core/src/debug.ts b/packages/core/src/debug.ts new file mode 100644 index 0000000..87d7c6e --- /dev/null +++ b/packages/core/src/debug.ts @@ -0,0 +1,3 @@ +import createDebug from "debug"; + +export const debug = createDebug("@argos-ci/core"); diff --git a/packages/core/src/discovery.test.ts b/packages/core/src/discovery.test.ts index e8fcbef..74bf425 100644 --- a/packages/core/src/discovery.test.ts +++ b/packages/core/src/discovery.test.ts @@ -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"), }, ]); }); diff --git a/packages/core/src/hashing.test.ts b/packages/core/src/hashing.test.ts index 484beb9..a6ff2fe 100644 --- a/packages/core/src/hashing.test.ts +++ b/packages/core/src/hashing.test.ts @@ -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( diff --git a/packages/core/src/index.cjs.ts b/packages/core/src/index.cjs.ts new file mode 100644 index 0000000..c8d9600 --- /dev/null +++ b/packages/core/src/index.cjs.ts @@ -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); +}; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 2da9db2..ab6125a 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1 +1 @@ -export { upload } from "./upload"; +export * from "./upload"; diff --git a/packages/core/src/optimize.test.ts b/packages/core/src/optimize.test.ts index 128bb5b..46ed233 100644 --- a/packages/core/src/optimize.test.ts +++ b/packages/core/src/optimize.test.ts @@ -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); diff --git a/packages/core/src/s3.test.ts b/packages/core/src/s3.test.ts index 873e75d..92ff92e 100644 --- a/packages/core/src/s3.test.ts +++ b/packages/core/src/s3.test.ts @@ -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", () => { diff --git a/packages/core/src/upload.test.ts b/packages/core/src/upload.test.ts index 49a7fdc..d574e70 100644 --- a/packages/core/src/upload.test.ts +++ b/packages/core/src/upload.test.ts @@ -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", () => { diff --git a/packages/core/src/upload.ts b/packages/core/src/upload.ts index 4921fe3..d5606f8 100644 --- a/packages/core/src/upload.ts +++ b/packages/core/src/upload.ts @@ -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[]; @@ -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, @@ -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); @@ -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) => ({ diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 511df3b..6c5f5f3 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -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,