Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a Meteor smart package

Tags:

package

meteor

How can one build a Meteor smart package that would show up in meteor list?

Building Atmosphere packages is reasonably well documented, but building Meteor packages isn't.

like image 533
Olivier Refalo Avatar asked Apr 11 '12 21:04

Olivier Refalo


3 Answers

Meteor now supports a create --package command.

See the meteor docs.

Example (substitute your own meteor developer account for "cunneen"):

meteor create --package cunneen:foo 

Output:

cunneen:foo: created in your app

Results:

packages/cunneen:foo/package.js

Package.describe({   name: 'cunneen:foo',   version: '0.0.1',   // Brief, one-line summary of the package.   summary: '',   // URL to the Git repository containing the source code for this package.   git: '',   // By default, Meteor will default to using README.md for documentation.   // To avoid submitting documentation, set this field to null.   documentation: 'README.md' });  Package.onUse(function(api) {   api.versionsFrom('1.0.3.1');   api.addFiles('cunneen:foo.js'); });  Package.onTest(function(api) {   api.use('tinytest');   api.use('cunneen:foo');   api.addFiles('cunneen:foo-tests.js'); }); 

packages/cunneen:foo/foo.js (empty file)

// Write your package code here! 

packages/cunneen:foo/foo-tests.js

// Write your tests here! // Here is an example. Tinytest.add('example', function (test) {   test.equal(true, true); }); 

packages/cunneen:foo/README.md (empty file)

# cunneen:foo package 

For a good (VERY comprehensive) example, take a look at iron-router.

like image 90
cobberboy Avatar answered Oct 07 '22 03:10

cobberboy


See cobberboy's answer below

Below is outdated information:

See info about the new meteor packaging system: https://meteorhacks.com/meteor-weekly-meteor-09-rc-meteor-new-logo-underscore-in-templates.html

** older information **

There is updated information about writing your own package and about repackaging existing 3rd party libraries. The API wont be stable till 1.0 though, so be prepared to make many changes.

I have included boiler plate to help w/ making it both a node and a meteor usable library at once. This took me quite some time to figure out, open to suggestions.

package: /lib/my.js

if (typeof Meteor === 'undefined) {
    // Not Running In Meteor (nodejs code)
    // example NPM/Node Dependencies that we'll use
    var async = require('async');
    var debug = require('debug')('my:package');
    var mongodb = require('mongodb');

    var http = require('http');  
} else {
    // Running as Meteor Package
    var async = Npm.require('async');
    var debug = Npm.require('debug')('my:package');
    var mongodb = Npm.require('mongodb');

    // node core module 'http'
    // use Npm.require to require node core modules
    // but doesnt need Npm.depends in the package.js file
    var http = Npm.require('http');
}

var constructor = function(property1) {
    this.property1 = property1; // or whatever in your constructor.
};

if (typeof Meteor === 'undefined') {
   // Export it node style
   My = exports = module.exports = constructor; // Limit scope to this nodejs file
} else {
   // Export it meteor style
   My = constructor; // Make it a global
}

// Proceed defining methods / properties as usual.
My.prototype.doStuff = function() { console.log('hello world'); }

package: /package.js

Package.describe({
  summary: "My Meteor Package"
});

/**
 * Ex: Some NPM Dependencies
 */
Npm.depends({
  'async': '0.2.9',
  'debug': '0.7.2',
  'mongodb': '1.3.18'
});

/**
 * On use we'll add files and export our tool
 */
Package.on_use(function (api) {
  /**
   * Add all the files, in the order of their dependence (eg, if A.js depends on B.js, B.js must be before A.js)
   */
  api.add_files([
    'lib/my.js' // <-- include all the necessary files in the package
    ],
    'server'); // Can be 'server', 'client' , ['client','server']

  /**
   * Only expose the My constructor, only export if meteor > 0.6.5
   */
  api.export && api.export(['My'], 'server'); // 1st arg can be array of exported constructors/objects, 2nd can be 'server', 'client', ['client', 'server']
});

meteor app: some file in the proper client/server context (as defined in package.js)

var my = new My('a property');
my.doStuff(); // console logs 'hello world' on the server

meteor app: smart.json , add your file to the packages list

{
    packages:{
        "node-my": {
            "git": "[email protected]:myAccount/node-my.git"
        }
    }
}

Finally run mrt install on the command line to get it to install the package .. Whew!

like image 24
Mike Graf Avatar answered Oct 07 '22 01:10

Mike Graf


NOTE: Package development is currently undocumented, and the API will change. You've been warned!

That said, it's actually pretty easy to get started:

First, git clone a copy of the meteor repo. Make yourself a new directory in /packages. Put a package.js file in the directory (see other packages for examples). Now you've got a package!

Next, run the meteor script from your checkout (not the one installed by the installer). When run from the checkout, the script will use the local packages directory in the checkout. It will even hot-reload when you change code in your package.

Have a look through the other packages for examples and to get an idea what the API does.

EDIT: much progress has been made in terms of third-party packages. Check out http://oortcloud.github.com/meteorite/ and https://atmosphere.meteor.com/

like image 24
n1mmy Avatar answered Oct 07 '22 01:10

n1mmy