Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

NPM package and demo

  • Loading branch information...
commit 28d0b4b97c63b218c859c02bd729eafb2ed7ba0c 1 parent 7b72304
Christoph Dorn cadorn authored
3  .gitignore
View
@@ -1,4 +1,5 @@
.DS_Store
.tmp*~
.pinf-*
-*.local.*
+*.local.*
+/.build/
11 bin/publish-to-npm
View
@@ -0,0 +1,11 @@
+#! /usr/bin/env commonjs
+
+exports.main = function(env)
+{
+ var parts = module.id.split("/");
+
+ env.bootProgram({
+ program: parts.slice(0, parts.length-2).join("/") + "/programs/publish-to-npm/program.json",
+ args: env.args
+ });
+}
40 demos/NPMPackage/README.md
View
@@ -0,0 +1,40 @@
+NPM Package
+===========
+
+Sample [NPM](http://npmjs.org/) package that depends on `pinf-loader-js` and loads a program.
+
+
+Test
+----
+
+Install:
+
+ npm install pinf-loader-js-demos-npmpackage
+
+ or
+
+ npm link .
+
+Run:
+
+ npm start pinf-loader-js-demos-npmpackage
+
+
+Usage
+-----
+
+Add dependency for `pinf-loader-js` to `package.json`:
+
+ {
+ "dependencies": {
+ "pinf-loader-js": ">=0.0.1"
+ }
+ }
+
+Boot program from within module:
+
+ var PINF_LOADER = require("pinf-loader-js/lib/pinf-loader-js/loader");
+
+ PINF_LOADER.boot({
+ program: "/.../program.json"
+ });
37 demos/NPMPackage/package.json
View
@@ -0,0 +1,37 @@
+{
+ "uid": "http://github.com/pinf/loader-js/demos/NPMPackage/",
+ "name": "pinf-loader-js-demos-npmpackage",
+ "version": "0.0.1",
+ "label": "PINF JavaScript Loader - Demo - NPMPackage",
+ "description": "Sample package that depends on pinf-loader-js and loads a program.",
+ "homepage": "https://github.com/pinf/loader-js/tree/master/demos/NPMPackage/",
+ "repositories": [
+ {
+ "type": "git",
+ "url": "git://github.com/pinf/loader-js.git",
+ "path": "demos/NPMPackage"
+ }
+ ],
+ "maintainers": [
+ {
+ "name": "Christoph Dorn",
+ "email": "christoph@christophdorn.com",
+ "web": "http://www.christophdorn.com/"
+ }
+ ],
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://www.opensource.org/licenses/mit-license.php"
+ }
+ ],
+ "engines": {
+ "node": ">= 0.4"
+ },
+ "dependencies": {
+ "pinf-loader-js": ">=0.0.1"
+ },
+ "scripts": {
+ "start": "node server.js"
+ }
+}
9 demos/NPMPackage/programs/HelloWorld/main.js
View
@@ -0,0 +1,9 @@
+
+module.declare([], function(require, exports, module)
+{
+ exports.main = function()
+ {
+ module.print("Hello World from HelloWorld program in NPMPackage!\n");
+ module.print("OK");
+ }
+});
4 demos/NPMPackage/programs/HelloWorld/package.json
View
@@ -0,0 +1,4 @@
+{
+ "uid": "http://github.com/pinf/loader-js/demos/NPMPackage/programs/HelloWorld/",
+ "main": "main.js"
+}
10 demos/NPMPackage/programs/HelloWorld/program.json
View
@@ -0,0 +1,10 @@
+{
+ "boot": "github.com/pinf/loader-js/demos/NPMPackage/programs/HelloWorld/",
+ "packages": {
+ "github.com/pinf/loader-js/demos/NPMPackage/programs/HelloWorld/": {
+ "locator": {
+ "location": "./"
+ }
+ }
+ }
+}
11 demos/NPMPackage/server.js
View
@@ -0,0 +1,11 @@
+
+var SYS = require("sys"),
+ PATH = require("path"),
+ PINF_LOADER = require("pinf-loader-js/lib/pinf-loader-js/loader");
+
+SYS.print("Hello World from NPMPackage!\n");
+
+PINF_LOADER.boot({
+ program: PATH.dirname(__filename) + "/programs/HelloWorld/program.json",
+ debug: true
+});
5 docs/Demos.md
View
@@ -42,6 +42,11 @@ Browser-based programs:
commonjs -v ./demos/ProgramServer
commonjs -v ./demos/ACE
+Via NPM:
+
+ npm install pinf-loader-js-demos-npmpackage
+ npm start pinf-loader-js-demos-npmpackage
+
Platform: Narwhal
=================
11 docs/UseCases.md
View
@@ -63,6 +63,17 @@ sets of modules available for loading on demand by the browser.
open http://localhost:8003/
+NPM
+===
+
+The loader can be used as an [NPM](http://npmjs.org/) package to boot a program.
+
+ npm install pinf-loader-js-demos-npmpackage
+ npm start pinf-loader-js-demos-npmpackage
+
+See: [../demos/NPMPackage](https://github.com/pinf/loader-js/tree/master/demos/NPMPackage)
+
+
Cloud-based Services
====================
8 lib/pinf-loader-js/loader.js
View
@@ -242,7 +242,13 @@ var boot = exports.boot = function(options)
}
if (!API.FILE.exists(scriptPath))
- throw new Error("Script not found at: " + scriptPath);
+ {
+ if (/\.js$/.test(scriptPath))
+ scriptPath = scriptPath.substring(0, scriptPath.length-3);
+
+ if (!API.FILE.exists(scriptPath))
+ throw new Error("Script not found at: " + scriptPath);
+ }
sandbox.init();
sandbox.declare([ scriptPath ], function(require, exports, module)
66 programs/publish-to-npm/main.js
View
@@ -0,0 +1,66 @@
+
+var PATH = require("nodejs/path"),
+ FS = require("nodejs/fs"),
+ EXEC = require("nodejs/child_process").exec;
+
+exports.main = function(env)
+{
+ var excludeFilePath = PATH.dirname(module.id) + "/rsync-exclude.txt",
+ sourcePath = PATH.dirname(PATH.dirname(PATH.dirname(module.id))),
+ targetPath = sourcePath + "/.build/npm/",
+ command;
+
+ command = "rm -Rf " + targetPath;
+ module.print(command + "\n");
+
+ EXEC(command, function (error, stdout, stderr)
+ {
+ if (!exists(targetPath))
+ {
+ module.print("Creating: " + targetPath + "\n");
+ mkdirs(PATH.dirname(targetPath), 0775);
+ }
+
+ command = "rsync -r --copy-links --exclude-from " + excludeFilePath + " " + sourcePath + "/* " + targetPath;
+ module.print(command + "\n");
+ EXEC(command, function (error, stdout, stderr)
+ {
+ module.print("Done. Now run: \0cyan(" + "sudo npm publish " + targetPath + "\0)" + "\n");
+ });
+ });
+
+}
+
+function exists(filename)
+{
+ try
+ {
+ FS.statSync(filename);
+ return true;
+ }
+ catch(e)
+ {
+ return false;
+ }
+}
+
+function mkdirs(filename, mode)
+{
+ // Oh boy. This is inefficient but should work for now.
+ filename = filename.split("/");
+ var parts = [];
+
+ while (!exists(filename.join("/")))
+ {
+ parts.push(filename.pop());
+ }
+
+ if (parts.length==0)
+ return;
+
+ while (parts.length > 0)
+ {
+ filename.push(parts.pop());
+ FS.mkdirSync(filename.join("/"), mode);
+ }
+}
15 programs/publish-to-npm/package.json
View
@@ -0,0 +1,15 @@
+{
+ "uid": "http://github.com/pinf/loader-js/programs/publish-to-npm/",
+ "main": "main.js",
+ "engine": [
+ "node"
+ ],
+ "directories": {
+ "lib": ""
+ },
+ "mappings": {
+ "nodejs": {
+ "id": "nodejs.org"
+ }
+ }
+}
16 programs/publish-to-npm/program.json
View
@@ -0,0 +1,16 @@
+{
+ "boot": "github.com/pinf/loader-js/programs/publish-to-npm/",
+ "engine": [
+ "node"
+ ],
+ "packages": {
+ "github.com/pinf/loader-js/programs/publish-to-npm/": {
+ "locator": {
+ "location": "./"
+ }
+ },
+ "nodejs.org": {
+ "provider": "nodejs.org"
+ }
+ }
+}
22 programs/publish-to-npm/rsync-exclude.txt
View
@@ -0,0 +1,22 @@
+.DS_Store
+.git/
+.tmp_*
+README.md
+.gitignore
+.pinf-*
+Thumbs.db
+.build/
+*.local.*
+
+/bin/
+/demos/
+/docs/
+/lib/pinf-loader-js/adapter/jetpack.js
+/lib/pinf-loader-js/adapter/narwhal.js
+/lib/pinf-loader-js/platform/jetpack/
+/tests/
+/programs/
+/NOTES.md
+/pinf-loader
+/pinf-loader.js
+/pinf-loader.sh
Please sign in to comment.
Something went wrong with that request. Please try again.