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 31, 2023
1 parent 9746820 commit c3018eb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 66 deletions.
77 changes: 11 additions & 66 deletions scripts/build/generateDocs.js → scripts/build/generateDocs.mjs
Original file line number Diff line number Diff line change
@@ -1,75 +1,20 @@
/* 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 result = content;

/**
* Replace placeholder strings
*/
result = result.replace(/\[npm]/g, nameExtractionData.npm);
result = result.replace(/\[name](?!\()/g, nameExtractionData.name);
result = result.replace(/\[Name](?!\()/g, nameExtractionData.nameCap);
result = result.replace(/\[namespace]/g, nameExtractionData.namespace);
result = result.replace(/\[Namespace]/g, nameExtractionData.namespaceCap);

/**
* Cleanup line breaks
*/
result = result.replace(/(\r\n|\r|\n)[\s]+(\r\n|\r|\n)/g, '\r\n\r\n'); // Replace lines containing only whitespace with a carriage return.
result = result.replace(/>(\r\n|\r|\n){2,}/g, '>\r\n'); // Remove empty lines directly after a closing html tag.
result = result.replace(/>(\r\n|\r|\n)```/g, '>\r\n\r\n```'); // Ensure an empty line before code samples.
result = result.replace(/>(\r\n|\r|\n){2,}```(\r\n|\r|\n)/g, '>\r\n```\r\n'); // Ensure no empty lines before close of code sample.
result = result.replace(/([^(\r\n|\r|\n)])(\r\n|\r|\n)+#/g, "$1\r\n\r\n#"); // Ensure empty line before header sections.

/**
* Write the result to the destination file
* Replace all instances of [npm], [name], [Name], [namespace] and [Namespace] accordingly
*/
fs.writeFileSync(destination, result, { encoding: 'utf8'});
};

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

let result = content;
Expand All @@ -82,7 +27,7 @@ function formatApiTableContents(content, destination) {
fs.writeFileSync(destination, result, { encoding: 'utf8'});

fs.readFile('./demo/api.md', 'utf8', function(err, data) {
formatTemplateFileContents(data, './demo/api.md');
auroLibraryUtils.formatFileContents(data, './demo/api.md');
});
}

Expand All @@ -95,7 +40,7 @@ function processReadme() {

if (fs.existsSync('./README.md')) {
fs.readFile('./README.md', 'utf8', function(err, data) {
formatTemplateFileContents(data, './README.md');
auroLibraryUtils.formatFileContents(data, './README.md');
});
} else {
console.log('ERROR: ./README.md file is missing');
Expand All @@ -120,7 +65,7 @@ function processDemo() {
const callback = function() {
if (fs.existsSync('./demo/demo.md')) {
fs.readFile('./demo/demo.md', 'utf8', function(err, data) {
formatTemplateFileContents(data, './demo/demo.md');
auroLibraryUtils.formatFileContents(data, './demo/demo.md');
});
} else {
console.log('ERROR: ./demo/demo.md file is missing');
Expand Down
59 changes: 59 additions & 0 deletions scripts/utils/auroLibraryUtils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,64 @@ 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;
}

/**
* Replace all instances of [abstractNodeVersion], [branchName], [npm], [name], [Name], [namespace] and [Namespace] accordingly
*/
formatFileContents(content, destination) {
let nameExtractionData = this.nameExtraction();
let result = content;

/**
* Replace placeholder strings
*/
result = result.replace(/\[npm]/g, nameExtractionData.npm);
result = result.replace(/\[name](?!\()/g, nameExtractionData.name);
result = result.replace(/\[Name](?!\()/g, nameExtractionData.nameCap);
result = result.replace(/\[namespace]/g, nameExtractionData.namespace);
result = result.replace(/\[Namespace]/g, nameExtractionData.namespaceCap);

/**
* Cleanup line breaks
*/
result = result.replace(/(\r\n|\r|\n)[\s]+(\r\n|\r|\n)/g, '\r\n\r\n'); // Replace lines containing only whitespace with a carriage return.
result = result.replace(/>(\r\n|\r|\n){2,}/g, '>\r\n'); // Remove empty lines directly after a closing html tag.
result = result.replace(/>(\r\n|\r|\n)```/g, '>\r\n\r\n```'); // Ensure an empty line before code samples.
result = result.replace(/>(\r\n|\r|\n){2,}```(\r\n|\r|\n)/g, '>\r\n```\r\n'); // Ensure no empty lines before close of code sample.
result = result.replace(/([^(\r\n|\r|\n)])(\r\n|\r|\n)+#/g, "$1\r\n\r\n#"); // Ensure empty line before header sections.

/**
* Write the result to the destination file
*/
fs.writeFileSync(destination, result, { encoding: 'utf8'});
}
}

0 comments on commit c3018eb

Please sign in to comment.