-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·129 lines (107 loc) · 3.32 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
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
#!/usr/bin/env node --harmony
"use strict"
let commandLineArgs = require("command-line-args")
let error = require("./lib/error")
let basename = require("basename")
let subcommandSync = require("./lib/subcommands/sync")
let subcommandConfig = require("./lib/subcommands/config")
let subcommandList = require("./lib/subcommands/list")
{
let args = getArguments()
if(args.subcommand === 'sync' && args.list) subcommandList(args.list, args.config)
else if(args.subcommand === 'sync') subcommandSync(args.config)
else if(args.subcommand === 'config') subcommandConfig()
}
/*
* Functions
*/
function verifyArguments(args, cliUsage) {
// just want help
if(args.help) {
console.log(cliUsage)
process.exit(0)
}
// no config file
if(args.subcommand === 'sync' && !args.config) {
console.log(cliUsage)
process.exit(1)
}
verifyListArg(args, cliUsage)
}
function verifyListArg(args, cliUsage) {
let validListArgs = [
"source.clients",
"source.projects",
"source.tasks",
"dest.clients",
"dest.projects",
"dest.tasks",
]
if(args.list && validListArgs.indexOf(args.list) === -1) {
console.log(cliUsage)
process.exit(21)
}
}
function getArguments() {
let me = basename(process.argv[1])
let cli = commandLineArgs(
[
{ name: "sync", type: String, multiple: true, defaultOption: true },
{ name: "list", type: String, multiple: false },
{ name: "help", alias: "h", type: Boolean, defaultValue: false },
]
)
let cliUsage = cli.getUsage({
title: "harvest-sync",
description: "Syncs time-sheets between two harvest accounts.",
footer: "Project Home: [underline]{https://github.com/cullylarson/harvest-sync}",
synopsis: [
"$ " + me + " path/to/your-config-file.json",
"$ " + me + " --list (source|dest).(clients|projects|tasks) path/to/your-config-file.json",
"$ " + me + " [bold]{--help}",
"$ " + me + " config"
],
})
let args = parseBareParams(cli.parse(), cliUsage)
verifyArguments(args, cliUsage)
return args
}
function parseBareParams(args, cliUsage) {
let validSubCommands = [
"sync",
"config",
]
let defaultSubCommand = 'sync'
if(args.sync) args.bare = args.sync
// parse the bare params
if(args.bare) {
// more than one
if(args.bare.length) {
// it's a sub-command
if(validSubCommands.indexOf(args.bare[0]) !== -1) {
args.subcommand = args.bare[0]
// if there's another argument, assume it's the config
if(args.bare.length > 1) {
args.config = args.bare[1]
}
}
// not a sub-command
else {
// if there's more than one
if(args.bare.length > 1) {
console.log(cliUsage)
process.exit(20)
}
// just one
else {
args.subcommand = defaultSubCommand
args.config = args.bare[0]
}
}
}
}
else {
args.subcommand = defaultSubCommand
}
return args
}