Remove node modules, add initial tests
This commit is contained in:
parent
97daf2f3a1
commit
7d0858b22e
86
gen-tests.js
86
gen-tests.js
|
@ -1,12 +1,12 @@
|
|||
/**
|
||||
* Generate asteroid unit tests from documentation...
|
||||
* Generate asteroid unit tests from README...
|
||||
*/
|
||||
|
||||
fs = require('fs')
|
||||
|
||||
readme = fs.readFileSync('./README.md').toString();
|
||||
readme = fs.readFileSync('../README.md').toString();
|
||||
|
||||
var alias = {
|
||||
alias = {
|
||||
myModel: 'Model',
|
||||
model: 'Model',
|
||||
ctx: 'Model',
|
||||
|
@ -24,6 +24,9 @@ function getName(line) {
|
|||
|
||||
function Doc(line, lineNum, docIndex) {
|
||||
this.name = getName(line);
|
||||
|
||||
line = line.replace(/#+\s/, '');
|
||||
|
||||
this.line = line;
|
||||
this.lineNum = lineNum;
|
||||
this.docIndex = docIndex;
|
||||
|
@ -59,7 +62,30 @@ Doc.prototype.example = function () {
|
|||
}
|
||||
});
|
||||
|
||||
return result.join('\n');
|
||||
return result;
|
||||
}
|
||||
|
||||
Doc.prototype.desc = function () {
|
||||
var content = this.contents();
|
||||
var result = [];
|
||||
var first;
|
||||
|
||||
content.forEach(function (line) {
|
||||
if(first) {
|
||||
// ignore
|
||||
} else if(line[0] === '#' || line[0] === ' ') {
|
||||
// ignore
|
||||
} else {
|
||||
first = line;
|
||||
}
|
||||
});
|
||||
|
||||
// only want the first sentence (to keep it brief)
|
||||
if(first) {
|
||||
first = first.split(/\.\s|\n/)[0]
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
lines = readme.split('\n')
|
||||
|
@ -84,27 +110,35 @@ sh.rm('-rf', 'g-tests');
|
|||
sh.mkdir('g-tests');
|
||||
|
||||
Object.keys(byName).forEach(function (group) {
|
||||
var testFile =
|
||||
"var asteroid = require('../');" +
|
||||
"\n" +
|
||||
"describe('app', function(){"
|
||||
testFile
|
||||
|
||||
describe('app', function(){
|
||||
var app;
|
||||
|
||||
beforeEach(function () {
|
||||
app = asteroid();
|
||||
});
|
||||
|
||||
describe('asteroid.createModel(name, properties, settings)', function(){
|
||||
var User = asteroid.createModel('user', {
|
||||
first: String,
|
||||
last: String,
|
||||
age: Number
|
||||
});
|
||||
});
|
||||
var testFile = [
|
||||
"describe('" + group + "', function() {",
|
||||
""];
|
||||
|
||||
byName[group].forEach(function (doc) {
|
||||
var example = doc.example();
|
||||
var exampleLines = example && example.length && example;
|
||||
|
||||
testFile = testFile.concat([
|
||||
" describe('" + doc.line + "', function() {",
|
||||
" it(\"" + doc.desc() + "\", function(done) {"]);
|
||||
|
||||
if(exampleLines) {
|
||||
exampleLines.unshift("/* example - ");
|
||||
exampleLines.push("*/")
|
||||
testFile = testFile.concat(
|
||||
exampleLines.map(function (l) {
|
||||
return ' ' + l;
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
testFile.push(
|
||||
" done(new Error('test not implemented'));",
|
||||
" });",
|
||||
" });",
|
||||
"});"
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
testFile.join('\n').to('g-tests/' + group + '.test.js');
|
||||
});
|
|
@ -1 +0,0 @@
|
|||
../mocha/bin/_mocha
|
|
@ -1 +0,0 @@
|
|||
../express/bin/express
|
|
@ -1 +0,0 @@
|
|||
../mocha/bin/mocha
|
|
@ -1 +0,0 @@
|
|||
../shelljs/bin/shjs
|
|
@ -1,10 +0,0 @@
|
|||
.DS_Store
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.swp
|
||||
*.swo
|
||||
node_modules/
|
|
@ -1,73 +0,0 @@
|
|||
# asteroid-module
|
||||
v0.0.1
|
||||
|
||||
## About
|
||||
|
||||
Asteroid applications are a combination of regular Node.js modules and Asteroid modules. Asteroid modules may be initialized using JavaScript or by writing config files.
|
||||
|
||||
## Using Asteroid Modules
|
||||
|
||||
There are two distinct ways to use an Asteroid Module in your application.
|
||||
|
||||
### App API
|
||||
|
||||
The `app` API allows you to define [data sources](../data-source) and [models](../model) in regular Node JavaScript. [See the docs for more info](../../readme.md#API).
|
||||
|
||||
### Config Files
|
||||
|
||||
You may also define [data sources](../data-source), [models](../model) and other asteroid modules by writing `config.json` files. See the documentation for a given module to see what config data it requires.
|
||||
|
||||
## Extending Asteroid
|
||||
|
||||
The core of asteroid is very lightweight and unopionated. All features are added on as `AsteroidModule`s. This means you can add your own functionality, modify existing functionality, or extend existing functionality by creating your own `AsteroidModule` class.
|
||||
|
||||
An `AsteroidModule` is an abstract class that provides a base for all asteroid modules. Its constructor takes an `options` argument provided by a `config.json`. It is also supplied with dependencies it lists on its constructor based on information in the `config.json` file.
|
||||
|
||||
See [model](../model) for an example.
|
||||
|
||||
### AsteroidModule.dependencies
|
||||
|
||||
An asteroid module may define dependencies on other modules that can be configured in `config.json`. Eg. the [collection](../collection/lib/collection.js) module defines a [model](../model) dependency.
|
||||
|
||||
Collection.dependencies = {
|
||||
model: 'model'
|
||||
}
|
||||
|
||||
A configuration then must define:
|
||||
|
||||
{
|
||||
"dependencies": {
|
||||
"model": "some-model-module"
|
||||
}
|
||||
}
|
||||
|
||||
Where `some-model-module` is an existing `model` instance.
|
||||
|
||||
### AsteroidModule.options
|
||||
|
||||
Asteroid Modules may also describe the options they accept. This will validate the configuration and make sure users have supplied required information and in a way that the module can use to construct a working instance.
|
||||
|
||||
Here is an example options description for the [oracle database connection module](../connections/oracle-connection).
|
||||
|
||||
OracleConnection.options = {
|
||||
'hostname': {type: 'string', required: true},
|
||||
'port': {type: 'number', min: 10, max: 99999},
|
||||
'username': {type: 'string'},
|
||||
'password': {type: 'string'}
|
||||
};
|
||||
|
||||
**key** the option name given in `config.json`.
|
||||
|
||||
**type** must be one of:
|
||||
|
||||
- string
|
||||
- boolean
|
||||
- number
|
||||
- array
|
||||
|
||||
**min/max** depend on the type
|
||||
|
||||
{
|
||||
min: 10, // minimum length or value
|
||||
max: 100, // max length or value
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* asteroid-module ~ public api
|
||||
*/
|
||||
|
||||
module.exports = require('./lib/asteroid-module');
|
|
@ -1,37 +0,0 @@
|
|||
/**
|
||||
* Expose `AsteroidModule`.
|
||||
*/
|
||||
|
||||
module.exports = AsteroidModule;
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Module = require('sl-module-loader').Module
|
||||
, debug = require('debug')('asteroid-module')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert');
|
||||
|
||||
/**
|
||||
* Create a new `AsteroidModule` with the given `options`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {AsteroidModule}
|
||||
*/
|
||||
|
||||
function AsteroidModule(options) {
|
||||
Module.apply(this, arguments);
|
||||
|
||||
// throw an error if args are not supplied
|
||||
assert(typeof options === 'object', 'AsteroidModule requires an options object');
|
||||
|
||||
debug('created with options', options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from `Module`.
|
||||
*/
|
||||
|
||||
inherits(AsteroidModule, Module);
|
|
@ -1,14 +0,0 @@
|
|||
{
|
||||
"name": "asteroid-module",
|
||||
"description": "asteroid-module",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "latest"
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
var AsteroidModule = require('../');
|
||||
|
||||
describe('AsteroidModule', function(){
|
||||
var asteroidModule;
|
||||
|
||||
beforeEach(function(){
|
||||
asteroidModule = new AsteroidModule;
|
||||
});
|
||||
|
||||
describe('.myMethod', function(){
|
||||
// example sync test
|
||||
it('should <description of behavior>', function() {
|
||||
asteroidModule.myMethod();
|
||||
});
|
||||
|
||||
// example async test
|
||||
it('should <description of behavior>', function(done) {
|
||||
setTimeout(function () {
|
||||
asteroidModule.myMethod();
|
||||
done();
|
||||
}, 0);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* asteroid-module test setup and support.
|
||||
*/
|
||||
|
||||
assert = require('assert');
|
|
@ -1,10 +0,0 @@
|
|||
.DS_Store
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.swp
|
||||
*.swo
|
||||
node_modules/
|
|
@ -1,11 +0,0 @@
|
|||
# data-source
|
||||
|
||||
## About
|
||||
|
||||
A `DataSource` is the base `AsteroidModule` class for all data sources. Data sources provide APIs for reading and writing remote data from databases and various http apis.
|
||||
|
||||
### Creating a custom Data Source
|
||||
|
||||
To create a custom data source you must define a class that inherits from `DataSource`. This class should define all the required options using the [asteroid module option api](../asteroid-module) (eg. host, port, username, etc).
|
||||
|
||||
The inherited class must provide a property `adapter` that points to a [jugglingdb adapter](https://github.com/1602/jugglingdb#jugglingdb-adapters).
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* connection ~ public api
|
||||
*/
|
||||
|
||||
module.exports = require('./lib/data-source');
|
|
@ -1,40 +0,0 @@
|
|||
/**
|
||||
* Expose `DataSource`.
|
||||
*/
|
||||
|
||||
module.exports = DataSource;
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var AsteroidModule = require('asteroid-module')
|
||||
, Schema = require('jugglingdb').Schema
|
||||
, debug = require('debug')('connection')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert');
|
||||
|
||||
/**
|
||||
* Create a new `DataSource` with the given `options`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {DataSource}
|
||||
*/
|
||||
|
||||
function DataSource(options) {
|
||||
AsteroidModule.apply(this, arguments);
|
||||
this.options = options;
|
||||
|
||||
// construct a schema with the available adapter
|
||||
// or use the default in memory adapter
|
||||
this.schema = new Schema(this.adapter || require('./memory'), options);
|
||||
|
||||
debug('created with options', options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from `AsteroidModule`.
|
||||
*/
|
||||
|
||||
inherits(DataSource, AsteroidModule);
|
|
@ -1,260 +0,0 @@
|
|||
exports.initialize = function initializeSchema(schema, callback) {
|
||||
schema.adapter = new Memory();
|
||||
schema.adapter.connect(callback);
|
||||
};
|
||||
|
||||
function Memory(m) {
|
||||
if (m) {
|
||||
this.isTransaction = true;
|
||||
this.cache = m.cache;
|
||||
this.ids = m.ids;
|
||||
this._models = m._models;
|
||||
} else {
|
||||
this.isTransaction = false;
|
||||
// use asteroid cache, otherwise state will be reset during configuration
|
||||
this.cache = process.__asteroidCache.memoryStore || (process.__asteroidCache.memoryStore = {});
|
||||
this.ids = {};
|
||||
this._models = {};
|
||||
}
|
||||
}
|
||||
|
||||
Memory.prototype.connect = function(callback) {
|
||||
if (this.isTransaction) {
|
||||
this.onTransactionExec = callback;
|
||||
} else {
|
||||
process.nextTick(callback);
|
||||
}
|
||||
};
|
||||
|
||||
Memory.prototype.define = function defineModel(descr) {
|
||||
var m = descr.model.modelName;
|
||||
this._models[m] = descr;
|
||||
// allow reuse of data
|
||||
this.cache[m] = this.cache[m] || {};
|
||||
this.ids[m] = 1;
|
||||
};
|
||||
|
||||
Memory.prototype.create = function create(model, data, callback) {
|
||||
var id = data.id || this.ids[model]++;
|
||||
data.id = id;
|
||||
this.cache[model][id] = JSON.stringify(data);
|
||||
process.nextTick(function() {
|
||||
callback(null, id);
|
||||
});
|
||||
};
|
||||
|
||||
Memory.prototype.updateOrCreate = function (model, data, callback) {
|
||||
var mem = this;
|
||||
this.exists(model, data.id, function (err, exists) {
|
||||
if (exists) {
|
||||
mem.save(model, data, callback);
|
||||
} else {
|
||||
mem.create(model, data, function (err, id) {
|
||||
data.id = id;
|
||||
callback(err, data);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Memory.prototype.save = function save(model, data, callback) {
|
||||
this.cache[model][data.id] = JSON.stringify(data);
|
||||
process.nextTick(function () {
|
||||
callback(null, data);
|
||||
});
|
||||
};
|
||||
|
||||
Memory.prototype.exists = function exists(model, id, callback) {
|
||||
process.nextTick(function () {
|
||||
callback(null, this.cache[model].hasOwnProperty(id));
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
Memory.prototype.find = function find(model, id, callback) {
|
||||
process.nextTick(function () {
|
||||
callback(null, id in this.cache[model] && this.fromDb(model, this.cache[model][id]));
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
Memory.prototype.destroy = function destroy(model, id, callback) {
|
||||
delete this.cache[model][id];
|
||||
process.nextTick(callback);
|
||||
};
|
||||
|
||||
Memory.prototype.fromDb = function(model, data) {
|
||||
if (!data) return null;
|
||||
data = JSON.parse(data);
|
||||
var props = this._models[model].properties;
|
||||
Object.keys(data).forEach(function (key) {
|
||||
var val = data[key];
|
||||
if (typeof val === 'undefined' || val === null) {
|
||||
return;
|
||||
}
|
||||
if (props[key]) {
|
||||
switch(props[key].type.name) {
|
||||
case 'Date':
|
||||
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
|
||||
break;
|
||||
case 'Boolean':
|
||||
val = new Boolean(val);
|
||||
break;
|
||||
}
|
||||
}
|
||||
data[key] = val;
|
||||
});
|
||||
return data;
|
||||
};
|
||||
|
||||
Memory.prototype.all = function all(model, filter, callback) {
|
||||
var self = this;
|
||||
var nodes = [];
|
||||
var data = this.cache[model];
|
||||
var keys = Object.keys(data);
|
||||
var scanned = 0;
|
||||
|
||||
while(scanned < keys.length) {
|
||||
nodes.push(this.fromDb(model, data[keys[scanned]]));
|
||||
scanned++;
|
||||
}
|
||||
|
||||
if (filter) {
|
||||
|
||||
// do we need some sorting?
|
||||
if (filter.order) {
|
||||
var props = this._models[model].properties;
|
||||
var orders = filter.order;
|
||||
if (typeof filter.order === "string") {
|
||||
orders = [filter.order];
|
||||
}
|
||||
orders.forEach(function (key, i) {
|
||||
var reverse = 1;
|
||||
var m = key.match(/\s+(A|DE)SC$/i);
|
||||
if (m) {
|
||||
key = key.replace(/\s+(A|DE)SC/i, '');
|
||||
if (m[1].toLowerCase() === 'de') reverse = -1;
|
||||
}
|
||||
orders[i] = {"key": key, "reverse": reverse};
|
||||
});
|
||||
nodes = nodes.sort(sorting.bind(orders));
|
||||
}
|
||||
|
||||
// do we need some filtration?
|
||||
if (filter.where) {
|
||||
nodes = nodes ? nodes.filter(applyFilter(filter)) : nodes;
|
||||
}
|
||||
|
||||
// skip
|
||||
if(filter.skip) {
|
||||
nodes = nodes.slice(filter.skip, nodes.length);
|
||||
}
|
||||
|
||||
if(filter.limit) {
|
||||
nodes = nodes.slice(0, filter.limit);
|
||||
}
|
||||
}
|
||||
|
||||
process.nextTick(function () {
|
||||
if (filter && filter.include) {
|
||||
self._models[model].model.include(nodes, filter.include, callback);
|
||||
} else {
|
||||
callback(null, nodes);
|
||||
}
|
||||
});
|
||||
|
||||
function sorting(a, b) {
|
||||
for (var i=0, l=this.length; i<l; i++) {
|
||||
if (a[this[i].key] > b[this[i].key]) {
|
||||
return 1*this[i].reverse;
|
||||
} else if (a[this[i].key] < b[this[i].key]) {
|
||||
return -1*this[i].reverse;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
function applyFilter(filter) {
|
||||
if (typeof filter.where === 'function') {
|
||||
return filter.where;
|
||||
}
|
||||
var keys = Object.keys(filter.where);
|
||||
return function (obj) {
|
||||
var pass = true;
|
||||
keys.forEach(function (key) {
|
||||
if (!test(filter.where[key], obj[key])) {
|
||||
pass = false;
|
||||
}
|
||||
});
|
||||
return pass;
|
||||
}
|
||||
|
||||
function test(example, value) {
|
||||
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
|
||||
return value.match(example);
|
||||
}
|
||||
if (typeof example === 'undefined') return undefined;
|
||||
if (typeof value === 'undefined') return undefined;
|
||||
if (typeof example === 'object') {
|
||||
if (example.inq) {
|
||||
if (!value) return false;
|
||||
for (var i = 0; i < example.inq.length; i += 1) {
|
||||
if (example.inq[i] == value) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// not strict equality
|
||||
return (example !== null ? example.toString() : example) == (value !== null ? value.toString() : value);
|
||||
}
|
||||
}
|
||||
|
||||
Memory.prototype.destroyAll = function destroyAll(model, callback) {
|
||||
Object.keys(this.cache[model]).forEach(function (id) {
|
||||
delete this.cache[model][id];
|
||||
}.bind(this));
|
||||
this.cache[model] = {};
|
||||
process.nextTick(callback);
|
||||
};
|
||||
|
||||
Memory.prototype.count = function count(model, callback, where) {
|
||||
var cache = this.cache[model];
|
||||
var data = Object.keys(cache)
|
||||
if (where) {
|
||||
data = data.filter(function (id) {
|
||||
var ok = true;
|
||||
Object.keys(where).forEach(function (key) {
|
||||
if (JSON.parse(cache[id])[key] != where[key]) {
|
||||
ok = false;
|
||||
}
|
||||
});
|
||||
return ok;
|
||||
});
|
||||
}
|
||||
process.nextTick(function () {
|
||||
callback(null, data.length);
|
||||
});
|
||||
};
|
||||
|
||||
Memory.prototype.updateAttributes = function updateAttributes(model, id, data, cb) {
|
||||
data.id = id;
|
||||
|
||||
var base = JSON.parse(this.cache[model][id]);
|
||||
this.save(model, merge(base, data), cb);
|
||||
};
|
||||
|
||||
Memory.prototype.transaction = function () {
|
||||
return new Memory(this);
|
||||
};
|
||||
|
||||
Memory.prototype.exec = function(callback) {
|
||||
this.onTransactionExec();
|
||||
setTimeout(callback, 50);
|
||||
};
|
||||
|
||||
function merge(base, update) {
|
||||
if (!base) return update;
|
||||
Object.keys(update).forEach(function (key) {
|
||||
base[key] = update[key];
|
||||
});
|
||||
return base;
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
{
|
||||
"name": "data-source",
|
||||
"description": "data-source",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": "latest",
|
||||
"jugglingdb": "~0.2.0-30"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "latest"
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
var DataSource = require('../');
|
||||
|
||||
describe('DataSource', function(){
|
||||
var connection;
|
||||
|
||||
beforeEach(function(){
|
||||
dataSource = new DataSource;
|
||||
});
|
||||
|
||||
describe('.myMethod', function(){
|
||||
// example sync test
|
||||
it('should <description of behavior>', function() {
|
||||
dataSource.myMethod();
|
||||
});
|
||||
|
||||
// example async test
|
||||
it('should <description of behavior>', function(done) {
|
||||
setTimeout(function () {
|
||||
dataSource.myMethod();
|
||||
done();
|
||||
}, 0);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* connection test setup and support.
|
||||
*/
|
||||
|
||||
assert = require('assert');
|
|
@ -1,10 +0,0 @@
|
|||
.DS_Store
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.swp
|
||||
*.swo
|
||||
node_modules/
|
|
@ -1,255 +0,0 @@
|
|||
# model
|
||||
|
||||
## About
|
||||
|
||||
A `Model` represents the data of your application. Asteroid `Model`s are mostly used for validating interactions with a [DataSource](../data-source). Usually your models will correspond to a namespace in your data source (database table, http url, etc). The bulk of your application's business logic will be in your `Model` or Node.js scripts that require your model.
|
||||
|
||||
## Data Definition Language
|
||||
|
||||
TODO ~ document
|
||||
|
||||
## API
|
||||
|
||||
### Defining Models
|
||||
|
||||
The following assumes your have reference to a class that inherits from `Model`. The simplest way to get this is by using the [app API](../../readme.md#API).
|
||||
|
||||
// define a model class using the app api
|
||||
var Color = app.define('color');
|
||||
|
||||
// provide an exact plural name
|
||||
var Color = app.define('color', {plural: 'colors'});
|
||||
|
||||
**Note:** If a plural name is not defined, the model will try to pluralize the singular form.
|
||||
|
||||
#### MyModel.defineSchema(schema)
|
||||
|
||||
Define the data the model represents using the data definition language.
|
||||
|
||||
// define the color model
|
||||
var Color = app.define('color');
|
||||
|
||||
// define the schema for the Color model
|
||||
Color.defineSchema({
|
||||
name: 'string',
|
||||
id: 'uid',
|
||||
tweets: 'array'
|
||||
});
|
||||
|
||||
##### MyModel.dataSource(name, namespace)
|
||||
|
||||
Set the data source for the model. Must provide a name of an existing data source. If the `namespace` is not provided the plural model name (eg. `colors`) will be used.
|
||||
|
||||
// set the data source
|
||||
Color.dataSource('color-db', 'COLOR_NAMES');
|
||||
|
||||
**Note:** If you do not set a data source or a map (or both) the default data source will be used (an in memory database).
|
||||
|
||||
#### MyModel.defineMap(map)
|
||||
|
||||
Define a mapping between the data source representation of your data and your app's representation.
|
||||
|
||||
// manually map Color to existing table columns
|
||||
Color.defineMap({
|
||||
dataSource: 'color-db', // optional, will use model's data source
|
||||
table: 'COLOR_NAMES', // required
|
||||
map: { // optional if schema defined
|
||||
id: 'COLOR_ID',
|
||||
name: 'COLOR_NAME'
|
||||
}
|
||||
});
|
||||
|
||||
// mix in a mapping from another data source
|
||||
Color.defineMap({
|
||||
dataSource: 'twitter',
|
||||
url: function(color) {
|
||||
return '/search?limit=5&q=' + color.name;
|
||||
},
|
||||
map: {
|
||||
// provides the color.tweets property
|
||||
tweets: function(tweets) {
|
||||
return tweets;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
**Note:** You may define multiple maps for a single model. The model will combine the data for you.
|
||||
|
||||
#### MyModel.discoverSchema(fn)
|
||||
|
||||
Using the mapped data source, try to discover the schema of a table or other namespace (url, collection, etc).
|
||||
|
||||
// use existing schema to map to desired properties
|
||||
Color.dataSource('color-db', 'COLOR_NAMES');
|
||||
Color.discoverSchema(function (err, oracleSchema) {
|
||||
var schema = {tweets: 'array'};
|
||||
var map = {dataSource: 'color-db', table: 'COLOR_NAMES'};
|
||||
|
||||
// inspect the existing table schema to create a mapping
|
||||
Object
|
||||
.keys(oracleSchema)
|
||||
.forEach(function (oracleProperty) {
|
||||
// remove prefix
|
||||
var property = oracleProperty.toLowerCase().split('_')[0];
|
||||
|
||||
// build new schema
|
||||
schema[property] = oracleProperty[oracleProperty];
|
||||
// create mapping to existing schema
|
||||
map[property] = oracleProperty;
|
||||
});
|
||||
|
||||
Color.defineSchema(schema);
|
||||
Color.defineMap(map);
|
||||
});
|
||||
|
||||
### Custom Methods
|
||||
|
||||
There are two types of custom methods. Static and instance.
|
||||
|
||||
**Static**
|
||||
|
||||
Static methods are available on the Model class itself and are used to operate on many models at the same time.
|
||||
|
||||
**Instance**
|
||||
|
||||
Instance methods are available on an instance of a Model and usually act on a single model at a time.
|
||||
|
||||
#### Defining a Static Method
|
||||
|
||||
The following example shows how to define a simple static method.
|
||||
|
||||
Color.myStaticMethod = function() {
|
||||
// only has access to other static methods
|
||||
this.find(function(err, colors) {
|
||||
console.log(colors); // [...]
|
||||
});
|
||||
}
|
||||
|
||||
#### Defining an Instance Method
|
||||
|
||||
The following is an example of a simple instance method.
|
||||
|
||||
Color.prototype.myInstanceMethod = function() {
|
||||
console.log(this.name); // red
|
||||
}
|
||||
|
||||
#### Remotable Methods
|
||||
|
||||
Both types of methods may be set as `remotable` as long as they conform to the remotable requirements. Asteroid will expose these methods over the network for you.
|
||||
|
||||
##### Remotable Requirements
|
||||
|
||||
Static and instance methods must accept a callback as the last argument. This callback must be called with an error as the first argument and the results as arguments afterward.
|
||||
|
||||
You must also define the input and output of your remoteable method. Describe the input or arguments of the function by providing an `accepts` array and describe the output by defining a `returns` array.
|
||||
|
||||
// this method meets the remotable requirements
|
||||
Color.getByName = function (name, callback) {
|
||||
Color.find({where: {name: name}}, function (err, colors) {
|
||||
// if an error occurs callback with the error
|
||||
if(err) {
|
||||
callback(err);
|
||||
} else {
|
||||
callback(null, colors);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// accepts a name of type string
|
||||
Color.getByName.accepts = [
|
||||
{arg: 'name', type: 'String'} // data definition language
|
||||
];
|
||||
|
||||
// returns an array of type Color
|
||||
Color.getByName.returns = [
|
||||
{arg: 'colors', type: ['Color']} // data definition language
|
||||
];
|
||||
|
||||
**Note:** any types included in `accepts`/`returns` must be native JavaScript types or Model classes.
|
||||
|
||||
### Working with Models
|
||||
|
||||
The following assumes you have access to an instance of a `Model` class.
|
||||
|
||||
// define a model
|
||||
var Color = app.define('color');
|
||||
|
||||
// create an instance
|
||||
var color = new Color({name: red});
|
||||
|
||||
#### myModel.save([options], [callback])
|
||||
|
||||
**Remoteable**
|
||||
|
||||
Save the model using its configured data source.
|
||||
|
||||
var color = new Color();
|
||||
color.name = 'green';
|
||||
|
||||
// fire and forget
|
||||
color.save();
|
||||
|
||||
// callback
|
||||
color.save(function(err, color) {
|
||||
if(err) {
|
||||
console.log(err); // validation or other error
|
||||
} else {
|
||||
console.log(color); // updated with id
|
||||
}
|
||||
});
|
||||
|
||||
#### myModel.destroy([callback])
|
||||
|
||||
**Remoteable**
|
||||
|
||||
Delete the instance using attached data source. Invoke callback when ready.
|
||||
|
||||
var color = Color.create({id: 10});
|
||||
|
||||
color.destroy(function(err) {
|
||||
if(err) {
|
||||
console.log(err); // could not destroy
|
||||
} else {
|
||||
console.log('model has been destroyed!');
|
||||
}
|
||||
});
|
||||
|
||||
#### MyModel.all()
|
||||
#### MyModel.find()
|
||||
#### MyModel.count()
|
||||
|
||||
### Model Relationships
|
||||
|
||||
## Config
|
||||
|
||||
### Options
|
||||
|
||||
#### namespace
|
||||
|
||||
A table, collection, url, or other namespace.
|
||||
|
||||
#### properties
|
||||
|
||||
An array of properties describing the model's schema.
|
||||
|
||||
"properties": [
|
||||
{
|
||||
"name": "title",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"name": "done",
|
||||
"type": "boolean"
|
||||
},
|
||||
{
|
||||
"name": "order",
|
||||
"type": "number"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
### Dependencies
|
||||
|
||||
#### data source
|
||||
|
||||
The name of a data source [data-source](../data-source) for persisting data.
|
|
@ -1,12 +0,0 @@
|
|||
/**
|
||||
* A generated `Model` example...
|
||||
*
|
||||
* Examples should show a working module api
|
||||
* and be used in tests to continously check
|
||||
* they function as expected.
|
||||
*/
|
||||
|
||||
var Model = require('../');
|
||||
var model = Model.create();
|
||||
|
||||
model.myMethod();
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* model ~ public api
|
||||
*/
|
||||
|
||||
module.exports = require('./lib/model-configuration');
|
|
@ -1,79 +0,0 @@
|
|||
/**
|
||||
* Expose `ModelConfiguration`.
|
||||
*/
|
||||
|
||||
module.exports = ModelConfiguration;
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var AsteroidModule = require('asteroid-module')
|
||||
, Model = require('./model')
|
||||
, debug = require('debug')('model-configuration')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert');
|
||||
|
||||
/**
|
||||
* Create a new `ModelConfiguration` with the given `options`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {Model}
|
||||
*/
|
||||
|
||||
function ModelConfiguration(options) {
|
||||
AsteroidModule.apply(this, arguments);
|
||||
this.options = options;
|
||||
|
||||
var dependencies = this.dependencies;
|
||||
var dataSource = dependencies['data-source'];
|
||||
var schema = this.schema = dataSource.schema;
|
||||
|
||||
assert(Array.isArray(options.properties), 'the ' + options._name + ' model requires an options.properties array');
|
||||
|
||||
// define model
|
||||
var ModelCtor = this.ModelCtor = this.BaseModel.extend(options);
|
||||
|
||||
assert(dataSource.name, 'cannot map a model to a datasource without a name');
|
||||
|
||||
// define provided mappings
|
||||
if(options.maps) {
|
||||
options.maps.forEach(function (config) {
|
||||
assert(config.dataSource, 'Model config.options.maps requires a `dataSource` when defining maps');
|
||||
assert(config.map, 'Model config.options.maps requires a `map` when defining maps');
|
||||
|
||||
ModelCtor.defineMap(dataSource.name, config);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from `AsteroidModule`.
|
||||
*/
|
||||
|
||||
inherits(ModelConfiguration, AsteroidModule);
|
||||
|
||||
/**
|
||||
* The model to extend (should be overridden in sub classes).
|
||||
*/
|
||||
|
||||
ModelConfiguration.prototype.BaseModel = Model;
|
||||
|
||||
/**
|
||||
* Dependencies.
|
||||
*/
|
||||
|
||||
ModelConfiguration.dependencies = {
|
||||
'data-source': 'data-source'
|
||||
};
|
||||
|
||||
/**
|
||||
* Options.
|
||||
*/
|
||||
|
||||
ModelConfiguration.options = {
|
||||
'name': {type: 'string'},
|
||||
'properties': {type: 'array'},
|
||||
'maps': {type: 'array'}
|
||||
};
|
|
@ -1,121 +0,0 @@
|
|||
/**
|
||||
* Expose `Model`.
|
||||
*/
|
||||
|
||||
module.exports = Model;
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
, debug = require('debug')('model')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert');
|
||||
|
||||
/**
|
||||
* Create a new `Model` with the given `options`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {Model}
|
||||
*/
|
||||
|
||||
function Model(data) {
|
||||
EventEmitter.call(this);
|
||||
|
||||
var ModelCtor = this.constructor;
|
||||
var schema = ModelCtor.schema;
|
||||
|
||||
// get properties that match the schema
|
||||
var matchedProperties = schema.getMatchedProperties(data);
|
||||
|
||||
// set properties that match the schema
|
||||
Object.keys(matchedProperties).forEach(function (property) {
|
||||
this[property] = matchedProperties[property];
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from `EventEmitter`.
|
||||
*/
|
||||
|
||||
inherits(Model, EventEmitter);
|
||||
|
||||
/**
|
||||
* Create a new Model class from the given options.
|
||||
*
|
||||
* @param options {Object}
|
||||
* @return {Model}
|
||||
*/
|
||||
|
||||
Model.extend = function (options) {
|
||||
var Super = this;
|
||||
|
||||
// the new constructor
|
||||
function Model() {
|
||||
Super.apply(this, arguments);
|
||||
}
|
||||
|
||||
Model.options = options;
|
||||
|
||||
assert(options.name, 'must provide a name when extending from model');
|
||||
|
||||
// model namespace
|
||||
Model.namespace = options.name;
|
||||
|
||||
// define the remote namespace
|
||||
Model.remoteNamespace = options.plural || pluralize(Model.namespace);
|
||||
|
||||
// inherit all static methods
|
||||
Object.keys(Super).forEach(function (key) {
|
||||
if(typeof Super[key] === 'function') {
|
||||
MyModel[key] = Super[key];
|
||||
}
|
||||
});
|
||||
|
||||
// inherit all other things
|
||||
inherits(MyModel, Super);
|
||||
|
||||
return Model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a model instance for remote use.
|
||||
*/
|
||||
|
||||
Model.sharedCtor = function (data, fn) {
|
||||
var ModelCtor = this;
|
||||
|
||||
fn(null, new ModelCtor(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the data the model represents using the data definition language.
|
||||
*/
|
||||
|
||||
Model.defineSchema = function (schema) {
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the data source for the model. Must provide a name of an existing data source.
|
||||
* If the namespace is not provided the plural model name (eg. colors) will be used.
|
||||
*
|
||||
* **Note:** If you do not set a data source or a map (or both) the default data source
|
||||
* will be used (an in memory database).
|
||||
*/
|
||||
|
||||
Model.dataSource = function (dataSourceName, namespace) {
|
||||
namespace = namespace || this.namespace;
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a mapping between the data source representation of your data and your app's representation.
|
||||
*/
|
||||
|
||||
Model.defineMap = function (mapping) {
|
||||
// see: https://github.com/strongloop/asteroid/tree/master/node_modules/model#mymodeldefinemapmap
|
||||
throw new Error('not implemented');
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
{
|
||||
"name": "model",
|
||||
"description": "model",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "latest"
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
var Model = require('../');
|
||||
|
||||
describe('Model', function(){
|
||||
var model;
|
||||
|
||||
beforeEach(function(){
|
||||
model = new Model;
|
||||
});
|
||||
|
||||
describe('.myMethod', function(){
|
||||
// example sync test
|
||||
it('should <description of behavior>', function() {
|
||||
model.myMethod();
|
||||
});
|
||||
|
||||
// example async test
|
||||
it('should <description of behavior>', function(done) {
|
||||
setTimeout(function () {
|
||||
model.myMethod();
|
||||
done();
|
||||
}, 0);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* model test setup and support.
|
||||
*/
|
||||
|
||||
assert = require('assert');
|
|
@ -1,10 +0,0 @@
|
|||
.DS_Store
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.swp
|
||||
*.swo
|
||||
node_modules/
|
|
@ -1,12 +0,0 @@
|
|||
# oracle-data-source
|
||||
|
||||
## About
|
||||
|
||||
Configures the oracle adapter for use in a [data store](../../store).
|
||||
|
||||
### Options
|
||||
|
||||
#### hostname
|
||||
#### port
|
||||
#### username
|
||||
#### password
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* connection ~ public api
|
||||
*/
|
||||
|
||||
module.exports = require('./lib/oracle-data-source');
|
|
@ -1,51 +0,0 @@
|
|||
/**
|
||||
* Expose `OracleDataSource`.
|
||||
*/
|
||||
|
||||
module.exports = OracleDataSource;
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var DataSource = require('data-source')
|
||||
, debug = require('debug')('oracle-data-source')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert');
|
||||
|
||||
/**
|
||||
* Create a new `OracleDataSource` with the given `options`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {DataSource}
|
||||
*/
|
||||
|
||||
function OracleDataSource(options) {
|
||||
DataSource.apply(this, arguments);
|
||||
debug('created with options', options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from `AsteroidModule`.
|
||||
*/
|
||||
|
||||
inherits(OracleDataSource, DataSource);
|
||||
|
||||
/**
|
||||
* Define options.
|
||||
*/
|
||||
|
||||
OracleDataSource.options = {
|
||||
'database': {type: 'string', required: true},
|
||||
'host': {type: 'string', required: true},
|
||||
'port': {type: 'number', min: 10, max: 99999},
|
||||
'username': {type: 'string'},
|
||||
'password': {type: 'string'}
|
||||
};
|
||||
|
||||
/**
|
||||
* Provide the oracle jugglingdb adapter
|
||||
*/
|
||||
|
||||
OracleDataSource.prototype.adapter = require('jugglingdb-oracle');
|
|
@ -1,15 +0,0 @@
|
|||
{
|
||||
"name": "oracle-data-source",
|
||||
"description": "oracle-data-source",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"dependencies": {
|
||||
"jugglingdb-oracle": "latest",
|
||||
"debug": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "latest"
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
.DS_Store
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.swp
|
||||
*.swo
|
||||
node_modules/
|
|
@ -1,73 +0,0 @@
|
|||
# asteroid.Route
|
||||
|
||||
## About
|
||||
|
||||
A `Route` inherits from the [asteroid module](../asteroid-module) class. It wraps an asteroid application so that it can be used as a sub application initialized by a configuration file.
|
||||
|
||||
This example shows the basic usage of a `Route` as a sub application. You should never have to write this code since the route will be created and mounted for you by asteroid.
|
||||
|
||||
var asteroid = require('asteroid');
|
||||
var Route = require('route');
|
||||
|
||||
var app = asteroid();
|
||||
var subApp = new Route({root: '/my-sub-app'});
|
||||
subApp.mount(app);
|
||||
|
||||
subApp.get('/', function (req, res) {
|
||||
res.send(req.url); // /my-sub-app
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
## route.app
|
||||
|
||||
Each route is constructed with a asteroid/express sub app at the path provided in the route's `config.json` options.
|
||||
|
||||
### myRoute.app.VERB(path, [callback...], callback)
|
||||
|
||||
The `myRoute.VERB()` methods provide routing functionality inherited from [Express](http://expressjs.com/api.html#app.get), where **VERB** is one of the HTTP verbs, such as `myRoute.post()`. See the [Express docs](http://expressjs.com/api.html#app.get) for more info.
|
||||
|
||||
|
||||
**Examples**
|
||||
|
||||
myRoute.get('/hello-world', function(req, res) {
|
||||
res.send('hello world');
|
||||
});
|
||||
|
||||
|
||||
### myRoute.app.use([path], middleware)
|
||||
|
||||
Use the given middleware function.
|
||||
|
||||
**Examples**
|
||||
|
||||
// a logger middleware
|
||||
myRoute.use(function(req, res, next){
|
||||
console.log(req.method, req.url); // GET /my-route
|
||||
next();
|
||||
});
|
||||
|
||||
## Config
|
||||
|
||||
### Options
|
||||
|
||||
#### path
|
||||
|
||||
The `asteroid.Route` path where the route will be mounted.
|
||||
|
||||
**Examples**
|
||||
|
||||
{
|
||||
"options": {
|
||||
"path": "/foo" // responds at /foo
|
||||
}
|
||||
}
|
||||
|
||||
<!-- ... -->
|
||||
|
||||
{
|
||||
"options": {
|
||||
"path": "/foo/:bar" // provides :bar param at `req.param('bar')`.
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* resource ~ public api
|
||||
*/
|
||||
|
||||
module.exports = require('./lib/route');
|
|
@ -1,114 +0,0 @@
|
|||
/**
|
||||
* Expose `HttpContext`.
|
||||
*/
|
||||
|
||||
module.exports = HttpContext;
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
, debug = require('debug')('http-context')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert');
|
||||
|
||||
/**
|
||||
* Create a new `HttpContext` with the given `options`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {HttpContext}
|
||||
*/
|
||||
|
||||
function HttpContext(resource, req, res, next) {
|
||||
EventEmitter.apply(this, arguments);
|
||||
|
||||
this.resource = resource;
|
||||
this.req = req;
|
||||
this.res = res;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from `EventEmitter`.
|
||||
*/
|
||||
|
||||
inherits(HttpContext, EventEmitter);
|
||||
|
||||
/**
|
||||
* Override the default emitter behavior to support async or sync hooks before and after an event.
|
||||
*/
|
||||
|
||||
HttpContext.prototype.emit = function (ev) {
|
||||
var ctx = this;
|
||||
var resource = this.resource;
|
||||
var origArgs = arguments;
|
||||
var args = Array.prototype.slice.call(arguments, 0)
|
||||
var success = arguments[arguments.length - 1];
|
||||
|
||||
assert(typeof success === 'function', 'ctx.emit requires a callback');
|
||||
args.pop();
|
||||
|
||||
var evName = ev;
|
||||
assert(typeof evName === 'string');
|
||||
args.shift();
|
||||
|
||||
var listeners = resource.listeners(evName);
|
||||
var listener;
|
||||
|
||||
// start
|
||||
next();
|
||||
|
||||
function next(err) {
|
||||
if(err) return fail(err);
|
||||
|
||||
try {
|
||||
if(listener = listeners.shift()) {
|
||||
var expectsCallback = listener._expects === args.length + 2;
|
||||
|
||||
// if a listener expects all the `args`
|
||||
// plus ctx, and a callback
|
||||
if(expectsCallback) {
|
||||
// include ctx (this) and pass next to continue
|
||||
listener.apply(resource, args.concat([this, next]));
|
||||
} else {
|
||||
// dont include the callback
|
||||
listener.apply(resource, args.concat([this]));
|
||||
// call next directly
|
||||
next();
|
||||
}
|
||||
} else {
|
||||
success(done);
|
||||
}
|
||||
} catch(e) {
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
function fail(err) {
|
||||
ctx.done(err);
|
||||
}
|
||||
|
||||
function done(err, result) {
|
||||
if(err) {
|
||||
return fail(err);
|
||||
}
|
||||
|
||||
ctx.emit.apply(ctx,
|
||||
['after:' + evName] // after event
|
||||
.concat(args) // include original arguments/data
|
||||
.concat([function () { // success callback
|
||||
ctx.done.call(ctx, err, result);
|
||||
}])
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
HttpContext.prototype.done = function (err, result) {
|
||||
if(err) {
|
||||
this.next(err);
|
||||
} else {
|
||||
this.res.send(result);
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
/**
|
||||
* Expose `Route`.
|
||||
*/
|
||||
|
||||
module.exports = Route;
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var asteroid = require('asteroid')
|
||||
, AsteroidModule = require('asteroid-module')
|
||||
, HttpContext = require('./http-context')
|
||||
, debug = require('debug')('asteroid:resource')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert');
|
||||
|
||||
/**
|
||||
* Create a new `Route` with the given `options`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {Route}
|
||||
*/
|
||||
|
||||
function Route(options) {
|
||||
AsteroidModule.apply(this, arguments);
|
||||
|
||||
// throw an error if args are not supplied
|
||||
assert(typeof options === 'object', 'Route requires an options object');
|
||||
assert(options.path, 'Route requires a path');
|
||||
|
||||
// create the sub app
|
||||
var app = this.app = asteroid();
|
||||
|
||||
this.options = options;
|
||||
|
||||
debug('created with options', options);
|
||||
|
||||
this.on('destroyed', function () {
|
||||
app.disuse(this.options.path);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from `AsteroidModule`.
|
||||
*/
|
||||
|
||||
inherits(Route, AsteroidModule);
|
||||
|
||||
/**
|
||||
* Mount the sub app on the given parent app at the configured path.
|
||||
*/
|
||||
|
||||
Route.prototype.mount = function (parent) {
|
||||
this.parent = parent;
|
||||
parent.use(this.options.path, this.app);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an http context bound to the current resource.
|
||||
*/
|
||||
|
||||
Route.prototype.createContext = function (req, res, next) {
|
||||
return new HttpContext(this, req, res, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override `on` to determine how many arguments an event handler expects.
|
||||
*/
|
||||
|
||||
Route.prototype.on = function () {
|
||||
var fn = arguments[arguments.length - 1];
|
||||
|
||||
if(typeof fn === 'function') {
|
||||
// parse expected arguments from function src
|
||||
// fn.listener handles the wrapped function during `.once()`
|
||||
var src = (fn.listener || fn).toString();
|
||||
fn._expects = src.split('{')[0].split(',').length;
|
||||
}
|
||||
|
||||
AsteroidModule.prototype.on.apply(this, arguments);
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
{
|
||||
"name": "resource",
|
||||
"description": "resource",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "latest"
|
||||
}
|
||||
}
|
|
@ -1,95 +0,0 @@
|
|||
var HttpContext = require('../lib/http-context.js');
|
||||
var Resource = require('../lib/resource.js');
|
||||
|
||||
describe('HttpContext', function(){
|
||||
var ctx;
|
||||
var resource;
|
||||
|
||||
function createRequest() {
|
||||
return {};
|
||||
}
|
||||
|
||||
function createResponse() {
|
||||
return {};
|
||||
}
|
||||
|
||||
beforeEach(function(){
|
||||
resource = new Resource({path: '/foo'});
|
||||
ctx = new HttpContext(resource, createRequest(), createResponse());
|
||||
});
|
||||
|
||||
describe('.emit(ev, arg, done)', function(){
|
||||
it('should emit events on a resource', function(done) {
|
||||
var emitted, data;
|
||||
|
||||
resource.once('foo', function (arg, ctx, fn) {
|
||||
emitted = true;
|
||||
data = arg;
|
||||
fn();
|
||||
});
|
||||
|
||||
ctx.emit('foo', {bar: true}, function () {
|
||||
assert(emitted, 'event should be emitted');
|
||||
assert(data, 'arg should be supplied');
|
||||
assert(data.bar, 'arg should be the correct object');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle multiple args', function(done) {
|
||||
var emitted, data;
|
||||
|
||||
resource.once('foo', function (arg1, arg2, arg3, arg4, ctx, fn) {
|
||||
emitted = true;
|
||||
assert(arg1 === 1, 'arg1 should equal 1');
|
||||
assert(arg2 === 2, 'arg2 should equal 2');
|
||||
assert(arg3 === 3, 'arg3 should equal 3');
|
||||
assert(arg4 === 4, 'arg4 should equal 4');
|
||||
fn();
|
||||
});
|
||||
|
||||
ctx.emit('foo', 1, 2, 3, 4, function (fn) {
|
||||
assert(emitted, 'event should be emitted');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should have an after event', function(done) {
|
||||
var emitted, emittedAfter;
|
||||
|
||||
ctx.done = done;
|
||||
|
||||
resource.once('foo', function (arg1, arg2, arg3, arg4, ctx, fn) {
|
||||
emitted = true;
|
||||
fn();
|
||||
});
|
||||
|
||||
resource.once('after:foo', function (arg1, arg2, arg3, arg4, ctx, fn) {
|
||||
emittedAfter = true;
|
||||
fn();
|
||||
});
|
||||
|
||||
ctx.emit('foo', 1, 2, 3, 4, function (fn) {
|
||||
assert(emitted, 'event should be emitted');
|
||||
fn();
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to emit synchronously', function(done) {
|
||||
var emitted, data;
|
||||
|
||||
resource.once('foo', function (arg1, arg2, arg3, arg4, ctx) {
|
||||
emitted = true;
|
||||
assert(arg1 === 1, 'arg1 should equal 1');
|
||||
assert(arg2 === 2, 'arg2 should equal 2');
|
||||
assert(arg3 === 3, 'arg3 should equal 3');
|
||||
assert(arg4 === 4, 'arg4 should equal 4');
|
||||
});
|
||||
|
||||
ctx.emit('foo', 1, 2, 3, 4, function () {
|
||||
assert(emitted);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,5 +0,0 @@
|
|||
/**
|
||||
* resource test setup and support.
|
||||
*/
|
||||
|
||||
assert = require('assert');
|
|
@ -1,6 +0,0 @@
|
|||
{
|
||||
"name": "ShellJS",
|
||||
"twitter": [
|
||||
"r2r"
|
||||
]
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
node_modules/
|
|
@ -1,5 +0,0 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- 0.6
|
||||
- 0.8
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
Copyright (c) 2012, Artur Adib <aadib@mozilla.com>
|
||||
All rights reserved.
|
||||
|
||||
You may use this project under the terms of the New BSD license as follows:
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of Artur Adib nor the
|
||||
names of the contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL ARTUR ADIB BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@ -1,513 +0,0 @@
|
|||
# ShellJS - Unix shell commands for Node.js [![Build Status](https://secure.travis-ci.org/arturadib/shelljs.png)](http://travis-ci.org/arturadib/shelljs)
|
||||
|
||||
ShellJS is a portable **(Windows/Linux/OS X)** implementation of Unix shell commands on top of the Node.js API. You can use it to eliminate your shell script's dependency on Unix while still keeping its familiar and powerful commands. You can also install it globally so you can run it from outside Node projects - say goodbye to those gnarly Bash scripts!
|
||||
|
||||
The project is [unit-tested](http://travis-ci.org/arturadib/shelljs) and battled-tested in projects like:
|
||||
|
||||
+ [PDF.js](http://github.com/mozilla/pdf.js) - Firefox's next-gen PDF reader
|
||||
+ [Firebug](http://getfirebug.com/) - Firefox's infamous debugger
|
||||
+ [JSHint](http://jshint.com) - Most popular JavaScript linter
|
||||
+ [Zepto](http://zeptojs.com) - jQuery-compatible JavaScript library for modern browsers
|
||||
+ [Yeoman](http://yeoman.io/) - Web application stack and development tool
|
||||
+ [Deployd.com](http://deployd.com) - Open source PaaS for quick API backend generation
|
||||
|
||||
and [many more](https://npmjs.org/browse/depended/shelljs).
|
||||
|
||||
## Installing
|
||||
|
||||
Via npm:
|
||||
|
||||
```bash
|
||||
$ npm install [-g] shelljs
|
||||
```
|
||||
|
||||
If the global option `-g` is specified, the binary `shjs` will be installed. This makes it possible to
|
||||
run ShellJS scripts much like any shell script from the command line, i.e. without requiring a `node_modules` folder:
|
||||
|
||||
```bash
|
||||
$ shjs my_script
|
||||
```
|
||||
|
||||
You can also just copy `shell.js` into your project's directory, and `require()` accordingly.
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
### JavaScript
|
||||
|
||||
```javascript
|
||||
require('shelljs/global');
|
||||
|
||||
if (!which('git')) {
|
||||
echo('Sorry, this script requires git');
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Copy files to release dir
|
||||
mkdir('-p', 'out/Release');
|
||||
cp('-R', 'stuff/*', 'out/Release');
|
||||
|
||||
// Replace macros in each .js file
|
||||
cd('lib');
|
||||
ls('*.js').forEach(function(file) {
|
||||
sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
|
||||
sed('-i', /.*REMOVE_THIS_LINE.*\n/, '', file);
|
||||
sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat('macro.js'), file);
|
||||
});
|
||||
cd('..');
|
||||
|
||||
// Run external tool synchronously
|
||||
if (exec('git commit -am "Auto-commit"').code !== 0) {
|
||||
echo('Error: Git commit failed');
|
||||
exit(1);
|
||||
}
|
||||
```
|
||||
|
||||
### CoffeeScript
|
||||
|
||||
```coffeescript
|
||||
require 'shelljs/global'
|
||||
|
||||
if not which 'git'
|
||||
echo 'Sorry, this script requires git'
|
||||
exit 1
|
||||
|
||||
# Copy files to release dir
|
||||
mkdir '-p', 'out/Release'
|
||||
cp '-R', 'stuff/*', 'out/Release'
|
||||
|
||||
# Replace macros in each .js file
|
||||
cd 'lib'
|
||||
for file in ls '*.js'
|
||||
sed '-i', 'BUILD_VERSION', 'v0.1.2', file
|
||||
sed '-i', /.*REMOVE_THIS_LINE.*\n/, '', file
|
||||
sed '-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat 'macro.js', file
|
||||
cd '..'
|
||||
|
||||
# Run external tool synchronously
|
||||
if (exec 'git commit -am "Auto-commit"').code != 0
|
||||
echo 'Error: Git commit failed'
|
||||
exit 1
|
||||
```
|
||||
|
||||
## Global vs. Local
|
||||
|
||||
The example above uses the convenience script `shelljs/global` to reduce verbosity. If polluting your global namespace is not desirable, simply require `shelljs`.
|
||||
|
||||
Example:
|
||||
|
||||
```javascript
|
||||
var shell = require('shelljs');
|
||||
shell.echo('hello world');
|
||||
```
|
||||
|
||||
## Make tool
|
||||
|
||||
A convenience script `shelljs/make` is also provided to mimic the behavior of a Unix Makefile. In this case all shell objects are global, and command line arguments will cause the script to execute only the corresponding function in the global `target` object. To avoid redundant calls, target functions are executed only once per script.
|
||||
|
||||
Example (CoffeeScript):
|
||||
|
||||
```coffeescript
|
||||
require 'shelljs/make'
|
||||
|
||||
target.all = ->
|
||||
target.bundle()
|
||||
target.docs()
|
||||
|
||||
target.bundle = ->
|
||||
cd __dirname
|
||||
mkdir 'build'
|
||||
cd 'lib'
|
||||
(cat '*.js').to '../build/output.js'
|
||||
|
||||
target.docs = ->
|
||||
cd __dirname
|
||||
mkdir 'docs'
|
||||
cd 'lib'
|
||||
for file in ls '*.js'
|
||||
text = grep '//@', file # extract special comments
|
||||
text.replace '//@', '' # remove comment tags
|
||||
text.to 'docs/my_docs.md'
|
||||
```
|
||||
|
||||
To run the target `all`, call the above script without arguments: `$ node make`. To run the target `docs`: `$ node make docs`, and so on.
|
||||
|
||||
|
||||
|
||||
<!--
|
||||
|
||||
DO NOT MODIFY BEYOND THIS POINT - IT'S AUTOMATICALLY GENERATED
|
||||
|
||||
-->
|
||||
|
||||
|
||||
## Command reference
|
||||
|
||||
|
||||
All commands run synchronously, unless otherwise stated.
|
||||
|
||||
|
||||
### cd('dir')
|
||||
Changes to directory `dir` for the duration of the script
|
||||
|
||||
### pwd()
|
||||
Returns the current directory.
|
||||
|
||||
### ls([options ,] path [,path ...])
|
||||
### ls([options ,] path_array)
|
||||
Available options:
|
||||
|
||||
+ `-R`: recursive
|
||||
+ `-A`: all files (include files beginning with `.`, except for `.` and `..`)
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
ls('projs/*.js');
|
||||
ls('-R', '/users/me', '/tmp');
|
||||
ls('-R', ['/users/me', '/tmp']); // same as above
|
||||
```
|
||||
|
||||
Returns array of files in the given path, or in current directory if no path provided.
|
||||
|
||||
### find(path [,path ...])
|
||||
### find(path_array)
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
find('src', 'lib');
|
||||
find(['src', 'lib']); // same as above
|
||||
find('.').filter(function(file) { return file.match(/\.js$/); });
|
||||
```
|
||||
|
||||
Returns array of all files (however deep) in the given paths.
|
||||
|
||||
The main difference from `ls('-R', path)` is that the resulting file names
|
||||
include the base directories, e.g. `lib/resources/file1` instead of just `file1`.
|
||||
|
||||
### cp([options ,] source [,source ...], dest)
|
||||
### cp([options ,] source_array, dest)
|
||||
Available options:
|
||||
|
||||
+ `-f`: force
|
||||
+ `-r, -R`: recursive
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
cp('file1', 'dir1');
|
||||
cp('-Rf', '/tmp/*', '/usr/local/*', '/home/tmp');
|
||||
cp('-Rf', ['/tmp/*', '/usr/local/*'], '/home/tmp'); // same as above
|
||||
```
|
||||
|
||||
Copies files. The wildcard `*` is accepted.
|
||||
|
||||
### rm([options ,] file [, file ...])
|
||||
### rm([options ,] file_array)
|
||||
Available options:
|
||||
|
||||
+ `-f`: force
|
||||
+ `-r, -R`: recursive
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
rm('-rf', '/tmp/*');
|
||||
rm('some_file.txt', 'another_file.txt');
|
||||
rm(['some_file.txt', 'another_file.txt']); // same as above
|
||||
```
|
||||
|
||||
Removes files. The wildcard `*` is accepted.
|
||||
|
||||
### mv(source [, source ...], dest')
|
||||
### mv(source_array, dest')
|
||||
Available options:
|
||||
|
||||
+ `f`: force
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
mv('-f', 'file', 'dir/');
|
||||
mv('file1', 'file2', 'dir/');
|
||||
mv(['file1', 'file2'], 'dir/'); // same as above
|
||||
```
|
||||
|
||||
Moves files. The wildcard `*` is accepted.
|
||||
|
||||
### mkdir([options ,] dir [, dir ...])
|
||||
### mkdir([options ,] dir_array)
|
||||
Available options:
|
||||
|
||||
+ `p`: full path (will create intermediate dirs if necessary)
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
mkdir('-p', '/tmp/a/b/c/d', '/tmp/e/f/g');
|
||||
mkdir('-p', ['/tmp/a/b/c/d', '/tmp/e/f/g']); // same as above
|
||||
```
|
||||
|
||||
Creates directories.
|
||||
|
||||
### test(expression)
|
||||
Available expression primaries:
|
||||
|
||||
+ `'-b', 'path'`: true if path is a block device
|
||||
+ `'-c', 'path'`: true if path is a character device
|
||||
+ `'-d', 'path'`: true if path is a directory
|
||||
+ `'-e', 'path'`: true if path exists
|
||||
+ `'-f', 'path'`: true if path is a regular file
|
||||
+ `'-L', 'path'`: true if path is a symboilc link
|
||||
+ `'-p', 'path'`: true if path is a pipe (FIFO)
|
||||
+ `'-S', 'path'`: true if path is a socket
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
if (test('-d', path)) { /* do something with dir */ };
|
||||
if (!test('-f', path)) continue; // skip if it's a regular file
|
||||
```
|
||||
|
||||
Evaluates expression using the available primaries and returns corresponding value.
|
||||
|
||||
### cat(file [, file ...])
|
||||
### cat(file_array)
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
var str = cat('file*.txt');
|
||||
var str = cat('file1', 'file2');
|
||||
var str = cat(['file1', 'file2']); // same as above
|
||||
```
|
||||
|
||||
Returns a string containing the given file, or a concatenated string
|
||||
containing the files if more than one file is given (a new line character is
|
||||
introduced between each file). Wildcard `*` accepted.
|
||||
|
||||
### 'string'.to(file)
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
cat('input.txt').to('output.txt');
|
||||
```
|
||||
|
||||
Analogous to the redirection operator `>` in Unix, but works with JavaScript strings (such as
|
||||
those returned by `cat`, `grep`, etc). _Like Unix redirections, `to()` will overwrite any existing file!_
|
||||
|
||||
### sed([options ,] search_regex, replace_str, file)
|
||||
Available options:
|
||||
|
||||
+ `-i`: Replace contents of 'file' in-place. _Note that no backups will be created!_
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
sed('-i', 'PROGRAM_VERSION', 'v0.1.3', 'source.js');
|
||||
sed(/.*DELETE_THIS_LINE.*\n/, '', 'source.js');
|
||||
```
|
||||
|
||||
Reads an input string from `file` and performs a JavaScript `replace()` on the input
|
||||
using the given search regex and replacement string. Returns the new string after replacement.
|
||||
|
||||
### grep([options ,] regex_filter, file [, file ...])
|
||||
### grep([options ,] regex_filter, file_array)
|
||||
Available options:
|
||||
|
||||
+ `-v`: Inverse the sense of the regex and print the lines not matching the criteria.
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
grep('-v', 'GLOBAL_VARIABLE', '*.js');
|
||||
grep('GLOBAL_VARIABLE', '*.js');
|
||||
```
|
||||
|
||||
Reads input string from given files and returns a string containing all lines of the
|
||||
file that match the given `regex_filter`. Wildcard `*` accepted.
|
||||
|
||||
### which(command)
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
var nodeExec = which('node');
|
||||
```
|
||||
|
||||
Searches for `command` in the system's PATH. On Windows looks for `.exe`, `.cmd`, and `.bat` extensions.
|
||||
Returns string containing the absolute path to the command.
|
||||
|
||||
### echo(string [,string ...])
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
echo('hello world');
|
||||
var str = echo('hello world');
|
||||
```
|
||||
|
||||
Prints string to stdout, and returns string with additional utility methods
|
||||
like `.to()`.
|
||||
|
||||
### dirs([options | '+N' | '-N'])
|
||||
|
||||
Available options:
|
||||
|
||||
+ `-c`: Clears the directory stack by deleting all of the elements.
|
||||
|
||||
Arguments:
|
||||
|
||||
+ `+N`: Displays the Nth directory (counting from the left of the list printed by dirs when invoked without options), starting with zero.
|
||||
+ `-N`: Displays the Nth directory (counting from the right of the list printed by dirs when invoked without options), starting with zero.
|
||||
|
||||
Display the list of currently remembered directories. Returns an array of paths in the stack, or a single path if +N or -N was specified.
|
||||
|
||||
See also: pushd, popd
|
||||
|
||||
### pushd([options,] [dir | '-N' | '+N'])
|
||||
|
||||
Available options:
|
||||
|
||||
+ `-n`: Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated.
|
||||
|
||||
Arguments:
|
||||
|
||||
+ `dir`: Makes the current working directory be the top of the stack, and then executes the equivalent of `cd dir`.
|
||||
+ `+N`: Brings the Nth directory (counting from the left of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
|
||||
+ `-N`: Brings the Nth directory (counting from the right of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
// process.cwd() === '/usr'
|
||||
pushd('/etc'); // Returns /etc /usr
|
||||
pushd('+1'); // Returns /usr /etc
|
||||
```
|
||||
|
||||
Save the current directory on the top of the directory stack and then cd to `dir`. With no arguments, pushd exchanges the top two directories. Returns an array of paths in the stack.
|
||||
|
||||
### popd([options,] ['-N' | '+N'])
|
||||
|
||||
Available options:
|
||||
|
||||
+ `-n`: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.
|
||||
|
||||
Arguments:
|
||||
|
||||
+ `+N`: Removes the Nth directory (counting from the left of the list printed by dirs), starting with zero.
|
||||
+ `-N`: Removes the Nth directory (counting from the right of the list printed by dirs), starting with zero.
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
echo(process.cwd()); // '/usr'
|
||||
pushd('/etc'); // '/etc /usr'
|
||||
echo(process.cwd()); // '/etc'
|
||||
popd(); // '/usr'
|
||||
echo(process.cwd()); // '/usr'
|
||||
```
|
||||
|
||||
When no arguments are given, popd removes the top directory from the stack and performs a cd to the new top directory. The elements are numbered from 0 starting at the first directory listed with dirs; i.e., popd is equivalent to popd +0. Returns an array of paths in the stack.
|
||||
|
||||
### exit(code)
|
||||
Exits the current process with the given exit code.
|
||||
|
||||
### env['VAR_NAME']
|
||||
Object containing environment variables (both getter and setter). Shortcut to process.env.
|
||||
|
||||
### exec(command [, options] [, callback])
|
||||
Available options (all `false` by default):
|
||||
|
||||
+ `async`: Asynchronous execution. Defaults to true if a callback is provided.
|
||||
+ `silent`: Do not echo program output to console.
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
var version = exec('node --version', {silent:true}).output;
|
||||
|
||||
var child = exec('some_long_running_process', {async:true});
|
||||
child.stdout.on('data', function(data) {
|
||||
/* ... do something with data ... */
|
||||
});
|
||||
|
||||
exec('some_long_running_process', function(code, output) {
|
||||
console.log('Exit code:', code);
|
||||
console.log('Program output:', output);
|
||||
});
|
||||
```
|
||||
|
||||
Executes the given `command` _synchronously_, unless otherwise specified.
|
||||
When in synchronous mode returns the object `{ code:..., output:... }`, containing the program's
|
||||
`output` (stdout + stderr) and its exit `code`. Otherwise returns the child process object, and
|
||||
the `callback` gets the arguments `(code, output)`.
|
||||
|
||||
**Note:** For long-lived processes, it's best to run `exec()` asynchronously as
|
||||
the current synchronous implementation uses a lot of CPU. This should be getting
|
||||
fixed soon.
|
||||
|
||||
### chmod(octal_mode || octal_string, file)
|
||||
### chmod(symbolic_mode, file)
|
||||
|
||||
Available options:
|
||||
|
||||
+ `-v`: output a diagnostic for every file processed
|
||||
+ `-c`: like verbose but report only when a change is made
|
||||
+ `-R`: change files and directories recursively
|
||||
|
||||
Examples:
|
||||
|
||||
```javascript
|
||||
chmod(755, '/Users/brandon');
|
||||
chmod('755', '/Users/brandon'); // same as above
|
||||
chmod('u+x', '/Users/brandon');
|
||||
```
|
||||
|
||||
Alters the permissions of a file or directory by either specifying the
|
||||
absolute permissions in octal form or expressing the changes in symbols.
|
||||
This command tries to mimic the POSIX behavior as much as possible.
|
||||
Notable exceptions:
|
||||
|
||||
+ In symbolic modes, 'a-r' and '-r' are identical. No consideration is
|
||||
given to the umask.
|
||||
+ There is no "quiet" option since default behavior is to run silent.
|
||||
|
||||
## Configuration
|
||||
|
||||
|
||||
### config.silent
|
||||
Example:
|
||||
|
||||
```javascript
|
||||
var silentState = config.silent; // save old silent state
|
||||
config.silent = true;
|
||||
/* ... */
|
||||
config.silent = silentState; // restore old silent state
|
||||
```
|
||||
|
||||
Suppresses all command output if `true`, except for `echo()` calls.
|
||||
Default is `false`.
|
||||
|
||||
### config.fatal
|
||||
Example:
|
||||
|
||||
```javascript
|
||||
config.fatal = true;
|
||||
cp('this_file_does_not_exist', '/dev/null'); // dies here
|
||||
/* more commands... */
|
||||
```
|
||||
|
||||
If `true` the script will die on errors. Default is `false`.
|
||||
|
||||
## Non-Unix commands
|
||||
|
||||
|
||||
### tempdir()
|
||||
Searches and returns string containing a writeable, platform-dependent temporary directory.
|
||||
Follows Python's [tempfile algorithm](http://docs.python.org/library/tempfile.html#tempfile.tempdir).
|
||||
|
||||
### error()
|
||||
Tests if error occurred in the last command. Returns `null` if no error occurred,
|
||||
otherwise returns string explaining the error
|
|
@ -1,51 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
require('../global');
|
||||
|
||||
if (process.argv.length < 3) {
|
||||
console.log('ShellJS: missing argument (script name)');
|
||||
console.log();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
var args,
|
||||
scriptName = process.argv[2];
|
||||
env['NODE_PATH'] = __dirname + '/../..';
|
||||
|
||||
if (!scriptName.match(/\.js/) && !scriptName.match(/\.coffee/)) {
|
||||
if (test('-f', scriptName + '.js'))
|
||||
scriptName += '.js';
|
||||
if (test('-f', scriptName + '.coffee'))
|
||||
scriptName += '.coffee';
|
||||
}
|
||||
|
||||
if (!test('-f', scriptName)) {
|
||||
console.log('ShellJS: script not found ('+scriptName+')');
|
||||
console.log();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
args = process.argv.slice(3);
|
||||
|
||||
for (var i = 0, l = args.length; i < l; i++) {
|
||||
if (args[i][0] !== "-"){
|
||||
args[i] = '"' + args[i] + '"'; // fixes arguments with multiple words
|
||||
}
|
||||
}
|
||||
|
||||
if (scriptName.match(/\.coffee$/)) {
|
||||
//
|
||||
// CoffeeScript
|
||||
//
|
||||
if (which('coffee')) {
|
||||
exec('coffee ' + scriptName + ' ' + args.join(' '), { async: true });
|
||||
} else {
|
||||
console.log('ShellJS: CoffeeScript interpreter not found');
|
||||
console.log();
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
//
|
||||
// JavaScript
|
||||
//
|
||||
exec('node ' + scriptName + ' ' + args.join(' '), { async: true });
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
var shell = require('./shell.js');
|
||||
for (var cmd in shell)
|
||||
global[cmd] = shell[cmd];
|
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"loopfunc": true,
|
||||
"sub": true
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
require('./global');
|
||||
config.fatal = true;
|
||||
|
||||
global.target = {};
|
||||
|
||||
// This ensures we only execute the script targets after the entire script has
|
||||
// been evaluated
|
||||
var args = process.argv.slice(2);
|
||||
setTimeout(function() {
|
||||
var t;
|
||||
|
||||
if (args.length === 1 && args[0] === '--help') {
|
||||
console.log('Available targets:');
|
||||
for (t in target)
|
||||
console.log(' ' + t);
|
||||
return;
|
||||
}
|
||||
|
||||
// Wrap targets to prevent duplicate execution
|
||||
for (t in target) {
|
||||
(function(t, oldTarget){
|
||||
|
||||
// Wrap it
|
||||
target[t] = function(force) {
|
||||
if (oldTarget.done && !force)
|
||||
return;
|
||||
oldTarget.done = true;
|
||||
return oldTarget.apply(oldTarget, arguments);
|
||||
};
|
||||
|
||||
})(t, target[t]);
|
||||
}
|
||||
|
||||
// Execute desired targets
|
||||
if (args.length > 0) {
|
||||
args.forEach(function(arg) {
|
||||
if (arg in target)
|
||||
target[arg]();
|
||||
else {
|
||||
console.log('no such target: ' + arg);
|
||||
exit(1);
|
||||
}
|
||||
});
|
||||
} else if ('all' in target) {
|
||||
target.all();
|
||||
}
|
||||
|
||||
}, 0);
|
File diff suppressed because one or more lines are too long
|
@ -1,15 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
require('../global');
|
||||
|
||||
echo('Appending docs to README.md');
|
||||
|
||||
cd(__dirname + '/..');
|
||||
|
||||
// Extract docs from shell.js
|
||||
var docs = grep('//@', 'shell.js');
|
||||
// Remove '//@'
|
||||
docs = docs.replace(/\/\/\@ ?/g, '');
|
||||
// Append docs to README
|
||||
sed('-i', /## Command reference(.|\n)*/, '## Command reference\n\n' + docs, 'README.md');
|
||||
|
||||
echo('All done.');
|
|
@ -1,50 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
require('../global');
|
||||
|
||||
var path = require('path');
|
||||
|
||||
var failed = false;
|
||||
|
||||
//
|
||||
// Lint
|
||||
//
|
||||
JSHINT_BIN = './node_modules/jshint/bin/jshint';
|
||||
cd(__dirname + '/..');
|
||||
|
||||
if (!test('-f', JSHINT_BIN)) {
|
||||
echo('JSHint not found. Run `npm install` in the root dir first.');
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (exec(JSHINT_BIN + ' --config jshint.json *.js test/*.js').code !== 0) {
|
||||
failed = true;
|
||||
echo('*** JSHINT FAILED! (return code != 0)');
|
||||
echo();
|
||||
} else {
|
||||
echo('All JSHint tests passed');
|
||||
echo();
|
||||
}
|
||||
|
||||
//
|
||||
// Unit tests
|
||||
//
|
||||
cd(__dirname + '/../test');
|
||||
ls('*.js').forEach(function(file) {
|
||||
echo('Running test:', file);
|
||||
if (exec('node ' + file).code !== 123) { // 123 avoids false positives (e.g. premature exit)
|
||||
failed = true;
|
||||
echo('*** TEST FAILED! (missing exit code "123")');
|
||||
echo();
|
||||
}
|
||||
});
|
||||
|
||||
if (failed) {
|
||||
echo();
|
||||
echo('*******************************************************');
|
||||
echo('WARNING: Some tests did not pass!');
|
||||
echo('*******************************************************');
|
||||
exit(1);
|
||||
} else {
|
||||
echo();
|
||||
echo('All tests passed.');
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,2 +0,0 @@
|
|||
tmp/
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
// save current dir
|
||||
var cur = shell.pwd();
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
shell.cat();
|
||||
assert.ok(shell.error());
|
||||
|
||||
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
|
||||
shell.cat('/adsfasdf'); // file does not exist
|
||||
assert.ok(shell.error());
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
// simple
|
||||
var result = shell.cat('resources/file1');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'test1');
|
||||
|
||||
// multiple files
|
||||
var result = shell.cat('resources/file2', 'resources/file1');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'test2\ntest1');
|
||||
|
||||
// multiple files, array syntax
|
||||
var result = shell.cat(['resources/file2', 'resources/file1']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'test2\ntest1');
|
||||
|
||||
var result = shell.cat('resources/file*.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.ok(result.search('test1') > -1); // file order might be random
|
||||
assert.ok(result.search('test2') > -1);
|
||||
|
||||
shell.exit(123);
|
|
@ -1,64 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
// save current dir
|
||||
var cur = shell.pwd();
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
shell.cd();
|
||||
assert.ok(shell.error());
|
||||
|
||||
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
|
||||
shell.cd('/adsfasdf'); // dir does not exist
|
||||
assert.ok(shell.error());
|
||||
|
||||
assert.equal(fs.existsSync('resources/file1'), true); // sanity check
|
||||
shell.cd('resources/file1'); // file, not dir
|
||||
assert.ok(shell.error());
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
shell.cd(cur);
|
||||
shell.cd('tmp');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(path.basename(process.cwd()), 'tmp');
|
||||
|
||||
shell.cd(cur);
|
||||
shell.cd('/');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), path.resolve('/'));
|
||||
|
||||
// cd + other commands
|
||||
|
||||
shell.cd(cur);
|
||||
shell.rm('-f', 'tmp/*');
|
||||
assert.equal(fs.existsSync('tmp/file1'), false);
|
||||
shell.cd('resources');
|
||||
assert.equal(shell.error(), null);
|
||||
shell.cp('file1', '../tmp');
|
||||
assert.equal(shell.error(), null);
|
||||
shell.cd('../tmp');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('file1'), true);
|
||||
|
||||
shell.exit(123);
|
|
@ -1,81 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
shell.chmod('blah'); // missing args
|
||||
assert.ok(shell.error());
|
||||
shell.chmod('893', 'resources/chmod'); // invalid permissions - mode must be in octal
|
||||
assert.ok(shell.error());
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
// Test files - the bitmasking is to ignore the upper bits.
|
||||
shell.chmod('755', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('777', 8), parseInt('755', 8));
|
||||
shell.chmod('644', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('777', 8), parseInt('644', 8));
|
||||
|
||||
shell.chmod('o+x', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('007', 8), parseInt('005', 8));
|
||||
shell.chmod('644', 'resources/chmod/file1');
|
||||
|
||||
shell.chmod('+x', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('777', 8), parseInt('755', 8));
|
||||
shell.chmod('644', 'resources/chmod/file1');
|
||||
|
||||
// Test setuid
|
||||
shell.chmod('u+s', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('4000', 8), parseInt('4000', 8));
|
||||
shell.chmod('u-s', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('777', 8), parseInt('644', 8));
|
||||
|
||||
// according to POSIX standards at http://linux.die.net/man/1/chmod,
|
||||
// setuid is never cleared from a directory unless explicitly asked for.
|
||||
shell.chmod('u+s', 'resources/chmod/c');
|
||||
shell.chmod('755', 'resources/chmod/c');
|
||||
assert.equal(fs.statSync('resources/chmod/c').mode & parseInt('4000', 8), parseInt('4000', 8));
|
||||
shell.chmod('u-s', 'resources/chmod/c');
|
||||
|
||||
// Test setgid
|
||||
shell.chmod('g+s', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('2000', 8), parseInt('2000', 8));
|
||||
shell.chmod('g-s', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('777', 8), parseInt('644', 8));
|
||||
|
||||
// Test sticky bit
|
||||
shell.chmod('+t', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('1000', 8), parseInt('1000', 8));
|
||||
shell.chmod('-t', 'resources/chmod/file1');
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('777', 8), parseInt('644', 8));
|
||||
assert.equal(fs.statSync('resources/chmod/file1').mode & parseInt('1000', 8), 0);
|
||||
|
||||
// Test directories
|
||||
shell.chmod('a-w', 'resources/chmod/b/a/b');
|
||||
assert.equal(fs.statSync('resources/chmod/b/a/b').mode & parseInt('777', 8), parseInt('555', 8));
|
||||
shell.chmod('755', 'resources/chmod/b/a/b');
|
||||
|
||||
// Test recursion
|
||||
shell.chmod('-R', 'a+w', 'resources/chmod/b');
|
||||
assert.equal(fs.statSync('resources/chmod/b/a/b').mode & parseInt('777', 8), parseInt('777', 8));
|
||||
shell.chmod('-R', '755', 'resources/chmod/b');
|
||||
assert.equal(fs.statSync('resources/chmod/b/a/b').mode & parseInt('777', 8), parseInt('755', 8));
|
||||
|
||||
// Test symbolic links w/ recursion - WARNING: *nix only
|
||||
fs.symlinkSync('resources/chmod/b/a', 'resources/chmod/a/b/c/link', 'dir');
|
||||
shell.chmod('-R', 'u-w', 'resources/chmod/a/b');
|
||||
assert.equal(fs.statSync('resources/chmod/a/b/c').mode & parseInt('700', 8), parseInt('500', 8));
|
||||
assert.equal(fs.statSync('resources/chmod/b/a').mode & parseInt('700', 8), parseInt('700', 8));
|
||||
shell.chmod('-R', 'u+w', 'resources/chmod/a/b');
|
||||
fs.unlinkSync('resources/chmod/a/b/c/link');
|
||||
|
||||
shell.exit(123);
|
|
@ -1,50 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
child = require('child_process');
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
//
|
||||
// config.silent
|
||||
//
|
||||
|
||||
assert.equal(shell.config.silent, false); // default
|
||||
|
||||
shell.config.silent = true;
|
||||
assert.equal(shell.config.silent, true);
|
||||
|
||||
shell.config.silent = false;
|
||||
assert.equal(shell.config.silent, false);
|
||||
|
||||
//
|
||||
// config.fatal
|
||||
//
|
||||
|
||||
assert.equal(shell.config.fatal, false); // default
|
||||
|
||||
//
|
||||
// config.fatal = false
|
||||
//
|
||||
shell.mkdir('-p', 'tmp');
|
||||
var file = 'tmp/tempscript'+Math.random()+'.js',
|
||||
script = 'require(\'../../global.js\'); config.silent=true; config.fatal=false; cp("this_file_doesnt_exist", "."); echo("got here");';
|
||||
script.to(file);
|
||||
child.exec('node '+file, function(err, stdout, stderr) {
|
||||
assert.ok(stdout.match('got here'));
|
||||
|
||||
//
|
||||
// config.fatal = true
|
||||
//
|
||||
shell.mkdir('-p', 'tmp');
|
||||
var file = 'tmp/tempscript'+Math.random()+'.js',
|
||||
script = 'require(\'../../global.js\'); config.silent=true; config.fatal=true; cp("this_file_doesnt_exist", "."); echo("got here");';
|
||||
script.to(file);
|
||||
child.exec('node '+file, function(err, stdout, stderr) {
|
||||
assert.ok(!stdout.match('got here'));
|
||||
|
||||
shell.exit(123);
|
||||
});
|
||||
});
|
|
@ -1,143 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
shell.cp();
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.cp('file1');
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.cp('-f');
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.rm('-rf', 'tmp/*');
|
||||
shell.cp('-@', 'resources/file1', 'tmp/file1'); // option not supported, files OK
|
||||
assert.ok(shell.error());
|
||||
assert.equal(fs.existsSync('tmp/file1'), false);
|
||||
|
||||
shell.cp('-Z', 'asdfasdf', 'tmp/file2'); // option not supported, files NOT OK
|
||||
assert.ok(shell.error());
|
||||
assert.equal(fs.existsSync('tmp/file2'), false);
|
||||
|
||||
shell.cp('asdfasdf', 'tmp'); // source does not exist
|
||||
assert.ok(shell.error());
|
||||
assert.equal(numLines(shell.error()), 1);
|
||||
assert.equal(fs.existsSync('tmp/asdfasdf'), false);
|
||||
|
||||
shell.cp('asdfasdf1', 'asdfasdf2', 'tmp'); // sources do not exist
|
||||
assert.ok(shell.error());
|
||||
assert.equal(numLines(shell.error()), 2);
|
||||
assert.equal(fs.existsSync('tmp/asdfasdf1'), false);
|
||||
assert.equal(fs.existsSync('tmp/asdfasdf2'), false);
|
||||
|
||||
shell.cp('asdfasdf1', 'asdfasdf2', 'resources/file1'); // too many sources (dest is file)
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.cp('resources/file1', 'resources/file2'); // dest already exists
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.cp('resources/file1', 'resources/file2', 'tmp/a_file'); // too many sources
|
||||
assert.ok(shell.error());
|
||||
assert.equal(fs.existsSync('tmp/a_file'), false);
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
// simple - to dir
|
||||
shell.cp('resources/file1', 'tmp');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/file1'), true);
|
||||
|
||||
// simple - to file
|
||||
shell.cp('resources/file2', 'tmp/file2');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/file2'), true);
|
||||
|
||||
// simple - file list
|
||||
shell.rm('-rf', 'tmp/*');
|
||||
shell.cp('resources/file1', 'resources/file2', 'tmp');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/file1'), true);
|
||||
assert.equal(fs.existsSync('tmp/file2'), true);
|
||||
|
||||
// simple - file list, array syntax
|
||||
shell.rm('-rf', 'tmp/*');
|
||||
shell.cp(['resources/file1', 'resources/file2'], 'tmp');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/file1'), true);
|
||||
assert.equal(fs.existsSync('tmp/file2'), true);
|
||||
|
||||
shell.cp('resources/file2', 'tmp/file3');
|
||||
assert.equal(fs.existsSync('tmp/file3'), true);
|
||||
shell.cp('-f', 'resources/file2', 'tmp/file3'); // file exists, but -f specified
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/file3'), true);
|
||||
|
||||
// wildcard
|
||||
shell.rm('tmp/file1', 'tmp/file2');
|
||||
shell.cp('resources/file*', 'tmp');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/file1'), true);
|
||||
assert.equal(fs.existsSync('tmp/file2'), true);
|
||||
|
||||
//recursive, nothing exists
|
||||
shell.rm('-rf', 'tmp/*');
|
||||
shell.cp('-R', 'resources/cp', 'tmp');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(shell.ls('-R', 'resources/cp') + '', shell.ls('-R', 'tmp/cp') + '');
|
||||
|
||||
//recursive, nothing exists, source ends in '/' (see Github issue #15)
|
||||
shell.rm('-rf', 'tmp/*');
|
||||
shell.cp('-R', 'resources/cp/', 'tmp/');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(shell.ls('-R', 'resources/cp') + '', shell.ls('-R', 'tmp') + '');
|
||||
|
||||
//recursive, everything exists, no force flag
|
||||
shell.rm('-rf', 'tmp/*');
|
||||
shell.cp('-R', 'resources/cp', 'tmp');
|
||||
shell.cp('-R', 'resources/cp', 'tmp');
|
||||
assert.equal(shell.error(), null); // crash test only
|
||||
|
||||
//recursive, everything exists, with force flag
|
||||
shell.rm('-rf', 'tmp/*');
|
||||
shell.cp('-R', 'resources/cp', 'tmp');
|
||||
'changing things around'.to('tmp/cp/dir_a/z');
|
||||
assert.notEqual(shell.cat('resources/cp/dir_a/z'), shell.cat('tmp/cp/dir_a/z')); // before cp
|
||||
shell.cp('-Rf', 'resources/cp', 'tmp');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(shell.cat('resources/cp/dir_a/z'), shell.cat('tmp/cp/dir_a/z')); // after cp
|
||||
|
||||
//recursive, creates dest dir since it's only one level deep (see Github issue #44)
|
||||
shell.rm('-rf', 'tmp/*');
|
||||
shell.cp('-r', 'resources/issue44/*', 'tmp/dir2');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(shell.ls('-R', 'resources/issue44') + '', shell.ls('-R', 'tmp/dir2') + '');
|
||||
assert.equal(shell.cat('resources/issue44/main.js'), shell.cat('tmp/dir2/main.js'));
|
||||
|
||||
//recursive, does *not* create dest dir since it's too deep (see Github issue #44)
|
||||
shell.rm('-rf', 'tmp/*');
|
||||
shell.cp('-r', 'resources/issue44/*', 'tmp/dir2/dir3');
|
||||
assert.ok(shell.error());
|
||||
assert.equal(fs.existsSync('tmp/dir2'), false);
|
||||
|
||||
shell.exit(123);
|
|
@ -1,37 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
var root = path.resolve();
|
||||
|
||||
shell.pushd('resources/pushd');
|
||||
shell.pushd('a');
|
||||
|
||||
var trail = [
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
];
|
||||
|
||||
assert.deepEqual(shell.dirs(), trail);
|
||||
|
||||
// Single items
|
||||
assert.equal(shell.dirs('+0'), trail[0]);
|
||||
assert.equal(shell.dirs('+1'), trail[1]);
|
||||
assert.equal(shell.dirs('+2'), trail[2]);
|
||||
assert.equal(shell.dirs('-0'), trail[2]);
|
||||
assert.equal(shell.dirs('-1'), trail[1]);
|
||||
assert.equal(shell.dirs('-2'), trail[0]);
|
||||
|
||||
// Clearing items
|
||||
assert.deepEqual(shell.dirs('-c'), []);
|
||||
assert(!shell.error());
|
||||
|
||||
shell.exit(123);
|
|
@ -1,50 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs'),
|
||||
child = require('child_process');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
|
||||
// From here on we use child.exec() to intercept the stdout
|
||||
|
||||
|
||||
// simple test with defaults
|
||||
shell.mkdir('-p', 'tmp');
|
||||
var file = 'tmp/tempscript'+Math.random()+'.js',
|
||||
script = 'require(\'../../global.js\'); echo("-asdf", "111");'; // test '-' bug (see issue #20)
|
||||
script.to(file);
|
||||
child.exec('node '+file, function(err, stdout, stderr) {
|
||||
assert.ok(stdout === '-asdf 111\n' || stdout === '-asdf 111\nundefined\n'); // 'undefined' for v0.4
|
||||
|
||||
// simple test with silent(true)
|
||||
shell.mkdir('-p', 'tmp');
|
||||
var file = 'tmp/tempscript'+Math.random()+'.js',
|
||||
script = 'require(\'../../global.js\'); config.silent=true; echo(555);';
|
||||
script.to(file);
|
||||
child.exec('node '+file, function(err, stdout, stderr) {
|
||||
assert.ok(stdout === '555\n' || stdout === '555\nundefined\n'); // 'undefined' for v0.4
|
||||
|
||||
theEnd();
|
||||
});
|
||||
});
|
||||
|
||||
function theEnd() {
|
||||
shell.exit(123);
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert');
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
assert.equal(shell.env['PATH'], process.env['PATH']);
|
||||
|
||||
shell.env['SHELLJS_TEST'] = 'hello world';
|
||||
assert.equal(shell.env['SHELLJS_TEST'], process.env['SHELLJS_TEST']);
|
||||
|
||||
shell.exit(123);
|
|
@ -1,109 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs'),
|
||||
util = require('util'),
|
||||
child = require('child_process');
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
shell.exec();
|
||||
assert.ok(shell.error());
|
||||
|
||||
var result = shell.exec('asdfasdf'); // could not find command
|
||||
assert.ok(result.code > 0);
|
||||
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
//
|
||||
// sync
|
||||
//
|
||||
|
||||
// check if stdout goes to output
|
||||
var result = shell.exec('node -e \"console.log(1234);\"');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.code, 0);
|
||||
assert.ok(result.output === '1234\n' || result.output === '1234\nundefined\n'); // 'undefined' for v0.4
|
||||
|
||||
// check if stderr goes to output
|
||||
var result = shell.exec('node -e \"console.error(1234);\"');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.code, 0);
|
||||
assert.ok(result.output === '1234\n' || result.output === '1234\nundefined\n'); // 'undefined' for v0.4
|
||||
|
||||
// check if stdout + stderr go to output
|
||||
var result = shell.exec('node -e \"console.error(1234); console.log(666);\"');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.code, 0);
|
||||
assert.ok(result.output === '1234\n666\n' || result.output === '1234\n666\nundefined\n'); // 'undefined' for v0.4
|
||||
|
||||
// check exit code
|
||||
var result = shell.exec('node -e \"process.exit(12);\"');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.code, 12);
|
||||
|
||||
// interaction with cd
|
||||
shell.cd('resources/external');
|
||||
var result = shell.exec('node node_script.js');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.code, 0);
|
||||
assert.equal(result.output, 'node_script_1234\n');
|
||||
shell.cd('../..');
|
||||
|
||||
// check quotes escaping
|
||||
var result = shell.exec( util.format('node -e "console.log(%s);"', "\\\"\\'+\\'_\\'+\\'\\\"") );
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.code, 0);
|
||||
assert.equal(result.output, "'+'_'+'\n");
|
||||
|
||||
//
|
||||
// async
|
||||
//
|
||||
|
||||
// no callback
|
||||
var c = shell.exec('node -e \"console.log(1234)\"', {async:true});
|
||||
assert.equal(shell.error(), null);
|
||||
assert.ok('stdout' in c, 'async exec returns child process object');
|
||||
|
||||
//
|
||||
// callback as 2nd argument
|
||||
//
|
||||
shell.exec('node -e \"console.log(5678);\"', function(code, output) {
|
||||
assert.equal(code, 0);
|
||||
assert.ok(output === '5678\n' || output === '5678\nundefined\n'); // 'undefined' for v0.4
|
||||
|
||||
//
|
||||
// callback as 3rd argument
|
||||
//
|
||||
shell.exec('node -e \"console.log(5566);\"', {async:true}, function(code, output) {
|
||||
assert.equal(code, 0);
|
||||
assert.ok(output === '5566\n' || output === '5566\nundefined\n'); // 'undefined' for v0.4
|
||||
|
||||
//
|
||||
// callback as 3rd argument (slient:true)
|
||||
//
|
||||
shell.exec('node -e \"console.log(5678);\"', {silent:true}, function(code, output) {
|
||||
assert.equal(code, 0);
|
||||
assert.ok(output === '5678\n' || output === '5678\nundefined\n'); // 'undefined' for v0.4
|
||||
|
||||
shell.exit(123);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
assert.equal(shell.error(), null);
|
|
@ -1,56 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
var result = shell.find(); // no paths given
|
||||
assert.ok(shell.error());
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
// current path
|
||||
shell.cd('resources/find');
|
||||
var result = shell.find('.');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('.hidden') > -1, true);
|
||||
assert.equal(result.indexOf('dir1/dir11/a_dir11') > -1, true);
|
||||
assert.equal(result.length, 11);
|
||||
shell.cd('../..');
|
||||
|
||||
// simple path
|
||||
var result = shell.find('resources/find');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('resources/find/.hidden') > -1, true);
|
||||
assert.equal(result.indexOf('resources/find/dir1/dir11/a_dir11') > -1, true);
|
||||
assert.equal(result.length, 11);
|
||||
|
||||
// multiple paths - comma
|
||||
var result = shell.find('resources/find/dir1', 'resources/find/dir2');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('resources/find/dir1/dir11/a_dir11') > -1, true);
|
||||
assert.equal(result.indexOf('resources/find/dir2/a_dir1') > -1, true);
|
||||
assert.equal(result.length, 6);
|
||||
|
||||
// multiple paths - array
|
||||
var result = shell.find(['resources/find/dir1', 'resources/find/dir2']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('resources/find/dir1/dir11/a_dir11') > -1, true);
|
||||
assert.equal(result.indexOf('resources/find/dir2/a_dir1') > -1, true);
|
||||
assert.equal(result.length, 6);
|
||||
|
||||
shell.exit(123);
|
|
@ -1,59 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
shell.grep();
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.grep(/asdf/g); // too few args
|
||||
assert.ok(shell.error());
|
||||
|
||||
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
|
||||
shell.grep(/asdf/g, '/asdfasdf'); // no such file
|
||||
assert.ok(shell.error());
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
var result = shell.grep('line', 'resources/a.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.split('\n').length - 1, 4);
|
||||
|
||||
var result = shell.grep('-v', 'line', 'resources/a.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.split('\n').length - 1, 8);
|
||||
|
||||
var result = shell.grep('line one', 'resources/a.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'This is line one\n');
|
||||
|
||||
// multiple files
|
||||
var result = shell.grep(/test/, 'resources/file1.txt', 'resources/file2.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'test1\ntest2\n');
|
||||
|
||||
// multiple files, array syntax
|
||||
var result = shell.grep(/test/, ['resources/file1.txt', 'resources/file2.txt']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'test1\ntest2\n');
|
||||
|
||||
shell.exit(123);
|
|
@ -1,202 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
|
||||
var result = shell.ls('/asdfasdf'); // no such file or dir
|
||||
assert.ok(shell.error());
|
||||
assert.equal(result.length, 0);
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
var result = shell.ls();
|
||||
assert.equal(shell.error(), null);
|
||||
|
||||
var result = shell.ls('/');
|
||||
assert.equal(shell.error(), null);
|
||||
|
||||
// no args
|
||||
shell.cd('resources/ls');
|
||||
var result = shell.ls();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('file1') > -1, true);
|
||||
assert.equal(result.indexOf('file2') > -1, true);
|
||||
assert.equal(result.indexOf('file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('filename(with)[chars$]^that.must+be-escaped') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir') > -1, true);
|
||||
assert.equal(result.length, 6);
|
||||
shell.cd('../..');
|
||||
|
||||
// simple arg
|
||||
var result = shell.ls('resources/ls');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('file1') > -1, true);
|
||||
assert.equal(result.indexOf('file2') > -1, true);
|
||||
assert.equal(result.indexOf('file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('filename(with)[chars$]^that.must+be-escaped') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir') > -1, true);
|
||||
assert.equal(result.length, 6);
|
||||
|
||||
// no args, 'all' option
|
||||
shell.cd('resources/ls');
|
||||
var result = shell.ls('-A');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('file1') > -1, true);
|
||||
assert.equal(result.indexOf('file2') > -1, true);
|
||||
assert.equal(result.indexOf('file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('filename(with)[chars$]^that.must+be-escaped') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir') > -1, true);
|
||||
assert.equal(result.indexOf('.hidden_file') > -1, true);
|
||||
assert.equal(result.indexOf('.hidden_dir') > -1, true);
|
||||
assert.equal(result.length, 8);
|
||||
shell.cd('../..');
|
||||
|
||||
// no args, 'all' option
|
||||
shell.cd('resources/ls');
|
||||
var result = shell.ls('-a'); // (deprecated) backwards compatibility test
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('file1') > -1, true);
|
||||
assert.equal(result.indexOf('file2') > -1, true);
|
||||
assert.equal(result.indexOf('file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('filename(with)[chars$]^that.must+be-escaped') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir') > -1, true);
|
||||
assert.equal(result.indexOf('.hidden_file') > -1, true);
|
||||
assert.equal(result.indexOf('.hidden_dir') > -1, true);
|
||||
assert.equal(result.length, 8);
|
||||
shell.cd('../..');
|
||||
|
||||
// wildcard, simple
|
||||
var result = shell.ls('resources/ls/*');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('resources/ls/file1') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file2') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/filename(with)[chars$]^that.must+be-escaped') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/a_dir') > -1, true);
|
||||
assert.equal(result.length, 6);
|
||||
|
||||
// wildcard, hidden only
|
||||
var result = shell.ls('resources/ls/.*');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('resources/ls/.hidden_file') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/.hidden_dir') > -1, true);
|
||||
assert.equal(result.length, 2);
|
||||
|
||||
// wildcard, mid-file
|
||||
var result = shell.ls('resources/ls/f*le*');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.length, 5);
|
||||
assert.equal(result.indexOf('resources/ls/file1') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file2') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/filename(with)[chars$]^that.must+be-escaped') > -1, true);
|
||||
|
||||
// wildcard, mid-file with dot (should escape dot for regex)
|
||||
var result = shell.ls('resources/ls/f*le*.js');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.length, 2);
|
||||
assert.equal(result.indexOf('resources/ls/file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file2.js') > -1, true);
|
||||
|
||||
// wildcard, should not do partial matches
|
||||
var result = shell.ls('resources/ls/*.j'); // shouldn't get .js
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.length, 0);
|
||||
|
||||
// wildcard, all files with extension
|
||||
var result = shell.ls('resources/ls/*.*');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.length, 3);
|
||||
assert.equal(result.indexOf('resources/ls/file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/filename(with)[chars$]^that.must+be-escaped') > -1, true);
|
||||
|
||||
// wildcard, with additional path
|
||||
var result = shell.ls('resources/ls/f*le*.js', 'resources/ls/a_dir');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.length, 4);
|
||||
assert.equal(result.indexOf('resources/ls/file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('b_dir') > -1, true); // no wildcard == no path prefix
|
||||
assert.equal(result.indexOf('nada') > -1, true); // no wildcard == no path prefix
|
||||
|
||||
// wildcard for both paths
|
||||
var result = shell.ls('resources/ls/f*le*.js', 'resources/ls/a_dir/*');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.length, 4);
|
||||
assert.equal(result.indexOf('resources/ls/file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/a_dir/b_dir') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/a_dir/nada') > -1, true);
|
||||
|
||||
// wildcard for both paths, array
|
||||
var result = shell.ls(['resources/ls/f*le*.js', 'resources/ls/a_dir/*']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.length, 4);
|
||||
assert.equal(result.indexOf('resources/ls/file1.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/file2.js') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/a_dir/b_dir') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/a_dir/nada') > -1, true);
|
||||
|
||||
// recursive, no path
|
||||
shell.cd('resources/ls');
|
||||
var result = shell.ls('-R');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('a_dir') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir/b_dir') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir/b_dir/z') > -1, true);
|
||||
assert.equal(result.length, 9);
|
||||
shell.cd('../..');
|
||||
|
||||
// recusive, path given
|
||||
var result = shell.ls('-R', 'resources/ls');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('a_dir') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir/b_dir') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir/b_dir/z') > -1, true);
|
||||
assert.equal(result.length, 9);
|
||||
|
||||
// recusive, path given - 'all' flag
|
||||
var result = shell.ls('-RA', 'resources/ls');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('a_dir') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir/b_dir') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir/b_dir/z') > -1, true);
|
||||
assert.equal(result.indexOf('a_dir/.hidden_dir/nada') > -1, true);
|
||||
assert.equal(result.length, 14);
|
||||
|
||||
// recursive, wildcard
|
||||
var result = shell.ls('-R', 'resources/ls/*');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.indexOf('resources/ls/a_dir') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/a_dir/b_dir') > -1, true);
|
||||
assert.equal(result.indexOf('resources/ls/a_dir/b_dir/z') > -1, true);
|
||||
assert.equal(result.length, 9);
|
||||
|
||||
shell.exit(123);
|
|
@ -1,20 +0,0 @@
|
|||
var shell = require('..'),
|
||||
child = require('child_process'),
|
||||
assert = require('assert');
|
||||
|
||||
shell.mkdir('-p', 'tmp');
|
||||
var file = 'tmp/tempscript'+Math.random()+'.js',
|
||||
script = 'require(\'../../make.js\');' +
|
||||
'target.all=function(){' +
|
||||
' echo("first"); '+
|
||||
' cp("this_file_doesnt_exist", ".");' +
|
||||
' echo("second");' +
|
||||
'}';
|
||||
|
||||
script.to(file);
|
||||
child.exec('node '+file, function(err, stdout, stderr) {
|
||||
assert.ok(stdout.match('first'));
|
||||
assert.ok(!stdout.match('second')); // Make should die on errors, so this should never get echoed
|
||||
|
||||
shell.exit(123);
|
||||
});
|
|
@ -1,79 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
shell.mkdir();
|
||||
assert.ok(shell.error());
|
||||
|
||||
var mtime = fs.statSync('tmp').mtime.toString();
|
||||
shell.mkdir('tmp'); // dir already exists
|
||||
assert.ok(shell.error());
|
||||
assert.equal(fs.statSync('tmp').mtime.toString(), mtime); // didn't mess with dir
|
||||
|
||||
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
|
||||
shell.mkdir('/asdfasdf/asdfasdf'); // root path does not exist
|
||||
assert.ok(shell.error());
|
||||
assert.equal(fs.existsSync('/asdfasdf'), false);
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
assert.equal(fs.existsSync('tmp/t1'), false);
|
||||
shell.mkdir('tmp/t1'); // simple dir
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/t1'), true);
|
||||
|
||||
assert.equal(fs.existsSync('tmp/t2'), false);
|
||||
assert.equal(fs.existsSync('tmp/t3'), false);
|
||||
shell.mkdir('tmp/t2', 'tmp/t3'); // multiple dirs
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/t2'), true);
|
||||
assert.equal(fs.existsSync('tmp/t3'), true);
|
||||
|
||||
assert.equal(fs.existsSync('tmp/t1'), true);
|
||||
assert.equal(fs.existsSync('tmp/t4'), false);
|
||||
shell.mkdir('tmp/t1', 'tmp/t4'); // one dir exists, one doesn't
|
||||
assert.equal(numLines(shell.error()), 1);
|
||||
assert.equal(fs.existsSync('tmp/t1'), true);
|
||||
assert.equal(fs.existsSync('tmp/t4'), true);
|
||||
|
||||
assert.equal(fs.existsSync('tmp/a'), false);
|
||||
shell.mkdir('-p', 'tmp/a/b/c');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/a/b/c'), true);
|
||||
shell.rm('-Rf', 'tmp/a'); // revert
|
||||
|
||||
// multiple dirs
|
||||
shell.mkdir('-p', 'tmp/zzza', 'tmp/zzzb', 'tmp/zzzc');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/zzza'), true);
|
||||
assert.equal(fs.existsSync('tmp/zzzb'), true);
|
||||
assert.equal(fs.existsSync('tmp/zzzc'), true);
|
||||
|
||||
// multiple dirs, array syntax
|
||||
shell.mkdir('-p', ['tmp/yyya', 'tmp/yyyb', 'tmp/yyyc']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('tmp/yyya'), true);
|
||||
assert.equal(fs.existsSync('tmp/yyyb'), true);
|
||||
assert.equal(fs.existsSync('tmp/yyyc'), true);
|
||||
|
||||
shell.exit(123);
|
|
@ -1,130 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
// Prepare tmp/
|
||||
shell.cp('resources/*', 'tmp');
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
shell.mv();
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.mv('file1');
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.mv('-f');
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.mv('-Z', 'tmp/file1', 'tmp/file1'); // option not supported
|
||||
assert.ok(shell.error());
|
||||
assert.equal(fs.existsSync('tmp/file1'), true);
|
||||
|
||||
shell.mv('asdfasdf', 'tmp'); // source does not exist
|
||||
assert.ok(shell.error());
|
||||
assert.equal(numLines(shell.error()), 1);
|
||||
assert.equal(fs.existsSync('tmp/asdfasdf'), false);
|
||||
|
||||
shell.mv('asdfasdf1', 'asdfasdf2', 'tmp'); // sources do not exist
|
||||
assert.ok(shell.error());
|
||||
assert.equal(numLines(shell.error()), 2);
|
||||
assert.equal(fs.existsSync('tmp/asdfasdf1'), false);
|
||||
assert.equal(fs.existsSync('tmp/asdfasdf2'), false);
|
||||
|
||||
shell.mv('asdfasdf1', 'asdfasdf2', 'tmp/file1'); // too many sources (dest is file)
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.mv('tmp/file1', 'tmp/file2'); // dest already exists
|
||||
assert.ok(shell.error());
|
||||
|
||||
shell.mv('tmp/file1', 'tmp/file2', 'tmp/a_file'); // too many sources (exist, but dest is file)
|
||||
assert.ok(shell.error());
|
||||
assert.equal(fs.existsSync('tmp/a_file'), false);
|
||||
|
||||
shell.mv('tmp/file*', 'tmp/file1'); // can't use wildcard when dest is file
|
||||
assert.ok(shell.error());
|
||||
assert.equal(fs.existsSync('tmp/file1'), true);
|
||||
assert.equal(fs.existsSync('tmp/file2'), true);
|
||||
assert.equal(fs.existsSync('tmp/file1.js'), true);
|
||||
assert.equal(fs.existsSync('tmp/file2.js'), true);
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
shell.cd('tmp');
|
||||
|
||||
// handles self OK
|
||||
shell.mkdir('tmp2');
|
||||
shell.mv('*', 'tmp2'); // has to handle self (tmp2 --> tmp2) without throwing error
|
||||
assert.ok(shell.error()); // there's an error, but not fatal
|
||||
assert.equal(fs.existsSync('tmp2/file1'), true); // moved OK
|
||||
shell.mv('tmp2/*', '.'); // revert
|
||||
assert.equal(fs.existsSync('file1'), true); // moved OK
|
||||
|
||||
shell.mv('file1', 'file3'); // one source
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('file1'), false);
|
||||
assert.equal(fs.existsSync('file3'), true);
|
||||
shell.mv('file3', 'file1'); // revert
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('file1'), true);
|
||||
|
||||
// two sources
|
||||
shell.rm('-rf', 't');
|
||||
shell.mkdir('-p', 't');
|
||||
shell.mv('file1', 'file2', 't');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('file1'), false);
|
||||
assert.equal(fs.existsSync('file2'), false);
|
||||
assert.equal(fs.existsSync('t/file1'), true);
|
||||
assert.equal(fs.existsSync('t/file2'), true);
|
||||
shell.mv('t/*', '.'); // revert
|
||||
assert.equal(fs.existsSync('file1'), true);
|
||||
assert.equal(fs.existsSync('file2'), true);
|
||||
|
||||
// two sources, array style
|
||||
shell.rm('-rf', 't');
|
||||
shell.mkdir('-p', 't');
|
||||
shell.mv(['file1', 'file2'], 't'); // two sources
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('file1'), false);
|
||||
assert.equal(fs.existsSync('file2'), false);
|
||||
assert.equal(fs.existsSync('t/file1'), true);
|
||||
assert.equal(fs.existsSync('t/file2'), true);
|
||||
shell.mv('t/*', '.'); // revert
|
||||
assert.equal(fs.existsSync('file1'), true);
|
||||
assert.equal(fs.existsSync('file2'), true);
|
||||
|
||||
shell.mv('file*.js', 't'); // wildcard
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('file1.js'), false);
|
||||
assert.equal(fs.existsSync('file2.js'), false);
|
||||
assert.equal(fs.existsSync('t/file1.js'), true);
|
||||
assert.equal(fs.existsSync('t/file2.js'), true);
|
||||
shell.mv('t/*', '.'); // revert
|
||||
assert.equal(fs.existsSync('file1.js'), true);
|
||||
assert.equal(fs.existsSync('file2.js'), true);
|
||||
|
||||
shell.mv('-f', 'file1', 'file2'); // dest exists, but -f given
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(fs.existsSync('file1'), false);
|
||||
assert.equal(fs.existsSync('file2'), true);
|
||||
|
||||
shell.exit(123);
|
|
@ -1,118 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
var root = path.resolve(), trail;
|
||||
|
||||
function reset() {
|
||||
shell.dirs('-c');
|
||||
shell.cd(root);
|
||||
}
|
||||
|
||||
// Valid
|
||||
shell.pushd('resources/pushd');
|
||||
trail = shell.popd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [ root ]);
|
||||
|
||||
shell.pushd('resources/pushd');
|
||||
shell.pushd('a');
|
||||
trail = shell.popd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
shell.pushd('b');
|
||||
trail = shell.popd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
shell.pushd('b');
|
||||
shell.pushd('c');
|
||||
trail = shell.popd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
trail = shell.popd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
trail = shell.popd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(trail.length, 1);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [ root ]);
|
||||
|
||||
// Valid by index
|
||||
shell.pushd('resources/pushd');
|
||||
trail = shell.popd('+0');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [ root ]);
|
||||
|
||||
shell.pushd('resources/pushd');
|
||||
trail = shell.popd('+1');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [ path.resolve(root, 'resources/pushd') ]);
|
||||
|
||||
reset(); shell.pushd('resources/pushd');
|
||||
trail = shell.popd('-0');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [ path.resolve(root, 'resources/pushd') ]);
|
||||
|
||||
reset(); shell.pushd('resources/pushd');
|
||||
trail = shell.popd('-1');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [ root ]);
|
||||
|
||||
|
||||
reset(); shell.pushd('resources/pushd');
|
||||
trail = shell.popd('-n');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [ path.resolve(root, 'resources/pushd') ]);
|
||||
|
||||
// Invalid
|
||||
trail = shell.popd();
|
||||
assert.ok(shell.error('popd: directory stack empty\n'));
|
||||
|
||||
// Test that the root dir is not stored
|
||||
shell.cd('resources/pushd');
|
||||
shell.pushd('b');
|
||||
trail = shell.popd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(trail[0], path.resolve(root, 'resources/pushd'));
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
shell.popd();
|
||||
assert.ok(shell.error(), null);
|
||||
|
||||
shell.cd(root);
|
||||
|
||||
shell.exit(123);
|
|
@ -1,228 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
var root = path.resolve(), trail;
|
||||
|
||||
function reset() {
|
||||
shell.dirs('-c');
|
||||
shell.cd(root);
|
||||
}
|
||||
|
||||
// Push valid directories
|
||||
trail = shell.pushd('resources/pushd');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
trail = shell.pushd('a');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
trail = shell.pushd('../b');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
trail = shell.pushd('c');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/b/c'),
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
// Push stuff around with positive indices
|
||||
trail = shell.pushd('+0');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/b/c'),
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
trail = shell.pushd('+1');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root,
|
||||
path.resolve(root, 'resources/pushd/b/c')
|
||||
]);
|
||||
|
||||
trail = shell.pushd('+2');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root,
|
||||
path.resolve(root, 'resources/pushd/b/c'),
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a')
|
||||
]);
|
||||
|
||||
trail = shell.pushd('+3');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root,
|
||||
path.resolve(root, 'resources/pushd/b/c')
|
||||
]);
|
||||
|
||||
trail = shell.pushd('+4');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/b/c'),
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
// Push stuff around with negative indices
|
||||
trail = shell.pushd('-0');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
root,
|
||||
path.resolve(root, 'resources/pushd/b/c'),
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd')
|
||||
]);
|
||||
|
||||
trail = shell.pushd('-1');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root,
|
||||
path.resolve(root, 'resources/pushd/b/c'),
|
||||
path.resolve(root, 'resources/pushd/b')
|
||||
]);
|
||||
|
||||
trail = shell.pushd('-2');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
root,
|
||||
path.resolve(root, 'resources/pushd/b/c'),
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd')
|
||||
]);
|
||||
|
||||
trail = shell.pushd('-3');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/b/c'),
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
trail = shell.pushd('-4');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
path.resolve(root, 'resources/pushd/b/c'),
|
||||
path.resolve(root, 'resources/pushd/b'),
|
||||
path.resolve(root, 'resources/pushd/a'),
|
||||
path.resolve(root, 'resources/pushd'),
|
||||
root
|
||||
]);
|
||||
|
||||
// Push without changing directory or resolving paths
|
||||
reset(); trail = shell.pushd('-n', 'resources/pushd');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
root,
|
||||
'resources/pushd'
|
||||
]);
|
||||
|
||||
trail = shell.pushd('-n', 'resources/pushd/a');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
assert.deepEqual(trail, [
|
||||
root,
|
||||
'resources/pushd/a',
|
||||
'resources/pushd'
|
||||
]);
|
||||
|
||||
// Push invalid directory
|
||||
shell.pushd('does/not/exist');
|
||||
assert.equal(shell.error(), 'pushd: no such file or directory: ' + path.resolve('.', 'does/not/exist') + '\n');
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
|
||||
// Push without arguments should swap top two directories when stack length is 2
|
||||
reset(); trail = shell.pushd('resources/pushd');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(trail.length, 2);
|
||||
assert.equal(path.relative(root, trail[0]), 'resources/pushd');
|
||||
assert.equal(trail[1], root);
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
trail = shell.pushd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(trail.length, 2);
|
||||
assert.equal(trail[0], root);
|
||||
assert.equal(path.relative(root, trail[1]), 'resources/pushd');
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
|
||||
// Push without arguments should swap top two directories when stack length is > 2
|
||||
trail = shell.pushd('resources/pushd/a');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(trail.length, 3);
|
||||
assert.equal(path.relative(root, trail[0]), 'resources/pushd/a');
|
||||
assert.equal(trail[1], root);
|
||||
assert.equal(path.relative(root, trail[2]), 'resources/pushd');
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
|
||||
trail = shell.pushd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(trail.length, 3);
|
||||
assert.equal(trail[0], root);
|
||||
assert.equal(path.relative(root, trail[1]), 'resources/pushd/a');
|
||||
assert.equal(path.relative(root, trail[2]), 'resources/pushd');
|
||||
assert.equal(process.cwd(), trail[0]);
|
||||
|
||||
// Push without arguments invalid when stack is empty
|
||||
reset(); shell.pushd();
|
||||
assert.equal(shell.error(), 'pushd: no other directory\n');
|
||||
|
||||
shell.exit(123);
|
|
@ -1,28 +0,0 @@
|
|||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path');
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
}
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
var _pwd = shell.pwd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(_pwd, path.resolve('.'));
|
||||
|
||||
shell.cd('tmp');
|
||||
var _pwd = shell.pwd();
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(path.basename(_pwd), 'tmp');
|
||||
|
||||
shell.exit(123);
|
|
@ -1,11 +0,0 @@
|
|||
This is line one
|
||||
This is line two
|
||||
|
||||
This is line four
|
||||
.
|
||||
.
|
||||
More content here
|
||||
.
|
||||
.
|
||||
|
||||
This is line eleven
|
|
@ -1,2 +0,0 @@
|
|||
this is test file 1
|
||||
default state should be 0644 (rw-r--r--)
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1,2 +0,0 @@
|
|||
console.log('node_script_1234');
|
||||
|
|
@ -1 +0,0 @@
|
|||
test1
|
|
@ -1 +0,0 @@
|
|||
test
|
|
@ -1 +0,0 @@
|
|||
test1
|
|
@ -1 +0,0 @@
|
|||
test2
|
|
@ -1 +0,0 @@
|
|||
test
|
|
@ -1 +0,0 @@
|
|||
test2
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
123
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
asdf
|
|
@ -1 +0,0 @@
|
|||
nada
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue