Skip to content

Commit 6b8db70

Browse files
committed
画像キャプチャツール : バージョンの最新化(v1.9→2.1) / TypeScriptからの脱却
1 parent bb456c0 commit 6b8db70

9 files changed

Lines changed: 74 additions & 162 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ html/styles/.sass-cache/*
1111
html-build/.sass-cache
1212
html-build/node_modules
1313
html-build/typescript/main.js.map
14+
/tool-generate-captures/package-lock.json

tool-generate-captures/ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npm install
1515
コマンドラインで次の命令を実行して、1分ぐらい待ちます。
1616

1717
```
18-
node index
18+
npm run capture
1919
```
2020

2121
実行が終わると(終了しないので、[cmd]+[c]で終了します)、imgsフォルダにキャプチャ画像が生成されます。

tool-generate-captures/index.js

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
1-
///<reference path="libs/node/node.d.ts" />
2-
///<reference path="libs/phantom/phantom.d.ts" />
3-
"use strict";
4-
// gazo_fileがあるかないかの確認
5-
var fs = require('fs');
6-
var childProcess = require('child_process');
7-
var phantomjs = require('phantomjs');
8-
let binPath = phantomjs.path;
9-
let render = "render.js";
10-
// この辺が引数みたいな感じで渡したい
11-
const width = "180";
12-
const height = "80";
13-
const IGNORE_LIST = [".idea", ".DS_Store", "imgs", "css"];
14-
fs.readdir("../samples", (err, files) => {
15-
let promises = [];
16-
for (let i = 0; i < files.length; i++) {
17-
if (IGNORE_LIST.indexOf(files[i]) > -1) {
18-
continue;
19-
}
1+
// 定数宣言 (必要に応じて書き換えてください)
2+
const TARGET_FOLDER = '../samples'; // キャプチャーしたいHTMLのフォルダー
3+
const OUTPUT_FOLDER = '../imgs'; // 保存先のフォルダー
4+
const IGNORE_LIST = ['.DS_Store', 'Thumbs.db', '.idea']; // 無視リスト
5+
const PHANTOM_JS_FILE = 'render.js'; // PhantomJSのパス
206

21-
let outputFilePath = `../imgs/${files[i]}.png`;
22-
let url = `../samples/${files[i]}`;
23-
let options = ["render.js", url, outputFilePath, width, height];
24-
let childPromise = new Promise((resolve) => {
25-
console.log(`${i} : ${outputFilePath}`);
26-
// ここでrender.jsをphantomjsで呼び出して実行する
27-
childProcess.execFile(binPath, options, (error, stdout, stderr) => {
28-
if (error != null) {
29-
console.error(stderr);
30-
}
31-
else {
32-
console.log(stdout);
33-
}
34-
resolve();
35-
});
7+
// 具体的な処理
8+
const fs = require('fs');
9+
const childProcess = require('child_process');
10+
const phantomjs = require('phantomjs');
11+
const binPath = phantomjs.path;
12+
const render = 'render.js';
13+
14+
fs.readdir(TARGET_FOLDER, (err, files) => {
15+
const promises = [];
16+
files.map(file => {
17+
if (IGNORE_LIST.includes(file) === false) {
18+
const targetFilePath = `${TARGET_FOLDER}/${file}`;
19+
const outputFilePath = `${OUTPUT_FOLDER}/${file}.png`;
20+
const options = [
21+
PHANTOM_JS_FILE,
22+
targetFilePath,
23+
outputFilePath,
24+
];
25+
const childPromise = new Promise((resolve) => {
26+
// ここでrender.jsをphantomjsで呼び出して実行する
27+
childProcess.execFile(binPath, options, (error, stdout, stderr) => {
28+
// プロセスの対象を出力
29+
console.log(`${file} をPhantomJSで変換を試みました`);
30+
// PhantomJS側のconsole情報を出力
31+
console.log(stdout);
32+
if (error) {
33+
// 書き出し失敗の詳細情報を出力
34+
console.error(error);
35+
} else {
36+
// 書き出し成功
37+
}
38+
resolve();
3639
});
37-
promises.push(childPromise);
40+
});
41+
promises.push(childPromise);
3842
}
39-
Promise
40-
.all(promises)
41-
.then((results) => {
42-
console.log("finish!!!");
43+
});
44+
45+
console.log(`${TARGET_FOLDER} フォルダーのキャプチャーを始めます。ちょっとまってね!`);
46+
47+
Promise
48+
.all(promises)
49+
.then((results) => {
50+
console.log(`${TARGET_FOLDER} のキャプチャーが終わったので ${OUTPUT_FOLDER} に入れておきました!!`);
4351
});
44-
console.log("step - 2");
45-
});
46-
console.log("step - 1");
52+
});

tool-generate-captures/index.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

tool-generate-captures/index.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
{
2-
"name": "tutorial-createjs",
3-
"version": "1.0.0",
4-
"description": "",
5-
"main": "index.js",
62
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
3+
"capture": "node index.js"
84
},
9-
"repository": {
10-
"type": "git",
11-
"url": "git+https://ics-ikeda@github.com/ics-creative/tutorial-createjs.git"
12-
},
13-
"author": "",
14-
"license": "ISC",
15-
"bugs": {
16-
"url": "https://github.com/ics-creative/tutorial-createjs/issues"
17-
},
18-
"homepage": "https://github.com/ics-creative/tutorial-createjs#readme",
195
"devDependencies": {
20-
"node-phantom": "^0.2.5",
21-
"phantomjs": "^1.9.18"
6+
"phantomjs": "^2.1.7"
227
}
238
}

tool-generate-captures/params.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tool-generate-captures/render.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
var page = require('webpage').create();
1+
const page = require('webpage').create();
2+
const system = require('system');
23

34
// argsは、phantom.argsでアクセスできる。
4-
var address = phantom.args[0];
5-
var output = phantom.args[1];
6-
var width = phantom.args[2];
7-
var height = phantom.args[3];
8-
9-
console.log(address + " : " + output);
5+
const address = system.args[1];
6+
const output = system.args[2];
107

118
page.viewportSize = {
12-
width: width,
13-
height: height,
14-
margin: "0px",
15-
padding: "0px"
9+
margin: '0px',
10+
padding: '0px'
1611
};
1712

1813
page.open(address,
19-
function (status) {
20-
console.log("status : " + status);
21-
if (status == 'success') {
22-
setTimeout(function () {
23-
// 出力
24-
page.render(output);
25-
// 終了
26-
phantom.exit();
27-
}, 500);
28-
} else {
29-
console.log('error');
30-
}
31-
}
14+
function (status) {
15+
if (status === 'success') {
16+
setTimeout(function () {
17+
// 出力
18+
page.render(output);
19+
console.error('キャプチャーに成功しました');
20+
// 成功として終了
21+
phantom.exit(0);
22+
}, 50)
23+
24+
} else {
25+
console.error('キャプチャーに失敗しました');
26+
// エラーとして終了
27+
phantom.exit(1);
28+
}
29+
}
3230
);

tool-generate-captures/tsconfig.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)