Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
[changed] JSCompiler#be now does not ignore non-JavaScript files, but…
Browse files Browse the repository at this point in the history
… simply copies them over
  • Loading branch information
thealjey committed Jan 8, 2016
1 parent 4cc3c3c commit bff6a80
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 14 deletions.
8 changes: 4 additions & 4 deletions docs/JSCompiler.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ <h4 class="name" id="be"><span class="type-signature"></span>be(inPath, outPath,


<div class="description">
<p>Compiles a JavaScript file or a directory for the back end</p>
<p>Compiles a JavaScript file or a directory for the back end. Non-JavaScript files are simply copied over.</p>
</div>


Expand Down Expand Up @@ -575,7 +575,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="JSCompiler.js.html">JSCompiler.js</a>,
<a href="JSCompiler.js.html#sunlight-1-line-155">line 155</a>
<a href="JSCompiler.js.html#sunlight-1-line-178">line 178</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -1008,7 +1008,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="JSCompiler.js.html">JSCompiler.js</a>,
<a href="JSCompiler.js.html#sunlight-1-line-179">line 179</a>
<a href="JSCompiler.js.html#sunlight-1-line-202">line 202</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -1402,7 +1402,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="JSCompiler.js.html">JSCompiler.js</a>,
<a href="JSCompiler.js.html#sunlight-1-line-133">line 133</a>
<a href="JSCompiler.js.html#sunlight-1-line-156">line 156</a>
</li>
</ul>
</dd>
Expand Down
29 changes: 26 additions & 3 deletions docs/JSCompiler.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h1 class="page-title">Source: JSCompiler.js</h1>
import {Compiler} from './Compiler';
import type {ProgramData} from './Compiler';
import {join, extname, dirname, basename} from 'path';
import {readdir, stat} from 'fs';
import {readdir, stat, createReadStream, createWriteStream} from 'fs';
import {transformFile} from 'babel-core';
import webpack from 'webpack';
import MemoryFS from 'memory-fs';
Expand Down Expand Up @@ -179,9 +179,30 @@ <h1 class="page-title">Source: JSCompiler.js</h1>
});
}

/**
* Compiles a files
*
* @memberOf JSCompiler
* @instance
* @private
* @method copyFile
* @param {string} inPath - the input path
* @param {string} outPath - the output path
* @param {Function} callback - a callback function
* @example
* compiler.copyFile('/path/to/an/input/file', '/path/to/the/output/file', callback);
*/
copyFile(inPath: string, outPath: string, callback: () => void) {
++this.processing;
this.mkdir(outPath, () => {
createReadStream(inPath).pipe(createWriteStream(outPath));
callback();
});
}

/**
* Compiles a JavaScript file for the back end or recursively traverses a directory, looking for the JavaScript files
* to compile
* to compile. Non-JavaScript files are simply copied over.
*
* @memberOf JSCompiler
* @instance
Expand All @@ -202,6 +223,8 @@ <h1 class="page-title">Source: JSCompiler.js</h1>
this.beDir(inPath, outPath, callback);
} else if ('.js' === extname(inPath)) {
this.beFile(inPath, outPath, callback);
} else {
this.copyFile(inPath, outPath, callback);
}
});
}
Expand Down Expand Up @@ -229,7 +252,7 @@ <h1 class="page-title">Source: JSCompiler.js</h1>
}

/**
* Compiles a JavaScript file or a directory for the back end
* Compiles a JavaScript file or a directory for the back end. Non-JavaScript files are simply copied over.
*
* @memberOf JSCompiler
* @instance
Expand Down
2 changes: 1 addition & 1 deletion docs/quicksearch.html

Large diffs are not rendered by default.

30 changes: 28 additions & 2 deletions lib/JSCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,33 @@ var JSCompiler = exports.JSCompiler = function (_Compiler) {
});
}

/**
* Compiles a files
*
* @memberOf JSCompiler
* @instance
* @private
* @method copyFile
* @param {string} inPath - the input path
* @param {string} outPath - the output path
* @param {Function} callback - a callback function
* @example
* compiler.copyFile('/path/to/an/input/file', '/path/to/the/output/file', callback);
*/

}, {
key: 'copyFile',
value: function copyFile(inPath, outPath, callback) {
++this.processing;
this.mkdir(outPath, function () {
(0, _fs.createReadStream)(inPath).pipe((0, _fs.createWriteStream)(outPath));
callback();
});
}

/**
* Compiles a JavaScript file for the back end or recursively traverses a directory, looking for the JavaScript files
* to compile
* to compile. Non-JavaScript files are simply copied over.
*
* @memberOf JSCompiler
* @instance
Expand All @@ -197,6 +221,8 @@ var JSCompiler = exports.JSCompiler = function (_Compiler) {
_this4.beDir(inPath, outPath, callback);
} else if ('.js' === (0, _path.extname)(inPath)) {
_this4.beFile(inPath, outPath, callback);
} else {
_this4.copyFile(inPath, outPath, callback);
}
});
}
Expand Down Expand Up @@ -227,7 +253,7 @@ var JSCompiler = exports.JSCompiler = function (_Compiler) {
}

/**
* Compiles a JavaScript file or a directory for the back end
* Compiles a JavaScript file or a directory for the back end. Non-JavaScript files are simply copied over.
*
* @memberOf JSCompiler
* @instance
Expand Down
29 changes: 26 additions & 3 deletions src/JSCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import config from '../config/babel';
import {Compiler} from './Compiler';
import type {ProgramData} from './Compiler';
import {join, extname, dirname, basename} from 'path';
import {readdir, stat} from 'fs';
import {readdir, stat, createReadStream, createWriteStream} from 'fs';
import {transformFile} from 'babel-core';
import webpack from 'webpack';
import MemoryFS from 'memory-fs';
Expand Down Expand Up @@ -103,9 +103,30 @@ export class JSCompiler extends Compiler {
});
}

/**
* Compiles a files
*
* @memberOf JSCompiler
* @instance
* @private
* @method copyFile
* @param {string} inPath - the input path
* @param {string} outPath - the output path
* @param {Function} callback - a callback function
* @example
* compiler.copyFile('/path/to/an/input/file', '/path/to/the/output/file', callback);
*/
copyFile(inPath: string, outPath: string, callback: () => void) {
++this.processing;
this.mkdir(outPath, () => {
createReadStream(inPath).pipe(createWriteStream(outPath));
callback();
});
}

/**
* Compiles a JavaScript file for the back end or recursively traverses a directory, looking for the JavaScript files
* to compile
* to compile. Non-JavaScript files are simply copied over.
*
* @memberOf JSCompiler
* @instance
Expand All @@ -126,6 +147,8 @@ export class JSCompiler extends Compiler {
this.beDir(inPath, outPath, callback);
} else if ('.js' === extname(inPath)) {
this.beFile(inPath, outPath, callback);
} else {
this.copyFile(inPath, outPath, callback);
}
});
}
Expand Down Expand Up @@ -153,7 +176,7 @@ export class JSCompiler extends Compiler {
}

/**
* Compiles a JavaScript file or a directory for the back end
* Compiles a JavaScript file or a directory for the back end. Non-JavaScript files are simply copied over.
*
* @memberOf JSCompiler
* @instance
Expand Down
51 changes: 50 additions & 1 deletion test/JSCompiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ chai.use(sinonChai);

const files = 10;

let cmp, transformFile, isDirectory, webpack, compiler, run, JSCompiler, callback;
let cmp, transformFile, isDirectory, webpack, compiler, run, JSCompiler, callback, pipe;

function req(options) {
return proxyquire('../src/JSCompiler', options).JSCompiler;
Expand Down Expand Up @@ -433,16 +433,61 @@ describe('JSCompiler', () => {

});

describe('copyFile', () => {

beforeEach(() => {
cmp.processing = 0;
pipe = spy();
stub(cmp, 'mkdir').callsArg(1);
stub(fs, 'createReadStream').returns({pipe});
stub(fs, 'createWriteStream').returns('write stream');
cmp.copyFile('/path/to/the/input/file.css', '/path/to/the/output/file.css', callback);
});

afterEach(() => {
cmp.mkdir.restore();
fs.createReadStream.restore();
fs.createWriteStream.restore();
});

it('increments the processing counter', () => {
expect(cmp.processing).equal(1);
});

it('calls mkdir', () => {
expect(cmp.mkdir).calledWith('/path/to/the/output/file.css', match.func);
});

it('calls createReadStream', () => {
expect(fs.createReadStream).calledWith('/path/to/the/input/file.css');
});

it('calls createWriteStream', () => {
expect(fs.createWriteStream).calledWith('/path/to/the/output/file.css');
});

it('calls pipe', () => {
expect(pipe).calledWith('write stream');
});

it('calls the callback', () => {
expect(callback).called;
});

});

describe('beTraverse', () => {

beforeEach(() => {
stub(cmp, 'beDir');
stub(cmp, 'beFile');
stub(cmp, 'copyFile');
});

afterEach(() => {
cmp.beDir.restore();
cmp.beFile.restore();
cmp.copyFile.restore();
});

describe('stat error', () => {
Expand Down Expand Up @@ -500,6 +545,10 @@ describe('JSCompiler', () => {
expect(cmp.beFile).not.called;
});

it('calls copyFile', () => {
expect(cmp.copyFile).calledWith('/path/to/the/input/file.css', '/path/to/the/output/file.css', callback);
});

});

describe('javascript', () => {
Expand Down

0 comments on commit bff6a80

Please sign in to comment.