blob: 2cc5f0340634e6c8e72e90fa982a03592cb92a58 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tim van der Lippeba26b2b2020-03-11 14:40:00 +00005const fs = require('fs');
6const http = require('http');
7const https = require('https');
8const path = require('path');
9const parseURL = require('url').parse;
10const shell = require('child_process').execSync;
11const Stream = require('stream').Transform;
Blink Reformat4c46d092018-04-07 15:32:37 +000012
13function fetch(url) {
14 return new Promise(fetchPromise);
15
16 function fetchPromise(resolve, reject) {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000017 let request;
18 const protocol = parseURL(url).protocol;
19 const handleResponse = getCallback.bind(null, resolve, reject);
Blink Reformat4c46d092018-04-07 15:32:37 +000020 if (protocol === 'https:') {
21 request = https.get(url, handleResponse);
22 } else if (protocol === 'http:') {
23 request = http.get(url, handleResponse);
24 } else {
25 reject(new Error(`Invalid protocol for url: ${url}`));
26 return;
27 }
28 request.on('error', err => reject(err));
29 }
30
31 function getCallback(resolve, reject, response) {
32 if (response.statusCode !== 200) {
33 reject(new Error(`Request error: + ${response.statusCode}`));
34 return;
35 }
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000036 const body = new Stream();
Blink Reformat4c46d092018-04-07 15:32:37 +000037 response.on('data', chunk => body.push(chunk));
38 response.on('end', () => resolve(body.read()));
39 }
40}
41
42function atob(str) {
43 return new Buffer(str, 'base64').toString('binary');
44}
45
46function isFile(path) {
47 try {
48 return fs.statSync(path).isFile();
49 } catch (error) {
50 return false;
51 }
52}
53
54function isDir(path) {
55 try {
56 return fs.statSync(path).isDirectory();
57 } catch (error) {
58 return false;
59 }
60}
61
62function copy(src, dest) {
63 try {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000064 const targetFilePath = path.resolve(dest, path.basename(src));
Blink Reformat4c46d092018-04-07 15:32:37 +000065 fs.writeFileSync(targetFilePath, fs.readFileSync(src));
66 } catch (error) {
67 throw new Error(`Received an error: [${error}] while trying to copy: ${src} -> ${dest}`);
68 }
69}
70
71function copyRecursive(src, dest) {
72 try {
73 if (isFile(src)) {
74 copy(src, dest);
75 return;
76 }
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000077 const targetDirPath = path.resolve(dest, path.basename(src));
78 if (!fs.existsSync(targetDirPath)) {
Blink Reformat4c46d092018-04-07 15:32:37 +000079 fs.mkdirSync(targetDirPath);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000080 }
Blink Reformat4c46d092018-04-07 15:32:37 +000081 if (isDir(src)) {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000082 const files = fs.readdirSync(src);
83 for (let i = 0; i < files.length; i++) {
84 const childPath = path.resolve(src, files[i]);
Blink Reformat4c46d092018-04-07 15:32:37 +000085 if (isDir(childPath)) {
86 copyRecursive(childPath, targetDirPath);
87 } else {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000088 const targetFilePath = path.resolve(targetDirPath, path.basename(childPath));
Blink Reformat4c46d092018-04-07 15:32:37 +000089 fs.writeFileSync(targetFilePath, fs.readFileSync(childPath));
90 }
91 }
92 }
93 } catch (error) {
94 throw new Error(`Received an error: [${error}] while trying to copy: ${src} -> ${dest}`);
95 }
96}
97
98function removeRecursive(filePath) {
99 try {
100 if (fs.existsSync(filePath)) {
101 if (isFile(filePath)) {
102 fs.unlinkSync(filePath);
103 return;
104 }
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000105 const files = fs.readdirSync(filePath);
106 for (let i = 0; i < files.length; i++) {
107 const childPath = path.resolve(filePath, files[i]);
108 if (isDir(childPath)) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000109 removeRecursive(childPath);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000110 } else {
Blink Reformat4c46d092018-04-07 15:32:37 +0000111 fs.unlinkSync(childPath);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000112 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000113 }
114 fs.rmdirSync(filePath);
115 }
116 } catch (error) {
117 throw new Error(`Received an error: [${error}] while trying to remove: ${filePath}`);
118 }
119}
120
121function includes(sequence, target) {
122 return sequence.indexOf(target) > -1;
123}
124
125function shellOutput(command) {
126 return shell(command).toString().trim();
127}
128
129function parseArgs(args) {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000130 const argObject = {};
131 for (let i = 0; i < args.length; i++) {
132 const arg = args[i];
133 const components = arg.split('=');
134 const key = components[0];
Blink Reformat4c46d092018-04-07 15:32:37 +0000135 argObject[key] = components[1] || true;
136 }
137 return argObject;
138}
139
140module.exports = {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000141 fetch,
142 atob,
143 isFile,
144 isDir,
145 copy,
146 copyRecursive,
147 removeRecursive,
148 includes,
149 shellOutput,
150 parseArgs,
151};