summaryrefslogtreecommitdiffstats
path: root/lib/models/file.js
blob: 8ddd4af2455b5d71680b01ec168b3e37dad3358b (plain)
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
var path = require('path');
var Immutable = require('immutable');

var parsers = require('../parsers');

var File = Immutable.Record({
    // Path of the file, relative to the FS
    path:       String(),

    // Time when file data last modified
    mtime:      Date()
});

File.prototype.getPath = function() {
    return this.get('path');
};

File.prototype.getMTime = function() {
    return this.get('mtime');
};

/**
    Does the file exists / is set

    @return {Boolean}
*/
File.prototype.exists = function() {
    return Boolean(this.getPath());
};

/**
    Return type of file ('markdown' or 'asciidoc')

    @return {String}
*/
File.prototype.getType = function() {
    var parser = this.getParser();
    if (parser) {
        return parser.getName();
    } else {
        return undefined;
    }
};

/**
    Return extension of this file (lowercased)

    @return {String}
*/
File.prototype.getExtension = function() {
    return path.extname(this.getPath()).toLowerCase();
};

/**
    Return parser for this file

    @return {Parser}
*/
File.prototype.getParser = function() {
    return parsers.getByExt(this.getExtension());
};

/**
    Create a file from stats informations

    @param {String} filepath
    @param {Object|fs.Stats} stat
    @return {File}
*/
File.createFromStat = function createFromStat(filepath, stat) {
    return new File({
        path: filepath,
        mtime: stat.mtime
    });
};

/**
    Create a file with only a path

    @param {String} filepath
    @return {File}
*/
File.createWithFilepath = function createWithFilepath(filepath) {
    return new File({
        path: filepath
    });
};

module.exports = File;