Skip to content

Commit 8eb083e

Browse files
authored
feat: Grab Provider Information (#12)
Grab the current provider information, and a bit of cleanup work.
1 parent 573b692 commit 8eb083e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+290
-116
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ jobs:
189189
- name: Run unit tests
190190
run: pnpm run test:unit:ci --maxWorkers=2
191191

192+
- name: Upload coverage reports to Codecov
193+
uses: codecov/codecov-action@v3
194+
env:
195+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
196+
192197
# fossa:
193198
# name: Run Fossa
194199
# runs-on: ubuntu-latest

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"lint:fix": "pnpm -r --filter='./packages/*' lint:fix",
1313
"format": "pnpm -r --filter='./packages/*' format && prettier --write '*.{cjs,json}' --ignore-unknown --no-error-on-unmatched-pattern",
1414
"format:check": "pnpm -r --filter='./packages/*' format:check && prettier --check '*.{cjs,json}' --ignore-unknown --no-error-on-unmatched-pattern",
15+
"test:unit": "pnpm -r --filter='./packages/*' run test:unit",
1516
"test:unit:ci": "pnpm -r --filter='./packages/*' run test:unit:ci"
1617
},
1718
"keywords": [],
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { detectProvider } from "../utils/provider.ts";
2+
import {
3+
type BundleAnalysisUploadPlugin,
4+
type Options,
5+
type Output,
6+
type ProviderUtilInputs,
7+
type UploadOverrides,
8+
} from "../types.ts";
9+
import { type UnpluginOptions } from "unplugin";
10+
import { debug } from "../utils/logging.ts";
11+
12+
interface BundleAnalysisUploadPluginArgs {
13+
userOptions: Options;
14+
bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin;
15+
}
16+
17+
export const bundleAnalysisPluginFactory = ({
18+
userOptions,
19+
bundleAnalysisUploadPlugin,
20+
}: BundleAnalysisUploadPluginArgs): UnpluginOptions => {
21+
// const dryRun = userOptions?.dryRun ?? false;
22+
const output: Output = {
23+
version: "1",
24+
};
25+
26+
const { pluginVersion, version, ...pluginOpts } = bundleAnalysisUploadPlugin({
27+
output,
28+
uploaderOverrides: userOptions?.uploaderOverrides,
29+
});
30+
31+
output.version = version;
32+
output.plugin = {
33+
name: pluginOpts.name,
34+
version: pluginVersion,
35+
};
36+
37+
return {
38+
...pluginOpts,
39+
buildStart() {
40+
output.builtAt = Date.now();
41+
},
42+
buildEnd(this) {
43+
output.duration = Date.now() - (output.builtAt ?? 0);
44+
},
45+
writeBundle: async () => {
46+
const args: UploadOverrides = userOptions.uploaderOverrides ?? {};
47+
const envs = process.env;
48+
const inputs: ProviderUtilInputs = { envs, args };
49+
const provider = await detectProvider(inputs);
50+
debug(`\nprovider: ${JSON.stringify(provider, null, 2)}`);
51+
},
52+
};
53+
};

packages/bundler-plugin-core/src/index.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import {
77
type Chunk,
88
type Module,
99
type Options,
10-
type Output,
10+
type ProviderUtilInputs,
11+
type UploadOverrides,
1112
} from "./types.ts";
1213

1314
import { jsonSchema } from "./schemas.ts";
1415
import { red } from "./utils/logging.ts";
16+
import { bundleAnalysisPluginFactory } from "./bundle-analysis/bundleAnalysisPluginFactory.ts";
1517

1618
const NODE_VERSION_RANGE = ">=18.18.0";
1719

@@ -33,38 +35,25 @@ export function codecovUnpluginFactory({
3335
}
3436

3537
if (userOptions?.enableBundleAnalysis) {
36-
const output: Output = {
37-
version: "1",
38-
};
39-
40-
let startTime = NaN;
41-
const { pluginVersion, version, ...pluginOpts } =
42-
bundleAnalysisUploadPlugin({
43-
output,
44-
uploaderOverrides: userOptions?.uploaderOverrides,
45-
});
46-
47-
plugins.push({
48-
...pluginOpts,
49-
buildStart() {
50-
startTime = Date.now();
51-
output.version = version;
52-
output.plugin = {
53-
name: pluginOpts.name,
54-
version: pluginVersion,
55-
};
56-
output.builtAt = startTime;
57-
},
58-
buildEnd(this) {
59-
const duration = Date.now() - startTime;
60-
output.duration = duration;
61-
},
62-
});
38+
plugins.push(
39+
bundleAnalysisPluginFactory({
40+
userOptions,
41+
bundleAnalysisUploadPlugin,
42+
}),
43+
);
6344
}
6445

6546
return plugins;
6647
});
6748
}
6849

69-
export type { BundleAnalysisUploadPlugin, Asset, Chunk, Module, Options };
50+
export type {
51+
BundleAnalysisUploadPlugin,
52+
Asset,
53+
Chunk,
54+
Module,
55+
Options,
56+
ProviderUtilInputs,
57+
UploadOverrides,
58+
};
7059
export { jsonSchema };

packages/bundler-plugin-core/src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export interface Options {
9191
/**
9292
* The amount of times the upload function will retry.
9393
*
94-
* Defaults to 3
94+
* Defaults to `3`
9595
*/
9696
retryCount?: number;
9797

@@ -100,6 +100,13 @@ export interface Options {
100100

101101
/** Override values for passing custom information to API. */
102102
uploaderOverrides?: UploadOverrides;
103+
104+
/**
105+
* When enabled information will not be uploaded to Codecov.
106+
*
107+
* Defaults to `false`
108+
*/
109+
dryRun?: boolean;
103110
}
104111

105112
export type BundleAnalysisUploadPlugin = (

packages/bundler-plugin-core/src/utils/__tests__/getPreSignedURL.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { HttpResponse, http } from "msw";
22
import { setupServer } from "msw/node";
33

44
import { getPreSignedURL } from "../getPreSignedURL.ts";
5-
import { FailedFetchError } from "@/errors/FailedFetchError.ts";
6-
import { NoUploadTokenError } from "@/errors/NoUploadTokenError.ts";
7-
import { UploadLimitReachedError } from "@/errors/UploadLimitReachedError.ts";
5+
import { FailedFetchError } from "../../errors/FailedFetchError.ts";
6+
import { NoUploadTokenError } from "../../errors/NoUploadTokenError.ts";
7+
import { UploadLimitReachedError } from "../../errors/UploadLimitReachedError.ts";
88

99
const server = setupServer();
1010

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { type ProviderUtilInputs } from "src/types.ts";
2+
import { detectProvider, setSlug } from "../provider.ts";
3+
import { isProgramInstalled } from "../isProgramInstalled";
4+
5+
jest.mock("../isProgramInstalled");
6+
const mockedIsProgramInstalled = isProgramInstalled as jest.Mock;
7+
8+
describe("detectProvider", () => {
9+
let consoleSpy: jest.SpyInstance;
10+
11+
beforeEach(() => {
12+
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => null);
13+
});
14+
15+
afterEach(() => {
16+
consoleSpy.mockReset();
17+
});
18+
19+
describe("when provider is detected", () => {
20+
beforeEach(() => {
21+
mockedIsProgramInstalled.mockReturnValue(false);
22+
});
23+
24+
it("returns the service params", async () => {
25+
const inputs: ProviderUtilInputs = {
26+
args: {},
27+
envs: {
28+
CI: "true",
29+
APPVEYOR: "true",
30+
},
31+
};
32+
33+
const result = await detectProvider(inputs);
34+
35+
expect(result.service).toEqual("appveyor");
36+
});
37+
});
38+
39+
describe("no provider is detected", () => {
40+
it("throws an error", async () => {
41+
let error;
42+
43+
try {
44+
const inputs: ProviderUtilInputs = {
45+
args: {},
46+
envs: {},
47+
};
48+
49+
const result = await detectProvider(inputs);
50+
console.info(result);
51+
} catch (e) {
52+
error = e;
53+
}
54+
55+
expect(error).toBeInstanceOf(Error);
56+
if (error instanceof Error) {
57+
expect(error.message).toEqual("Could not detect provider");
58+
}
59+
});
60+
});
61+
});
62+
63+
describe("setSlug", () => {
64+
describe("when slugArg is defined and not empty", () => {
65+
it("returns slugArg", () => {
66+
const slugArg = "cool-slug";
67+
68+
const result = setSlug(slugArg, undefined, undefined);
69+
expect(result).toEqual(slugArg);
70+
});
71+
});
72+
73+
describe("when slugArg is not defined or empty and orgEnv and repoEnv are defined and not empty", () => {
74+
it("returns orgEnv/repoEnv", () => {
75+
const orgEnv = "cool-org";
76+
const repoEnv = "cool-repo";
77+
78+
const result = setSlug(undefined, orgEnv, repoEnv);
79+
expect(result).toEqual(`${orgEnv}/${repoEnv}`);
80+
});
81+
});
82+
83+
describe("all values are undefined", () => {
84+
it("returns an empty string", () => {
85+
const result = setSlug(undefined, undefined, undefined);
86+
expect(result).toEqual("");
87+
});
88+
});
89+
});

packages/bundler-plugin-core/src/utils/__tests__/uploadStats.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HttpResponse, http } from "msw";
22
import { setupServer } from "msw/node";
33

44
import { uploadStats } from "../uploadStats";
5-
import { FailedUploadError } from "@/errors/FailedUploadError";
5+
import { FailedUploadError } from "../../errors/FailedUploadError";
66

77
const server = setupServer();
88

packages/bundler-plugin-core/src/utils/getPreSignedURL.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { z } from "zod";
22

3-
import { FailedFetchError } from "@/errors/FailedFetchError.ts";
4-
import { NoUploadTokenError } from "@/errors/NoUploadTokenError.ts";
5-
import { UploadLimitReachedError } from "@/errors/UploadLimitReachedError.ts";
6-
import { type ProviderServiceParams } from "@/types.ts";
3+
import { FailedFetchError } from "../errors/FailedFetchError.ts";
4+
import { NoUploadTokenError } from "../errors/NoUploadTokenError.ts";
5+
import { UploadLimitReachedError } from "../errors/UploadLimitReachedError.ts";
6+
import { type ProviderServiceParams } from "../types.ts";
77
import { DEFAULT_RETRY_COUNT } from "./constants.ts";
88
import { fetchWithRetry } from "./fetchWithRetry.ts";
99
import { red, yellow } from "./logging.ts";

packages/bundler-plugin-core/src/utils/provider.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
import {
2+
type ProviderServiceParams,
3+
type ProviderUtilInputs,
4+
} from "../types.ts";
5+
import { cyan, red } from "./logging.ts";
6+
import { providerList } from "./providers";
7+
8+
export async function detectProvider(
9+
inputs: ProviderUtilInputs,
10+
): Promise<ProviderServiceParams> {
11+
cyan("Detecting CI provider");
12+
for (const provider of providerList) {
13+
if (provider.detect(inputs.envs)) {
14+
cyan(`Detected CI provider: ${provider.getServiceName()}`);
15+
return await provider.getServiceParams(inputs);
16+
}
17+
}
18+
red("Could not detect CI provider");
19+
throw new Error("Could not detect provider");
20+
}
21+
122
export function setSlug(
223
slugArg: string | undefined,
324
orgEnv: string | undefined,

0 commit comments

Comments
 (0)