forked from Andr3wHur5t/mcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (87 loc) · 2.47 KB
/
index.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
const _ = require('lodash')
const _debug = require('debug')
// Help Is In main to get access to commands object to auto populate help text.
const printHelp = (log, args, done)=> {
let lines = []
var indentTxt = ""
const indent = (level)=> { indentTxt = new Array(level + 1).join(" ") }
const addLine = (text)=> { lines.push(text ? indentTxt + text : '') }
// Header
indent(0)
addLine('CLI tool to interface with the Monstercat Connect API.')
addLine()
addLine("This tool is best used for syncing a target group of tracks with your local machine (i.e. iTunes).")
addLine()
// Commands Block
addLine("Commands:")
addLine()
indent(1)
targetCharCount = _.max(_.map(_.keys(COMMANDS), "length"))
_.each(COMMANDS, ({info}, name)=> {
deltaSpaces = new Array((targetCharCount - name.length) + 1 + 3).join(' ')
addLine(`${name}${deltaSpaces} - ${info}`)
});
console.log(lines.join("\n"))
done()
}
const COMMANDS = {
auth: {
info: 'Create a session with the Monstercat Connnect service.',
cmd: require('./cmds/auth'),
},
session: {
info: "Check the status of the current session.",
cmd: require('./cmds/session'),
},
syncCatalog: {
info: 'Syncs Monstercat\'s entire catalog to the local cache.',
cmd: require('./cmds/syncCatalog'),
},
select: {
info: 'Selects a set of tracks from the cache using the given options.',
cmd: require('./cmds/select'),
},
download: {
info: 'Downloads the tracks the specified file.',
cmd: require('./cmds/download'),
},
importItunes: {
info: 'Imports the paths into iTunes.',
cmd: require('./cmds/importItunes'),
},
logOut: {
info: 'Destroys the current session.',
cmd: require('./cmds/logout'),
},
help: {
info: 'Prints this help text.',
cmd: printHelp,
},
}
// Map `lowercase:fullMapName`
const INSENSITIVE_MAP = _.fromPairs(_.map(_.keys(COMMANDS), (k)=> [k.toLowerCase(), k] ))
const runCommand = (name, args, done)=> {
let realName = INSENSITIVE_MAP[(name || "").toLowerCase()] || "help"
let cmdFn = COMMANDS[realName].cmd
let dbg = _debug(`mcat:${realName}`)
dbg("Starting!")
cmdFn(dbg, args, (err, exitCode)=> {
if (!!err) {
dbg(`ERROR! $err`)
return done(err, exitCode)
}
dbg("Finished!")
return done(null)
})
}
const main = (done)=> {
runCommand(
process.argv[2],
process.argv.slice(2),
done
)
}
main((err, exitCode)=>{
err && console.error(err)
process.exit(exitCode || Number(!!err))
})