shellfie 1.3.2 → 1.3.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shellfie",
3
- "version": "1.3.2",
3
+ "version": "1.3.6",
4
4
  "description": "create beautiful terminal screenshots programmatically",
5
5
  "main": "shellfie.js",
6
6
  "directories": {
@@ -24,7 +24,7 @@
24
24
  "author": "Tal Hayut",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
- "puppeteer": "^5.5.0",
27
+ "puppeteer": "latest",
28
28
  "xterm": "^4.14.1",
29
29
  "xterm-addon-fit": "^0.5.0"
30
30
  },
package/shellfie.js CHANGED
@@ -5,98 +5,111 @@ const fs = require('fs').promises;
5
5
  const { getStyles } = require('./utils/styles');
6
6
 
7
7
  async function shellfie(data, config) {
8
- try {
9
- if (!data || data.length === 0) {
10
- throw new Error('no data provided.\nshelffie needs a string || string[] to produce an image.')
11
- }
12
- const localPath = process.env.INIT_CWD || process.cwd();
13
- const { name, location, style, theme, ext, puppeteerOptions, viewport, mode, rendererType } = getConfig(config, localPath);
14
- const browser = await puppeteer.launch(puppeteerOptions);
15
- const page = await browser.newPage();
8
+ try {
9
+ if (!data || data.length === 0) {
10
+ throw new Error('no data provided.\nshelffie needs a string || string[] to produce an image.');
11
+ }
12
+ const localPath = process.env.INIT_CWD || process.cwd();
13
+ const {
14
+ name,
15
+ location,
16
+ style,
17
+ theme,
18
+ ext,
19
+ puppeteerOptions,
20
+ viewport,
21
+ mode,
22
+ rendererType
23
+ } = getConfig(config,localPath);
24
+ const browser = await puppeteer.launch(puppeteerOptions);
25
+ const page = await browser.newPage();
16
26
 
17
- await page.setViewport({ ...viewport, deviceScaleFactor: 4 });
18
- // read html template
19
- const templatePath = __dirname + '/template/template.html';
20
- let html = await fs.readFile(templatePath, 'utf-8');
21
- const lines = (Array.isArray(data) || mode === 'raw') ? data : data.split('\n');
27
+ await page.setViewport({ ...viewport, deviceScaleFactor: 4 });
28
+ // read html template
29
+ const templatePath = __dirname + '/template/template.html';
30
+ let html = await fs.readFile(templatePath, 'utf-8');
31
+ const lines = Array.isArray(data) || mode === 'raw' ? data : data.split('\n');
22
32
 
23
- // inject js scripts
24
- await page.addScriptTag({ path: require.resolve('xterm/lib/xterm.js') });
25
- await page.addScriptTag({ path: require.resolve('xterm-addon-fit/lib/xterm-addon-fit.js') });
33
+ // inject js scripts
34
+ await page.addScriptTag({ path: require.resolve('xterm/lib/xterm.js') });
35
+ await page.addScriptTag({ path: require.resolve('xterm-addon-fit/lib/xterm-addon-fit.js') });
26
36
 
27
- // set page html
28
- await page.setContent(html);
29
- await page.waitForSelector('.main');
30
- page.on('console', txt => console.log(txt.text()));
31
-
32
- // setup terminal
33
- await page.evaluate(({ options, lines, mode, viewport }) => {
34
- const fit = new FitAddon.FitAddon();
35
- const term = new Terminal({ ...options });
36
- term.open(document.getElementById('terminal'));
37
- term.loadAddon(fit);
38
- term.writeln('');
39
-
40
- if (mode === 'default') {
41
- lines.forEach(line => {
42
- line = line.replace(/\\x1.?\[/g, "\x1b[");
43
- line = `${line}\x1b[0m`;
44
- term.writeln(line);
45
- });
46
- } else {
47
- if (Array.isArray(lines)) {
48
- lines = lines.length > 1 ? lines : lines[0]
49
- }
50
- lines = lines.replace(/\n/g, '\r\n');
51
- term.write(lines.trim());
52
- }
53
- const height = viewport.height - 60;
54
- document.querySelector('.xterm-screen').style.width = `${viewport.width}px`;
55
- document.querySelector('.xterm-screen').style.height = `${height}px`;
56
- fit.fit();
37
+ // set page html
38
+ await page.setContent(html);
39
+ await page.waitForSelector('.main');
40
+ page.on('console', (txt) => console.log(txt.text()));
57
41
 
58
- }, { lines, mode, options: { theme, rendererType }, viewport });
59
-
60
- await page.evaluate((theme) => document.querySelector('.terminal').style.background = theme.background, theme);
61
- const templateStyle = __dirname + '/template/template.css';
42
+ // setup terminal
43
+ await page.evaluate(
44
+ ({ options, lines, mode, viewport }) => {
45
+ const fit = new FitAddon.FitAddon();
46
+ const term = new Terminal({ ...options, convertEol: true });
47
+ term.open(document.getElementById('terminal'));
48
+ term.loadAddon(fit);
49
+ term.writeln('');
62
50
 
63
- // inject user style
64
- if (style) {
65
- const styles = getStyles(style);
66
- const className = rendererType === 'dom' ? '.terminal.xterm' : 'canvas';
67
- const content = `${className} {${styles}}`
68
- await page.addStyleTag({content})
51
+ if (mode === 'default') {
52
+ lines.forEach((line) => {
53
+ line = line.replace(/\\x1.?\[/g, "\x1b[");
54
+ line = `${line}\x1b[0m`;
55
+ term.writeln(line);
56
+ });
57
+ } else {
58
+ if (Array.isArray(lines)) {
59
+ lines = lines.length > 1 ? lines : lines[0];
60
+ }
61
+ lines = lines.replace(/\n/g, '\r\n');
62
+ term.write(lines.trim());
69
63
  }
64
+ const height = viewport.height - 60;
65
+ document.querySelector('.xterm-screen').style.width = `${viewport.width}px`;
66
+ document.querySelector('.xterm-screen').style.height = `${height}px`;
67
+ fit.fit();
68
+ },
69
+ { lines, mode, options: { theme, rendererType }, viewport }
70
+ );
70
71
 
71
- // inject styles
72
- await page.addStyleTag({ path: templateStyle });
73
- await page.addStyleTag({ path: `${localPath}/node_modules/xterm/css/xterm.css` });
74
-
72
+ await page.evaluate((theme) => (document.querySelector('.terminal').style.background = theme.background), theme);
73
+ const templateStyle = __dirname + '/template/template.css';
75
74
 
75
+ // inject user style
76
+ if (style) {
77
+ const styles = getStyles(style);
78
+ const className = rendererType === 'dom' ? '.terminal.xterm' : 'canvas';
79
+ const content = `${className} {${styles}}`;
80
+ console.log(content);
81
+ await page.addStyleTag({ content });
82
+ }
76
83
 
77
- await page.evaluateHandle('document.fonts.ready');
78
-
79
- // crop image
80
- const clip = await (await page.$(".main")).boundingBox();
81
- clip.height = Math.min(clip.height, page.viewport().height);
82
- clip.width = Math.min(clip.width, page.viewport().width);
84
+ // inject styles
85
+ await page.addStyleTag({ path: templateStyle });
86
+ await page.addStyleTag({ path: `${localPath}/node_modules/xterm/css/xterm.css` });
83
87
 
84
- await page.evaluate((height) => document.querySelector('.main').style.height = height, clip.height);
88
+ await page.evaluateHandle('document.fonts.ready');
85
89
 
86
- // create shellfies dir
87
- const pngsDir = path.resolve(location);
88
- const exists = await fs.access(pngsDir).then(() => 1).catch(() => 0);
89
- if (!exists) await fs.mkdir(pngsDir);
90
+ // crop image
91
+ const clip = await (await page.$('.main')).boundingBox();
92
+ clip.height = Math.min(clip.height, page.viewport().height);
93
+ clip.width = Math.min(clip.width, page.viewport().width);
90
94
 
91
- // take screenshot
92
- await page.screenshot({ path: `${pngsDir}/${name}.${ext}`, clip, omitBackground: true, type: ext });
93
- await browser.close();
94
- } catch (err) {
95
- console.error(new Error(`\x1b[32mshellfie error: ${err.message}\x1b[0m`))
96
- console.error(err.stack)
97
- throw err;
98
- }
99
- }
95
+ await page.evaluate((height) => (document.querySelector('.main').style.height = height), clip.height);
96
+
97
+ // create shellfies dir
98
+ const pngsDir = path.resolve(location);
99
+ const exists = await fs
100
+ .access(pngsDir)
101
+ .then(() => 1)
102
+ .catch(() => 0);
103
+ if (!exists) await fs.mkdir(pngsDir);
100
104
 
105
+ // take screenshot
106
+ await page.screenshot({ path: `${pngsDir}/${name}.${ext}`, clip, omitBackground: true, type: ext });
107
+ await browser.close();
108
+ } catch (err) {
109
+ console.error(new Error(`\x1b[32mshellfie error: ${err.message}\x1b[0m`));
110
+ console.error(err.stack);
111
+ throw err;
112
+ }
113
+ }
101
114
 
102
- module.exports = shellfie
115
+ module.exports = shellfie;
package/utils/styles.js CHANGED
@@ -1,6 +1,9 @@
1
1
  function processLine(key, value) {
2
- const [word] = key.match(/(^[a-z]|[A-Z0-9])[a-z]*/);
3
- if (key !== word) key = `${word}-${key.replace(word, '').toLowerCase()}`;
2
+ const words = key.split(/(?=[A-Z])/);
3
+ if (words.length > 1) {
4
+ key = words.map(word => word.toLowerCase()).join('-')
5
+ }
6
+
4
7
  return `${key}: ${value};`
5
8
  }
6
9