mirror of
https://gitlab.silvrtree.co.uk/martind2000/ft.git
synced 2025-02-10 08:09:16 +00:00
232 lines
5.9 KiB
JavaScript
232 lines
5.9 KiB
JavaScript
/**
|
|
* Created by mdonnel on 11/05/2017.
|
|
*/
|
|
|
|
const meow = require('meow');
|
|
const util = require('util');
|
|
const jsonfile = require('jsonfile');
|
|
const fs = require('fs');
|
|
const glob = require('glob');
|
|
const chalk = require('chalk');
|
|
const path = require('path');
|
|
const _ = require('lodash');
|
|
|
|
const cli = meow({
|
|
help: `
|
|
Usage
|
|
node getLabels.js -p <path> <options>
|
|
|
|
Path
|
|
Where to check for html files. Only looks at .html files.
|
|
|
|
Outputs
|
|
Outputs a labels.json file
|
|
|
|
Options
|
|
-p, --path Path to search for HTMl files
|
|
-r, --replace Replace label['id'] with getLabel('id')
|
|
-l, --label Path to search for JSON files
|
|
|
|
Examples
|
|
# Check all the html files in a given folder
|
|
|
|
node getLabels.js -p C:\\dev\\web-platform\\src\\components\\products
|
|
|
|
# Check all the html files in a given folder then replace all old label
|
|
references with the new method
|
|
|
|
node getLabels.js -p C:\\dev\\web-platform\\src\\components\\products --replace
|
|
`},
|
|
{
|
|
alias: {
|
|
p: 'path',
|
|
l: 'labels',
|
|
r: 'replace'
|
|
},
|
|
default: {
|
|
path: null,
|
|
labels: null,
|
|
replace: false
|
|
},
|
|
boolean: [ 'replace'],
|
|
string: ['path', 'labels']
|
|
});
|
|
|
|
|
|
const globOptions = {};
|
|
const options = {
|
|
path: cli.flags.path,
|
|
replace: cli.flags.replace,
|
|
labels: cli.flags.labels
|
|
};
|
|
|
|
const labelsPattern = /label\[\s?\'(\S+)\'\s?\]/g;
|
|
const getLabelsPattern = /getLabel\(\s?\'(\S+)\'\s?\)/g;
|
|
const fileArray = './labels-array.json';
|
|
const fileObject = './labels-object.json';
|
|
|
|
const log = function() {
|
|
process.stdout.write(util.format.apply(this, arguments) + '\n');
|
|
};
|
|
|
|
function consolidateLabels(a) {
|
|
const [labelList, labelObj] = a;
|
|
const newLabels = {};
|
|
return new Promise(function(resolve, reject) {
|
|
if (typeof labelObj === 'undefined') {
|
|
return resolve();
|
|
}
|
|
log('Consolidating labels...');
|
|
|
|
for (const listItem of labelList) {
|
|
const newEntry = [];
|
|
labelObj.forEach((obj) => {
|
|
if (Object.keys(obj).indexOf(listItem) !== -1) {
|
|
const tempStr = obj[listItem];
|
|
if (newEntry.indexOf(tempStr) === -1) {
|
|
newEntry.push(tempStr);
|
|
}
|
|
}
|
|
newLabels[listItem] = (newEntry.length === 1) ? newEntry[0] : newEntry;
|
|
});
|
|
}
|
|
return resolve(newLabels);
|
|
});
|
|
}
|
|
|
|
function getLabels(rawLabels) {
|
|
const newLabels = {};
|
|
const lSource = _.get(rawLabels, 'provider.labels.label');
|
|
|
|
|
|
if (typeof lSource !== 'undefined') {
|
|
for (const item of lSource) {
|
|
const key = _.get(item, 'key');
|
|
newLabels[`${key.toString()}`] = _.get(item, 'value');
|
|
}
|
|
}
|
|
|
|
return newLabels;
|
|
}
|
|
function getAllLabels(labelPath, d) {
|
|
const searchPath = `${labelPath}\\**\\*.json`;
|
|
const found = [];
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
if (labelPath === null) {
|
|
return resolve [d];
|
|
}
|
|
log(`Looking for label files: ${labelPath}`);
|
|
glob(searchPath, globOptions, function(er, files) {
|
|
files.forEach(function(element) {
|
|
if (path.parse(element).name.indexOf('label') !== -1) {
|
|
log(element);
|
|
const labelfile = jsonfile.readFileSync(element);
|
|
const labeldata = getLabels(labelfile);
|
|
found.push(labeldata);
|
|
}
|
|
});
|
|
|
|
return resolve([d, found]);
|
|
});
|
|
});
|
|
}
|
|
|
|
function processFiles(srcPath, doReplace = false) {
|
|
const searchPath = `${srcPath}\\**\\*.html`;
|
|
|
|
log(`Searching: ${searchPath}`);
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
let found = [];
|
|
glob(searchPath, globOptions, function(er, files) {
|
|
files.forEach(function(element) {
|
|
try {
|
|
const openedFile = fs.readFileSync(element, 'utf8');
|
|
let matched;
|
|
|
|
// find plain labels
|
|
matched = labelsPattern.exec(openedFile);
|
|
while (matched) {
|
|
const str = matched[1];
|
|
if (found.indexOf(str) === -1) {
|
|
found.push(str);
|
|
}
|
|
matched = labelsPattern.exec(openedFile);
|
|
}
|
|
|
|
// find new getLabels
|
|
matched = getLabelsPattern.exec(openedFile);
|
|
while (matched) {
|
|
const str = matched[1];
|
|
if (found.indexOf(str) === -1) {
|
|
found.push(str);
|
|
}
|
|
matched = getLabelsPattern.exec(openedFile);
|
|
}
|
|
|
|
if (doReplace && labelsPattern.test(openedFile)) {
|
|
log(`Updating: ${chalk.cyan(truncatePath(element, 4))}`);
|
|
const newFile = openedFile.replace(labelsPattern, 'getLabel(\'$1\')');
|
|
fs.writeFile(element, newFile);
|
|
}
|
|
} catch (e) {
|
|
return reject(e);
|
|
}
|
|
});
|
|
|
|
found = found.sort(function(a, b) {
|
|
return a.toLowerCase().localeCompare(b.toLowerCase());
|
|
});
|
|
return resolve(found);
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
function truncatePath(pathStr, parts) {
|
|
const oldBits = pathStr.split('/');
|
|
return ['...'].concat(oldBits.slice(oldBits.length - (parts - 1))).join('/');
|
|
}
|
|
function saveJSON(data) {
|
|
log(chalk.yellow('Saving...'));
|
|
jsonfile.writeFileSync(fileArray, data);
|
|
return data;
|
|
}
|
|
|
|
function saveJSONObject(data) {
|
|
log(chalk.yellow('Saving...'));
|
|
jsonfile.writeFileSync(fileObject, data);
|
|
return data;
|
|
}
|
|
|
|
function processLabels(opt) {
|
|
log('GetLabels');
|
|
if (opt.path !== null) {
|
|
log(chalk.magenta('Working...'));
|
|
processFiles(opt.path, opt.replace)
|
|
.then((f) => {
|
|
return saveJSON(f);
|
|
})
|
|
.then(function(d) {
|
|
return getAllLabels(opt.labels, d);
|
|
})
|
|
.then((d) => {
|
|
return consolidateLabels(d);
|
|
})
|
|
.then((f) => {
|
|
return saveJSONObject(f);
|
|
})
|
|
.then(() => {
|
|
log(chalk.green('Done.'));
|
|
}).catch((e) => {
|
|
log(chalk.red(e));
|
|
});
|
|
} else {
|
|
log('Nothing to do. Try adding a path. Or use --help for help.');
|
|
}
|
|
}
|
|
|
|
processLabels(options);
|
|
|