-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·238 lines (230 loc) · 9.05 KB
/
index.js
File metadata and controls
executable file
·238 lines (230 loc) · 9.05 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
#!/usr/bin/env node
// Allow reading to the current working directory
const process = require('process');
// These 2 lines alone allow all scripts to access the env file through process.env
const dotenvLoad = require('dotenv').config({ path: __dirname + '/.env' });
// 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,
}),
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',
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',
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',
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'),
})
.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);
});