Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/lib/tusk.js
blob: b14428d74ef4828d3fd594a68ac521b21c353d1e (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

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

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 () {
    return json.decode(exports.getCatalogPath().read(
        {charset: 'utf-8'}
    ));
};

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

exports.getSourcesPath = function () {
    return exports.getDirectory().join('sources.json');
};

exports.readSources = function () {
    return json.decode(exports.getSourcesPath().read(
        {charset: 'utf-8'}
    ));
};

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'}
    );
};

// commands
//

exports.update = function (options) {
    print('Downloading catalog.');
    var catalogData = http.read('http://github.com/tlrobinson/narwhal/raw/master/catalog.json');
    print('Saving catalog.');
    exports.getCatalogPath().write(catalogData, 'b');
};

exports.createCatalog = function (options) {
    var sources = exports.readSources();
    var catalog = {};
    catalog.version = util.copy(sources.version);
    var packages = catalog.packages = {};
    util.forEachApply(
        util.items(sources.packages || {}),
        function (name, source) {
            var info;
            print(name);
            if (source.type == "inline") {
                info = util.copy(source['package.json']);
            } else if (source.type == "github") {
                var githubName = source.name || name;
                if (!source.user)
                    throw new Error("package source " + util.enquote(name) + " did not have a github user name");
                var project = source.user + '/' + githubName;
                var url = "http://github.com/" + project + "/raw/master/package.json";
                var text = http.read(url).toString('utf-8');
                info = json.decode(text);
                info.type = "zip";
                info.location = 'http://github.com/' + source.user + '/' + githubName + '/zipball/master';
            } else {
                throw new Error(
                    "Project " + exports.enquote(name) +
                    " has an urecognized type: " +
                    exports.enquote(source.type)
                );
            }
            packages[name] = info;
        }
    );
    exports.writeCatalog(catalog);
};

exports.list = function (options) {
    var self = this;
    Object.keys(packages.catalog).forEach(function (name) {
        self.print(
            name + ' \0magenta(' +
            packages.catalog[name].directory + '\0)'
        );
    });
};

exports.catalog = function (options) {
    Object.keys(exports.readCatalog().packages).forEach(print);
};


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', exports.list)
    .help('lists all installed packages');

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

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

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

parser.command('update', exports.update)
    .help('downloads the newest package catalog');

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

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

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

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

parser.command('reheat', exports.reheat)
    .help('reconsistutes the exact versions of all dependencies from a frozen site.');

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

parser.command('catalog', exports.catalog)
    .help('lists all packages in the catalog');

parser.command('create-catalog', exports.createCatalog)
    .help('creates a catalog of packages from sources.json');

parser.command('update-sources')
    .help('downloads the newest list of package sources')

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

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

parser.helpful();

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

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