-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcliArguments.js
More file actions
114 lines (108 loc) · 4.96 KB
/
Copy pathcliArguments.js
File metadata and controls
114 lines (108 loc) · 4.96 KB
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
var fs = require('fs');
var path = require('path');
class CLIArguments {
/**
* @param {string} inputFolderPath
* @param {string} outputImagePath
* @param {string} outputDescriptorPath
* @param {number} paddingRight
* @param {number} paddingBottom
* @param {?{width: number, height: number}} gridCell
*/
constructor(inputFolderPath, outputImagePath, outputDescriptorPath, paddingRight, paddingBottom, gridCell) {
this.inputFolderPath = inputFolderPath;
this.outputImagePath = outputImagePath;
this.outputDescriptorPath = outputDescriptorPath;
this.paddingRight = paddingRight;
this.paddingBottom = paddingBottom;
this.gridCell = gridCell;
}
static parse(args) {
var inputFolderPath;
var outputImagePath;
var outputDescriptorPath;
var paddingBottom;
var paddingRight;
var gridCell = null;
for (var i = 0; i < args.length; i += 2) {
var key = args[i].toLowerCase();
var value = args[i + 1];
if (key === '-i' || key === '--input-folder') {
if (inputFolderPath)
die('duplicate argument --input-folder');
inputFolderPath = resolvePath(value);
} else if (key === '-o' || key === '--output-image') {
if (outputImagePath)
die('duplicate argument --output-imaage');
var dirName = path.dirname(value);
var fileName = path.basename(value);
outputImagePath = path.join(resolvePath(dirName), fileName);
} else if (key === '-d' || key === '--output-descriptor') {
if (outputDescriptorPath)
die('duplicate argument --output-descriptor');
var dirName = path.dirname(value);
var fileName = path.basename(value);
outputDescriptorPath = path.join(resolvePath(dirName), fileName);
} else if (key === '-p' || key === '--padding') {
if (paddingBottom !== undefined || paddingRight !== undefined)
die('cannot set --padding argument when either --padding-bottom or --padding-right is given');
if (!/^[1-9]\d*$/.test(value))
die('failed to parse number from --padding argument - ' + value);
paddingBottom = parseInt(value, 10);
paddingRight = paddingBottom;
} else if (key === '--padding-bottom') {
if (paddingBottom !== undefined)
die('duplicate attempt to set padding-bottom argument');
if (!/^[1-9]\d*$/.test(value))
die('failed to parse number from --padding-bottom argument - ' + value);
paddingBottom = parseInt(value, 10);
} else if (key === '--padding-right') {
if (paddingRight !== undefined)
die('duplicate attempt to set padding-right argument');
if (!/^[1-9]\d*$/.test(value))
die('failed to parse number from --padding-right argument - ' + value);
paddingRight = parseInt(value);
} else if (key === '--grid-cell') {
if (gridCell)
die('duplicate attempt to set grid-cell argument');
if (!/^[1-9]\d*x[1-9]\d*$/.test(value))
die('failed to parse grid-cell in format N x M from --grid-cell - ' + value);
var gridWidth = parseInt(value.split('x')[0], 10);
var gridHeight = parseInt(value.split('x')[1], 10);
gridCell = {width: gridWidth, height: gridHeight};
} else {
die('unknown argument: ' + key);
}
}
// Assert required options.
if (!inputFolderPath)
die("Required argument --input-folder is missing");
if (!outputImagePath)
die("Required argument --output-image is missing");
if (!outputDescriptorPath)
die("Required argument --output-descriptor is missing");
// Assign default options.
if (!paddingRight)
paddingRight = 0;
if (!paddingBottom)
paddingBottom = 0;
// Validate options.
if (!fs.existsSync(inputFolderPath))
die("Input folder does not exist " + inputFolderPath);
if (!fs.existsSync(path.dirname(outputImagePath)))
die("Folder for output image does not exist " + outputImagePath);
if (!fs.existsSync(path.dirname(outputDescriptorPath)))
die("Folder for output descriptor does not exist " + outputDescriptorPath);
return new CLIArguments(inputFolderPath, outputImagePath, outputDescriptorPath, paddingRight, paddingBottom, gridCell);
}
}
function resolvePath(p) {
if (fs.existsSync(p))
return p;
p = path.join(process.cwd(), p);
return p;
}
function die(message) {
throw new Error(message);
}
module.exports = CLIArguments;