-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkout-creator-cli.js
145 lines (128 loc) · 3.96 KB
/
workout-creator-cli.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
136
137
138
139
140
141
142
143
144
145
// REQUIRE LIBRARIES
var fs = require('fs');
var Prompt = require('prompt');
Prompt.message = '';
Prompt.delimiter = ' >';
var Optimist = require('optimist');
var CSVConverter = require('csvtojson').Converter;
var Converter = new CSVConverter({});
var ErrorHandler = require('./libs/error');
ErrorHandler = new ErrorHandler();
var WorkoutCreator = require('./libs/WorkoutCreator.js');
WorkoutProcessor = new WorkoutCreator();
// COMMAND LINE ARGUMENT OVERRIDES
Prompt.override = Optimist.argv;
// PROMPT FOR INPUT
/*{
name: // The name of the prompt field value.
description: '', // Prompt displayed to the user. If not supplied name will be used.
type: 'string|boolean|integer', // Specify the type of input to expect.
pattern: /^\w+$/, // Regular expression that input must be valid against.
message: '', // Warning message to display if validation fails.
hidden: false, // If true, characters entered will either not be output to console or will be outputed using the `replace` string.
replace: '*', // If `hidden` is set it will replace each hidden character with the specified string.
default: '', // Default value to use if no value is entered.
required: true, // If true, value entered must be non-empty.
before: function(value) { // Runs before node-prompt callbacks. It modifies user's input
return 'v' + value;
},
}
*/
var properties = [{
name: 'input',
description: 'Input [.csv]',
pattern: /[^\0]*.csv/,
message: 'File must be a .csv',
type: 'string',
hidden: false,
default: 'WorkoutCreator.csv',
required: true
},
{
name: 'output',
description: 'Output Directory',
type: 'string',
hidden: false,
default: './',
required: true,
},
{
name: 'template',
description: 'Template [.mustache]',
pattern: /[^\0]*.mustache/,
message: 'File must be a .mustache',
type: 'string',
hidden: false,
default: 'WorkoutCreator.mustache',
required: true
},
{
name: 'verbose',
description: 'Verbose',
type: 'boolean',
hidden: false,
default: true,
required: false
},
{
name: 'debug',
description: 'Debug',
type: 'boolean',
hidden: false,
default: false,
required: false
}];
var prompts = [];
var inputs = {}
for (var i=0;i<properties.length;i++) {
prompts.push(properties[i].name);
inputs[properties[i].name] = properties[i].default;
}
Prompt.start();
Prompt.get(properties, function (err, result) {
prompts = result;
// INITIALIZE VARIABLES
var input_filename = prompts.input;
var output_file_prefix = prompts.output;
var output_file_suffix = '.zwo';
// CONVERT CSV to JSON
if (prompts.verbose) console.log('Loading Data: '+input_filename);
Converter.fromFile(input_filename,function(err,result){
if (err) {
console.log(err);
process.exit(0);
} else {
if (prompts.verbose) console.log(' Loaded');
}
var output = '';
var output_file = '';
var output_filename = '';
if (prompts.verbose) console.log('Processing Views');
var views = WorkoutProcessor.process(result);
if (prompts.verbose) console.log(' Processed');console.log(' ');
var template = prompts.template;
var cn, lastphase;
for(var view in views) {
if (view !== 'durations') {
if (view.split(':')[0] !== lastphase) cn = 0;
lastphase = view.split(':')[0];
output_filename = view.split(':')[0]+'-';
if(cn+1<10) {
output_filename += '0';
}
output_filename += (cn+1);
output_file = output_file_prefix+output_filename+output_file_suffix;
if (prompts.verbose) console.log('Rendering View: '+view);
output = WorkoutProcessor.render(template, views[view]);
if (prompts.verbose) console.log(' Rendered');
if (prompts.verbose) console.log('Writing File: '+output_file);
fs.writeFileSync(output_file, output);
if (prompts.verbose) console.log(' Written'); console.log(' ');
cn++;
}
}
if (prompts.debug) console.log(TextEvents.dump());
if (prompts.debug) ErrorHandler.flushErrors();
process.exit(0);
});
});