| "Use strict "; Var fs = require ('fs '); Var http = require ('http '); Var path = require ('path '); Var r_url = require ('url '); Var dirCache ={}; // check whether the cache is reduced Function makedir (pathStr, callback ){ If (dirCache [pathStr] = 1 ){ Callback (); } Else { Fs. exists (pathStr, function (exists ){ If (exists = true ){ DirCache [pathStr] = 1; Callback (); } Else { Makedir (path. dirname (pathStr), function (){ Fs. mkdir (pathStr, function (){ DirCache [pathStr] = 1; Callback (); }) }); } }) } }; Var reg =/[:,] \ s * url \ (['"]?. *? (\ 1) \)/g Var reg2 = // \ (['"]?) (.*?) (\ 1 )\)/ Var isDownMap = {}; Var downImgFromCss = function (URL ){ Http. get (URL, function (res ){ // Console. log (path. resolve (process. cwd (), 'index.min.css ')) // Res. pipe (fs. createWriteStream (path. resolve (process. cwd (), 'index.min.css '))); Var body = ""; Res. setEncoding ('utf8 '); Res. on ('data', function (chunk ){ Body + = chunk; }); Res. on ('end', function (){ Var match = body. match (reg ); For (var I = 0, len = match. length; I <len; I ++ ){ Var m = match [I]. match (reg2 ); If (m & m [2]) { Var url = m [2]; Let imgUrl = r_url.resolve (URL, url ); If (! IsDownMap [imgUrl]) { Var uo = r_url.parse (imgUrl ); Let filepath = CWD + '/' + uo. hostname + uo. pathname; Makedir (path. dirname (filepath), function (){ Http. get (imgUrl, function (res ){ Res. pipe (fs. createWriteStream (filepath )); }) }) IsDownMap [imgUrl] = 1; } } } }); }); } Var URLS = process. argv [2]. split (','); Var CWD = process. cwd (); // Download resources URLS. forEach (function (URL ){ Var uo = r_url.parse (URL ); Var filepath; If (uo. pathname = '/' | uo. pathname = ''){ Filepath = CWD + '/' + uo. hostname + '/index.html '; } Else { Filepath = CWD + '/' + uo. hostname + uo. pathname; } Makedir (path. dirname (filepath), function (){ Http. get (URL, function (res ){ If (URL.indexOf('.css ')! =-1 | (res. headers ["content-type"] & res. headers ["content-type"]. indexOf ('text/css ')! =-1 )){ Console. log ('down images form css file: '+ URL + '.'); DownImgFromCss (URL ); } Res. pipe (fs. createWriteStream (filepath )); }) }); }); |