Skip to content

Commit

Permalink
moved to typescript, added prettier/eslint, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmujadzic committed Jun 6, 2020
1 parent 0166eeb commit 73c3625
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"env": {
"browser": true,
"es2020": true
},
"extends": [
"google",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 11,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
}
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "all",
"tabWidth": 4,
"semi": true,
"printWidth": 120,
"singleQuote": true
}
75 changes: 75 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import _ from 'lodash';
import fs from 'fs';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const axios = require('axios').default;

// the location of the markdown file
const url =
'https://raw.githubusercontent.com/colemujadzic/azure-docs/master/articles/active-directory/users-groups-roles/licensing-service-plan-reference.md';

// a Microsoft online service product
interface Product {
productName: string;
stringID: string;
guid: string;
servicePlansIncluded: string | string[];
servicePlansIncludedFriendlyNames: string | string[];
}

// fetchMarkdown() makes a GET request to fetch the contents of the markdown file
const fetchMarkdown = () => {
const options = {
method: 'get',
url: url,
};
// TODO: figure out a type here!
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return axios(options).then((response: any) => {
return Promise.resolve(response.data);
});
};

// parseArray() takes an array of type string and parses out objects of type Product into a single array of objects
const parseArray = (arr: Array<string>) => {
const products: Product[] = [];
_.some(arr, (l: string, index: number) => {
const productString = _.trim(l, '|').split('|');
products.push({
productName: productString[0].trim(),
stringID: productString[1].trim(),
guid: productString[2].trim(),
servicePlansIncluded:
productString[3].indexOf('<br/>') > -1
? productString[3].trim().split('<br/>')
: productString[3].trim(),
servicePlansIncludedFriendlyNames:
productString[4].indexOf('<br/>') > -1
? productString[4].trim().split('<br/>')
: productString[4].trim(),
});
return arr[index + 1] === '';
});
return products;
};

// writeToFile() writes the resultant Product array to a file
const writeToFile = (arr: Product[]) => {
return fs.writeFileSync('./data.json', JSON.stringify(arr, null, 2), 'utf-8');
};

const main = async () => {
try {
const response = await fetchMarkdown();
const lines = response.split('\n');
const index = _.findIndex(lines, (l: string) => {
return _.startsWith(l, '|');
});
const splitLines = lines.splice(index + 2);
const parsed = parseArray(splitLines);
writeToFile(parsed);
} catch (error) {
console.log(error);
}
};

main();
24 changes: 24 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
],
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"allowJs": true,
"outDir": "./dist",
"rootDir": "./src",
"pretty": true,
"esModuleInterop": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noImplicitThis": true,
}
}

0 comments on commit 73c3625

Please sign in to comment.