UNPKG

@tangelo/tangelo-configuration-toolkit

Version:

Tangelo Configuration Toolkit is a command-line toolkit which offers support for developing a Tangelo configuration.

109 lines (91 loc) 3.96 kB
const {execSync} = require('child_process'); const fs = require('fs-extra'); const globby = require('globby'); const inquirer = require('inquirer'); const runSqlScript = (path, db, un, pw) => { const connect = db ? `${un}/${pw}@${db}` : '/nolog'; const [dir, script] = path.split(/\/([^/]+)$/); _info('Running in new window:'); _write(path); if (!fs.existsSync(path)) _error('Path does not exist!'); execSync(`start sqlplus ${connect} @${script}`, {cwd: dir}); _write('Done.\n'); }; const checkLog = (logFilePath, remove=true) => { if (fs.existsSync(logFilePath)) { const logFile = fs.readFileSync(logFilePath).toString(); // Check for ORA- error messages if (logFile.match(/ORA-/g)) { // Select each ORA- error message and up to 6 lines before (the lines before may not contain ORA- themselves) const errors = logFile.match(/(?:(?![^\n]*ORA-)[^\n]*\n){0,6}ORA-[^\n\r]*/gms); _info(`${errors.length} error(s) during SQL script:\n`); // Print each error + lines before: errors.forEach((e, i) => { _info(`Tail of error ${i+1}:\n`); _warn(`${e.trim()}\n`); }); } if (remove) { // remove log file fs.removeSync(logFilePath); } } }; module.exports = function sql (argv) { if ((argv.install || argv.configure) && _git.commitLocal().date < _git.commitRemote().date) { _warn(`You're not deploying from the most recent git commit!\n`); } if (argv.install) { // tdi const dir = `${_paths.tdi}/${_isPre51() ? 'sources' : 'src'}/database/tdi/${_isPre42() ? 'install/' : ''}`; runSqlScript(dir + 'install.sql'); checkLog(dir + 'tdi-install.log', false); // custom runSqlScript('database/install.sql'); checkLog('database/install.log', false); } if (argv.configure) { if (_repoconfig.projects && _repoconfig.projects[0] || _repoconfig[0]) { const cwd = 'database/config/'; const file = cwd + 'tmp-config-loader.sql'; inquirer .prompt([ { message: 'Choose projects: ', name: 'projects', type: 'checkbox', validate: v => !!v[0], choices: _repoconfig.map(p => ({name: `${p.customer_name} ${p.project_name}`, value: p})) }, {message: 'Database TNS name: ', name: 'db', default: _appconfig.defaultDatabase}, {message: 'TANCMS password: ', name: 'pw', default: 'tancms'} ]) .then(a => { _write(); const script = [`define l_env = ${a.pw=='tancms' ? 'dev' : 'prd'}`, 'set verify off define off']; script.push(`spool install-config.log`); a.projects.forEach(p => { const dir = p.path_dbconfig ? p.path_dbconfig.join('/') : (_repoconfig.customer.dirname+'/'+p.dirname); script.push('prompt ', `prompt Loading configuration for: ${p.name}`); script.push(...globby.sync(`${dir}/*_install.sql`, {cwd}).map(s => `@${s}`)); }); script.push(`spool off`); script.push('prompt ', 'pause Press ENTER to exit', 'exit'); fs.writeFileSync(file, script.join('\n')); runSqlScript(file, a.db, 'tancms', a.pw); fs.removeSync(file); checkLog(cwd + 'install-config.log'); }); } else _error('No projects defined in repo config!'); } if (argv.generate) { const dir = _paths.tdi + (_isPre51() ? '/util/db-config-generator/' : '/tct/sql/generate/'); fs.ensureSymlinkSync(_paths.apply, dir + 'dist', 'junction'); runSqlScript(dir + 'start.sql'); fs.removeSync(dir + 'dist'); checkLog(dir + 'generate-config.log'); } if (argv.remove) { const dir = _paths.tdi + (_isPre51() ? '/util/db-config-remover/' : '/tct/sql/remove/'); runSqlScript(dir + 'start.sql'); checkLog(dir + 'remove-config.log'); } };