Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·281 lines (273 loc) · 10.6 KB
/
Copy pathindex.js
File metadata and controls
executable file
·281 lines (273 loc) · 10.6 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env node
// Allow reading to the current working directory
const process = require('process');
// Load .env file but let variables already set in the environment take priority
const dotenvLoad = require('dotenv').config({ path: __dirname + '/.env', override: false });
// The environment can be provided also through the command line as environment variables
// e.g. NODE_ENV=production node index.js load /path/to/project
// If that is the case, we should not rely on the .env file and thus we can skip
// checking if there was any error loading the .env file
// if (dotenvLoad.error) throw dotenvLoad.error;
// "yargs" is a library used to manage script calls from unix console
const yargs = require('yargs');
// Import auxiliar functions
const { idOrAccessionCoerce } = require('./src/utils/auxiliar-functions');
// Hanlde the setup before any command is run
const commonHandler = require('./src/commands');
// -----------------------------------------------------------------------------------------
// Execute different functions and scripts according to the input commands and options
// Display help info when this script is called with no commands or with a "--help" commands
// yargs API: https://github.com/yargs/yargs/blob/HEAD/docs/api.md
yargs
// load
.command({
command: 'load <pdir>', // Command name and positional arguments. Useful for the help
desc: 'load data from specified file or folder', // Command description. Useful for the help
builder: yargs => yargs
// All values from options and positionals are saved at the "argv" object (explained below)
// project directory
.positional('pdir', {
alias: 'project-directory',
describe: 'Project directory containing all files to be loaded',
type: 'string'
})
// --accession
.option('a', {
alias: 'accession',
description: 'Set the accession.\n' +
'Use this to append new data to an already existing project.\n' +
'Also use this to create a new project with a specific accession.',
type: 'string',
default: null,
coerce: idOrAccessionCoerce,
})
// --conserve
.option('c', {
alias: 'conserve',
description: 'Restrict the data append so the user is never asked and new data is loaded only when there is no conflict',
type: 'boolean',
default: false,
})
// --overwrite
.option('o', {
alias: 'overwrite',
description: 'Priorize the data append so the user is never asked and current data is overwritten when there is any conflict',
type: 'boolean',
default: false,
})
// --md-directories
.option('mdirs', {
alias: 'md-directories',
description: 'Set which MD directories are to be loaded',
type: 'array'
})
// --include
.option('i', {
alias: 'include',
description: 'Load only the specified files',
type: 'array',
})
// --exclude
.option('e', {
alias: 'exclude',
description: 'Load all but the specified files',
type: 'array',
})
// --skip-trajectories
.option('st', {
alias: 'skip-trajectories',
description: 'Skip the load of any trajectory file (xtc) in the binary format (bin)',
type: 'boolean',
default: false,
})
// --skip-files
.option('sf', {
alias: 'skip-files',
description: 'Skip the load of any file (pdb, xtc, ...)',
type: 'boolean',
default: false,
})
// --skip-analyses
.option('sa', {
alias: 'skip-analyses',
description: 'Skip the load of any analyses (md.whatever.json files)',
type: 'boolean',
default: false,
})
// --gromacs-path
.option('gro', {
alias: 'gromacs-path', // Option name. Useful for the help
description: 'path to gromacs command-line tool', // Option description. Useful for the help
type: 'string',
default: null,
coerce: idOrAccessionCoerce,
})
// --timeout-ms
.option('t', {
alias: 'timeout',
description: 'Wait time in seconds for newly uploaded file documents to become visible in MongoDB',
type: 'integer',
default: null,
}),
handler: commonHandler('load'), // Call the command script with the command name as argument
})
// book
.command({
command: 'book <count>',
desc: 'create empty projects in order to book their accessions',
builder: yargs => yargs
// count
.positional('count', {
describe: 'Number of projects to book',
type: 'integer',
}),
handler: commonHandler('book'),
})
// publish
.command({
command: 'publish <id|accession>',
desc: 'set as "published" a specified project by its accession or internal id',
builder: yargs => yargs
// id
.positional('id', {
describe: 'ID to process',
type: 'string',
coerce: idOrAccessionCoerce,
}),
handler: commonHandler('publish'),
})
// unpublish
.command({
command: 'unpublish <id|accession>',
desc: 'set as NOT "published" a specified project by its accession or internal id',
builder: yargs => yargs
// id
.positional('id', {
describe: 'ID or accession to unpublish',
type: 'string',
coerce: idOrAccessionCoerce,
}),
handler: commonHandler('unpublish'),
})
// publish many
.command({
command: 'publishall <query>',
desc: 'Set as "published" multiple projects at once using a query\n' +
'Example: publishall \'{\"metadata.COLLECTIONS\": [\"mdbind\"]}\'',
builder: yargs => yargs
.positional('query', {
description: 'Use a query to select projects to be published',
type: 'string',
}),
handler: commonHandler('publishall'),
})
// unpublish many
.command({
command: 'unpublishall <query>',
desc: 'Set as NOT "published" multiple projects at once using a query\n' +
'Example: unpublishall \'{\"metadata.COLLECTIONS\": [\"mdbind\"]}\'',
builder: yargs => yargs
.positional('query', {
description: 'Use a query to select projects to be unpublished',
type: 'string',
}),
handler: commonHandler('unpublishall'),
})
// list
// DANI: Nunca lo uso
.command({
command: 'list',
desc: 'list all projects and their status',
builder: yargs => yargs
.option('l', {
alias: 'limit',
description: 'Limit the number of rows shown in the table',
type: 'integer',
default: null,
}),
handler: commonHandler('list'),
})
// delete
.command({
command: 'delete [id]',
aliases: ['drop', 'erase', 'remove'],
desc: 'Delete any document by its ID. To clean up a published project, you must first unpublish it',
builder: yargs => yargs
// --confirm
.option('y', {
alias: 'confirm',
description: 'Confirm already, so the user is never asked for confirmation before deletion',
type: 'boolean',
default: false,
})
// id
.positional('id', {
describe: 'ID of document to delete',
type: 'string',
coerce: idOrAccessionCoerce,
}),
handler: commonHandler('delete'),
})
// cleanup
// NOTE: ask user to unpublish before cleaning up, to make them think twice
// NOTE: about what they're about to do since there is no going back from that
.command({
command: 'cleanup',
aliases: ['clean', 'clear'],
desc: 'Remove orphan data',
builder: yargs => yargs
// --confirm
.option('y', {
alias: 'confirm',
description: 'Confirm already, so the user is never asked for confirmation before deletion',
type: 'boolean',
default: false,
}),
handler: commonHandler('cleanup'),
})
// setup
.command({
command: 'setup',
aliases: ['install'],
desc: 'Setup the database collections and configure their indexation',
builder: yargs => yargs,
handler: commonHandler('setup'),
})
// update
.command({
command: 'update',
aliases: ['upgrade'],
desc: 'Update the database schema and data to the latest version',
builder: yargs => yargs
.options('force', {
alias: 'f',
description: 'Apply the update even if the database is already at the latest version',
type: 'boolean',
default: false,
})
.options('verbose', {
alias: 'v',
description: 'Display detailed information about the update process',
type: 'boolean',
default: false,
}),
handler: commonHandler('update'),
})
// check
.command({
command: 'check',
desc: 'print the number of projects in the database',
builder: yargs => yargs,
handler: commonHandler('check'),
})
.demandCommand() // Demand a minimmum of 1 command
.strict()
// Display all descriptions when the command --help is asked or there is no command
.help().argv; // "argv" is a normal object passed from yargs library
// This object contains the input values of options and positionals from the command
// e.g. in load command, argv contains the values of {folder, gromacs-path}
// in case an exception manages to escape us
process.on('unhandledRejection', error => {
console.error('Unhandled rejection');
console.error(error);
});