-
Notifications
You must be signed in to change notification settings - Fork 0
/
musica.js
135 lines (110 loc) · 4.12 KB
/
musica.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
126
127
128
129
130
131
132
133
134
135
/**
* musica.js
* Command-line tool for managing the musica backend.
* For more information, run `node musica --help`
* @license MIT License (c) copyright 2018 Patrick Barnes
* @author Patrick Barnes
*/
require('dotenv/config');
const program = require('commander');
const plimit = require('p-limit');
const { promisify } = require('util');
const glob = promisify( require('glob') );
const { ingestFile } = require('./src/ingest');
const storageBuilder = require('./src/storage');
//==============================================================================
program
.version('0.1.0')
.description("Command-line tool for managing the musica backend");
//------------------------------------------------------------------------------
program
.command('init')
.description('Create and initialise the data store')
.option('-t, --type <t>', 'Specify the storage type')
.option('-f, --force', 'Continue even if the data store already exists')
.action(function(options) {
console.log("Connecting to storage...");
const storage = storageBuilder(options);
storage.establishStorage(options)
.then(()=>{
console.log("Data store initialised and ready for use.");
process.exit(0);
}).catch((err)=>{
console.error("Error: "+err);
process.exit(1);
});
});
//------------------------------------------------------------------------------
program
.command('insert [files...]')
.description('Scan, convert and upload any number of audio files.')
.option('-t, --type <t>', 'Specify the storage type')
.action(function(files, options) {
if (files.length == 0) {
console.error("Error: Must provide at least one input file.");
process.exit(1);
}
const storage = storageBuilder(options);
const count = { processed: 0, failed: 0 };
//Expand any glob file patterns
const scanFiles = Promise.all(files.map((file)=>glob(file)))
.then((files)=>[].concat(...files));
//Process them
const processFiles = scanFiles.then((files)=>{
console.log(`Have ${files.length} files to process...`);
const throttle = plimit(5);
const throttledIngest = (file)=>throttle(()=>{
console.log(`Processing ${file}...`);
return ingestFile(file, storage)
.then((id)=>{
console.log(`Finished processing ${file}. ID is ${id}.\n`);
count.processed++;
})
.catch((err)=>{
console.log(`Failed to process ${file}: ${err}`);
count.failed++;
});
});
return Promise.all(files.map(throttledIngest));
});
processFiles.then(()=>{
console.log("Finished");
console.log(`Processed ${count.processed} audio tracks.`);
console.log(`Could not process ${count.failed} audio tracks.`);
process.exit(count.failed ? 1 : 0);
});
});
//----------------------------------------------------------------------------
program
.command('export')
.description('Export the metadata index from the data store.')
.action(function(options) {
const storage = storageBuilder(options);
storage.indexMetadata()
.then((metadata)=>console.log(JSON.stringify(metadata)));
});
//----------------------------------------------------------------------------
program
.command('clear')
.description('Remove any data (audio and metadata) from the data store.')
.option('-t, --type <t>', 'Specify the storage type')
.option('-f, --force', 'Must be set, to confirm removal')
.action(function(options) {
console.log("Clearing all data...");
const storage = storageBuilder(options);
storage.clearStorage(options)
.then(()=>{
console.log("Data store cleared successfully.");
process.exit(0);
}).catch((err)=>{
console.error("Error: "+err);
process.exit(1);
});
});
//==============================================================================
program.on('command:*', function () {
console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args[0]);
process.exit(1);
});
program.parse(process.argv);
if (program.args.length == 0) program.help();