File size: 2,532 Bytes
8f3f8db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function patchSqueakForNode(root) {
    var fs = require("fs");
    var Squeak = window.Squeak;
    Object.extend(Squeak, {
        fileGet: (filepath, thenDo, errorDo) => {
            fs.readFile(root + filepath, (err, data) => {
                if (err) {
                    errorDo(err);
                } else {
                    thenDo((data instanceof ArrayBuffer) ? data : data.buffer);
                }
            });
        },
        filePut: (filepath, contents, optSuccess) => {
            var path = Squeak.splitFilePath(filepath); if (!path.basename) return null;
            var now = Squeak.totalSeconds();
            var entry = [path.basename, now, 0, false, contents.byteLength || contents.length || 0];
            fs.writeFileSync(root + filepath, contents);
            if (optSuccess) optSuccess();
            return entry;
        },
        fileDelete: (filepath, entryOnly) => {
            fs.unlinkSync(root + filepath);
        },
        fileRename: (from, to) => {
            fs.renameSync(root + from, root + to);
        },
        fileExists: (filepath) => {
            try {
                fs.accessSync(root + filepath);
                return true;
            } catch(e) {
                return false;
            }
        },
        dirCreate: (dirpath, withParents) => {
            var path = Squeak.splitFilePath(dirpath); if (!path.basename) return false;
            if (withParents && !Squeak.fileExists(path.dirname)) {
                Squeak.dirCreate(path.dirname, true);
            }
            try {
                fs.mkdirSync(root + dirpath);
                return true;
            } catch(e) {
                return false;
            }
        },
        dirDelete: (dirpath) => {
            try {
                fs.rmdirSync(root + dirpath);
                return true;
            } catch(e) {
                return false;
            }
        },
        dirList: (dirpath, includeTemplates) => {
            try {
                var dir = {};
                var files = fs.readdirSync(root + dirpath);
                files.forEach(f => {
                    var stats = fs.statSync(root + dirpath + f);
                    dir[f] = [f, stats.ctime, stats.mtime, stats.isDirectory(), stats.size];
                });
                return dir;
            } catch(e) {
                return null;
            }
        },
        primitiveQuit: (argCount) => {
            Squeak.flushAllFiles();
            nw.App.quit();
        },
    });
}