forked from bids-standard/legacy-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
125 lines (113 loc) · 4.95 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*eslint no-console: ["error", {allow: ["log"]}] */
var validate = require('./index.js');
var colors = require('colors/safe');
var cliff = require('cliff');
var pluralize = require('pluralize');
var bytes = require('bytes');
var fs = require('fs');
module.exports = function(dir, options) {
if (fs.existsSync(dir)) {
if (options.json) {
validate.BIDS(dir, options, function (issues, summary) {
console.log(JSON.stringify({ issues, summary }));
});
} else {
validate.BIDS(dir, options, function (issues, summary) {
var errors = issues.errors;
var warnings = issues.warnings;
if (issues.errors.length === 1 && issues.errors[0].code === "61") {
console.log(
colors.red(
"The directory " +
dir +
" failed an initial Quick Test. This means the basic names and structure of the files and directories do not comply with BIDS specification. For more info go to http://bids.neuroimaging.io/"
)
);
} else if (issues.config && issues.config.length >= 1) {
console.log(colors.red("Invalid Config File"));
for (var i = 0; i < issues.config.length; i++) {
var issue = issues.config[i];
issue.file.file = { relativePath: issue.file.path };
issue.files = [issue.file];
}
logIssues(issues.config, "red", options);
} else if (errors.length >= 1 || warnings.length >= 1) {
logIssues(errors, "red", options);
logIssues(warnings, "yellow", options);
} else {
console.log(
colors.green("This dataset appears to be BIDS compatible.")
);
}
logSummary(summary);
if (
issues === "Invalid" ||
(errors && errors.length >= 1) ||
(issues.config && issues.config.length >= 1)
) {
process.exit(1);
}
});
}
} else {
console.log(colors.red(dir + " does not exist"));
process.exit(2);
}
};
function logIssues (issues, color, options) {
for (var i = 0; i < issues.length; i++) {
var issue = issues[i];
console.log('\t' + colors[color]((i + 1) + ': ' + issue.reason + ' (code: ' + issue.code + ' - ' + issue.key + ')'));
for (var j = 0; j < issue.files.length; j++) {
var file = issues[i].files[j];
if (!file || !file.file) {continue;}
console.log('\t\t.' + file.file.relativePath);
if (options.verbose) {console.log('\t\t\t' + file.reason);}
if (file.line) {
var msg = '\t\t\t@ line: ' + file.line;
if (file.character) {
msg += ' character: ' + file.character;
}
console.log(msg);
}
if (file.evidence) {
console.log('\t\t\tEvidence: ' + file.evidence);
}
}
if (issue.additionalFileCount > 0) {
console.log('\t\t'+colors[color]('... and '+issue.additionalFileCount+' more files having this issue (Use --verbose to see them all).'));
}
console.log();
}
}
function logSummary (summary) {
if (summary) {
var numSessions = summary.sessions.length > 0 ? summary.sessions.length : 1;
// data
var column1 = [
summary.totalFiles + ' ' + pluralize('File', summary.totalFiles) + ', ' + bytes(summary.size),
summary.subjects.length + ' - ' + pluralize('Subject', summary.subjects.length),
numSessions + ' - ' + pluralize('Session', numSessions)
],
column2 = summary.tasks,
column3 = summary.modalities;
var longestColumn = Math.max(column1.length, column2.length, column3.length);
var pad = ' ';
// headers
var headers = [pad, colors.blue.underline('Summary:') + pad, colors.blue.underline('Available Tasks:') + pad, colors.blue.underline('Available Modalities:')]
// rows
var rows = [headers];
for (var i = 0; i < longestColumn; i++) {
var val1, val2, val3;
val1 = column1[i] ? column1[i] + pad : '';
val2 = column2[i] ? column2[i] + pad : '';
val3 = column3[i] ? column3[i] : '';
rows.push([' ', val1, val2, val3]);
}
console.log(cliff.stringifyRows(rows));
console.log();
//Neurostars message
console.log(colors.red("If you have any questions please post on https://neurostars.org/tags/bids"));
console.log();
}
}