Skip to content

Commit d4cc5b4

Browse files
committed
fix javascript component and code
1 parent ca5b341 commit d4cc5b4

2 files changed

Lines changed: 14 additions & 20 deletions

File tree

src/main/resources/io/viash/languages/javascript/ViashParseJson.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs');
1+
const _viashFs = require('fs');
22

33
/**
44
* Parse JSON parameters file into a JavaScript object.
@@ -14,12 +14,12 @@ function viashParseJson(jsonPath = null) {
1414
}
1515
}
1616

17-
if (!fs.existsSync(jsonPath)) {
17+
if (!_viashFs.existsSync(jsonPath)) {
1818
throw new Error(`Parameters file not found: ${jsonPath}`);
1919
}
2020

2121
try {
22-
const jsonText = fs.readFileSync(jsonPath, 'utf8');
22+
const jsonText = _viashFs.readFileSync(jsonPath, 'utf8');
2323
return JSON.parse(jsonText);
2424
} catch (error) {
2525
throw new Error(`Error parsing JSON file: ${error.message}`);

src/test/resources/test_languages/js/code.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,36 @@ let meta = {
2121
const fs = require('fs')
2222

2323
let logFun;
24-
if (typeof par['log'] === 'undefined') {
24+
if (!par['log']) {
2525
logFun = function(out) {
26-
console.log("INFO:" + out);
26+
console.log("INFO:" + out);
2727
}
2828
} else {
2929
logFun = function(out) {
30-
fs.appendFile(par['log'], "INFO:" + out + "\n", function (err) {
31-
if (err) throw err;
32-
});
30+
fs.appendFileSync(par['log'], "INFO:" + out + "\n");
3331
}
3432
}
3533
let outFun;
36-
if (typeof par['output'] === 'undefined') {
34+
if (!par['output']) {
3735
outFun = console.log
3836
} else {
3937
outFun = function(out) {
40-
fs.appendFile(par['output'], out + "\n", function (err) {
41-
if (err) throw err;
42-
});
38+
fs.appendFileSync(par['output'], out + "\n");
4339
}
4440
}
4541

4642
// process parameters
4743
logFun('Parsed input arguments.')
4844

49-
if (typeof par['output'] === 'undefined') {
45+
if (!par['output']) {
5046
logFun('Printing output to console')
5147
} else {
5248
logFun('Writing output to file')
5349
}
5450

5551
for (const key in par) {
5652
if (Array.isArray(par[key]) && par[key].length == 0)
57-
outFun(`${key}: |empty array|`)
53+
outFun(`${key}: ||`)
5854
else if (Array.isArray(par[key]))
5955
outFun(`${key}: |${par[key].join(';')}|`)
6056
else if (par[key] == undefined)
@@ -63,12 +59,10 @@ for (const key in par) {
6359
outFun(`${key}: |${par[key]}|`)
6460
}
6561

66-
fs.readFile(par['input'], 'utf8', function(err, data){
67-
outFun(`head of input: |${data.split('\n')[0]}|`)
68-
})
69-
fs.readFile(meta['resources_dir'] + '/resource1.txt', 'utf8', function(err, data){
70-
outFun(`head of resource1: |${data.split('\n')[0]}|`)
71-
})
62+
let inputData = fs.readFileSync(par['input'], 'utf8');
63+
outFun(`head of input: |${inputData.split('\n')[0]}|`)
64+
let resourceData = fs.readFileSync(meta['resources_dir'] + '/resource1.txt', 'utf8');
65+
outFun(`head of resource1: |${resourceData.split('\n')[0]}|`)
7266

7367
for (const key in meta) {
7468
if (meta[key] == undefined || String(meta[key]) == 'NaN')

0 commit comments

Comments
 (0)