forked from bandhavya/wm-reactnative-cli
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutils.js
More file actions
89 lines (79 loc) · 2.89 KB
/
Copy pathutils.js
File metadata and controls
89 lines (79 loc) · 2.89 KB
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
const fs = require('fs');
const os = require('os');
const axios = require('axios');
const path = require('path');
const crypto = require('crypto');
const logger = require('./logger');
const taskLogger = require('./custom-logger/task-logger').spinnerBar;
const loggerLabel = 'wm-reactnative-cli';
const { exec } = require('./exec');
function isWindowsOS() {
return (os.platform() === "win32" || os.platform() === "win64");
}
async function readAndReplaceFileContent(path, writeFn) {
const content = fs.readFileSync(path, 'utf-8');
return Promise.resolve().then(() => {
return writeFn && writeFn(content);
}).then((modifiedContent) => {
if (modifiedContent !== undefined && modifiedContent !== null) {
fs.writeFileSync(path, modifiedContent);
return modifiedContent;
}
return content;
});
}
function streamToString (stream) {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
stream.on('error', (err) => reject(err));
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
})
}
async function iterateFiles(path, callBack) {
if (fs.lstatSync(path).isDirectory()) {
await Promise.all(fs.readdirSync(path).map((p) => iterateFiles(`${path}/${p}`, callBack)));
} else {
await callBack && callBack(path);
}
}
async function isExpoWebPreviewContainer(previewUrl) {
const response = await axios.get(`${previewUrl}/rn-bundle/index.html`).catch((e) => e.response);
return response.data.includes("index.bundle") && response.data.includes("platform=web");
}
async function getDestPathForWindows(mode, projectDir = ''){
let destHash = '';
let destPath = '';
let updatePath = '';
let appendPath = '';
if(mode == 'preview') {
updatePath = `${projectDir}/target/generated-expo-app`;
} else if (mode == 'build'){
appendPath = '/' ;
}
destHash = crypto.createHash("shake256", { outputLength: 1 }).update(updatePath).digest("hex");
destPath = path.resolve(`${global.rootDir}/${mode}/` + destHash + appendPath);
return destPath;
}
async function cleanNpmCache(cwd) {
try {
taskLogger.start('Cleaning npm cache...');
await exec('npm', ['cache', 'clean', '--force'], { cwd });
taskLogger.succeed('Npm cache cleaned successfully');
} catch (e) {
logger.error({
label: loggerLabel,
message: `Npm cache clean failed: ${e}`
});
taskLogger.fail('Npm cache clean failed');
}
}
module.exports = {
isWindowsOS: isWindowsOS,
readAndReplaceFileContent: readAndReplaceFileContent,
iterateFiles: iterateFiles,
streamToString: streamToString,
isExpoWebPreviewContainer: isExpoWebPreviewContainer,
getDestPathForWindows: getDestPathForWindows,
cleanNpmCache: cleanNpmCache
};