Skip to content

Commit b07fdf0

Browse files
author
Guillaume Martigny
committed
Add meow for the CLI and eslint
1 parent 67240b8 commit b07fdf0

File tree

7 files changed

+1537
-15
lines changed

7 files changed

+1537
-15
lines changed

api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const loremPix = require(".");
21
const { parse } = require("url");
2+
const loremPix = require(".");
33

44
module.exports = (request, response) => {
55
const { query } = parse(request.url, true);
66
const { width, height, color } = query;
77

8-
const data = loremPix(width && Number(width), height && Number(height) || undefined, color || undefined);
8+
const data = loremPix(width && Number(width), (height && Number(height)) || undefined, color || undefined);
99

1010
response.writeHead(200, {
1111
"Content-Type": "image/png",

cli.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
#!/usr/bin/env node
2-
const loremPix = require(".");
32
const { writeFileSync } = require("fs");
3+
const meow = require("meow");
4+
const loremPix = require(".");
5+
6+
const run = (cli) => {
7+
const [width, height, color] = cli.input;
8+
9+
if (!width) {
10+
cli.showHelp();
11+
return;
12+
}
13+
14+
try {
15+
const dataURL = loremPix(Number(width), height && Number(height), color);
16+
17+
writeFileSync(cli.flags.output, dataURL.split(",")[1], "base64");
18+
console.log("Done creating image.");
19+
}
20+
catch (error) {
21+
console.error(error.message);
22+
}
23+
};
24+
25+
const defaultFileName = "lorem.png";
26+
const cli = meow(`Create a placeholder image at light speed.
27+
If omitted, color is picked randomly and height equals width.
28+
29+
Usage
30+
$ lorem-pix <width> [<height> [<color> [--output <file>]]]
431
5-
const [width, height, color] = process.argv.slice(2);
32+
Options
33+
--output, -o Output file path, default to ${defaultFileName}
634
7-
try {
8-
const dataURL = loremPix(width && Number(width), height && Number(height), color);
35+
Example
36+
$ lorem-pix 800 600 red
37+
`, {
38+
flags: {
39+
output: {
40+
alias: "o",
41+
type: "string",
42+
default: defaultFileName,
43+
},
44+
},
45+
});
946

10-
writeFileSync("lorem.png", dataURL.split(",")[1], "base64");
11-
console.log("Done creating image.");
12-
}
13-
catch (error) {
14-
console.error(error.message);
15-
}
47+
run(cli);

front/live-preview.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ const livePreview = (img, width, height, color) => {
55
img.src = url;
66
return url;
77
};
8+
9+
export default livePreview;

index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ <h1>Lorem pix</h1>
3131
<figcaption id="caption"></figcaption>
3232
<img src="" alt="live preview" id="preview"/>
3333
</figure>
34-
<script src="front/live-preview.js"></script>
35-
<script>
34+
<script src="front/live-preview.js" type="module"></script>
35+
<script type="module">
36+
import livePreview from "./front/live-preview.js";
3637
const form = document.getElementById("form");
3738
const caption = document.getElementById("caption");
3839
const img = document.getElementById("preview");

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
const { createCanvas } = require("canvas");
22
const clamp = require("clamp");
33

4+
/**
5+
* Create a placeholder image at light speed.
6+
* @param {Number} width - Width of the image in pixels (from 1 to 10,000)
7+
* @param {Number} [height=width] - Height of the image in pixels (from 1 to 10,000), equal <width> if omitted
8+
* @param {String} [color] - Background color of the image, picked randomly if omitted, can be any valid CSSColor [https://developer.mozilla.org/docs/Web/CSS/color_value]
9+
* @example loremPix(800, 600, "red");
10+
* @return {String} Base 64 encoding of the image
11+
*/
412
const loremPix = (width, height = width, color = `#${Math.random().toString(16).slice(-6)}`) => {
513
if (!width) {
614
throw RangeError("Lorem-pix function need at least one non-null argument.");

0 commit comments

Comments
 (0)