Skip to content

Commit

Permalink
refactor build scripts helpers and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Nov 2, 2022
1 parent d2b9c93 commit f0e919f
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 202 deletions.
78 changes: 48 additions & 30 deletions scripts/build-compatibility-table.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
const path = require('path');
const fs = require('fs');
const os = require('os');
const { EOL } = require('os');

const {
REMOVED_MARKER,
COMPATIBILITY_TABLE_DATA_PATH,
WIKI_COMPATIBILITY_TABLE_PATH,
WIKI_DIR_PATH,
} = require('./constants');

const COMPATIBILITY_TABLE_INPUT_FILENAME = 'compatibility-table.json';
const COMPATIBILITY_TABLE_OUTPUT_FILENAME = 'compatibility-table.md';

/**
* Path to **input** compatibility data source json
*/
const COMPATIBILITY_TABLE_DATA_PATH = path.resolve(__dirname, COMPATIBILITY_TABLE_INPUT_FILENAME);

/**
* Path to **output** wiki compatibility table file
*/
const WIKI_COMPATIBILITY_TABLE_PATH = path.resolve(
__dirname,
WIKI_DIR_PATH,
COMPATIBILITY_TABLE_OUTPUT_FILENAME,
);

/**
* Returns data for compatibility tables
* Returns data for compatibility table
*/
function getTableData() {
const getTableData = () => {
const rawData = fs.readFileSync(COMPATIBILITY_TABLE_DATA_PATH);
const parsed = JSON.parse(rawData);
return parsed;
}
return JSON.parse(rawData);
};

// FIXME: update jsdoc - id arg is missed
/**
* Returns markdown row of compatibility table
* @param {{
Expand All @@ -28,53 +46,53 @@ const getRow = (id, item) => {
if (item.adg) {
adgCell = item.adg.includes(REMOVED_MARKER)
? item.adg
: `[${item.adg}](../wiki/about-${id}.md#${item.adg})`;
: `[${item.adg}](${WIKI_DIR_PATH}/about-${id}.md#${item.adg})`;
}

return `| ${adgCell} | ${item.ubo || ''} | ${item.abp || ''} |${os.EOL}`;
return `| ${adgCell} | ${item.ubo || ''} | ${item.abp || ''} |${EOL}`;
};

/**
* Generates table header
*/
const getTableHeader = () => {
let res = `| AdGuard | uBO | Adblock Plus |${os.EOL}`;
res += `|---|---|---|${os.EOL}`;
let res = `| AdGuard | uBO | Adblock Plus |${EOL}`;
res += `|---|---|---|${EOL}`;
return res;
};

// FIXME: add missed args, add @returns, fix Array type
/**
* Builds markdown string with scriptlets compatibility table
* @param {Array} data array with scriptlets names
* Builds markdown string of scriptlets/redirect compatibility table
* @param {Array} data array of scriptlets/redirect names
*/
function buildTable(title, data = [], id = '') {
const buildTable = (title, data = [], id = '') => {
// title
let res = `# <a id="${id}"></a> ${title}${os.EOL}${os.EOL}`;
let res = `# <a id="${id}"></a> ${title}${EOL}${EOL}`;
// header
res += getTableHeader();
// rows
res += data
.map((item) => {
const row = getRow(id, item);
return row;
})
.map((item) => getRow(id, item))
.join('');

return res;
}
};

/**
* Save tables to compatibility table
* Saves tables to compatibility table
*
* @param {string[]} args
*/
function saveTables(...args) {
const res = args.join(`${os.EOL}${os.EOL}`);
const saveTables = (...args) => {
const res = args.join(`${EOL}${EOL}`);
fs.writeFileSync(WIKI_COMPATIBILITY_TABLE_PATH, res);
}
};

/**
* Entry function
* Builds full compatibility table
*/
function buildTables() {
const buildCompatibilityTable = () => {
const { scriptlets, redirects } = getTableData();

const scriptletsTable = buildTable(
Expand All @@ -89,10 +107,10 @@ function buildTables() {
);

saveTables(scriptletsTable, redirectsTable);
}
};

buildTables();
buildCompatibilityTable();

module.exports = {
buildTables,
buildCompatibilityTable,
};
Loading

0 comments on commit f0e919f

Please sign in to comment.