Skip to content

Commit

Permalink
perf(extraction): extract functions and insert into library utils #36
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanjones243 committed Aug 24, 2023
1 parent fa13d43 commit 57b7162
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
42 changes: 8 additions & 34 deletions scripts/build/generateDocs.js → scripts/build/generateDocs.mjs
Original file line number Diff line number Diff line change
@@ -1,47 +1,21 @@
/* eslint-disable */
import markdownMagic from 'markdown-magic';
import * as fs from 'fs';
import * as https from 'https';

const markdownMagic = require('markdown-magic');
const fs = require('fs');
const https = require('https');
import AuroLibraryUtils from "../utils/auroLibraryUtils.mjs";

const auroLibraryUtils = new AuroLibraryUtils();

const readmeTemplateUrl = 'https://raw.githubusercontent.com/AlaskaAirlines/WC-Generator/master/componentDocs/README.md';
const dirDocTemplates = './docTemplates';
const readmeFilePath = dirDocTemplates + '/README.md';

/**
* Extract NPM, NAMESPACE and NAME from package.json.
*/

function nameExtraction() {
const packageJson = fs.readFileSync('package.json', 'utf8', function(err) {
if (err) {
console.log('ERROR: Unable to read package.json file', err);
}
});

const pName = JSON.parse(packageJson).name;

let npmStart = pName.indexOf('@');
let namespaceStart = pName.indexOf('/');
let nameStart = pName.indexOf('-');

let result = {
'npm': pName.substring(npmStart, namespaceStart),
'namespace': pName.substring(namespaceStart + 1, nameStart),
'namespaceCap': pName.substring(namespaceStart + 1)[0].toUpperCase() + pName.substring(namespaceStart + 2, nameStart),
'name': pName.substring(nameStart + 1),
'nameCap': pName.substring(nameStart + 1)[0].toUpperCase() + pName.substring(nameStart + 2)
};

return result;
};

/**
* Replace all instances of [npm], [name], [Name], [namespace] and [Namespace] accordingly
*/

function formatTemplateFileContents(content, destination) {
let nameExtractionData = nameExtraction();
let nameExtractionData = auroLibraryUtils.nameExtraction();
let result = content;

/**
Expand Down Expand Up @@ -69,7 +43,7 @@ function formatTemplateFileContents(content, destination) {
};

function formatApiTableContents(content, destination) {
const nameExtractionData = nameExtraction();
const nameExtractionData = auroLibraryUtils.nameExtraction();
const wcName = nameExtractionData.namespace + '-' + nameExtractionData.name;

let result = content;
Expand Down
28 changes: 28 additions & 0 deletions scripts/utils/auroLibraryUtils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,33 @@ export default class AuroLibraryUtils {
}
}
}

/**
* Extracts NPM, NAMESPACE, NAME, NPM VERSION and BRANCH NAME from package.json.
* @returns {Object} result - Object containing data from package.json.
*/
nameExtraction() {
const packageJson = fs.readFileSync('package.json', 'utf8', function(err) {
if (err) {
console.log('ERROR: Unable to read package.json file', err);
}
});

const pName = JSON.parse(packageJson).name;

let npmStart = pName.indexOf('@');
let namespaceStart = pName.indexOf('/');
let nameStart = pName.indexOf('-');

let result = {
'npm': pName.substring(npmStart, namespaceStart),
'namespace': pName.substring(namespaceStart + 1, nameStart),
'namespaceCap': pName.substring(namespaceStart + 1)[0].toUpperCase() + pName.substring(namespaceStart + 2, nameStart),
'name': pName.substring(nameStart + 1),
'nameCap': pName.substring(nameStart + 1)[0].toUpperCase() + pName.substring(nameStart + 2)
};

return result;
};
}

0 comments on commit 57b7162

Please sign in to comment.