UNPKG

@sap/dwf-generator

Version:

SAP HANA Data Warehousing Foundation - Generator

100 lines (93 loc) 2.66 kB
'use strict'; /** * check whether the default folders src and cfg already exists, * if not, create new folders */ const coFs = require('fs'); const coPath = require('path'); const coCommon = require('./generate/common'); const csRootNamespace = require('./const').rootNamespace; const coLogMsg = require('./const').logMsg; function lfnCheckDefaultFolders(isPath, isDefaultFolders) { lfnCheckFolder(coPath.join(isPath, isDefaultFolders.src)); lfnCheckFolder(coPath.join(isPath, isDefaultFolders.cfg)); lfnCheckDefaultFile({ path: coPath.join(isPath, isDefaultFolders.src), fileName: '.hdiconfig', template: 'hdiconfig' }); lfnCheckDefaultFile({ path: coPath.join(isPath, isDefaultFolders.cfg), fileName: '.hdiconfig', template: 'hdiconfig' }); lfnCheckDefaultFile({ path: coPath.join(isPath, isDefaultFolders.src), fileName: '.hdinamespace', template: 'namespace', value: { namespace: csRootNamespace, subfolder: 'append' } }); lfnCheckDefaultFile({ path: coPath.join(isPath, isDefaultFolders.src), fileName: `${coLogMsg.logContext}.hdbcds`, template: 'logTable', value: { namespace: csRootNamespace, logTableName: coLogMsg.logTable, context: coLogMsg.logContext } }); lfnCheckDefaultFile({ path: coPath.join( isPath, isDefaultFolders.src ), fileName: 'TABLE_TYPE_LOG.hdbtabletype', template: 'msgTable', value: { namespace: csRootNamespace, tableTypeName: coLogMsg.tableType, context: coLogMsg.logContext } }); } function lfnCheckFolder(isPath, ibNotOverwrite) { // check parent folder let lsParentPath = coPath.join(isPath, '..'); try { coFs.accessSync(lsParentPath, coFs.F_OK); } catch (ioErr) { lfnCheckFolder(lsParentPath); } // check current folder try { coFs.mkdirSync(isPath); // create a folder } catch (ioErr) { if (ibNotOverwrite && ioErr.code == 'EEXIST') { console.warn(ioErr); return true; } if (ioErr.code != 'EEXIST') { console.error(ioErr); } } } function lfnCheckDefaultFile(ioObject) { let lsFullPath = coPath.join(ioObject.path, ioObject.fileName); try { coFs.accessSync(lsFullPath, coFs.F_OK); } catch (ioErr) { // file doesn't exist let lsHdiconfigContent = coCommon.replace(ioObject.template, ioObject.value); try { coFs.writeFileSync(lsFullPath, lsHdiconfigContent); } catch (ioErr) { console.error(`Fail to create "${lsFullPath}"`); process.exit(1); } } } module.exports = { checkDefaultFolders: lfnCheckDefaultFolders, checkFolder: lfnCheckFolder };