UNPKG

@sap/dwf-generator

Version:

SAP HANA Data Warehousing Foundation - Generator

119 lines (112 loc) 3.89 kB
'use strict'; const coFs = require('fs'); const coPath = require('path'); const coAsync = require('async'); const coGenerator = require('./generator'); const cfnGenerateFolders = require('./generate_folders'); const coDefaultFolders = require('./const').defaultFolders; var gaTasks = []; /** * Starting point of generation * * @param {string} isDWFPath * @param {string[]} iaRelativePaths */ function lfnStart(isDWFPath, isRelativePaths) { // find path of HDB module let lsHDBPath; try { lsHDBPath = coPath.resolve(isDWFPath, require(coPath.resolve(isDWFPath, 'package.json'))['dwfConfig']['hdb-module']['path']); } catch (ioErr) { throw new Error('Path of hdb-module cannot be found. \n' + 'Make sure that the path of hdb-module is defined inside package.json of the dwf-module.'); } // check the default folders src, cfg and the default file .config // if they don't exist, create them cfnGenerateFolders.checkDefaultFolders(lsHDBPath, coDefaultFolders); console.log('Generating files...'); if (isRelativePaths) { let laRelativePaths = isRelativePaths.split(','); _checkProfiles(isDWFPath, lsHDBPath, laRelativePaths); } else { _scanProfiles(isDWFPath, lsHDBPath, ''); } coAsync.parallel(gaTasks, function (ioErr) { if (ioErr) { console.error(ioErr); process.exit(1); } else { process.exit(0); } }); } /** * look into a DWF folder and search for DLM profiles inclusive subfolders * * @param {string} isSourcePath - where dlm profiles are stored * @param {string} isHDBPath - where generated artefacts will be stored * @param {string} isDiff - relative path from module root folder to artefacts folder */ function _scanProfiles(isDWFPath, isHDBPath, isDiff) { // dlm profiles are stored in the src folder let lsPathToBeCheck = coPath.join(isDWFPath, 'src', isDiff); try { let loStats = coFs.lstatSync(lsPathToBeCheck); if (typeof loStats == 'undefined') { console.error(new Error('Invalid path of DWF Module!')); process.exit(1); } if (loStats.isDirectory()) { // it is a folder let loFiles = coFs.readdirSync(lsPathToBeCheck); for (let i in loFiles) { _scanProfiles(isDWFPath, isHDBPath, coPath.join(isDiff, loFiles[i])); } } else { // it is a single file // check file extension, ignore all other file extensions if (coPath.extname(lsPathToBeCheck) == '.dwfdlmprofile') { let lsRelativePath = isDiff.replace(coPath.extname(isDiff), ''); let loPaths = { hdb: isHDBPath, dwf: isDWFPath, relativeToRoot: lsRelativePath }; let loGenerator = new coGenerator(loPaths); let lfnTask = coAsync.asyncify(loGenerator.generateFiles.bind(loGenerator)); gaTasks.push(lfnTask); } } } catch (ioErr) { console.error(ioErr); process.exit(1); } } /** * Generate artefacts by given relative path of profiles * * @param {string} isSourcePath - where dlm profiles are stored * @param {string} isHDBPath - where generated artefacts will be stored * @param {string[]} iaRelativePaths - relative path to from isSourcePath to dlm profile */ function _checkProfiles(isDWFPath, isHDBPath, iaRelativePaths) { for (let i in iaRelativePaths) { if (coPath.extname(iaRelativePaths[i]) == '.dwfdlmprofile') { let lsRelativePath = iaRelativePaths[i].replace(coPath.extname(iaRelativePaths[i]), ''); let loPaths = { hdb: isHDBPath, dwf: isDWFPath, relativeToRoot: lsRelativePath }; let loGenerator = new coGenerator(loPaths); let lfnTask = coAsync.asyncify(loGenerator.generateFiles.bind(loGenerator)); gaTasks.push(lfnTask); } else { console.error(`"${iaRelativePaths[i]}" is not a DML profile`); } } } module.exports = { start: lfnStart };