Skip to content

Commit

Permalink
Committing dist directory to version control in preparation for npm p…
Browse files Browse the repository at this point in the history
…ublish.
  • Loading branch information
khalidx committed Jul 24, 2017
1 parent 0cf4160 commit cb531d3
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 0 deletions.
22 changes: 22 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export declare enum ContentType {
SwaggerVersion2 = "swagger.yaml;version=2",
JsonSchemaVersion4 = "jsonschema;version=4",
}
export declare enum ResourceType {
schemas = 0,
scenarios = 1,
specifications = 2,
services = 3,
}
export declare class Resource {
name: string;
type: ResourceType;
content: string;
contentType: ContentType;
constructor(name: string, type: ResourceType, content: string, contentType: ContentType);
}
export declare class Component {
name: string;
resources: Array<Resource>;
constructor(name: string, resources: Array<Resource>);
}
45 changes: 45 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ContentType;
(function (ContentType) {
ContentType["SwaggerVersion2"] = "swagger.yaml;version=2";
ContentType["JsonSchemaVersion4"] = "jsonschema;version=4";
})(ContentType = exports.ContentType || (exports.ContentType = {}));
var ResourceType;
(function (ResourceType) {
ResourceType[ResourceType["schemas"] = 0] = "schemas";
ResourceType[ResourceType["scenarios"] = 1] = "scenarios";
ResourceType[ResourceType["specifications"] = 2] = "specifications";
ResourceType[ResourceType["services"] = 3] = "services";
})(ResourceType = exports.ResourceType || (exports.ResourceType = {}));
class Resource {
constructor(name, type, content, contentType) {
this.name = name;
this.type = type;
this.content = content;
this.contentType = contentType;
}
}
exports.Resource = Resource;
class Component {
constructor(name, resources) {
this.name = name;
this.resources = resources;
}
}
exports.Component = Component;
/* Usage:
Promise
.resolve()
.then(function () {
return provider.initialize(component)
})
.then(function () {
return provider.deploy(component)
})
.catch(function (error) {
console.error(error)
})
*/
3 changes: 3 additions & 0 deletions dist/provider/aws/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Component } from '../../';
export declare function initialize(component: Component): Promise<void>;
export declare function deploy(component: Component): Promise<void>;
135 changes: 135 additions & 0 deletions dist/provider/aws/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require("../../");
const CloudFormation = require("aws-sdk/clients/cloudformation");
const S3 = require("aws-sdk/clients/s3");
let cloudformation = new CloudFormation({ apiVersion: '2010-05-15', region: 'us-east-1' });
let s3 = new S3({ apiVersion: '2006-03-01' });
/* Initializes the cloud environment for the specified component */
function initialize(component) {
return Promise
.resolve()
.then(function () {
console.log('Checking if stack named [%s] exists ...', component.name);
return cloudformation
.describeStacks({
StackName: component.name
})
.promise()
.then(function (stack) {
if (stack && stack.Stacks && stack.Stacks.length > 0) {
console.log('The [%s] stack exists.', component.name);
return true;
}
return false;
})
.catch(function (error) {
if (error) {
if (error.code === 'ValidationError') {
console.log('The [%s] stack does not exist.', component.name);
return false;
}
console.error(error.message);
}
throw new Error('Unable to query if the stack exists in AWS.');
});
})
.then(function (exists) {
if (exists) {
return Promise.resolve();
}
else {
console.log('Creating the stack ...');
return cloudformation.createStack({
StackName: component.name,
OnFailure: 'DELETE',
Tags: [
{
Key: 'Name',
Value: 'specification.cloud storage bucket'
}
],
TemplateBody: `
AWSTemplateFormatVersion: '2010-09-09'
Description: 'This stack contains the specification.cloud s4 resources.'
Resources:
Bucket:
Type: 'AWS::S3::Bucket'
Outputs:
BucketUrl:
Value: !Join [ '', [ 'https://', !GetAtt Bucket.DomainName ] ]
`
})
.promise()
.then(function (output) {
console.log('Stack creation in progress ...');
return;
})
.catch(function (error) {
console.error(error.message);
throw new Error('Unable to create the stack.');
});
}
})
.then(function () {
console.log('Waiting for a valid stack state ...');
return cloudformation.waitFor('stackCreateComplete', {
StackName: component.name
})
.promise()
.then(function (output) {
console.log('Stack is in a valid state.');
return;
})
.catch(function (error) {
console.error(error.message);
throw new Error('Stack is in an invalid state.');
});
})
.catch(function (error) {
if (error) {
console.error(error);
}
throw new Error('Unable to initialize the AWS provider for the specified component.');
});
}
exports.initialize = initialize;
/* Deploys an item to S3 */
function deploy(component) {
return Promise
.resolve()
.then(function () {
return cloudformation.describeStackResource({
StackName: component.name,
LogicalResourceId: 'Bucket'
})
.promise();
})
.then(function (output) {
if (!output || !output.StackResourceDetail || !output.StackResourceDetail.PhysicalResourceId) {
throw new Error('No data received from AWS');
}
let uploads = [];
for (let resource of component.resources) {
uploads.push(s3.putObject({
Body: resource.content,
Bucket: output.StackResourceDetail.PhysicalResourceId,
Key: _1.ResourceType[resource.type] + '/' + resource.name
// ServerSideEncryption: "AES256"
// Tagging: "key1=value1&key2=value2"
})
.promise());
}
return Promise.all(uploads);
})
.then(function (output) {
console.log(output);
console.log('Items uploaded successfully.');
return;
})
.catch(function (error) {
console.error(error);
throw new Error('Unable to deploy items.');
});
}
exports.deploy = deploy;

0 comments on commit cb531d3

Please sign in to comment.