Skip to content

Commit

Permalink
Build: Add eslint and jscs presets & update code
Browse files Browse the repository at this point in the history
  • Loading branch information
pdehaan authored and phated committed Sep 27, 2016
1 parent 72475a6 commit 6c0f046
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 142 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "gulp"
}
3 changes: 3 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "gulp"
}
2 changes: 0 additions & 2 deletions .jshintignore

This file was deleted.

36 changes: 0 additions & 36 deletions .jshintrc

This file was deleted.

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_js:
- "0.10"
after_script:
- npm run coveralls
- npm run lint
100 changes: 63 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ var Stream = require('stream');
var replaceExt = require('replace-ext');

function File(file) {
if (!file) file = {};
if (!file) {
file = {};
}

// record path change
// Record path change
var history = file.path ? [file.path] : file.history;
this.history = history || [];

this.cwd = file.cwd || process.cwd();
this.base = file.base || this.cwd;

// stat = files stats object
// Stat = files stats object
this.stat = file.stat || null;

// contents = stream, buffer, or null if not read
// Contents = stream, buffer, or null if not read
this.contents = file.contents || null;

this._isVinyl = true;
Expand All @@ -40,7 +42,7 @@ File.prototype.isNull = function() {
return isNull(this.contents);
};

// TODO: should this be moved to vinyl-fs?
// TODO: Should this be moved to vinyl-fs?
File.prototype.isDirectory = function() {
return this.isNull() && this.stat && this.stat.isDirectory();
};
Expand All @@ -49,19 +51,19 @@ File.prototype.clone = function(opt) {
if (typeof opt === 'boolean') {
opt = {
deep: opt,
contents: true
contents: true,
};
} else if (!opt) {
opt = {
deep: true,
contents: true
contents: true,
};
} else {
opt.deep = opt.deep === true;
opt.contents = opt.contents !== false;
}

// clone our file contents
// Clone our file contents
var contents;
if (this.isStream()) {
contents = this.contents.pipe(new Stream.PassThrough());
Expand All @@ -75,12 +77,12 @@ File.prototype.clone = function(opt) {
base: this.base,
stat: (this.stat ? cloneStats(this.stat) : null),
history: this.history.slice(),
contents: contents
contents: contents,
});

// clone our custom properties
// Clone our custom properties
Object.keys(this).forEach(function(key) {
// ignore built-in fields
// Ignore built-in fields
if (key === '_contents' || key === 'stat' ||
key === 'history' || key === 'path' ||
key === 'base' || key === 'cwd') {
Expand All @@ -92,8 +94,12 @@ File.prototype.clone = function(opt) {
};

File.prototype.pipe = function(stream, opt) {
if (!opt) opt = {};
if (typeof opt.end === 'undefined') opt.end = true;
if (!opt) {
opt = {};
}
if (typeof opt.end === 'undefined') {
opt.end = true;
}

if (this.isStream()) {
return this.contents.pipe(stream, opt);
Expand All @@ -107,19 +113,21 @@ File.prototype.pipe = function(stream, opt) {
return stream;
}

// isNull
if (opt.end) stream.end();
// Check if isNull
if (opt.end) {
stream.end();
}
return stream;
};

File.prototype.inspect = function() {
var inspect = [];

// use relative path if possible
// Use relative path if possible
var filePath = (this.base && this.path) ? this.relative : this.path;

if (filePath) {
inspect.push('"'+filePath+'"');
inspect.push('"' + filePath + '"');
}

if (this.isBuffer()) {
Expand All @@ -130,15 +138,15 @@ File.prototype.inspect = function() {
inspect.push(inspectStream(this.contents));
}

return '<File '+inspect.join(' ')+'>';
return '<File ' + inspect.join(' ') + '>';
};

File.isVinyl = function(file) {
return file && file._isVinyl === true;
};

// virtual attributes
// or stuff with extra logic
// Virtual attributes
// Or stuff with extra logic
Object.defineProperty(File.prototype, 'contents', {
get: function() {
return this._contents;
Expand All @@ -148,66 +156,84 @@ Object.defineProperty(File.prototype, 'contents', {
throw new Error('File.contents can only be a Buffer, a Stream, or null.');
}
this._contents = val;
}
},
});

// TODO: should this be moved to vinyl-fs?
// TODO: Should this be moved to vinyl-fs?
Object.defineProperty(File.prototype, 'relative', {
get: function() {
if (!this.base) throw new Error('No base specified! Can not get relative.');
if (!this.path) throw new Error('No path specified! Can not get relative.');
if (!this.base) {
throw new Error('No base specified! Can not get relative.');
}
if (!this.path) {
throw new Error('No path specified! Can not get relative.');
}
return path.relative(this.base, this.path);
},
set: function() {
throw new Error('File.relative is generated from the base and path attributes. Do not modify it.');
}
},
});

Object.defineProperty(File.prototype, 'dirname', {
get: function() {
if (!this.path) throw new Error('No path specified! Can not get dirname.');
if (!this.path) {
throw new Error('No path specified! Can not get dirname.');
}
return path.dirname(this.path);
},
set: function(dirname) {
if (!this.path) throw new Error('No path specified! Can not set dirname.');
if (!this.path) {
throw new Error('No path specified! Can not set dirname.');
}
this.path = path.join(dirname, path.basename(this.path));
}
},
});

Object.defineProperty(File.prototype, 'basename', {
get: function() {
if (!this.path) throw new Error('No path specified! Can not get basename.');
if (!this.path) {
throw new Error('No path specified! Can not get basename.');
}
return path.basename(this.path);
},
set: function(basename) {
if (!this.path) throw new Error('No path specified! Can not set basename.');
if (!this.path) {
throw new Error('No path specified! Can not set basename.');
}
this.path = path.join(path.dirname(this.path), basename);
}
},
});

Object.defineProperty(File.prototype, 'extname', {
get: function() {
if (!this.path) throw new Error('No path specified! Can not get extname.');
if (!this.path) {
throw new Error('No path specified! Can not get extname.');
}
return path.extname(this.path);
},
set: function(extname) {
if (!this.path) throw new Error('No path specified! Can not set extname.');
if (!this.path) {
throw new Error('No path specified! Can not set extname.');
}
this.path = replaceExt(this.path, extname);
}
},
});

Object.defineProperty(File.prototype, 'path', {
get: function() {
return this.history[this.history.length - 1];
},
set: function(path) {
if (typeof path !== 'string') throw new Error('path should be string');
if (typeof path !== 'string') {
throw new Error('path should be string');
}

// record history only when path changed
// Record history only when path changed
if (path && path !== this.path) {
this.history.push(path);
}
}
},
});

module.exports = File;
12 changes: 8 additions & 4 deletions lib/inspectStream.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
var isStream = require('./isStream');

module.exports = function(stream) {
if (!isStream(stream)) return;
if (!isStream(stream)) {
return;
}

var streamType = stream.constructor.name;
// avoid StreamStream
if (streamType === 'Stream') streamType = '';
// Avoid StreamStream
if (streamType === 'Stream') {
streamType = '';
}

return '<'+streamType+'Stream>';
return '<' + streamType + 'Stream>';
};
2 changes: 1 addition & 1 deletion lib/isStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ var Stream = require('stream').Stream;

module.exports = function(o) {
return !!o && o instanceof Stream;
};
};
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@
},
"devDependencies": {
"buffer-equal": "0.0.1",
"eslint": "^1.7.3",
"eslint-config-gulp": "^2.0.0",
"event-stream": "^3.1.0",
"istanbul": "^0.3.0",
"istanbul-coveralls": "^1.0.1",
"jshint": "^2.4.1",
"jscs": "^2.3.5",
"jscs-preset-gulp": "^1.0.0",
"lodash.templatesettings": "^3.1.0",
"mocha": "^2.0.0",
"rimraf": "^2.2.5",
"should": "^7.0.0"
},
"scripts": {
"test": "mocha && jshint lib",
"lint": "eslint . && jscs *.js lib/ test/",
"pretest": "npm run lint",
"test": "mocha",
"coveralls": "istanbul cover _mocha && istanbul-coveralls"
},
"engines": {
Expand Down
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "gulp/test"
}
Loading

0 comments on commit 6c0f046

Please sign in to comment.