forked from Shopify/react-native-skia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
112 lines (102 loc) · 2.87 KB
/
utils.ts
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
import { exec, execSync } from "child_process";
import { exit } from "process";
import path from "path";
import fs from "fs";
export const executeCmdSync = (command: string) => {
try {
return execSync(command);
} catch (e) {
exit(1);
}
};
export const executeCmd = (
command: string,
label: string,
callback: () => void
) => {
const proc = exec(command, { env: process.env }, callback);
if (proc) {
proc.stdout?.on("data", function (data) {
console.log(`[${label}]:`, data.trim());
});
proc.stderr?.on("data", function (data) {
console.error(`[${label}]:`, data.trim());
});
proc.on("close", function (code) {
if (code) {
console.log(`[${label}] exited with code ${code}`);
exit(code);
}
});
}
};
export const getDistFolder = () => "./dist";
export const ensureFolderExists = (dirPath: string) => {
try {
console.log(`Ensuring that ${dirPath} exists...`);
return fs.mkdirSync(dirPath, { recursive: true });
} catch (err) {
// @ts-ignore
if (err.code !== "EEXIST") throw err;
}
};
export const checkFileExists = (
filePath: string,
message: string,
error: string
) => {
const exists = fs.existsSync(filePath);
if (!exists) {
console.log("");
console.log("Failed:");
console.log(message + " not found. (" + filePath + ")");
console.log(error);
console.log("");
exit(1);
} else {
console.log("☑ " + message);
}
};
const getBackupFilename = (filePath: string) => filePath + ".bak";
export const backupAndCopyFile = (
filePathToBeReplacedAndBackedUp: string,
filePathToCopy: string
) => {
// Back up and replace
console.log(`Backing up and replacing ${filePathToBeReplacedAndBackedUp}...`);
const backupFilePath = getBackupFilename(filePathToBeReplacedAndBackedUp);
fs.renameSync(filePathToBeReplacedAndBackedUp, backupFilePath);
// Copy the Package file from the npm folder
fs.copyFileSync(filePathToCopy, filePathToBeReplacedAndBackedUp);
};
export const restoreFile = (filePathToBeRestored: string) => {
console.log(`Restoring ${getBackupFilename(filePathToBeRestored)}...`);
fs.unlinkSync(filePathToBeRestored);
fs.renameSync(getBackupFilename(filePathToBeRestored), filePathToBeRestored);
};
/**
* Look ma, it's cp -R.
* @param {string} src The path to the thing to copy.
* @param {string} dest The path to the new copy.
*/
export var copyRecursiveSync = function (src: string, dest: string) {
var exists = fs.existsSync(src);
if (!exists) {
return;
}
var stats = fs.statSync(src);
var isDirectory = stats.isDirectory();
if (isDirectory) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
fs.readdirSync(src).forEach((childItemName) => {
copyRecursiveSync(
path.join(src, childItemName),
path.join(dest, childItemName)
);
});
} else {
fs.copyFileSync(src, dest);
}
};