/
file.ts
74 lines (68 loc) · 1.95 KB
/
file.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
import * as fs from "fs";
import * as path from "path";
import {getReadableAndWritable} from "./getAppPath";
import trimPath from "./trimPath";
import formatByteString from "./renderer/formatByteString";
export class File
{
public alias : string
public path : string;
public imported : boolean;
public uuid : string;
public reachable : boolean;
public size : number;
public sizeString : string;
public constructor(path : string)
{
const uuidv4 : () => string = require("uuid/v4");
this.path = path;
this.alias = trimPath(path);
this.uuid = uuidv4();
this.reachable = true;
let stats : fs.Stats = fs.statSync(path);
this.size = stats.size;
this.sizeString = formatByteString(this.size);
this.imported = false;
}
}
export function importIntoProject(file : File) : Promise<Error> | undefined
{
const fse = require("fs-extra");
return new Promise<Error>((resolve,reject) => {
if(file.imported)
return reject(new Error(`${file.alias} has already been imported`));
try
{
fs.mkdirSync(getReadableAndWritable(`rt/imported/${file.uuid}`));
fse.copy(
getPath(file),
getReadableAndWritable(`rt/imported/${file.uuid}/${file.alias}`),
function(err : Error){
if(err)
reject(err);
else
{
file.path = `rt/imported/${file.uuid}/${file.alias}`;
file.imported = true;
resolve(undefined);
}
}
);
}
catch(err)
{
reject(err);
}
});
}
export function getPath(file : File) : string
{
if(!file.imported)
{
return path.resolve(file.path);
}
else
{
return getReadableAndWritable(file.path);
}
}