Skip to content

Commit

Permalink
remove logFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
metelkin committed Jan 15, 2025
1 parent 36c61e9 commit 237a68d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 46 deletions.
41 changes: 20 additions & 21 deletions bin/heta-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ program
.description('Compile Heta based platform and create set of export files.')
//.arguments('<cmd> [dir]')
.usage('[options] [dir]')
.option('-d, --declaration <filepath>', 'declaration file name without extension to search throught extensions: ["", ".json", ".yml"]')
.option('--log-level <debug|info|warn|error|crit>', 'Set log level to display.')
// options
.option('--units-check', 'Check all Records for unit consistency.')
.option('--debug', 'If set the raw module output will be stored in "meta".')
Expand All @@ -39,13 +37,20 @@ program
.option('-s, --source <filepath>', 'path to main heta module.')
.option('-t, --type <heta|table|xlsx|json|yaml|sbml>', 'type of source file.')
.option('-e, --export <formats>', 'export formats: "JSON,XLSX" or "{format:JSON},{format:XLSX,omitRows:3}"')
// checking newer version of heta-compiler
.option('--skip-updates', 'Skip checking newer version of heta-compiler.')
// cli level
.option('-d, --declaration <filepath>', 'declaration file name without extension to search throught extensions: ["", ".json", ".yml"]')
.option('--skip-updates', 'Skip checking newer version of heta-compiler.') // checking newer version of heta-compiler
.option('--log-level <debug|info|warn|error|crit>', 'Set log level to display.')
.option('-L, --log-mode <never|error|always>', 'When to create log file.', 'error')
.option('--log-path <filepath>', 'Set log file path.', 'build.log')
//.options('--log-format <json|text>', 'Set log format.', 'text') // not implemented
.parse(process.argv);

let logStream = null;
function message(message) {
process.stdout.write(message + '\n');
logStream?.write(message + '\n');
}

async function main() {
let args = program.args; // target directory
Expand All @@ -54,15 +59,14 @@ async function main() {
// set target directory of platform and check if exist
let targetDir = path.normalize(args[0] || '.');
if (!fs.existsSync(targetDir) || !fs.statSync(targetDir).isDirectory()) { // check if it does not exist or not a directory
process.stdout.write(`Target directory "${targetDir}" does not exist.\nSTOP!`);
message(`Target directory "${targetDir}" does not exist.\nSTOP!`);
process.exit(2); // BRAKE
}

// init logging to file
logStream = fs.createWriteStream(cliOptions.logPath, { flags: 'w' }); // or 'a' to append

process.stdout.write(`Running compilation in directory "${path.resolve(targetDir)}"...\n`); // global path
logStream.write(`Running compilation in directory "${path.resolve(targetDir)}"...\n`);
message(`Running compilation in directory "${path.resolve(targetDir)}"...\n`); // global path

// set targetDir as working directory
process.chdir(targetDir);
Expand All @@ -82,16 +86,13 @@ async function main() {
.indexOf(true);
// is declaration file found ?
if (!cliOptions.declaration && extensionNumber === -1) {
process.stdout.write('No declaration file, running with defaults...\n');
logStream.write('No declaration file, running with defaults...\n');
message('No declaration file, running with defaults...\n');
} else if (extensionNumber === -1) {
process.stdout.write(`Declaration file "${cliOptions.declaration}" not found.\nSTOP!`);
logStream.write(`Declaration file "${cliOptions.declaration}" not found.\nSTOP!`);
message(`Declaration file "${cliOptions.declaration}" not found.\nSTOP!`);
process.exit(2); // BRAKE
} else {
let declarationFile = searches[extensionNumber];
process.stdout.write(`Reading declaration file "${declarationFile}"...\n`);
logStream.write(`Reading declaration file "${declarationFile}"...\n`);
message(`Reading declaration file "${declarationFile}"...\n`);
let declarationText = fs.readFileSync(declarationFile);
try {
let declarationFromFile = YAML.load(declarationText);
Expand All @@ -100,8 +101,7 @@ async function main() {
}
Object.assign(declaration, declarationFromFile);
} catch (e) {
process.stdout.write(`Wrong format of declaration file: \n"${e.message}"\n`);
logStream.write(`Wrong format of declaration file: \n"${e.message}"\n`);
message(`Wrong format of declaration file: \n"${e.message}"\n`);
process.exit(2); // BRAKE
}
}
Expand All @@ -111,8 +111,7 @@ async function main() {
try {
var exportItems = parseExportOption(cliOptions.export);
} catch (e) {
process.stdout.write(`Wrong format of export option: "${cliOptions.export}"\n`);
logStream.write(`Wrong format of export option: "${cliOptions.export}"\n`);
message(`Wrong format of export option: "${cliOptions.export}"\n`);
process.exit(2); // BRAKE
}

Expand All @@ -132,22 +131,22 @@ async function main() {
fs.outputFileSync,
[
new StdoutTransport(logLevel), // log to stdout
new FileTransport('debug', logStream) // log to file
new FileTransport(logLevel, logStream) // log to file
]
).run();

return builder;
}

class FileTransport extends Transport {
constructor(level, logStream) {
constructor(level, stream) {
super(level);
this.logStream = logStream;
this.stream = stream;
}
analyzer(level, msg, opt, levelNum) {
if (levelNum >= this.showLevelNum) {
let line = `[${level}]\t${msg}`;
this.logStream.write(line + '\n');
this.stream?.write(line + '\n');
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions cli-references.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ There are properties in declaration file which do not change compilation process
| importModule.source | string | --source | index.heta | Path to index heta module. Absolute and relative filepaths are applicable. Example: "src/table.xlsx" |
| importModule.type | string | --type | heta | Type of source file. This option set type of module which will be applied for parsing. Available values: heta/xlsx/json/yaml/sbml. |
| options | object | | {} | A set of compiler options. |
| options.logFormat | string | | `string` | The format of saving logs to file. The default value is `string` which corresponds the format similar to console. Full list of options is : `string`, `json`.|
| options.unitsCheck | boolean | --units-check | false | If `true` all Record will be checked for units consistency. |
| options.distDir | string | --dist-dir | dist | At default all export files are created inside **dist** directory. The option can set the another target for storing outputs. |
| options.debug | boolean | --debug | false | Working in debugging mode. All parsed modules will be saved in JSON files in meta directory. |
Expand All @@ -204,7 +203,6 @@ Using neither declaration file nor CLI options is equivalent to the following de
{
"builderVersion": "<current buider version>",
"options": {
"logFormat": "string",
"distDir": "dist",
"metaDir": "meta",
"debug": false,
Expand Down
1 change: 0 additions & 1 deletion src/builder/declaration-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"errorMessage": "${0#} is unknown key in options"
},
"properties": {
"logFormat": {"type": "string", "default": "string", "enum": ["string", "json"]},
"unitsCheck": {"type": "boolean", "default": false},
"distDir": {"type": "string", "default": "dist" },
"debug": {"type": "boolean", "default": false, "description": "Should we put parsed modules to meta?"},
Expand Down
22 changes: 0 additions & 22 deletions src/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ class Builder {
* 8. Checking unit\'s terms.
* 9. Export of a platform to series of formats.
* Depending on the declaration file it runs {@link Builder#exportMany} or {@link Builder#exportJuliaOnly}.
* 10. Saving logs if required
*
* @method Builder#run
*
Expand Down Expand Up @@ -191,27 +190,6 @@ class Builder {
this.logger.warn('Units checking and export were skipped because of errors in compilation.');
}

// 10. save logs if required
/*
let hetaErrors = this.container.hetaErrors();
let createLog = this.options.logMode === 'always'
|| (this.options.logMode === 'error' && hetaErrors.length > 0);
if (createLog) {
switch (this.options.logFormat) {
case 'json':
var logs = JSON.stringify(this.container.defaultLogs, null, 2);
break;
default:
logs = this.container.defaultLogs
.map(x => `[${x.level}]\t${x.msg}`)
.join('\n');
}
this.fileWriteHandler(this._logPath, logs);
this.logger.info(`All logs was saved to file: "${this._logPath}"`);
}
*/

return this;
}

Expand Down

0 comments on commit 237a68d

Please sign in to comment.