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+ } ) ;
0 commit comments