Skip to content

Commit

Permalink
Report duraction when storing artifacts in remote-nx (#18)
Browse files Browse the repository at this point in the history
* Report duraction when storing artifacts in remote-nx

* Revert to previous lockfile

* Add changeset

* Remove test console log
  • Loading branch information
tom-sherman authored Feb 5, 2024
1 parent 4723c29 commit bf56be9
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 327 deletions.
7 changes: 7 additions & 0 deletions .changeset/warm-hats-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@vercel/remote-nx': major
---

Report duraction when storing artifacts in remote-nx. This allows "time saved" to be reported in the Vercel dashboard.

Requires NX v17 or higher.
5 changes: 3 additions & 2 deletions packages/remote-nx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
"author": "Vercel",
"devDependencies": {
"@babel/core": "^7.22.5",
"@nx/workspace": "^16.3.2",
"@nx/workspace": "^17.2.8",
"@types/yargs": "^17.0.24",
"nx": "^17.2.8",
"tsconfig": "workspace:*",
"typescript": "5.1.3",
"vitest": "^0.32.0"
},
"dependencies": {
"@vercel/remote": "workspace:*",
"chalk": "^4.1.0",
"nx-remotecache-custom": "^4.0.0"
"nx-remotecache-custom": "^17.1.1"
}
}
21 changes: 19 additions & 2 deletions packages/remote-nx/src/setup-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import type { Task } from 'nx/src/config/task-graph';
import { initEnv } from 'nx-remotecache-custom';
import { getVercelRemoteCacheClient } from './remote-client';
import type { Readable } from 'stream';

Check warning on line 4 in packages/remote-nx/src/setup-sdk.ts

View workflow job for this annotation

GitHub Actions / typecheck | lint | test (18.x)

`stream` type import should occur before type import of `nx/src/config/task-graph`

Check warning on line 4 in packages/remote-nx/src/setup-sdk.ts

View workflow job for this annotation

GitHub Actions / typecheck | lint | test (18.x)

Prefer `node:stream` over `stream`
import type { VercelRemoteCacheOptions } from './remote-client';

// eslint-disable-next-line @typescript-eslint/require-await
const setupSDK = async (options: VercelRemoteCacheOptions) => {
const setupSDK = async (options: VercelRemoteCacheOptions, tasks: Task[]) => {

Check warning on line 8 in packages/remote-nx/src/setup-sdk.ts

View workflow job for this annotation

GitHub Actions / typecheck | lint | test (18.x)

Missing return type on function
initEnv(options);
const remote = getVercelRemoteCacheClient(options);

return {
name: 'Vercel Remote Cache',
fileExists: (filename: string) => remote.exists(filename).send(),

Check warning on line 14 in packages/remote-nx/src/setup-sdk.ts

View workflow job for this annotation

GitHub Actions / typecheck | lint | test (18.x)

Missing return type on function
retrieveFile: (filename: string) => remote.get(filename).stream(),
storeFile: (filename: string, stream: Readable) =>
remote.put(filename).stream(stream),
remote
.put(filename, {
duration: totalDuration(tasks),
})
.stream(stream),
};
};

function totalDuration(tasks: Pick<Task, 'endTime' | 'startTime'>[]) {
let total = 0;
for (const task of tasks) {
if (task.startTime && task.endTime) {
total += task.endTime - task.startTime;
}
}

return total;
}

export { setupSDK };
8 changes: 4 additions & 4 deletions packages/remote-nx/test/setup-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('sdk', () => {
lifeCycle: {},
};

const client = await setupSDK(options);
const client = await setupSDK(options, []);
expect(client).toBeDefined();
});

Expand All @@ -27,7 +27,7 @@ describe('sdk', () => {
lifeCycle: {},
};

const client = await setupSDK(options);
const client = await setupSDK(options, []);
expect(client.fileExists).toBeInstanceOf(Function);
expect(client.retrieveFile).toBeInstanceOf(Function);
expect(client.storeFile).toBeInstanceOf(Function);
Expand All @@ -40,7 +40,7 @@ describe('sdk', () => {
lifeCycle: {},
};

await expect(setupSDK(options)).rejects.toThrowError(
await expect(setupSDK(options, [])).rejects.toThrowError(
'Missing a Vercel access token',
);
});
Expand All @@ -52,6 +52,6 @@ describe('sdk', () => {
};
// eslint-disable-next-line turbo/no-undeclared-env-vars
process.env.NX_VERCEL_REMOTE_CACHE_TOKEN = 'token';
await expect(setupSDK(options)).resolves.toBeDefined();
await expect(setupSDK(options, [])).resolves.toBeDefined();
});
});
2 changes: 1 addition & 1 deletion packages/remote/src/remote-cache-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface RemoteClientOptions {
export interface RemoteClient {
exists: (hash: string) => ArtifactExistsRequest;
get: (hash: string) => ArtifactGetRequest;
put: (hash: string) => ArtifactPutRequest;
put: (hash: string, options?: ArtifactOptions) => ArtifactPutRequest;
}

export class RemoteClientImpl implements RemoteClient {
Expand Down
Loading

0 comments on commit bf56be9

Please sign in to comment.