Skip to content

Commit

Permalink
fix: publish to cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosdevpereira committed Oct 8, 2022
1 parent 1ccc7e5 commit 0ad953f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"@actions/core": "^1.9.1",
"@actions/exec": "^1.1.1",
"@actions/github": "^5.1.0",
"@actions/http-client": "^2.0.1"
"@actions/http-client": "^2.0.1",
"shellac": "^0.7.2"
},
"devDependencies": {
"@types/jest": "^29.1.2",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

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

14 changes: 6 additions & 8 deletions src/Cloudflare.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { execSync } = require('child_process');
const shellac = require('shellac').default;

const COVERAGE_OUTPUT_FOLDER = './coverage';

Expand All @@ -10,13 +10,11 @@ class Cloudflare {
this.baseUrl = config.baseUrl;
}

publish(commitSha) {
execSync(`npx wrangler@2 pages publish "${COVERAGE_OUTPUT_FOLDER}" --project-name="${this.projectName}" --branch="${commitSha}"`, {
env: {
CLOUDFLARE_API_TOKEN: this.apiToken,
CLOUDFLARE_ACCOUNT_ID: this.accountId
}
});
async publish(commitSha) {
await shellac`
$ export CLOUDFLARE_API_TOKEN="${this.apiToken}"
$ export CLOUDFLARE_ACCOUNT_ID="${this.accountId}"
$$ npx wrangler@2 pages publish "${COVERAGE_OUTPUT_FOLDER}" --project-name="${this.projectName}" --branch="${commitSha}"`;

return `https://${commitSha}.${this.baseUrl}`;
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Cloudflare.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
jest.mock('shellac', () => ({
default: jest.fn()
}));

describe('Cloudflare', () => {
const shellac = require('shellac').default;
const Cloudflare = require('../src/Cloudflare');
const cloudflareInstance = new Cloudflare({
apiToken: 'api-token',
accountId: 'account-id',
projectName: 'project-name',
baseUrl: 'project-url.pages.dev'
});

it('publishes to cloudflare', async () => {
await cloudflareInstance.publish('1234');

expect(shellac).toHaveBeenCalledWith(
['\n $ export CLOUDFLARE_API_TOKEN="', '"\n $ export CLOUDFLARE_ACCOUNT_ID="', '"\n $$ npx wrangler@2 pages publish "', '" --project-name="', '" --branch="', '"'],
'api-token',
'account-id',
'./coverage',
'project-name',
'1234'
);
});

it('returns the report url', async () => {
const reportUrl = await cloudflareInstance.publish('1234');

expect(reportUrl).toBe('https://1234.project-url.pages.dev');
});
});

0 comments on commit 0ad953f

Please sign in to comment.