Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/lib/narwhal/tusk.js
blob: ded05cc18cdc2d44f9872483cbf2321da8836609 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

var fs = require("file");
var packages = require("packages");
var util = require("util");
var json = require("json");
var http = require("http");

var minCatalogVersion = 1;

var args = require("args");
var parser = exports.parser = new args.Parser();

parser.help('A Narwhal project package manager.');

// parser.option('sources', exports.sources);

parser.command('list', module.id + '/list')

parser.command('install', module.id + '/install');

parser.command('upgrade', null)
    .help('downloads the latest version of a package');

parser.command('remove', null)
    .help('removes the local copy of package');

parser.command('update', module.id + '/update');

parser.command('search', null)
    .help('searches the package catalog');

parser.command('init', module.id + '/init');

parser.command('platform', module.id + '/platform');

parser.command('freeze', null)
    .help('writes a freeze.json file');

parser.command('bundle', null)
    .help('creates an archive of your project and its package dependencies');

parser.command('reheat', module.id + '/reheat');

parser.command('clone', module.id + '/clone');

parser.command('catalog', module.id + '/catalog');

parser.command('create-catalog', module.id + '/create-catalog');

parser.command('orphans', null)
    .help('lists packages that are no longer wanted by the user or other packages.')

parser.command('consolidate', module.id + '/consolidate');

parser.helpful();

// utilities
//

exports.getDirectory = function () {
    return fs.path(system.packagePrefixes[0]);
};

exports.getPackagesDirectory = function () {
    return exports.getDirectory().join('packages');
};

exports.getTuskDirectory = function () {
    var tuskDirectory = exports.getDirectory().join('.tusk');
    tuskDirectory.mkdirs();
    return tuskDirectory;
}

exports.getZipsDirectory = function () {
    return exports.getDirectory().join('zips');
};

exports.getCatalogPath = function () {
    return exports.getTuskDirectory().join('catalog.json');
};

exports.readCatalog = function () {
    var catalogPath = exports.getCatalogPath();
    if (!catalogPath.exists())
        throw new Error(catalogPath + " does not exist.");
    if (!catalogPath.isFile())
        throw new Error(catalogPath + " is not a file.");
    var catalog = json.decode(catalogPath.read({charset: 'utf-8'}));
    if (catalog.version === undefined || +catalog.version < minCatalogVersion)
        throw new Error("catalog is out of date.  use tusk update or create-catalog");
    return catalog;
};

exports.writeCatalog = function (catalog) {
    var catalogPath = exports.getCatalogPath();
    print('Writing ' + catalogPath);
    return catalogPath.write(
        json.encode(catalog, null, 4),
        {charset: 'utf-8'}
    );
};

exports.update = function (options) {
    require('./tusk/update').update.call(this, options);
};

exports.getSourcesPath = function () {
    var try1 = exports.getTuskDirectory().join('sources.json');
    var try2 = exports.getDirectory().join('sources.json');
    if (try1.isFile())
        return try1;
    if (try2.isFile())
        return try2;
};

exports.readSources = function () {
    var sources = json.decode(exports.getSourcesPath().read(
        {charset: 'utf-8'}
    ));
    if (
        sources.version === undefined ||
        +sources.version < minCatalogVersion
    )
        throw new Error(
            "sources file is out of date.  version " +
            minCatalogVersion + " is required."
        );
    sources.packages = sources.packages || {};
    return sources;
};

exports.writeSources = function (sources) {
    return exports.getSourcesPath().write(
        json.encode(sources, null, 4),
        {charset: 'utf-8'}
    );
};

exports.getNotesPath = function () {
    return exports.getTuskDirectory().join('notes.json');
};

exports.readNotes = function () {
    var notesPath = exports.getNotesPath();
    if (!notesPath.isFile())
        return {};
    return json.decode(notesPath.read(
        {charset: 'utf-8'}
    ));
};

exports.writeNotes = function (notes) {
    return exports.getNotesPath().write(
        json.encode(notes, null, 4),
        {charset: 'utf-8'}
    );
};

// run it

exports.main = function (args) {
    var options = parser.parse(args);
    if (!options.acted)
        parser.printHelp(options);
};

if (module.id == require.main)
    exports.main(system.args);