-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathutils.js
160 lines (146 loc) · 3.87 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const {execSync: shell} = require('child_process');
const fs = require('fs');
const http = require('http');
const https = require('https');
const path = require('path');
const {Transform: Stream} = require('stream');
const {parse: parseURL} = require('url');
function fetch(url) {
return new Promise(fetchPromise);
function fetchPromise(resolve, reject) {
let request;
const protocol = parseURL(url).protocol;
const handleResponse = getCallback.bind(null, resolve, reject);
if (protocol === 'https:') {
request = https.get(url, handleResponse);
} else if (protocol === 'http:') {
request = http.get(url, handleResponse);
} else {
reject(new Error(`Invalid protocol for url: ${url}`));
return;
}
request.on('error', err => reject(err));
}
function getCallback(resolve, reject, response) {
if (response.statusCode !== 200) {
reject(new Error(`Request error: + ${response.statusCode}`));
return;
}
const body = new Stream();
response.on('data', chunk => body.push(chunk));
response.on('end', () => resolve(body.read()));
}
}
function atob(str) {
return new Buffer(str, 'base64').toString('binary');
}
function isFile(path) {
try {
return fs.statSync(path).isFile();
} catch {
return false;
}
}
function isDir(path) {
try {
return fs.statSync(path).isDirectory();
} catch {
return false;
}
}
function copy(src, dest) {
try {
const targetFilePath = path.resolve(dest, path.basename(src));
fs.writeFileSync(targetFilePath, fs.readFileSync(src));
} catch (error) {
throw new Error(
`Received an error: [${error}] while trying to copy: ${src} -> ${dest}`,
);
}
}
function copyRecursive(src, dest) {
try {
if (isFile(src)) {
copy(src, dest);
return;
}
const targetDirPath = path.resolve(dest, path.basename(src));
if (!fs.existsSync(targetDirPath)) {
fs.mkdirSync(targetDirPath);
}
if (isDir(src)) {
const files = fs.readdirSync(src);
for (let i = 0; i < files.length; i++) {
const childPath = path.resolve(src, files[i]);
if (isDir(childPath)) {
copyRecursive(childPath, targetDirPath);
} else {
const targetFilePath = path.resolve(
targetDirPath,
path.basename(childPath),
);
fs.writeFileSync(targetFilePath, fs.readFileSync(childPath));
}
}
}
} catch (error) {
throw new Error(
`Received an error: [${error}] while trying to copy: ${src} -> ${dest}`,
);
}
}
function removeRecursive(filePath) {
try {
if (fs.existsSync(filePath)) {
if (isFile(filePath)) {
fs.unlinkSync(filePath);
return;
}
const files = fs.readdirSync(filePath);
for (let i = 0; i < files.length; i++) {
const childPath = path.resolve(filePath, files[i]);
if (isDir(childPath)) {
removeRecursive(childPath);
} else {
fs.unlinkSync(childPath);
}
}
fs.rmdirSync(filePath);
}
} catch (error) {
throw new Error(
`Received an error: [${error}] while trying to remove: ${filePath}`,
);
}
}
function includes(sequence, target) {
return sequence.indexOf(target) > -1;
}
function shellOutput(command) {
return shell(command).toString().trim();
}
function parseArgs(args) {
const argObject = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
const components = arg.split('=');
const key = components[0];
argObject[key] = components[1] || true;
}
return argObject;
}
module.exports = {
atob,
copy,
copyRecursive,
fetch,
includes,
isDir,
isFile,
parseArgs,
removeRecursive,
shellOutput,
};