Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Commit

Permalink
Refactor JavaScript to TypeScript (#72)
Browse files Browse the repository at this point in the history
Addresses issue #70 
* typescript infrastructure
* es6 -> ts
* Convert to Typescript
* Move tests into src
  • Loading branch information
akdor1154 authored and martysweet committed Nov 13, 2017
1 parent 9e1360a commit 28299ff
Show file tree
Hide file tree
Showing 75 changed files with 428 additions and 270 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

24 changes: 0 additions & 24 deletions gulpfile.js

This file was deleted.

24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@
"cloudformation-js-yaml-schema": "0.3.0",
"colors": "latest",
"commander": "latest",
"fs": "0.0.2",
"js-yaml": "^3.7.0",
"opn": "latest",
"winston": "latest"
},
"devDependencies": {
"gulp": "latest",
"babel-polyfill": "latest",
"gulp-babel": "latest",
"gulp-sourcemaps": "latest",
"babel-preset-es2015": "latest",
"babel-register": "latest",
"babel-cli": "latest",
"@types/chai": "^4.0.4",
"@types/colors": "^1.1.3",
"@types/commander": "^2.11.0",
"@types/js-yaml": "^3.9.1",
"@types/mocha": "^2.2.44",
"@types/node": "^8.0.50",
"@types/opn": "^3.0.28",
"@types/winston": "^2.3.7",
"chai": "latest",
"mocha": "latest",
"chai": "latest"
"typescript": "^2.6.1"
},
"scripts": {
"build": "babel src --presets babel-preset-es2015 --out-dir lib",
"test": "mocha --compilers js:babel-register --require babel-polyfill",
"build": "tsc",
"test": "mocha lib/test",
"watch": "tsc -w",
"prepublish": "npm run build",
"deploy": "npm publish"
},
Expand Down
123 changes: 123 additions & 0 deletions src/awsData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
type AWSExtraDocs = {[k: string]: string}

export const awsExtraDocs = require('../data/aws_extra_docs.json') as AWSExtraDocs;

export type AWSPrimitiveType = 'String' | 'Integer' | 'Boolean' | 'Json';

interface PropertyBase {
Required: boolean,
Documentation: string,
UpdateType: 'Mutable' | 'Immutable' | 'Conditional',
}

interface PrimitiveProperty extends PropertyBase {
PrimitiveType: AWSPrimitiveType,

Type: undefined
ItemType: undefined
PrimitiveItemType: undefined
}

interface ComplexProperty extends PropertyBase {
Type: string,

PrimitiveType: undefined
ItemType: undefined
PrimitiveItemType: undefined
}

interface ListPropertyBase extends PropertyBase {
Type: 'List',
DuplicatesAllowed: boolean,

PrimitiveType: undefined
}

interface PrimitiveItemType {
PrimitiveItemType: AWSPrimitiveType,

ItemType: undefined
}

interface ItemType {
ItemType: string,

PrimitiveItemType: undefined
}

type ListProperty = ListPropertyBase & (PrimitiveItemType | ItemType)

interface MapPropertyBase extends PropertyBase {
Type: 'Map',
DuplicatesAllowed: boolean,

PrimitiveType: undefined
ItemType: undefined
}

type MapProperty = MapPropertyBase & (PrimitiveItemType | ItemType)

export type Property = PrimitiveProperty | ComplexProperty | ListProperty | MapProperty

export interface ResourcePropertyType {
Documentation: string,
Properties: {[propertyName: string]: Property}
}

export interface ResourceType {
Documentation: string,
Properties: {[propertyName: string]: Property},
Attributes?: {[attributeName: string]: Attribute},
AdditionalProperties?: boolean
}

interface PrimitiveAttribute {
ItemType: AWSPrimitiveType
}

interface ListAttribute {
Type: 'List',
PrimitiveItemType: AWSPrimitiveType
}

type Attribute = PrimitiveAttribute | ListAttribute;

interface AWSResourcesSpecification {
PropertyTypes: {[propertyName: string]: ResourcePropertyType}
ResourceTypes: {[resourceName: string]: ResourceType}
}

export const awsResources = require('../data/aws_resources_specification.json') as AWSResourcesSpecification;

type ResourceRefTypes = {[resourceName: string]: string};

export const awsResourceRefTypes = require('../data/aws_resource_ref_types.json') as ResourceRefTypes;

type ParameterTypes = {[parameterName: string]: 'string' | 'array'};

export const awsParameterTypes = require('../data/aws_parameter_types.json') as ParameterTypes;

type IntrinsicFunctions = {
[functionName: string]: {
supportedFunctions: string[]
}
}

export const awsIntrinsicFunctions = require('../data/aws_intrinsic_functions.json') as IntrinsicFunctions;

// avoid "does this exist" checks everywhere
for (let functionName in awsIntrinsicFunctions) {
awsIntrinsicFunctions[functionName].supportedFunctions = awsIntrinsicFunctions[functionName].supportedFunctions || [];
}

type RefOverrides = {
"AWS::AccountId": string,
"AWS::NotificationARNs": string[],
"AWS::NoValue": "",
"AWS::Region": string,
"AWS::StackId": string,
"AWS::StackName": string,
[refOverride: string]: any
};

export const awsRefOverrides = require('../data/aws_ref_override.json') as RefOverrides;
15 changes: 7 additions & 8 deletions src/docs.es6 → src/docs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
let awsResources = require('../data/aws_resources_specification.json');
let awsExtraDocs = require('../data/aws_extra_docs.json');
let opn = require('opn');
import {awsResources, awsExtraDocs} from './awsData';
import opn = require('opn');


exports.getDoc = function getDoc(search, browse = true){
export function getDoc(search: string | null, browse: boolean = true){

let formattedSearch = search;

Expand All @@ -22,7 +21,7 @@ exports.getDoc = function getDoc(search, browse = true){

};

exports.getUrls = function getUrls(search = ''){
export function getUrls(search: string | null = ''){

if(search == null) search = '';

Expand All @@ -41,7 +40,7 @@ exports.getUrls = function getUrls(search = ''){
return docs;
};

function searchInResources(search){
function searchInResources(search: string): string[] {
let dotCount = (search.match(/\./g) || []).length;


Expand All @@ -54,7 +53,7 @@ function searchInResources(search){

}else if(dotCount == 1){

let urls = new Array();
let urls: string[] = new Array();

// Check PropertyTypes
if(awsResources['PropertyTypes'].hasOwnProperty(search)){
Expand Down Expand Up @@ -90,7 +89,7 @@ function searchInResources(search){
return [];
}

function searchExtraDocs(search){
function searchExtraDocs(search: string) {
if(awsExtraDocs.hasOwnProperty(search)){
return [ awsExtraDocs[search] ];
}else{
Expand Down
7 changes: 7 additions & 0 deletions src/fixes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import yaml = require('js-yaml')

declare module 'js-yaml' {
export interface LoadOptions {
onWarning?: (warning: string) => void;
}
}
19 changes: 11 additions & 8 deletions src/index.es6 → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
* Module dependencies.
*/

let program = require('commander');
let colors = require('colors');
import program = require('commander');
require('colors');
let version = require('../package').version;
let firstArg, secondArg, params = null;
let firstArg: string | undefined = undefined
let secondArg: string = undefined!;

function list(val) {
function list(val: string) {
return val.split(',');
}

Expand All @@ -31,8 +32,12 @@ if (typeof firstArg === 'undefined') {
process.exit(1);
}

import validatorBaseImport = require('./validator');
import docsBaseImport = require('./docs');

if(firstArg == "validate"){
const validator = require('./validator');

const validator = require('./validator') as typeof validatorBaseImport;

if(program.parameters){
for(let param of program.parameters){
Expand All @@ -51,7 +56,6 @@ if(firstArg == "validate"){
}

let result = validator.validateFile(secondArg);

// Show the errors
console.log((result['errors']['info'].length + " infos").grey);
for(let info of result['errors']['info']){
Expand Down Expand Up @@ -82,8 +86,7 @@ if(firstArg == "validate"){
process.exit(0)
}


}else if(firstArg == "docs"){
const docs = require('./docs');
const docs = require('./docs') as typeof docsBaseImport;
console.log(docs.getDoc(secondArg))
}
4 changes: 2 additions & 2 deletions src/logger.es6 → src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let winston = require('winston');
import winston = require('winston');

let logger = new (winston.Logger)({
transports: [
Expand All @@ -10,4 +10,4 @@ let logger = new (winston.Logger)({
exitOnError: false
});

module.exports = logger;
export = logger;
15 changes: 7 additions & 8 deletions src/parser.es6 → src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import yaml from 'js-yaml';
import { CLOUDFORMATION_SCHEMA} from 'cloudformation-js-yaml-schema';
import fs from 'fs';
import yaml = require('js-yaml');
const { CLOUDFORMATION_SCHEMA } = require('cloudformation-js-yaml-schema');
import fs = require('fs');


exports.openFile = function openFile(path){
export function openFile(path: string){

// Check the file path is valid
if (!fs.existsSync(path)) {
Expand All @@ -26,7 +25,7 @@ exports.openFile = function openFile(path){

};

function openYaml(path){
function openYaml(path: string){

// Try and load the Yaml
let yamlParse = yaml.safeLoad(fs.readFileSync(path, 'utf8'), {
Expand All @@ -49,15 +48,15 @@ function openYaml(path){

}

function openJson(path){
function openJson(path: string){

return JSON.parse(fs.readFileSync(path, 'utf8'));

}

let lastPlaceInTemplate = null;
let lastKeyInTemplate = null;
function cleanupYaml(ref){
function cleanupYaml(ref: any){

// Step into next attribute
for(let i=0; i < Object.keys(ref).length; i++){
Expand Down
Loading

0 comments on commit 28299ff

Please sign in to comment.