-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample-scan.js
executable file
·67 lines (57 loc) · 2 KB
/
sample-scan.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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const mm = require('music-metadata');
const walk = require('./common').walk
const usage = `sample-scan [search-dir=.] [outfile.js]
sample-scan recursively searches "search-dir" for audio files, and generates a
common.js file containing meta data about the files.
The metadata for each file includes a ".path" property which will be specified
relative to the current working directory.
`
console.warn(usage);
// getAndHandleReport requests audio file information from music-metadata, and
// adds them to the `results` object.
const cwd = process.cwd();
const results = {}
const getAndHandleReport = async (filename) => {
try {
const metadata = await mm.parseFile(filename);
const relativePath = path.relative(cwd, filename);
const key = path.basename(filename);
if (results.hasOwnProperty(key)) {
console.warn(`WARNING: omitting non-unique (${key}) filename: ${relativePath}`);
} else {
results[key] = { path: relativePath, info: metadata.format };
console.warn('FOUND:', relativePath);
}
} catch(e) {
console.error(e);
}
}
const args = process.argv.slice(2);
const arg = args[0] || '.';
const inputPath = path.isAbsolute(arg) ? arg : path.join(cwd, arg);
const stats = fs.lstatSync(inputPath);
const writer = args[1] ? fs.createWriteStream(args[1]) : process.stdout;
if (args[1]) console.log(`Writing to: ${args[1]}`);
let promise;
if (stats.isDirectory()) {
console.warn('found directory:', inputPath);
promise = walk(inputPath, getAndHandleReport);
} else if (stats.isFile()) {
console.warn('found file:', inputPath);
promise = getAndHandleReport(inputPath);
} else {
console.error(`ERROR: "${arg}" is not a file or directory`);
process.kill();
}
promise.then(() => {
writer.write('module.exports = ');
writer.write(JSON.stringify(results, null, 2));
writer.write('\n');
}).catch((e) => {
throw e;
}).finally(() => {
console.warn('COMPLETE');
});