@tangelo/tangelo-configuration-toolkit
Version:
Tangelo Configuration Toolkit is a command-line toolkit which offers support for developing a Tangelo configuration.
202 lines (172 loc) • 6.74 kB
JavaScript
const fs = require('fs-extra');
const globby = require('globby');
const path = require('path');
const {Table} = require('console-table-printer');
const execGitCommand = require('../../lib/exec-git-command');
const getTdiBranch = require('../../lib/get-tdi-branch');
const c = require('../deploy/config');
const RemoteExec = require('../../lib/remote-exec');
const getGitInfo = () => {
// Version info TDI submodule
const gitSubmoduleInfo = new Table({
columns: [
{name: 'property', title: 'TDI - submodule', alignment: 'left'},
{name: 'value', alignment: 'left'}
],
});
// Fetch all
const cmdFetch = execGitCommand('fetch -pf --all', path.join(_paths.repo, _paths.tdi));
if (cmdFetch.error) _warn(`Fetch failed\n${cmdFetch.error}`);
// Set branch name of firstBranch without 'remotes/origin/'
const tdiBranch = getTdiBranch();
// Get number of commits behind
tdiBranch.commitsBehind = execGitCommand(`rev-list HEAD...origin/${tdiBranch.name} --count`, path.join(_paths.repo, _paths.tdi));
// Create table rows for TDI submodule info
gitSubmoduleInfo.addRow({
property: 'Commit date',
value: _formatDate(_git.commitTdi.local().date)
});
if (tdiBranch) {
gitSubmoduleInfo.addRow({
property: 'Branch',
value: tdiBranch.name
});
gitSubmoduleInfo.addRow({
property: 'Commits behind',
value: tdiBranch.commitsBehind
});
} else {
gitSubmoduleInfo.addRow({
property: 'Branch could not be determined',
value: ''
});
}
// Print TDI submodule info
gitSubmoduleInfo.printTable();
};
const getFileExtractInfo = (sorting) => {
// version info miscellaneous
const projects = new Set;
const types = new Set;
const versionInfo = new Table({
columns: [
{name: 'path', alignment: 'left'},
{name: 'type', alignment: 'left'},
{name: 'version', alignment: 'left'},
{name: 'sort'}
],
disabledColumns: ['sort'],
sort: (a, b) => a.sort.toLowerCase() > b.sort.toLowerCase() ? 1 : -1
});
const versionInfoConfig = _modulesTdi.require('version/versionInfo.js');
if (versionInfoConfig) {
versionInfoConfig.forEach(v => {
const location = path.join(_paths.repo, v.glob);
globby
.sync(location)
.forEach(f => {
const filePathExtract = f.match(/.*(?<path>(cmscustom|site-stylesheets)\/(?<customer>[^/]*)\/(?<project>[^/]*)\/.*)/);
const path = filePathExtract.groups.path || '';
const project = filePathExtract.groups.project || '';
const fileContent = fs.readFileSync(f).toString();
v.extracts.forEach(e => {
const extract = fileContent.match(e.regex);
if (extract) {
projects.add(project); // Store the projects where versioninfo is found
types.add(e.type); // Store the types for which versioninfo is found
versionInfo.addRow({ // Create row with version information to output
path,
sort: `${sorting=='project' ? project : e.type}_2${sorting=='project' ? e.type : extract.groups.version}`, // Output is sorted on project or type: '_2' ensures it is rendered after the empty row and the row with the project name
type: e.type,
version: extract.groups.version
});
}
});
});
});
if (sorting=='project') {
// For projects containing version information
projects.forEach(p => {
versionInfo.addRow({ // Add empty row after project
path: '',
sort: `${p}_3`,
type: '',
version: ''
});
versionInfo.addRow({ // Add row with project name
path: `-- ${p}:`,
sort: `${p}_1`,
type: '',
version: ''
}, {
color: 'yellow'
});
});
}
else if (sorting=='type') {
types.forEach(t => {
versionInfo.addRow({ // Add empty row after type
path: '',
sort: `${t}_3`,
type: '',
version: ''
});
});
}
versionInfo.printTable();
}
else {
_warn('Version info of miscellaneous items cannot be extracted:\nCannot find required files in TDI submodule. Try updating TDI submodule.');
}
};
const getServerInfo = (server) => {
// Remote server info
// common setup
_write();
c.setServer(server);
if (!c.envDev) {
_info(`Remote version info for '${c.server.ftpConfig.host}':\n`);
new RemoteExec(c.server.ftpConfig).add('sudo ~root/scripts/version.sh', 'STDOUT').process();
}
else {
_info('For development environments no server version information is available. Check rancher / database for this information.\nAdd the --server option with a non-dev environment to see version information for that server.');
}
};
module.exports = function info (argv) {
if (argv.doctypes) {
_info('Document type information for this git repository\n');
const doctypesInfo = new Table({
columns: [
{name: 'id', alignment: 'right'},
{name: 'name', alignment: 'left'},
{name: 'paths', alignment: 'left'}
],
});
globby
.sync(_paths.repo + '/database/config/**/txd_document_types.sql')
.forEach((p, i, a) => {
fs.readFileSync(p).toString().match(/select([\s\S]+?)from\s+dual/gmi)
.forEach((dtRow, i, a) => {
const ntSqlInsert = fs.readFileSync(p.replace('txd_document_types', 'txd_node_types')).toString().match(/select(.*?)from\s+dual/s)[1];
const id = dtRow.match(/(\d+) id/)?.[1];
const name = dtRow.match(/'([^']+)' display_name/)?.[1];
const dbPath = p.match(/(database\/config\/(:?.*)\/)txd_document_types.sql/i)?.[1];
const xincl = ntSqlInsert.match(/'([^']+)' xsl_prep_inc/)?.[1] ?? dtRow.match(/'([^']+)' xsl_xincludes/)[1];
const prPath = xincl.replace('prepare_xincludes.xsl', '');
doctypesInfo.addRows([
{id, name, paths: 'config/cmscustom/'+ prPath},
{paths: dbPath}
]);
if (i!==a.length-1) doctypesInfo.addRow({});
});
if (i!==a.length-1) doctypesInfo.addRow({});
});
doctypesInfo.printTable();
}
if (argv.versions) {
_info('Version information for this git repository\n');
getGitInfo();
getFileExtractInfo(argv.versions);
getServerInfo(argv.server);
}
};