Skip to content

Commit

Permalink
[test] npm test passes, [doc] updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Fragomeni committed Jan 31, 2012
1 parent 1e69bd8 commit f994de5
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 92 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<img src="http://github.com/hij1nx/codesurgeon/raw/master/logo.png">

# Synopsis
Aggregate code, refine, manipulate and finalize.
A build automation tool that allows you to aggregate, manipulate, refine and finalize a code base.

# Motivation
Every time a platform emerges, developers create `build tools` that reflect what they learned from existing platforms. This often leads to solving old problems that aren't necessarily relevant.
A build automation tool specifically made for Node.js.

# How it works
Codesurgon reads files and/or piped input into a buffer. From there you can operate on the buffer. Extract functions or variables, obfuscate, lint/hint, write to disk/stream, etc.
Codesurgon reads files and/or piped input into an input buffer. The input buffer is the source used to create the output.

# Features
- Precision extraction of functions or variables by name
- Precision extraction of functions or variables from the input buffer
- Rename extracted items as they are extracted
- Control the depth at which variables and function are searched for
- Extract any arbitrary value
Expand All @@ -38,7 +38,7 @@ $node mybuildscript.js
Codesurgeon will appreciate piped input!

```bash
$cat myfile1 myfile2 | node mybuildfile.js
$cat myfile1.js myfile2.js | node mybuildfile.js
```

## Synchronous example
Expand Down Expand Up @@ -197,7 +197,16 @@ Read one or more files from disk.
```
function read(file [, file, ...])
@files file {String} a string that represent the locations of a file.
@param file {String} a string that represent the locations of a file.
```

### clear()
Provides the means to clear the input and or output buffers before the next read and write.

```
function clear(option)
@param option {String} A string that identifies the buffer to be cleared.
```

### wrap()
Expand Down
41 changes: 21 additions & 20 deletions lib/codesurgeon.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ var fs = require('fs'),
uglify = require('uglify-js');

//
// Codesurgeon
// function Codesurgeon()
// @param options {Object} an object literal of configuration values
//
// Traversing the AST can be strange and ugly. There are arrays of arrays
// and sometimes it looks ugly. But in this case its preferred rather than
// incuring the overhead of an API wrapper, for what is already a low level
// library.
// Traversing the AST can be strange and ugly. There are arrays of arrays of arrays,
// accessing these arrays can casue code to appear fragile. But in fact AST strcutures
// are well known and predictable. Why not make it look nicer by wrapping it in an API?
// This is already a low level library and the performance cost of a high-level wrapper
// wouldn't be worth it.
//
var Codesurgeon = exports.Codesurgeon = function (options) {
if (!(this instanceof Codesurgeon)) return new Codesurgeon(options);

options = options || {};

var buffer = '';

this.options = {
encoding: options.encoding || 'utf8',
quiet: options.quiet || false,
Expand All @@ -36,8 +36,10 @@ var Codesurgeon = exports.Codesurgeon = function (options) {
};

//
// ### function clear
// Provides the opportunity to clear the input and output buffers
// function clear(option)
// @option option {String} A string that identifies the buffer to be cleared.
//
// Provides the means to clear the input and or output buffers
// before the next read and write.
//
Codesurgeon.prototype.clear = function(option) {
Expand All @@ -55,8 +57,9 @@ Codesurgeon.prototype.clear = function(option) {
};

//
// ### function configure (options)
// #### @options {Object} **Optional** Options to configure this instance with
// function configure (options)
// @param options {Object} **Optional** Options to configure this instance with
//
// Configures this instance with the specified `options`.
//
Codesurgeon.prototype.configure = function (options) {
Expand All @@ -70,17 +73,19 @@ Codesurgeon.prototype.configure = function (options) {
return this;
};

// ### function package (file)
// ##### @file {String} A string representing the path to a package.json file.
// function package (file)
// @param file {String} A string representing the path to a package.json file.
//
// Read in the package.json file for making the output filename and headers nice.
//
Codesurgeon.prototype.package = function(file) {
this.packageJSON = JSON.parse(fs.readFileSync(file, 'utf8'));
return this;
};

// ### function read (...files)
// ##### @files {...String} One or more strings representing files to be read.
// function read (...files)
// @files {...String} One or more strings representing files to be read.
//
// Read one or more files async or sync from which to create output.
//
Codesurgeon.prototype.read = function (files) {
Expand Down Expand Up @@ -307,11 +312,6 @@ Codesurgeon.prototype.extract = function (identifiers) {
}

if (name === arg) {
// if (chunks.length > 2) {
// chunks.slice(1).reduce(function (acc, name) {
// return acc + '.' + name;
// });
// }
output[i] = uglify.uglify.gen_code(this.parent.node, opts);
}
}
Expand Down Expand Up @@ -507,6 +507,7 @@ Codesurgeon.prototype.validate = function(options, output) {
var requirements = [];

var sandbox = {

//
// hijack the require function.
//
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "codesurgeon",
"description": "Build JS files based on JS code extracted from files",
"version": "0.2.1",
"version": "0.2.2",
"author": "Nodejitsu Inc <info@nodejitsu.com>",
"contributors": [
{ "name": "Paolo Fragomeni", "email": "hij1nx@nodejitsu.com" }
Expand All @@ -23,9 +23,12 @@
},
"devDependencies": {
"colors": "0.5.x",
"nodeunit": "0.5.x"
"nodeunit": "0.6.x"
},
"main": "./lib/codesurgeon",
"scripts": {
"test": "nodeunit test/validation-test.js"
},
"engines": { "node": ">= 0.4.0" },
"scripts": {
"test": "nodeunit test/*-test.js"
Expand Down
10 changes: 10 additions & 0 deletions test/output/write-test-output-0.2.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


//
// Generated on Tue Jan 31 2012 05:38:29 GMT-0500 (EST) by Nodejitsu Inc <info@nodejitsu.com>
// Version 0.2.2
//
function test5() {
return true;
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@


//
// Generated on Sun Jan 29 2012 22:14:17 GMT-0500 (EST) by Nodejitsu Inc <info@nodejitsu.com>
// Version 0.2.1
// Generated on Tue Jan 31 2012 05:38:30 GMT-0500 (EST) by Nodejitsu Inc <info@nodejitsu.com>
// Version 0.2.2
//

var test1 = 10 + 10;
Expand Down
7 changes: 7 additions & 0 deletions test/output/write-test-output-uglifyd-0.2.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


//
// Generated on Tue Jan 31 2012 05:38:30 GMT-0500 (EST) by Nodejitsu Inc <info@nodejitsu.com>
// Version 0.2.2
//
function test5(){return!0}
14 changes: 14 additions & 0 deletions test/output/write-test-output-wrap-0.2.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


//
// Generated on Tue Jan 31 2012 05:38:29 GMT-0500 (EST) by Nodejitsu Inc <info@nodejitsu.com>
// Version 0.2.2
//

(function (exports) {

exports.hello = "Hello, World.";



}(typeof process !== "undefined" && process.title ? module : window));
9 changes: 8 additions & 1 deletion test/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = write({
var surgeon = new Codesurgeon;

surgeon
.configure({ quiet: true })
.read(__dirname + '/fixture1.js')
.extract(
'NODEJITSU.B',
Expand All @@ -34,6 +35,7 @@ module.exports = write({
var surgeon = new Codesurgeon;

surgeon
.configure({ quiet: true })
.read(__dirname + '/fixture1.js')
.extract(
'NODEJITSU.B',
Expand All @@ -51,6 +53,7 @@ module.exports = write({
var surgeon = new Codesurgeon;

surgeon
.configure({ quiet: true })
.read(__dirname + '/fixture1.js')
.extract(
'NODEJITSU.B',
Expand All @@ -68,6 +71,7 @@ module.exports = write({
var surgeon = new Codesurgeon;

surgeon
.configure({ quiet: true })
.read(__dirname + '/fixture1.js')
.extract(
'NODEJITSU.B',
Expand All @@ -89,7 +93,10 @@ module.exports = write({
};

surgeon
.package('../package.json')
.configure({
quiet: true,
package: __dirname + '/../package.json'
})
.read(__dirname + '/fixture2.js')
.extract()
.validate()
Expand Down
30 changes: 0 additions & 30 deletions test/write-test-output-0.2.1.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/write-test-output-uglifyd-0.2.1.js

This file was deleted.

14 changes: 0 additions & 14 deletions test/write-test-output-wrap-0.2.1.js

This file was deleted.

20 changes: 10 additions & 10 deletions test/write-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ module.exports = write({
surgeon
.configure({
quiet: true,
package: '../package.json'
package: __dirname + '/../package.json'
})
.read(__dirname + '/fixture1.js')
.extract('test5')
.write('write-test-output.js')
.write(__dirname + '/output/write-test-output.js')
;

var file = fs.readFileSync(surgeon.newfile, 'utf8');
Expand All @@ -47,12 +47,12 @@ module.exports = write({
surgeon
.configure({
quiet: true,
package: '../package.json'
package: __dirname + '/../package.json'
})
.read(__dirname + '/fixture1.js')
.extract('exports.hello')
.wrap()
.write('write-test-output-wrap.js')
.write(__dirname + '/output/write-test-output-wrap.js')
;

var file = fs.readFileSync(surgeon.newfile, 'utf8');
Expand All @@ -72,7 +72,7 @@ module.exports = write({
surgeon
.configure({
quiet: true,
package: '../package.json'
package: __dirname + '/../package.json'
})
.read(__dirname + '/fixture1.js')
.extract('test6')
Expand All @@ -98,7 +98,7 @@ module.exports = write({
surgeon
.configure({
quiet: true,
package: '../package.json'
package: __dirname + '/../package.json'
})
.read(__dirname + '/fixture1.js')
.extract('test10')
Expand All @@ -125,12 +125,12 @@ module.exports = write({
surgeon
.configure({
quiet: true,
package: '../package.json'
package: __dirname + '/../package.json'
})
.read(__dirname + '/fixture1.js')
.extract('test5')
.uglify()
.write('write-test-output-uglifyd.js')
.write(__dirname + '/output/write-test-output-uglifyd.js')
;

var file = fs.readFileSync(surgeon.newfile, 'utf8');
Expand All @@ -153,11 +153,11 @@ module.exports = write({
surgeon
.configure({
quiet: true,
package: '../package.json'
package: __dirname + '/../package.json'
})
.read(__dirname + '/fixture1.js')
.extract()
.write('write-test-output-broad.js')
.write(__dirname + '/output/write-test-output-broad.js')
;

var file = fs.readFileSync(surgeon.newfile, 'utf8');
Expand Down

0 comments on commit f994de5

Please sign in to comment.