From e5e896d9f3a2c88777797e96b16be3ff438463d5 Mon Sep 17 00:00:00 2001 From: Evgeny Metelkin Date: Wed, 22 Apr 2020 12:35:24 +0300 Subject: [PATCH] check index file before building --- bin/heta-build.js | 53 ++++++++++++++++-------------- cases/0-hello-world/src/index.heta | 4 +-- src/builder/index.js | 9 ++++- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/bin/heta-build.js b/bin/heta-build.js index fde5c247..e8722fb8 100644 --- a/bin/heta-build.js +++ b/bin/heta-build.js @@ -22,14 +22,14 @@ program .description('Compile Heta based platform and create set of export files.') //.arguments(' [dir]') .usage('[options] [dir]') - .option('-d, --declaration ', 'platform declaration file without extension, search extensions: ["", ".json", ".json5", ".yml"]', 'platform') + .option('-d, --declaration ', 'declaration file name without extension to search throught extensions: ["", ".json", ".json5", ".yml"]', 'platform') // options - .option('--skip-export', 'do not export files to local directory') - .option('--log-mode ', 'When to create log file.') - .option('--debug', 'If set the raw module output will be stored in "meta".') + .option('-S, --skip-export', 'do not export files to local directory') + .option('-L, --log-mode ', 'When to create log file.') + .option('-d, --debug', 'If set the raw module output will be stored in "meta".') // moduleImport - .option('--source ', 'path to main heta module.') - .option('--type ', 'type of source file.') + .option('-s, --source ', 'path to main heta module.') + .option('-t, --type ', 'type of source file.') .parse(process.argv); // set target directory of platform @@ -46,17 +46,16 @@ let extensionNumber = searches .indexOf(true); // is declaration file found ? if (extensionNumber === -1) { + process.stdout.write('Running compilation without declaration file...\n'); var declaration = {}; - //console.log( 'Declaration file is not found in paths:\n', JSON.stringify(searches, null, 2)); - console.log('Running compilation without declaration file.'); } else { - let delarationFile = searches[extensionNumber]; - console.log(`Running compilation with declaration "${delarationFile}"`); - let declarationText = fs.readFileSync(delarationFile); + let declarationFile = searches[extensionNumber]; + process.stdout.write(`Running compilation with declaration file "${declarationFile}"...\n`); + let declarationText = fs.readFileSync(declarationFile); try { declaration = safeLoad(declarationText); } catch (e) { - console.log('Wrong format of declaration file:', e.message); + process.stdout.write(`Wrong format of declaration file: \n"${e.message}"\n`); process.exit(1); } } @@ -74,7 +73,7 @@ let CLIDeclaration = { } }; -// === combine options === +// === combine options CLI => declaration => default index === let integralDeclaration = _.defaultsDeep(CLIDeclaration, declaration); // wrong version throws, if no version stated than skip @@ -82,31 +81,35 @@ let satisfiesVersion = integralDeclaration.builderVersion ? semver.satisfies(version, integralDeclaration.builderVersion) : true; if (!satisfiesVersion) { - console.log(`Version of declaration file "${integralDeclaration.builderVersion}" does not satisfy current builder.`); + process.stdout.write(`Version of declaration file "${integralDeclaration.builderVersion}" does not satisfy current builder.\n`); process.exit(1); } -// === runBuilder === -let builder = new Builder(integralDeclaration, targetDir); +// === this part uses "send errors to developer" message === +try { + var builder = new Builder(integralDeclaration, targetDir); +} catch(e) { + process.stdout.write(contactMessage + '\n'); + throw e; +} if (builder.logger.hasErrors) { - console.log('Declaration ERROR! See logs.'); + process.stdout.write('Declaration ERROR! See logs.\n'); process.exit(1); } -// send errors to developer try { builder.run(); } catch(e) { - console.log(contactMessage); + process.stdout.write(contactMessage + '\n'); throw e; } - if (builder.logger.hasErrors) { - console.log('Compilation ERROR! See logs.'); - integralDeclaration.options.exitWithoutError - ? process.exit(0) - : process.exit(1); + process.stdout.write('Compilation ERROR! See logs.\n'); + if (integralDeclaration.options.exitWithoutError) + process.exit(0); + else + process.exit(1); } else { - console.log('Compilation OK!'); + process.stdout.write('Compilation OK!\n'); process.exit(0); } diff --git a/cases/0-hello-world/src/index.heta b/cases/0-hello-world/src/index.heta index e091f9c2..507c8bd5 100644 --- a/cases/0-hello-world/src/index.heta +++ b/cases/0-hello-world/src/index.heta @@ -65,8 +65,8 @@ end filepath: matlab1, }; -/* -mm::mm_sbml @SBMExport { title: Exported mm model}; +/* +mm::mm_sbml @SBMLExport { title: Exported mm model}; mm::mm_simbio @SimbioExport; mm::full_json @JSONExport; mm::full_yaml @YAMLExport; diff --git a/src/builder/index.js b/src/builder/index.js index 89b82b23..d97ccf47 100644 --- a/src/builder/index.js +++ b/src/builder/index.js @@ -48,7 +48,7 @@ class Builder { return; } - // assignments + // file paths Object.assign(this, declaration); this._coreDirname = path.resolve(coreDirname); this._distDirname = path.resolve(coreDirname, declaration.options.distDir); @@ -58,7 +58,14 @@ class Builder { // create container this.container = new Container(); this.logger.info(`Builder initialized in directory "${this._coreDirname}".`); + + // index file not found + let indexFilepath = path.resolve(coreDirname, declaration.importModule.source); + if (!fs.existsSync(indexFilepath)) { + this.logger.error(`index file "${indexFilepath}" does not exist.`, 'BuilderError'); + } } + run(){ this.logger.info(`Compilation of module "${this.importModule.source}" of type "${this.importModule.type}"...`);