Skip to content

Commit fd96578

Browse files
committed
feat: create image gists
1 parent e708e88 commit fd96578

File tree

5 files changed

+196
-31
lines changed

5 files changed

+196
-31
lines changed

.DS_Store

6 KB
Binary file not shown.

cli.js

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#!/usr/bin/env node
22
"use strict";
3+
const { parse, resolve } = require("path");
34

45
const sharp = require("sharp");
56
const execa = require("execa");
67
const gifsicle = require("gifsicle");
78
const updateNotifier = require("update-notifier");
89
const meow = require("meow");
10+
const got = require("got");
11+
const cp = require("cp-file");
12+
13+
const { cloneGistPath, addAll, commitAll, push } = require("./git-util");
914

1015
const CONTAINER_WIDTH = 727;
1116
const CUT_WIDTH = 325;
@@ -26,7 +31,12 @@ const cli = meow(
2631
$ crop-github-images unicorn.png
2732
`,
2833
{
29-
string: ["_"]
34+
flags: {
35+
"github-token": {
36+
type: "string",
37+
alias: "t"
38+
}
39+
}
3040
}
3141
);
3242

@@ -54,14 +64,15 @@ const getXY = index => {
5464

5565
const cropImage = async path => {
5666
try {
57-
const ext = path.split(".").reverse()[0];
67+
const { ext } = parse(path);
5868
const image = sharp(path);
5969
const { width, height } = await image.metadata();
6070
const resizeOpts =
6171
width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
6272
? { width: CONTAINER_WIDTH, height: MINIMUM_HEIGHT, fit: "outside" }
6373
: { width: CONTAINER_WIDTH };
6474
const resized = image.clone().resize(resizeOpts);
75+
const files = [];
6576
for (let i = 0; i < 6; i++) {
6677
const filename = `${i}.${ext}`;
6778
const { x, y } = getXY(i);
@@ -70,37 +81,88 @@ const cropImage = async path => {
7081
.extract({ left: x, top: y, width: CUT_WIDTH, height: CUT_HEIGHT })
7182
.toFile(filename);
7283
console.log(`Successfully cropped ${filename}.`);
84+
files.push(filename);
7385
}
86+
return files;
7487
} catch (e) {
7588
console.error(e);
7689
}
7790
};
7891

7992
const cropGif = async path => {
80-
const { width, height } = await sharp(path).metadata();
81-
const resizeOpts =
82-
width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
83-
? ["--resize", "_x513"]
84-
: ["--resize", "727x_"];
85-
const resized = "resized.gif";
86-
await execa(gifsicle, [...resizeOpts, "-o", resized, path]);
87-
for (let i = 0; i < 6; i++) {
88-
const filename = `${i}.gif`;
89-
const { x, y } = getXY(i);
90-
await execa(gifsicle, [
91-
"--crop",
92-
`${x},${y}+${CUT_WIDTH}x${CUT_HEIGHT}`,
93-
"-o",
94-
filename,
95-
resized
96-
]);
97-
console.log(`Successfully cropped ${filename}.`);
93+
try {
94+
const { width, height } = await sharp(path).metadata();
95+
const resizeOpts =
96+
width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
97+
? ["--resize", "_x513"]
98+
: ["--resize", "727x_"];
99+
const resized = "resized.gif";
100+
await execa(gifsicle, [...resizeOpts, "-o", resized, path]);
101+
const files = [];
102+
for (let i = 0; i < 6; i++) {
103+
const filename = `${i}.gif`;
104+
const { x, y } = getXY(i);
105+
await execa(gifsicle, [
106+
"--crop",
107+
`${x},${y}+${CUT_WIDTH}x${CUT_HEIGHT}`,
108+
"-o",
109+
filename,
110+
resized
111+
]);
112+
console.log(`Successfully cropped ${filename}.`);
113+
files.push(filename);
114+
}
115+
return files;
116+
} catch (e) {
117+
console.error(e);
118+
}
119+
};
120+
121+
const crop = async (path, githubToken) => {
122+
let files = [];
123+
const isGif = path.endsWith(".gif");
124+
if (isGif) {
125+
files = await cropGif(path);
126+
} else {
127+
files = await cropImage(path);
128+
}
129+
if (githubToken) {
130+
try {
131+
for (const file of files) {
132+
const files = {
133+
[file]: {
134+
content: "Hello"
135+
}
136+
};
137+
const body = JSON.stringify({
138+
description: `Gist for ${file}`,
139+
public: true,
140+
files
141+
});
142+
const { body: res } = await got.post("gists", {
143+
baseUrl: "https://api.github.com",
144+
headers: {
145+
Authorization: `token ${githubToken}`
146+
},
147+
body
148+
});
149+
const data = JSON.parse(res);
150+
console.log(`Gist for ${file} has been created in ${data.html_url}`);
151+
await cloneGistPath(data.id);
152+
await cp(file, `${data.id}/${file}`);
153+
const gitPath = resolve(`./${data.id}/.git`);
154+
await addAll(gitPath);
155+
await commitAll(gitPath);
156+
await push(gitPath);
157+
console.log(`${isGif ? "GIF" : "Image"} has been added to the gist`);
158+
}
159+
} catch (e) {
160+
console.error(e);
161+
}
98162
}
99163
};
100164

101165
const path = cli.input[0];
102-
if (path.endsWith(".gif")) {
103-
cropGif(path);
104-
} else {
105-
cropImage(path);
106-
}
166+
const { githubToken } = cli.flags;
167+
168+
crop(path, githubToken);

git-util.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const execa = require("execa");
2+
3+
exports.cloneGistPath = hash =>
4+
execa("git", ["clone", `https://gist.github.com/${hash}.git`]);
5+
6+
exports.addAll = gitPath => execa("git", [`--git-dir=${gitPath}`, "add", "."]);
7+
8+
exports.commitAll = gitPath =>
9+
execa("git", [`--git-dir=${gitPath}`, "commit", "-am", '"update gist file"']);
10+
11+
exports.push = gitPath => execa("git", [`--git-dir=${gitPath}`, "push"]);

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
"profile"
2727
],
2828
"dependencies": {
29+
"cp-file": "^6.2.0",
2930
"execa": "^1.0.0",
3031
"gifsicle": "^4.0.1",
32+
"got": "^9.6.0",
3133
"meow": "^5.0.0",
3234
"sharp": "^0.22.0",
3335
"update-notifier": "^2.5.0"

0 commit comments

Comments
 (0)