| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var fs = require("fs"); | 5 var fs = require("fs"); |
| 6 var http = require("http"); | 6 var http = require("http"); |
| 7 var https = require("https"); | 7 var https = require("https"); |
| 8 var path = require("path"); |
| 8 var parseURL = require("url").parse; | 9 var parseURL = require("url").parse; |
| 9 var Stream = require("stream").Transform; | 10 var Stream = require("stream").Transform; |
| 10 | 11 |
| 11 function fetch(url) | 12 function fetch(url) |
| 12 { | 13 { |
| 13 return new Promise(fetchPromise); | 14 return new Promise(fetchPromise); |
| 14 | 15 |
| 15 function fetchPromise(resolve, reject) | 16 function fetchPromise(resolve, reject) |
| 16 { | 17 { |
| 17 var request; | 18 var request; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 38 response.on("data", chunk => body.push(chunk)); | 39 response.on("data", chunk => body.push(chunk)); |
| 39 response.on("end", () => resolve(body.read())); | 40 response.on("end", () => resolve(body.read())); |
| 40 } | 41 } |
| 41 } | 42 } |
| 42 | 43 |
| 43 function atob(str) | 44 function atob(str) |
| 44 { | 45 { |
| 45 return new Buffer(str, "base64").toString("binary"); | 46 return new Buffer(str, "base64").toString("binary"); |
| 46 } | 47 } |
| 47 | 48 |
| 48 | |
| 49 function isFile(path) | 49 function isFile(path) |
| 50 { | 50 { |
| 51 try { | 51 try { |
| 52 return fs.statSync(path).isFile(); | 52 return fs.statSync(path).isFile(); |
| 53 } catch (e) { | 53 } catch (error) { |
| 54 return false; | 54 return false; |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 | 57 |
| 58 function isDir(path) |
| 59 { |
| 60 try { |
| 61 return fs.statSync(path).isDirectory(); |
| 62 } catch (error) { |
| 63 return false; |
| 64 } |
| 65 } |
| 66 |
| 67 function copy(src, dest) |
| 68 { |
| 69 try { |
| 70 var targetFilePath = path.resolve(dest, path.basename(src)); |
| 71 fs.writeFileSync(targetFilePath, fs.readFileSync(src)); |
| 72 } catch (error) { |
| 73 throw new Error(`Received an error: [${error}] while trying to copy: ${s
rc} -> ${dest}`); |
| 74 } |
| 75 } |
| 76 |
| 77 function copyRecursive(src, dest) |
| 78 { |
| 79 try { |
| 80 var targetDirPath = path.resolve(dest, path.basename(src)); |
| 81 if (!fs.existsSync(targetDirPath)) |
| 82 fs.mkdirSync(targetDirPath); |
| 83 if (isDir(src)) { |
| 84 var files = fs.readdirSync(src); |
| 85 for (var i = 0; i < files.length; i++) { |
| 86 var childPath = path.resolve(src, files[i]); |
| 87 if (isDir(childPath)) { |
| 88 copyRecursive(childPath, targetDirPath); |
| 89 } else { |
| 90 var targetFilePath = path.resolve(targetDirPath, path.basen
ame(childPath)); |
| 91 fs.writeFileSync(targetFilePath, fs.readFileSync(childPath))
; |
| 92 } |
| 93 } |
| 94 } |
| 95 } catch (error) { |
| 96 throw new Error(`Received an error: [${error}] while trying to copy: ${s
rc} -> ${dest}`); |
| 97 } |
| 98 } |
| 99 |
| 100 function removeRecursive(filePath) |
| 101 { |
| 102 try { |
| 103 if (fs.existsSync(filePath)) { |
| 104 var files = fs.readdirSync(filePath); |
| 105 for (var i = 0; i < files.length; i++) { |
| 106 var childPath = path.resolve(filePath, files[i]); |
| 107 if (isDir(childPath)) |
| 108 removeRecursive(childPath); |
| 109 else |
| 110 fs.unlinkSync(childPath); |
| 111 } |
| 112 fs.rmdirSync(filePath); |
| 113 } |
| 114 } catch (error) { |
| 115 throw new Error(`Received an error: [${error}] while trying to remove: $
{filePath}`); |
| 116 } |
| 117 } |
| 118 |
| 119 function includes(sequence, target) |
| 120 { |
| 121 return sequence.indexOf(target) > -1; |
| 122 } |
| 123 |
| 58 module.exports = { | 124 module.exports = { |
| 59 fetch, | 125 fetch, |
| 60 atob, | 126 atob, |
| 61 isFile, | 127 isFile, |
| 128 isDir, |
| 129 copy, |
| 130 copyRecursive, |
| 131 removeRecursive, |
| 132 includes, |
| 62 }; | 133 }; |
| OLD | NEW |