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

var PREFIX = require('../constants/pluginPrefix');
var DEFAULT_VERSION = '*';

var Plugin = Immutable.Record({
    name:       String(),

    // Requirement version (ex: ">1.0.0")
    version:    String(DEFAULT_VERSION),

    // Path to load this plugin
    path:       String(),

    // Depth of this plugin in the dependency tree
    depth:      Number(0),

    // Content of the "package.json"
    package:    Immutable.Map(),

    // Content of the package itself
    content:    Immutable.Map()
}, 'Plugin');

Plugin.prototype.getName = function() {
    return this.get('name');
};

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

Plugin.prototype.getVersion = function() {
    return this.get('version');
};

Plugin.prototype.getPackage = function() {
    return this.get('package');
};

Plugin.prototype.getContent = function() {
    return this.get('content');
};

Plugin.prototype.getDepth = function() {
    return this.get('depth');
};

/**
    Return the ID on NPM for this plugin

    @return {String}
*/
Plugin.prototype.getNpmID = function() {
    return PREFIX + this.getName();
};

/**
    Check if a plugin is loaded

    @return {Boolean}
*/
Plugin.prototype.isLoaded = function() {
    return Boolean(this.getPackage().size > 0 && this.getContent().size > 0);
};

/**
    Create a plugin from a string

    @param {String}
    @return {Plugin}
*/
Plugin.createFromString = function(s) {
    var parts = s.split('@');
    var name = parts[0];
    var version = parts.slice(1).join('@');

    return new Plugin({
        name: name,
        version: version || DEFAULT_VERSION
    });
};

module.exports = Plugin;