1 | import * as os from "os";
|
2 | import * as crypto from "crypto";
|
3 | import * as fse from "fs-extra";
|
4 |
|
5 | const originalCwd = process.cwd();
|
6 | const createdDirectories: string[] = [];
|
7 |
|
8 | process.on("exit", () => {
|
9 |
|
10 |
|
11 | createdDirectories.forEach(path => {
|
12 | fse.removeSync(path);
|
13 | });
|
14 | });
|
15 |
|
16 | const setCleanTestCwd = () => {
|
17 | const random = crypto.randomBytes(16).toString("hex");
|
18 | const path = `${os.tmpdir()}/fs-jetpack-test-${random}`;
|
19 | fse.mkdirSync(path);
|
20 | createdDirectories.push(path);
|
21 | process.chdir(path);
|
22 | };
|
23 |
|
24 | const switchBackToCorrectCwd = () => {
|
25 | const path = createdDirectories.pop();
|
26 | process.chdir(originalCwd);
|
27 | try {
|
28 | fse.removeSync(path);
|
29 | } catch (err) {
|
30 |
|
31 |
|
32 |
|
33 | fse.removeSync(path);
|
34 | }
|
35 | };
|
36 |
|
37 | const parseMode = (modeAsNumber: number) => {
|
38 | const mode = modeAsNumber.toString(8);
|
39 | return mode.substring(mode.length - 3);
|
40 | };
|
41 |
|
42 |
|
43 | function osSep(path: string): string;
|
44 | function osSep(path: string[]): string[];
|
45 | function osSep(path: any): any {
|
46 | if (Array.isArray(path)) {
|
47 | return path.map(osSep);
|
48 | }
|
49 |
|
50 | if (process.platform === "win32") {
|
51 | return path.replace(/\//g, "\\");
|
52 | }
|
53 | return path.replace(/\\/g, "/");
|
54 | }
|
55 |
|
56 | export default {
|
57 | setCleanTestCwd,
|
58 | switchBackToCorrectCwd,
|
59 | parseMode,
|
60 | osSep
|
61 | };
|