forked from UTS-eResearch/ro-crate-excel
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrocxl
executable file
·192 lines (161 loc) · 6.68 KB
/
rocxl
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env node
/*
This is part of ro-crate-excel tool for implementing the RO-Crate data packaging
spec. Copyright (C) 2018 University of Technology Sydney and 2023 University of Queensland
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const Bag = require("./lib/bag.js");
const update = require("./lib/update.js");
//
const ROCrate = require("ro-crate").ROCrate;
const Preview = require("ro-crate-html").Preview;
const HtmlFile = require("ro-crate-html").HtmlFile;
const htmlDefaults = require("ro-crate-html").Defaults;
const path = require("path");
const shell = require("shelljs");
const { Command } = require('commander');
const program = new Command();
const defaults = require("./lib/defaults.js");
const htmlFileName = defaults.html_file_name;
const metadata_file_name = defaults.metadata_json_file_name;
const fs = require("fs-extra");
const { version } = require("./package.json");
var dirs = undefined;
main();
async function main() {
program
.version(version)
.description(
"Generates an an Excel spreadsheet and RO-Crate metadata from a set of files and updates the RO-Crate with data " +
"filled in the spreadsheet. If a spreadsheet is newer than the RO-Crate metadata file then then xlro will use that or vice versa. " +
"The file system is ALWAYS traversed and file information merged into existing metadata."
)
.arguments("<directories...>")
.action(function(directories) {
dirs = directories;
})
.option("-b, --bag [bag-dir]", "Create Bagit Bag(s) under [bag-dir])")
.option("-a, --add", "Add metadata from additional-ro-crate-metadata.xlsx to an existing ro-crate-metadata.json crate). Does not re-write the Excel input file.")
.option("-z --zip", "Zip the bagged ro-crate (only works with --bag")
.option("-j --JSON", "Use the ro-crate-metadata.json file rather than ro-crate-metadata.xlsx")
.option("-p --partOf [partOf]", "This is part of another RO-Crate, supply the ro-crate-metadata.jsonld path.")
.option("-d, --depth [depth]", "Maximum depth to recurse into directories")
.option("-r, --recurse", "Recurse into directories looking for files")
.option("-c, --cratescript [cratesript]", "URL of Crate-script directory")
.option("-m, --maxfiles [maxfiles]", `Maximum number of files to itemise per directory (default is ${defaults.max_files_in_dir})`)
//.option("-f, --filesOnly", "Find files and stop") NOT IMPLEMEMTED YET
.option(
"-u, --url [distro]",
"Distribution URL"
)
.showHelpAfterError();
var depth = 0;
program.parse(process.argv);
if (program.recurse) {
depth = defaults.max_depth;
}
if (program.depth) {
depth = program.depth;
}
if (program.maxfiles) {
defaults.maxFilesInDir = program.maxfiles;
}
if (!program.rawArgs.length || !dirs) program.help();
for( let dir of dirs ) {
if (shell.test("-d", dir)) {
shell.rm("-rf", path.join(dir, "ro-crate-metadata_files"));
await processDirectory(dir, depth);
await generateHTML(path.join(dir, defaults.metadata_json_file_name));
}
}
}
async function processDirectory(dir, depth) {
await update(dir, depth, program.JSON, program.add);
}
async function generateHTML(metadataPath) {
// Save an HTML file
console.log("Generating html from: " + metadataPath);
if (!path.isAbsolute(metadataPath)) {
metadataPath = path.join(process.cwd(), metadataPath);
}
var dir = path.dirname(metadataPath)
shell.rm("-rf", path.join(dir, "ro-crate-metadata_files"));
var crate = new ROCrate(JSON.parse(fs.readFileSync(metadataPath, "utf8")));
crate.index();
var root = crate.getRootDataset();
if (program.url){
// Add dowload info
if (!root["distribution"]) {
root["distribution"] = []
}
root["distribution"] = crate.utils.asArray(root["distribution"])
root["distribution"].push(
{
"@id": program.url
}
);
var graph = crate.getGraph();
graph.push(
{
"@id": program.url,
"contentUrl": program.url,
"@type": "DataDownload",
"encodingFormat": "zip"
});
fs.writeFileSync(metadataPath, JSON.stringify(crate.getJson(), null, 2), "utf8")
crate = new ROCrate(crate.getJson());
}
if (program.partOf) {
var parentCrate = new ROCrate(JSON.parse(fs.readFileSync(program.partOf, "utf8")));
await parentCrate.index();
parentRoot = await parentCrate.getRootDataset();
// Inherit metadata
if (!root.datePublished) {
root.datePublished = parentRoot.datePublished;
}
if (!root.name) {
root.name = `${root['@id']} [Part of]: ${parentRoot.name}`;
}
var desc = crate.utils.asArray(root.description)
desc.push(`This dataset is a part of: ${parentRoot.name} [${parentRoot.description}]`);
root.description = desc;
if (!root.isPartOf && parentRoot.identifier) {
root.isPartOf = parentRoot.identifier;
}
}
const preview = await new Preview(crate);
const f = new HtmlFile(preview);
fs.writeFileSync(path.join(dir, htmlFileName),
await f.render(program.cratescript || htmlDefaults.render_script), "utf-8");
if (program.bag) {
var bagger = new Bag();
var dest = path.join(program.bag, path.basename(dir));
shell.rm("-rf", dest);
dir = await bagger.bag(dir, dest, crate);
console.log("bagged", dir)
bagger.generate_bag_info();
bagger.save_bag_info();
if (!path.isAbsolute(dir)) {
dir = path.join(process.cwd(), dir);
}
metadata_path = path.join(dir, metadata_file_name);
const index = await fs.copyFile(path.join(__dirname, "defaults", "index.html"), path.join(dest, "index.html"));
//bagger.update();
if (program.zip){
var zipname = path.resolve(path.join(program.bag, path.basename(path.dirname(metadataPath)).replace(" ","_") + ".zip"));
console.log('Zipping to', zipname)
shell.cd(dir);
shell.rm("-f", `"${zipname}"`);
shell.exec(`zip -r "${zipname}" *`);
}
}
}