Skip to content

Commit eed2648

Browse files
committed
First swing at npm build
1 parent fa20d32 commit eed2648

File tree

10 files changed

+251
-156
lines changed

10 files changed

+251
-156
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ nbproject
44
gitup.dat
55
.project
66
.gitup.dat
7-
tests/node_modules/
7+
node_modules/
88
.DS_Store

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ setup:
99
test:
1010
#node tests/cli.js --debug --abort # To abort at first failure
1111
cd tests && npm install
12-
node tests/cli.js --debug
12+
node bin/phpjs.js --action test --debug
1313

1414
# Apply code standards
1515
cleanup:
@@ -23,6 +23,11 @@ cleanup:
2323
--strict \
2424
--jslint_error all
2525

26+
npm:
27+
node bin/phpjs.js --action buildnpm --debug
28+
29+
build: npm
30+
2631
hook:
2732
mkdir -p ~/.gittemplate/hooks
2833
curl https://raw.github.com/kvz/ochtra/master/pre-commit -o ~/.gittemplate/hooks/pre-commit

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,26 @@ make site-preview
3636
```shell
3737
make site-clean
3838
```
39+
40+
# Testing
41+
42+
43+
# Cli
44+
45+
```bash
46+
make test
47+
```
48+
49+
```bash
50+
node bin/phpjs.js --action test --name sort
51+
node bin/phpjs.js --action test --category array
52+
```
53+
54+
# Web
55+
56+
```bash
57+
PORT=8080 node _tests/server.js
58+
```
59+
60+
Point your webbrowser to http://localhost:8080
61+

bin/phpjs.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
var cli = require('cli').enable('status', 'help', 'version', 'glob', 'timeout');
2+
var fs = require('fs');
3+
var glob = require('glob');
4+
var path = require('path');
5+
var phpjsutil = new require('../lib/phpjsutil');
6+
var equal = require('deep-equal');
7+
var __root = __dirname + '/..';
8+
9+
10+
var PhpjsUtil = phpjsutil({
11+
injectDependencies: ['ini_set', 'ini_get'],
12+
equal : equal,
13+
debug : cli.debug
14+
});
15+
16+
// Environment-specific file opener. function name needs to
17+
// be translated to code. The difficulty is in finding the
18+
// category.
19+
PhpjsUtil.opener = function(name, cb) {
20+
glob(__root + '/functions/*/' + name + '.js', {}, function(err, files) {
21+
if (err) {
22+
return cb(err);
23+
}
24+
var filepath = files[0];
25+
26+
if (!filepath) {
27+
return cb('could not find ' + __root + '/functions/*/' + name + '.js');
28+
}
29+
30+
fs.readFile(filepath, 'utf-8', function(err, code) {
31+
if (err) {
32+
return cb(err);
33+
}
34+
return cb(null, code);
35+
});
36+
});
37+
};
38+
39+
// --debug works out of the box. See -h
40+
cli.parse({
41+
action : ['a', 'Test / Build', 'string', 'test'],
42+
name : ['n', 'Function name to test', 'path', '*'],
43+
category: ['c', 'Category to test', 'path', '*'],
44+
abort : ['a', 'Abort on first failure']
45+
});
46+
47+
// cli.pad = function(str, pad, chr, dir) {
48+
// if (!pad) pad = 20;
49+
// if (!chr) chr = ' ';
50+
// if (!dir) dir = 'left';
51+
52+
// if ((str + '').length >= pad) {
53+
// return str;
54+
// }
55+
56+
// if (dir === 'right') {
57+
// return String(str + Array(pad).join(chr)).slice(0, pad);
58+
// }
59+
60+
// return String(Array(pad).join(chr) + str).slice(-pad);
61+
// };
62+
63+
// var width = 120;
64+
65+
cli.buildnpm = function(args, options) {
66+
var self = this;
67+
var globpath = __root + '/functions/' + options.category + '/' + options.name + '.js';
68+
self.glob(globpath, function (err, params) {
69+
console.log(params.signature);
70+
71+
var buf = '';
72+
buf += 'exports.decbin = function (number) {\n';
73+
buf += ' // Returns a string containing a binary representation of the number \n';
74+
buf += ' // \n';
75+
buf += ' // version: 1008.1718\n';
76+
buf += ' // discuss at: http://phpjs.org/functions/decbin\n';
77+
buf += ' // + original by: Enrique Gonzalez\n';
78+
buf += ' // + bugfixed by: Onno Marsman\n';
79+
buf += ' // + improved by: http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript\n';
80+
buf += ' // + input by: pilus\n';
81+
buf += ' // + input by: nord_ua\n';
82+
buf += ' // * example 1: \php.decbin(12);\n';
83+
buf += ' // * returns 1: \'1100\'\n';
84+
buf += ' // * example 2: \php.decbin(26);\n';
85+
buf += ' // * returns 2: \'11010\'\n';
86+
buf += ' // * example 3: \php.decbin(\'26\');\n';
87+
buf += ' // * returns 3: \'11010\'\n';
88+
buf += ' if (number < 0) {\n';
89+
buf += ' number = 0xFFFFFFFF + number + 1;\n';
90+
buf += ' }\n';
91+
buf += ' return parseInt(number, 10).toString(2);\n';
92+
buf += '};\n';
93+
94+
fs.writeFileSync(__root + '/build/npm.js', buf);
95+
});
96+
};
97+
98+
cli.glob = function(globpath, cb) {
99+
glob(globpath, {}, function(err, files) {
100+
var names = [];
101+
for (var i in files) {
102+
var file = files[i];
103+
if (file.indexOf('/_') === -1) {
104+
names.push(path.basename(file, '.js'));
105+
}
106+
}
107+
names.forEach(function(name) {
108+
PhpjsUtil.load(name, function(err, params) {
109+
if (err) {
110+
return cb(err);
111+
}
112+
113+
return cb(null, params);
114+
});
115+
});
116+
});
117+
};
118+
119+
cli.test = function(args, options) {
120+
var self = this;
121+
var globpath = __root + '/functions/' + options.category + '/' + options.name + '.js';
122+
123+
process.on('exit', function() {
124+
var msg = self.pass_cnt + ' passed / ' + self.fail_cnt + ' failed / ' + self.skip_cnt + ' skipped';
125+
if (self.fail_cnt) {
126+
cli.fatal(msg);
127+
} else {
128+
cli.ok(msg);
129+
}
130+
});
131+
132+
self.pass_cnt = 0;
133+
self.fail_cnt = 0;
134+
self.skip_cnt = 0;
135+
self.glob(globpath, function(err, params) {
136+
if (err) {
137+
return cli.fatal(err);
138+
}
139+
140+
if (params['headKeys']['test'] && params['headKeys']['test'][0] === 'skip') {
141+
self.skip_cnt++;
142+
return cli.info('--> ' + params['name'] + ' skipped as instructed. ');
143+
}
144+
145+
PhpjsUtil.test(params, function(err, test, params) {
146+
if (!err) {
147+
self.pass_cnt++;
148+
cli.debug('--> ' + params['name'] + '#' + (+test['number'] + 1) + ' passed. ');
149+
} else {
150+
self.fail_cnt++;
151+
cli.error('--> ' + params['name'] + '#' + (+test['number'] + 1) + ' failed. ');
152+
cli.error(err);
153+
if (options.abort) {
154+
cli.fatal('Aborting on first failure as instructed. ');
155+
}
156+
}
157+
});
158+
159+
});
160+
};
161+
162+
cli.main(function(args, options) {
163+
cli[options['action']](args, options);
164+
});
165+

build/npm.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
exports.decbin = function (number) {
2+
// Returns a string containing a binary representation of the number
3+
//
4+
// version: 1008.1718
5+
// discuss at: http://phpjs.org/functions/decbin
6+
// + original by: Enrique Gonzalez
7+
// + bugfixed by: Onno Marsman
8+
// + improved by: http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript
9+
// + input by: pilus
10+
// + input by: nord_ua
11+
// * example 1: php.decbin(12);
12+
// * returns 1: '1100'
13+
// * example 2: php.decbin(26);
14+
// * returns 2: '11010'
15+
// * example 3: php.decbin('26');
16+
// * returns 3: '11010'
17+
if (number < 0) {
18+
number = 0xFFFFFFFF + number + 1;
19+
}
20+
return parseInt(number, 10).toString(2);
21+
};

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
phpjs = require('./build/npm');
2+
3+
phpjs.registerGlobals = function() {
4+
for(var key in this) {
5+
global[key] = this[key];
6+
}
7+
};
8+
9+
module.exports = phpjs;

tests/phpjsutil.js renamed to lib/phpjsutil.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ PhpjsUtil.prototype.equal = function(a, b) {
2727
};
2828

2929
PhpjsUtil.prototype._commentBlocks = function(code) {
30-
var cnt = 0;
31-
var comment = [];
30+
var cnt = 0;
31+
var comment = [];
3232
var commentBlocks = [];
33-
var i = 0;
34-
var lines = [];
35-
var raise = false;
33+
var i = 0;
34+
var lines = [];
35+
var raise = false;
3636
for (i in (lines = code.replace('\r', '').split('\n'))) {
3737
// Detect if line is a comment, and return the actual comment
3838
if ((comment = lines[i].match(/^\s*(\/\/|\/\*|\*)\s*(.*)$/))) {
@@ -56,12 +56,12 @@ PhpjsUtil.prototype._commentBlocks = function(code) {
5656

5757
PhpjsUtil.prototype._headKeys = function(headLines) {
5858
var i;
59-
var keys = {};
60-
var match = [];
59+
var keys = {};
60+
var match = [];
6161
var dmatch = [];
62-
var key = '';
63-
var val = '';
64-
var num = 0;
62+
var key = '';
63+
var val = '';
64+
var num = 0;
6565

6666
for (i in headLines) {
6767
if (!(match = headLines[i].match(/^[\s\W]*(.*?)\s*:\s*(.*)\s*$/))) {
@@ -135,26 +135,29 @@ PhpjsUtil.prototype._loadDependencies = function(name, headKeys, dependencies, c
135135

136136
PhpjsUtil.prototype.parse = function(name, code, cb) {
137137
var commentBlocks = this._commentBlocks(code);
138-
var head = commentBlocks[0].raw.join('\n');
139-
var body = code.replace(head, '');
140-
var headKeys = this._headKeys(commentBlocks[0].clean);
141-
142-
// @todo(kvz) If we add function signature, we can use
143-
// body to generate CommonJs compatible output
144-
// in the browser.
138+
var head = commentBlocks[0].raw.join('\n');
139+
var body = code.replace(head, '');
140+
var headKeys = this._headKeys(commentBlocks[0].clean);
145141

146142
this._loadDependencies(name, headKeys, {}, function(err, dependencies) {
147143
if (err) {
148144
return cb(err);
149145
}
150146

147+
//+([^\(\s])\s*\([^\)]+\)
148+
var match = code.match(/^\s*function\s*([^\s\()]+)\(/img);
149+
delete match.input;
150+
delete match.index;
151+
// delete match[0];
152+
151153
cb(null, {
152-
headKeys: headKeys,
153-
body: body,
154-
head: head,
155-
name: name,
156-
code: code,
157-
dependencies: dependencies,
154+
headKeys : headKeys,
155+
body : body,
156+
head : head,
157+
name : name,
158+
code : code,
159+
dependencies : dependencies,
160+
signature : match,
158161
commentBlocks: commentBlocks
159162
});
160163
});
File renamed without changes.

tests/README.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)