UNPKG

5.78 kBJavaScriptView Raw
1var path = require('path');
2var Project = require('../lib/project');
3var Atmosphere = require('../lib/atmosphere');
4var fs = require('fs');
5var _ = require('underscore');
6var wrench = require('wrench');
7
8Meteorite = function(args, meteorArgs) {
9 this.args = args;
10 this.meteorArgs = meteorArgs;
11
12 // Make directories we'll need
13 Meteorite.prepareFS();
14
15 this.project = new Project(process.cwd(), args);
16};
17
18///////// Global meteorite commands that don't operate WRT to the current project
19
20// Meteorite command
21Meteorite.prototype.create = function(fn) {
22 var self = this;
23
24 // in this case the project's root is _not_ process.cwd
25 var appName = this.args._[1] || this.args.example;
26
27 if (!appName)
28 throw 'No name provided to mrt create!';
29
30 // if you specify a path to create, you mean relative to CWD, NOT project root.
31 if (this.args.path)
32 this.args.path = path.resolve(process.cwd(), this.args.path);
33
34 self.project = new Project(path.join(process.cwd(), appName), this.args);
35 self.project.meteor.execute(this.meteorArgs, function() {
36
37 // New project needs a new smart json file
38 self.project.writeSmartJson();
39 fn();
40
41 });
42};
43
44Meteorite.prototype['create-package'] = function(fn) {
45 var self = this;
46
47 var packageDir = this.args._[1]
48 var packageName = path.basename(packageDir);
49
50 console.log("Creating package named " + packageName + " in " + packageDir);
51
52 var packageFile = packageName + '.js';
53 var testFile = packageName + '_tests.js';
54
55 wrench.mkdirSyncRecursive(packageDir);
56
57 // write package.js
58 fs.writeFileSync(path.join(packageDir, 'package.js'),
59 'Package.describe({\n' +
60 ' summary: "/* fill me in */"\n' +
61 '});\n\n' +
62 'Package.on_use(function (api, where) {\n' +
63 ' api.add_files(\'' + packageFile + '\', [\'client\', \'server\']);\n' +
64 '});\n\n' +
65 'Package.on_test(function (api) {\n' +
66 ' api.use(\'' + packageName + '\');\n\n' +
67 ' api.add_files(\'' + testFile + '\', [\'client\', \'server\']);\n' +
68 '});\n'
69 );
70
71 // touch relevant files
72 fs.writeFileSync(path.join(packageDir, packageFile), '');
73 fs.writeFileSync(path.join(packageDir, testFile), '');
74
75 // write simple smart.json
76 fs.writeFileSync(path.join(packageDir, 'smart.json'), JSON.stringify({
77 "name": packageName,
78 "description": "",
79 "homepage": "",
80 "author": "",
81 "version": "0.0.1",
82 "git": "",
83 "packages": {}
84 }, null, 2));
85}
86
87// XXX: this should probably track this (so further calls to mrt install
88// don't overwrite it), or else mrt install shouldn't remove symlinks by default
89Meteorite.prototype['link-package'] = function(fn) {
90 var self = this;
91
92 var packageDir = this.args._[1];
93 var packageName = path.basename(packageDir);
94 var packagePath = 'packages/' + packageName;
95
96 console.log("Linking package named " + packageName + " to " + packageDir);
97
98 var old;
99 try { old = fs.readlinkSync(packagePath); } catch (err) {}
100
101 // already done
102 if (old && old === packageDir)
103 return;
104
105 // pointing to the wrong spot
106 if (old)
107 fs.unlinkSync(packagePath);
108
109 fs.symlinkSync(packageDir, packagePath);
110}
111
112/////// Package level meteorite commands
113
114Meteorite.prototype.install = function(fn) {
115 this.project.install(fn);
116};
117
118Meteorite.prototype.update = function(fn) {
119 this.project.update(fn);
120};
121
122Meteorite.prototype.publish = function(fn) {
123 Atmosphere.publish(fn);
124};
125
126Meteorite.prototype.release = function(fn) {
127 Atmosphere.release(fn);
128};
129
130Meteorite.prototype.uninstall = function(fn) {
131 if (this.args.system)
132 Meteorite.uninstall();
133 else
134 this.project.uninstall(fn);
135};
136
137// if the package isn't in meteor's list, add it to smart.json
138Meteorite.prototype.add = function(fn) {
139 var self = this;
140 var packageName = this.meteorArgs[1];
141
142 var version = this.args['pkg-version'];
143
144 // TODO: resolve the complexity of supporting more than one package being added at once
145 // for now we just spit out an error
146 if (this.args._.length > 2)
147 console.log("NOTE: mrt add only supports adding a single package at a time, truncating.");
148
149 // ensure we have the package
150 self.project.installPackage(packageName, version, function() {
151
152 // OK. We've "meteorite" installed it (i.e. put it in packages/)
153 // now we just need to test if this means Meteor thinks it's installed,
154 // or if we need to `meteor add` it too.
155
156 // XXX: at some future point when we no longer have to do this, remove.
157 self.project.isUsing(packageName, function(success) {
158 if (!success) {
159 self.project.execute(['add', packageName], fn);
160 } else {
161 fn();
162 }
163 });
164 });
165};
166
167// Meteor commands, will get run either by the project install or the default meteor
168// FIXME -- 'update'?
169// TODO -- treat add, remove, list specially
170_.each([
171 'run',
172 'help',
173 'remove',
174 'list',
175 'bundle',
176 'mongo',
177 'deploy',
178 'logs',
179 'reset',
180 'test-packages'
181], function(command) {
182 Meteorite.prototype[command] = function(fn) {
183 this.project.execute(this.meteorArgs, fn);
184 };
185});
186
187// Class methods
188
189Meteorite.root = function() {
190 var homeDir = process.env.HOME;
191 return path.join(homeDir, '.meteorite');
192};
193
194// Creates the path to ~/.meteorite
195Meteorite.prepareFS = function() {
196 var root = Meteorite.root();
197 if (!fs.existsSync(root))
198 fs.mkdirSync(root);
199};
200
201// Uninstall everything from ~/.meteorite
202Meteorite.uninstall = function() {
203 // TODO prompt for confirmation
204 console.log('Deleting ~/.meteorite. Note that previously installed projects will no longer work');
205
206 var root = Meteorite.root();
207 if (fs.existsSync(root))
208 wrench.rmdirSyncRecursive(root);
209};
210
211module.exports = Meteorite;
212
213// var _debug = require('./debug');
214// _.debugClass('Meteorite');