This repository was archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageMarkupCall.js
More file actions
140 lines (127 loc) · 3.64 KB
/
ImageMarkupCall.js
File metadata and controls
140 lines (127 loc) · 3.64 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
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
var { Int, cleanJSON } = require("./src/utils");
var convertMarkupToJSON = require("./src/markup_to_json");
var ImageMarkupBuilder = require("./ImageMarkupBuilder").Builder;
var Fabric = require("fabric").fabric;
const path = require("path");
const RequiredCommands =
"Invalid usage: Please provide exactly one of the `markup` or the `json` commands";
var yargs = require("yargs")
.command("json <json_string>", "Process JSON input", {}, jsonCommand)
.command(
"markup <markup_string>",
"Process markup string input",
{
input: {
requiresArg: true,
demandOption: true,
describe: "Input image file to apply markup on",
string: true,
},
output: {
requiresArg: true,
demandOption: true,
describe: "Output image file to write to",
string: true,
},
stroke: {
requiresArg: true,
describe: "Stroke width to apply.",
number: true,
},
},
markupCommand
)
.check((argv) => argv._.length <= 1 || RequiredCommands)
.command(
"$0",
"Default command (shim for flags)",
{
json: {
requiresArg: true,
describe: "String of JSON",
string: true,
conflicts: ["markup", "input", "output", "stroke"],
},
markup: {
requiresArg: true,
describe: "String of Markup",
string: true,
conflicts: "json",
implies: ["input", "output"],
},
input: {
requiresArg: true,
describe: "Input image file to apply markup on",
string: true,
},
output: {
requiresArg: true,
describe: "Output image file to write to",
string: true,
},
stroke: {
requiresArg: true,
describe: "Stroke width to apply.",
number: true,
},
},
shimForFlags
)
.option("debug", {
describe: "Enable debug output.",
})
.alias("h", "help")
.strict()
.wrap(null);
var argv = yargs.argv;
function jsonCommand(argv) {
var parsed = JSON.parse(argv.json_string);
processJSON(parsed);
}
function markupCommand(argv) {
const stroke = argv.stroke ? Int(argv.stroke) : null;
convertMarkupToJSON(argv.markup_string, argv.input, argv.output, stroke).then(
processJSON
);
}
// Support flag form arguments (--json and --markup) for backwards CLI
// compatibility. Yargs is not able to support commands with long flag format.
// Even using aliases, the parser gets confused. Admittedly using options flags
// as commands was misguided on the original interface. In order preserve the
// existing behavior, we can continue to use flags, and this shim, to support
// flag form commands (actually options, just on the default command).
// Once we've deprecated these, we can drop this function, drop
// the `command $0` default command, and resume the `check` for exactly one
// command.
function shimForFlags(argv) {
if (!argv.json && !argv.markup) {
console.error(RequiredCommands);
process.exit(-1);
}
if (argv.json) {
argv.json_string = argv.json;
return jsonCommand(argv);
}
if (argv.markup) {
argv.markup_string = argv.markup;
return markupCommand(argv);
}
}
function processJSON(json) {
cleanJSON(json);
if (json.sourceFile) {
json.sourceFile = `file://${path.resolve(json.sourceFile)}`;
}
var finalSize = json["finalDimensions"];
var canvas = new Fabric.StaticCanvas(null, {
width: finalSize["width"],
height: finalSize["height"],
renderOnAddRemove: false,
});
var builder = ImageMarkupBuilder(canvas);
builder.processJSON(json, function () {
if (argv.debug) {
console.log(builder.getMarkupString());
}
});
}