electron 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/electron.js CHANGED
@@ -1,8 +1,49 @@
1
- var CLI = require('./electron/cli');
1
+ /*!
2
+ * Electron
3
+ * Copyright (c) 2012 Jake Luer <jake@alogicalparadox.com>
4
+ * MIT Licensed
5
+ */
2
6
 
3
- var exports = module.exports = function (name, opts) {
4
- return new CLI(name, opts);
7
+ /*!
8
+ * Electron constructors
9
+ */
10
+
11
+ var Program = require('./electron/program')
12
+ , Args = require('./electron/args');
13
+
14
+ /*!
15
+ * Primary export - program factory
16
+ */
17
+
18
+ var exports = module.exports = defineProgram;
19
+
20
+ /*!
21
+ * defineProgram long form
22
+ */
23
+
24
+ exports.defineProgram = defineProgram;
25
+
26
+ /*!
27
+ * Electron version
28
+ */
29
+
30
+ exports.version = '0.2.0';
31
+
32
+ /*!
33
+ * Argv parsing factory
34
+ */
35
+
36
+ exports.argv = function (args) {
37
+ args = args || process.argv;
38
+ var argv = new Args(args);
39
+ return argv;
5
40
  };
6
41
 
7
- exports.CLI = CLI;
8
- exports.version = '0.1.0';
42
+ /*!
43
+ * program factory
44
+ */
45
+
46
+ function defineProgram (name, opts) {
47
+ var program = new Program(name, opts);
48
+ return program;
49
+ }
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "author": "Jake Luer <jake@alogicalparadox.com>",
3
3
  "name": "electron",
4
- "description": "Event-based tiny CLI famework.",
5
- "version": "0.1.0",
4
+ "description": "A simple command-line interface framework for node.js.",
5
+ "keywords": [ "cli", "command", "option", "parser", "argv" ],
6
+ "homepage": "http://alogicalparadox.com/electron",
7
+ "license": "MIT",
8
+ "version": "0.2.1",
6
9
  "repository": {
7
10
  "type": "git",
8
11
  "url": "git://github.com/logicalparadox/electron.git"
@@ -12,7 +15,7 @@
12
15
  "test": "make test"
13
16
  },
14
17
  "dependencies": {
15
- "drip": "0.2.x"
18
+ "drip": "0.3.x"
16
19
  },
17
20
  "devDependencies": {
18
21
  "mocha": "*"
package/examples/basic.js DELETED
@@ -1,20 +0,0 @@
1
- var electron = require('..');
2
-
3
- var program = electron('testing');
4
-
5
- program
6
- .name('Electron Testing')
7
- .version('0.0.1');
8
-
9
- program
10
- .command('simple')
11
- .description('A simple task')
12
- .option('-p, --port [6000]', 'Set the simple port.')
13
- .option('-f, --file', 'Use a file', true)
14
- .action(function (args) {
15
- console.log('simple');
16
- console.log(args);
17
- });
18
-
19
-
20
- program.parse(process.argv);
@@ -1,169 +0,0 @@
1
- var Drip = require('drip')
2
- , util = require('util');
3
-
4
- var Command = require('./command');
5
-
6
- module.exports = Cli;
7
-
8
- function Cli (base, opts) {
9
- Drip.call(this, { delimeter: ' ' });
10
- this.commands = [];
11
- this.opts = opts || {};
12
- this.opts.base = base;
13
- }
14
-
15
- util.inherits(Cli, Drip);
16
-
17
- Cli.prototype.command = function (name) {
18
- var cmd = new Command(name);
19
- this.commands.push(cmd);
20
- return cmd;
21
- };
22
-
23
- Cli.prototype.cwd = function (p) {
24
- this.opts.cwd = p;
25
- return this;
26
- };
27
-
28
- Cli.prototype.parse = function (args) {
29
- mountCommands.call(this);
30
- var argv = processArgs(args)
31
- , command = argv.args.slice(0);
32
- argv.cwd = this.opts.cwd || process.cwd();
33
- if (~argv.modes.indexOf('v') || ~argv.modes.indexOf('version'))
34
- return displayVersion.call(this);
35
- if (~argv.modes.indexOf('h') || ~argv.modes.indexOf('help'))
36
- return displayHelp.call(this);
37
- if (command.length)
38
- this.emit(command, argv, null, null);
39
- };
40
-
41
- Cli.prototype.version = function (v) {
42
- this.opts.version = v;
43
- return this;
44
- };
45
-
46
- Cli.prototype.name = function (name) {
47
- this.opts.name = name;
48
- return this;
49
- };
50
-
51
- function processArgs (args) {
52
- var param_key = null
53
- , parts = process.argv.slice(2)
54
- , input = {
55
- modes: []
56
- , args: []
57
- , params: {}
58
- };
59
-
60
- function checkParamKey () {
61
- if (param_key !== null) {
62
- input.modes.push(param_key);
63
- param_key = null;
64
- }
65
- }
66
-
67
- parts.forEach(function (part) {
68
- if (part.substr(0, 2) === '--') {
69
- checkParamKey();
70
- if (part.indexOf('=') !== -1) {
71
- part = part.substr(2).split('=', 2);
72
- return input.params[part[0]] = part[1]
73
- }
74
-
75
- return param_key = part.substr(2);
76
- }
77
-
78
- if (part[0] === '-') {
79
- checkParamKey();
80
- var sstr = part.substr(1);
81
- if (sstr.length > 1) {
82
- for (var i = 0; i < sstr.length; i++)
83
- input.modes.push(sstr[i]);
84
- return;
85
- } else {
86
- return param_key = part.substr(1);
87
- }
88
- }
89
-
90
- part = Number(part) || part
91
-
92
- if (param_key !== null) {
93
- input.params[param_key] = part;
94
- param_key = null;
95
- } else {
96
- input.args.push(part);
97
- }
98
- });
99
-
100
- checkParamKey();
101
- return input;
102
- }
103
-
104
- function mountCommands () {
105
- var self = this
106
- , cmds = [];
107
-
108
- this.commands.forEach(function (command) {
109
- var ev = command.opts.cmd
110
- , fn = command.opts.action;
111
- if (!~cmds.indexOf(ev)) {
112
- cmds.push(ev);
113
- self.on(ev, fn);
114
- }
115
- });
116
- }
117
-
118
- function displayVersion () {
119
- console.log(this.opts.version);
120
- }
121
-
122
- function displayHelp () {
123
- var colors = {
124
- 'red': '\u001b[31m'
125
- , 'green': '\u001b[32m'
126
- , 'yellow': '\u001b[33m'
127
- , 'blue': '\u001b[34m'
128
- , 'magenta': '\u001b[35m'
129
- , 'cyan': '\u001b[36m'
130
- , 'gray': '\u001b[90m'
131
- , 'reset': '\u001b[0m'
132
- };
133
-
134
- Object.keys(colors).forEach(function (color) {
135
- Object.defineProperty(String.prototype, color, {
136
- get: function () { return colors[color] + this + colors['reset']; }
137
- });
138
- });
139
-
140
- function l (s) {
141
- console.log(' ' + s);
142
- }
143
-
144
- function pad (str, width) {
145
- return Array(width - str.length).join(' ') + str;
146
- }
147
-
148
- var name = this.opts.name || this.opts.base
149
- , base = this.opts.base;
150
-
151
- l('');
152
- l(name.magenta + ' ' + this.opts.version);
153
- l('');
154
-
155
- this.commands.forEach(function (cmd) {
156
- var c = cmd.opts;
157
- l(base.gray + ' ' + c.cmd.green + (c.options.length ? ' <options>' : ''));
158
- l(pad('', 4) + c.desc.blue);
159
-
160
- if(c.options.length) {
161
- c.options.forEach(function (opt) {
162
- l(pad('', 6) + opt.opts.join(' ') + ' ' + opt.description.gray);
163
- });
164
- }
165
- l('');
166
- });
167
-
168
- process.exit();
169
- }