forked from UTS-eResearch/ro-crate-js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
roc-validate.js
executable file
·74 lines (64 loc) · 2.04 KB
/
roc-validate.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
#!/usr/bin/env node
const program = require('commander');
const fs = require('fs');
const path = require('path');
const { ROCrate } = require('./lib/rocrate');
const { Validator } = require('./lib/validator');
var crateDir;
program
.version('0.1.0')
.description('Runs a minimal RO-Crate validation')
.option(
'-f, --files-path <csv-path>',
'Path to a csv file into which the tool will write a summary of which files are in the crate directory and mentioned in the crate'
)
.option(
'-r, --report-path <report-path>',
'Path to a JSON file into which the tool will write a json file containing errors, warnings and info'
)
.arguments('<dir>')
.action((dir) => {
crateDir = dir;
});
program.parse(process.argv);
const outPath = program.outputPath ? program.outputPath : crateDir;
async function main() {
const rawJson = fs.readFileSync(
path.join(crateDir, 'ro-crate-metadata.json')
);
const validator = new Validator();
validator.parseJSON(rawJson);
await validator.validate();
if (program.filesPath) {
const files = fs.readdirSync(crateDir, { recursive: true });
// Initialise a files object which has all the files found in the crate
const filesObj = Object.fromEntries(
files.map((value) => [
path.join(value),
{
exists: true,
inCrate: false,
isDir: fs.lstatSync(path.join(crateDir, value)).isDirectory(),
},
])
);
validator.checkFiles(filesObj);
//console.log(filesObj)
var csvString = 'file,exists,inCrate,isDir,dirDescribed\n';
for (let key of Object.keys(filesObj)) {
csvString += `"${key.replace(/(["])/g, '$1$1')}",${
filesObj[key].exists
},${filesObj[key].inCrate},${filesObj[key].isDir},${filesObj[key].dirDescribed}\n`;
}
fs.writeFileSync(program.filesPath, csvString);
}
if (program.reportPath) {
fs.writeFileSync(
program.reportPath,
JSON.stringify(validator.result, null, 2)
);
} else {
//console.log(SON.stringify(validator.result, null, 2))
}
}
main();