Skip to content

Commit

Permalink
refactor schema publisher to aid testability
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Apr 1, 2021
1 parent 369b470 commit b32fe3f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 46 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"cypress": "^3.8.2",
"fetch-mock": "^9.9.0",
"isomorphic-fetch": "^2.2.1",
"jest": "^24.8.0",
"jest": "^26.6.3",
"jest-junit": "^7.0.0",
"lolex": "^4.2.0",
"mini-css-extract-plugin": "^0.8.0",
Expand Down Expand Up @@ -75,7 +75,12 @@
],
"nodemonConfig": {
"ext": "js,graphql,yaml,jsx",
"ignore": ["*.cyp.js", "**/__tests__/*.js", "test-helpers", "cypress"]
"ignore": [
"*.cyp.js",
"**/__tests__/*.js",
"test-helpers",
"cypress"
]
},
"engines": {
"node": "14.x"
Expand Down
3 changes: 2 additions & 1 deletion packages/tc-schema-publisher/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const crypto = require('crypto');
const AWS = require('aws-sdk');
const { Readable } = require('stream');
const schema = require('@financial-times/tc-schema-sdk');
const singletonSchema = require('@financial-times/tc-schema-sdk');

const s3Client = new AWS.S3({ region: 'eu-west-1' });

Expand All @@ -13,6 +13,7 @@ const getVersion = schemaObject => {
const sendSchemaToS3 = async (
environment,
bucketName = process.env.TREECREEPER_SCHEMA_BUCKET,
schema = singletonSchema,
) => {
await schema.ready();
const schemaObject = {
Expand Down
2 changes: 1 addition & 1 deletion packages/tc-schema-publisher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"description": "This package saves a treecreeper schema to s3",
"bin": {
"tc-schema-publisher": "scripts/deploy.js"
"tc-schema-publisher": "scripts/command.js"
},
"main": "index.js",
"author": "",
Expand Down
50 changes: 50 additions & 0 deletions packages/tc-schema-publisher/scripts/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env node
/* eslint-disable no-console */
const program = require('commander');
const { join } = require('path');
const { readFileSync } = require('fs');

const pkg = JSON.parse(
readFileSync(join(__dirname, '../package.json'), 'utf8'),
);

const { deploy } = require('./deploy');

const action = async (...args) => {
const [command] = args;

try {
await deploy(command);
} catch (err) {
process.exit(2); // eslint-disable-line unicorn/no-process-exit
}
};

const help = () => {
const template = `
Example:

tc-schema-publisher -D ./example-schema -B schema-bucket -v latest
`;
console.log(template);
};

program
.name('tc-schema-publisher')
.description('Publish schemas to S3 bucket')
.option(
'-D, --schema-directory <directory>',
'directory to the schema. (default: "process.env.TREECREEPER_SCHEMA_DIRECTORY")',
process.env.TREECREEPER_SCHEMA_DIRECTORY,
)
.option(
'-B, --bucket-name <bucket>',
'S3 bucket name which you want to upload. (default: "process.env.TREECREEPER_SCHEMA_BUCKET")',
process.env.TREECREEPER_SCHEMA_BUCKET,
)
.option('-E, --env <env>', 'specify publish environment', 'latest')
.version(pkg.version)
.action(action)
.on('--help', help);

program.parse(process.argv);
54 changes: 12 additions & 42 deletions packages/tc-schema-publisher/scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/usr/bin/env node
/* eslint-disable no-console */

const program = require('commander');
const { join } = require('path');
const { statSync, readFileSync } = require('fs');
const schema = require('@financial-times/tc-schema-sdk');
const { statSync } = require('fs');
const { SDK } = require('@financial-times/tc-schema-sdk');
const { sendSchemaToS3 } = require('..');

const pkg = JSON.parse(
readFileSync(join(__dirname, '../package.json'), 'utf8'),
);
const deploy = async command => {
const {
schemaDirectory,
bucketName,
env,
} = command;

const action = async (...args) => {
const [command] = args;
const { schemaDirectory, bucketName, env } = command;
try {
if (!bucketName) {
throw new Error(
Expand All @@ -34,42 +32,14 @@ const action = async (...args) => {
if (!stat.isDirectory()) {
throw new Error('schema directory is not a directory');
}

schema.init({ schemaDirectory });
await sendSchemaToS3(env, bucketName);
const schema = new SDK({ schemaDirectory, includeTestDefinitions });
await sendSchemaToS3(env, bucketName, schema);
console.log('successfully deployed');
} catch (err) {
console.error('Failed to deploy');
console.error(err);
process.exit(2); // eslint-disable-line unicorn/no-process-exit
throw err;
}
};

const help = () => {
const template = `
Example:

tc-schema-publisher -D ./example-schema -B schema-bucket -v latest
`;
console.log(template);
};

program
.name('tc-schema-publisher')
.description('Publish schemas to S3 bucket')
.option(
'-D, --schema-directory <directory>',
'directory to the schema. (default: "process.env.TREECREEPER_SCHEMA_DIRECTORY")',
process.env.TREECREEPER_SCHEMA_DIRECTORY,
)
.option(
'-B, --bucket-name <bucket>',
'S3 bucket name which you want to upload. (default: "process.env.TREECREEPER_SCHEMA_BUCKET")',
process.env.TREECREEPER_SCHEMA_BUCKET,
)
.option('-E, --env <env>', 'specify publish environment', 'latest')
.version(pkg.version)
.action(action)
.on('--help', help);

program.parse(process.argv);
module.exports = { deploy };

0 comments on commit b32fe3f

Please sign in to comment.