canvasxpress-cli 61.0.0 → 61.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cmds/io-playwright.js +149 -0
- package/index.js +7 -4
- package/package.json +2 -1
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
const util = require('util');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const ora = require('ora');
|
|
5
|
+
const { chromium } = require('playwright');
|
|
6
|
+
|
|
7
|
+
module.exports = async (obj) => {
|
|
8
|
+
|
|
9
|
+
const dirname = process.argv[1].replace('/bin/canvasxpress', '');
|
|
10
|
+
|
|
11
|
+
const today = new Date().toISOString().replace('-', '').split('T')[0].replace('-', '');
|
|
12
|
+
|
|
13
|
+
const logFile = fs.createWriteStream(('/tmp/io-' + today + '.log'), { flags: 'a' });
|
|
14
|
+
|
|
15
|
+
const logStdout = process.stdout;
|
|
16
|
+
|
|
17
|
+
const html = (obj.input && obj.input.match(/.html$/) ? obj.input : ("file://" + dirname + "/src/canvasXpress.html")) + (obj.debug ? "?cXH" : "");
|
|
18
|
+
|
|
19
|
+
console.log = function () {
|
|
20
|
+
logFile.write(util.format.apply(null, arguments) + '\n');
|
|
21
|
+
logStdout.write(util.format.apply(null, arguments) + '\n');
|
|
22
|
+
}
|
|
23
|
+
console.error = console.log;
|
|
24
|
+
|
|
25
|
+
const spinner = ora().start();
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
|
|
29
|
+
const browser = await chromium.launch({
|
|
30
|
+
headless: obj.debug ? false : true,
|
|
31
|
+
devtools: obj.debug ? true : false,
|
|
32
|
+
args: [
|
|
33
|
+
'--no-sandbox',
|
|
34
|
+
'--disable-setuid-sandbox',
|
|
35
|
+
'--allow-file-access-from-files',
|
|
36
|
+
'--enable-local-file-accesses'
|
|
37
|
+
]
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const context = await browser.newContext({
|
|
41
|
+
viewport: {
|
|
42
|
+
width: 1000,
|
|
43
|
+
height: 1000
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const page = await context.newPage();
|
|
48
|
+
|
|
49
|
+
page.on('console', (consoleObj) => console.log(consoleObj.text()));
|
|
50
|
+
|
|
51
|
+
if (obj.input && !obj.input.match(/^file|^http/)) {
|
|
52
|
+
obj.input = "file://" + path.resolve(obj.input);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const func = function (obj) {
|
|
56
|
+
|
|
57
|
+
if (obj.debug) {
|
|
58
|
+
debugger;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
var exec = function (cx, tg) {
|
|
62
|
+
switch (obj.cmd) {
|
|
63
|
+
case 'png':
|
|
64
|
+
cx.print(false, tg + '.png');
|
|
65
|
+
break;
|
|
66
|
+
case 'svg':
|
|
67
|
+
cx.saveSVG(false, tg + '.svg');
|
|
68
|
+
break;
|
|
69
|
+
case 'json':
|
|
70
|
+
cx.save(false, tg + '.json');
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
var clbk = function () {
|
|
76
|
+
var cx = CanvasXpress.instances[0];
|
|
77
|
+
var dt = arguments[0];
|
|
78
|
+
if (dt.config) {
|
|
79
|
+
dt.config.isNode = true;
|
|
80
|
+
}
|
|
81
|
+
cx.updateData(dt, false, false, JSON.parse(JSON.stringify(dt.config)));
|
|
82
|
+
console.log(logStr);
|
|
83
|
+
exec(cx, dt.renderTo);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
var cx = CanvasXpress.instances[0];
|
|
87
|
+
|
|
88
|
+
const extFile = obj.input ? obj.input.split(".").pop() : false;
|
|
89
|
+
|
|
90
|
+
const logStr = "Creating " + obj.cmd + " file from " + (obj.input ? obj.input : 'input data') + " (" + obj.output + (obj.target || cx.target) + (extFile == 'html' ? '' : '-1') + "." + obj.cmd + ")";
|
|
91
|
+
|
|
92
|
+
cx.setDimensions(obj.width, obj.height);
|
|
93
|
+
|
|
94
|
+
if (obj.input) {
|
|
95
|
+
try {
|
|
96
|
+
switch (extFile) {
|
|
97
|
+
case 'html':
|
|
98
|
+
console.log(logStr);
|
|
99
|
+
exec(cx, obj.target || cx.target);
|
|
100
|
+
return;
|
|
101
|
+
case 'json':
|
|
102
|
+
case 'csv':
|
|
103
|
+
case 'text':
|
|
104
|
+
case 'png':
|
|
105
|
+
console.log(logStr);
|
|
106
|
+
CanvasXpress.loadRemoteData(obj.target || cx.target, obj.input, obj.config, false, clbk);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
} catch (err) {
|
|
110
|
+
console.error(err);
|
|
111
|
+
}
|
|
112
|
+
} else if (obj.data) {
|
|
113
|
+
try {
|
|
114
|
+
console.log(logStr);
|
|
115
|
+
cx.updateData(obj.data, false, false, obj.config);
|
|
116
|
+
exec(cx, obj.target || cx.target);
|
|
117
|
+
return;
|
|
118
|
+
} catch (err) {
|
|
119
|
+
console.error(err);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const client = await context.newCDPSession(page);
|
|
126
|
+
|
|
127
|
+
await client.send('Page.setDownloadBehavior', {
|
|
128
|
+
behavior: 'allow',
|
|
129
|
+
downloadPath: obj.output
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
await page.goto(html);
|
|
133
|
+
|
|
134
|
+
await page.waitForFunction('typeof (CanvasXpress) !== undefined && CanvasXpress.ready');
|
|
135
|
+
|
|
136
|
+
await page.evaluate(`(${func.toString()})(${JSON.stringify(obj)})`);
|
|
137
|
+
|
|
138
|
+
await setTimeout(() => {
|
|
139
|
+
browser.close();
|
|
140
|
+
spinner.stop();
|
|
141
|
+
}, obj.tmout);
|
|
142
|
+
|
|
143
|
+
} catch (err) {
|
|
144
|
+
|
|
145
|
+
console.error(err);
|
|
146
|
+
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
}
|
package/index.js
CHANGED
|
@@ -30,6 +30,8 @@ module.exports = () => {
|
|
|
30
30
|
|
|
31
31
|
let tmout = args.timeout || args.q || 2500;
|
|
32
32
|
|
|
33
|
+
let usePlaywright = args.playwright || args.pw || process.argv.includes('-playwright') || false;
|
|
34
|
+
|
|
33
35
|
let data = dat ? JSON.parse(dat) : false;
|
|
34
36
|
|
|
35
37
|
let config = conf ? JSON.parse(conf) : false;
|
|
@@ -46,6 +48,7 @@ module.exports = () => {
|
|
|
46
48
|
width: width,
|
|
47
49
|
height: height,
|
|
48
50
|
tmout: tmout,
|
|
51
|
+
usePlaywright: usePlaywright,
|
|
49
52
|
args: args
|
|
50
53
|
}
|
|
51
54
|
|
|
@@ -75,7 +78,7 @@ module.exports = () => {
|
|
|
75
78
|
case 'svg':
|
|
76
79
|
case 'json':
|
|
77
80
|
if (input || data) {
|
|
78
|
-
require('./cmds/io')(obj);
|
|
81
|
+
require(usePlaywright ? './cmds/io-playwright' : './cmds/io')(obj);
|
|
79
82
|
} else {
|
|
80
83
|
require('./cmds/help')(args);
|
|
81
84
|
}
|
|
@@ -90,7 +93,7 @@ module.exports = () => {
|
|
|
90
93
|
require('./cmds/help')(args);
|
|
91
94
|
break;
|
|
92
95
|
case 'test':
|
|
93
|
-
require('./cmds/io')({
|
|
96
|
+
require(usePlaywright ? './cmds/io-playwright' : './cmds/io')({
|
|
94
97
|
cmd: "png",
|
|
95
98
|
input: "https://www.canvasxpress.org/examples/bar-1.html",
|
|
96
99
|
output: "./test/",
|
|
@@ -104,7 +107,7 @@ module.exports = () => {
|
|
|
104
107
|
tmout: tmout,
|
|
105
108
|
args: args
|
|
106
109
|
});
|
|
107
|
-
require('./cmds/io')({
|
|
110
|
+
require(usePlaywright ? './cmds/io-playwright' : './cmds/io')({
|
|
108
111
|
cmd: "svg",
|
|
109
112
|
input: "https://www.canvasxpress.org/examples/bar-1.html",
|
|
110
113
|
output: "./test/",
|
|
@@ -118,7 +121,7 @@ module.exports = () => {
|
|
|
118
121
|
tmout: tmout,
|
|
119
122
|
args: args
|
|
120
123
|
});
|
|
121
|
-
require('./cmds/io')({
|
|
124
|
+
require(usePlaywright ? './cmds/io-playwright' : './cmds/io')({
|
|
122
125
|
cmd: "json",
|
|
123
126
|
input: "https://www.canvasxpress.org/examples/bar-1.html",
|
|
124
127
|
output: "./test/",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "canvasxpress-cli",
|
|
3
|
-
"version": "61.0.
|
|
3
|
+
"version": "61.0.2",
|
|
4
4
|
"license": {
|
|
5
5
|
"type": "Dual licensing",
|
|
6
6
|
"url": "https://www.canvasxpress.org/docs/license.html"
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"figlet": "^1.2.1",
|
|
35
35
|
"minimist": "^1.2.0",
|
|
36
36
|
"ora": "^3.4.0",
|
|
37
|
+
"playwright": "^1.40.0",
|
|
37
38
|
"puppeteer": "^13.0.0"
|
|
38
39
|
}
|
|
39
40
|
}
|