Skip to content

Commit

Permalink
feat: Implemented publish command
Browse files Browse the repository at this point in the history
  • Loading branch information
pawfa committed Mar 17, 2021
1 parent 578718a commit 0a5fb5b
Show file tree
Hide file tree
Showing 20 changed files with 476 additions and 9 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
preset: 'ts-jest',
setupFilesAfterEnv: ['./test/setupTests.ts'],
};
143 changes: 143 additions & 0 deletions package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
"homepage": "https://github.com/HLTech/judge-d-js",
"devDependencies": {
"@types/jest": "^26.0.20",
"@types/mock-fs": "^4.13.0",
"@types/yargs": "^15.0.13",
"factory.ts": "^0.5.1",
"husky": "^4.3.8",
"jest": "^26.6.3",
"mock-fs": "^4.13.0",
"prettier": "^2.2.1",
"pretty-quick": "^3.1.0",
"ts-jest": "^26.5.2",
Expand All @@ -44,6 +47,7 @@
"tag": "latest"
},
"dependencies": {
"axios": "^0.21.1",
"yargs": "^15.4.1"
}
}
15 changes: 15 additions & 0 deletions src/commands/publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { generateContractsForm } from '../utils/generate-contracts-form';
import { CliArguments } from '../utils/define-args';
import { readPacts } from '../utils/read-pacts';
import { buildUrl } from '../utils/build-url';
import { postPacts } from '../utils/post-pacts';

export async function publish(argv: CliArguments) {
const { pactsDir, serviceName, url, serviceVersion } = argv;

const pacts = readPacts(pactsDir);
const contractsForm = generateContractsForm(pacts);
const judgeDUrl = buildUrl(url, serviceName, serviceVersion);

await postPacts(judgeDUrl, contractsForm);
}
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { defineArgs } from './utils/define-args';
import { publish } from './commands/publish';

export function run(process: NodeJS.Process) {
export async function run(process: NodeJS.Process) {
const argv = defineArgs(process.argv.slice(2));
console.log(argv);

if (argv._.includes('publish')) {
try {
await publish(argv);
} catch (e) {
process.exit(1);
}
}
}
7 changes: 7 additions & 0 deletions src/utils/build-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function buildUrl(
url: string,
serviceName: string,
serviceVersion: string
) {
return `${url}/contracts/services/${serviceName}/versions/${serviceVersion}`;
}
4 changes: 2 additions & 2 deletions src/utils/define-args.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import yargs from 'yargs';

interface CliArguments {
export interface CliArguments {
url: string;
pactsDir: string;
serviceName: string;
serviceVersion: string;
}

export function defineArgs(cliArgs: string[]): CliArguments {
export function defineArgs(cliArgs: string[]): yargs.Arguments<CliArguments> {
return yargs(cliArgs)
.command<CliArguments>('publish', 'Publish contracts', (yargs) => {
yargs.options({
Expand Down
39 changes: 39 additions & 0 deletions src/utils/generate-contracts-form.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Pact } from './read-pacts';

export function generateContractsForm(pactFiles: Pact[]): ServiceContractsForm {
return pactFiles.reduce<ServiceContractsForm>(
(serviceContractsForm, fileContent) => {
if (fileContent.provider?.name) {
return {
...serviceContractsForm,
expectations: {
...serviceContractsForm.expectations,
[fileContent.provider.name]: {
rest: {
value: JSON.stringify(fileContent),
mimeType: 'application-json',
},
},
},
};
}
return serviceContractsForm;
},
{
capabilities: {},
expectations: {},
}
);
}

export interface ServiceContractsForm {
capabilities: Record<string, RestContract>;
expectations: Record<string, RestContract>;
}

interface RestContract {
rest: {
value: string;
mimeType: 'application-json';
};
}
6 changes: 6 additions & 0 deletions src/utils/post-pacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ServiceContractsForm } from './generate-contracts-form';
import axios from 'axios';

export function postPacts(url: string, pacts: ServiceContractsForm) {
return axios.post(url, pacts);
}
Loading

0 comments on commit 0a5fb5b

Please sign in to comment.