-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.ts
37 lines (30 loc) · 825 Bytes
/
write.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
import shell from "shelljs";
import { flexi } from "..";
import { FlexiPath, PathType } from "../../types";
import { exists, parent, type } from ".";
/**
* Writes the current `path` to disk if possible
* @category path
*/
const write = (path: string): FlexiPath => {
const parsedType = type(path);
if (parsedType === PathType.Unknown) {
throw new Error("Path is not valid or type cannot be determined");
}
if (exists(path)) {
throw new Error("Path already exists");
}
if (parsedType === PathType.Directory) {
shell.mkdir("-p", path);
} else {
const parsedParent = parent(path)();
if (parsedParent) {
if (!exists(parsedParent.path)) {
shell.mkdir("-p", parsedParent.path);
}
shell.touch(path);
}
}
return flexi.path(path);
};
export default write;