Skip to content

Commit

Permalink
move index code to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
11joselu committed Apr 26, 2019
1 parent e88ea48 commit 46418bf
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 157 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ __tests__/build/*.less
coverage/
.nyc_output/

static/
build/
static/*
build/*

*.config.js
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
static/
build/
*.config.js
__tests__/build/*.less
.vscode/

coverage/
.nyc_output/
40 changes: 20 additions & 20 deletions __tests__/compiler/Renderer.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
const expect = require('chai').expect;

const Renderer = require('../../lib/compiler/Renderer');
const LessRenderer = require('../../lib/compiler/LessRenderer');
const FileManager = require('../../lib/compiler/FileManager');

describe('Test Renderer', () => {
let renderer;
let lessRenderer;

beforeEach(() => {
const manager = new FileManager('test.less', 'test.css', process.cwd());
renderer = new Renderer(manager, []);
lessRenderer = new LessRenderer(manager, []);
});

it('Should create a correct instance', () => {
expect(renderer.getManager()).to.be.an.instanceof(FileManager);
expect(lessRenderer.getManager()).to.be.an.instanceof(FileManager);

expect(renderer)
expect(lessRenderer)
.to.be.an('object')
.to.have.own.property('paths')
.that.is.a('array');
expect(renderer.getHasherPlugin()).to.be.an('object');
expect(renderer.getHasherPlugin())
expect(lessRenderer.getHasherPlugin()).to.be.an('object');
expect(lessRenderer.getHasherPlugin())
.to.have.own.property('install')
.that.is.a('function');
});

it('Should render styles', () => {
const toBuild = '';
const resultPromise = renderer.render(toBuild);
const resultPromise = lessRenderer.render(toBuild);
expect(resultPromise).to.be.a('promise');
resultPromise.then(({ css }) => {
expect(css).to.equal(toBuild);
Expand All @@ -35,36 +35,36 @@ describe('Test Renderer', () => {

it('Should push new path inside array of paths', () => {
const newPath = 'static';
renderer.pushNewPath(newPath);
expect(renderer.getPaths()).to.include(newPath);
renderer.pushNewPath(newPath);
expect(renderer.getPaths()).to.include(newPath);
lessRenderer.pushNewPath(newPath);
expect(lessRenderer.getPaths()).to.include(newPath);
lessRenderer.pushNewPath(newPath);
expect(lessRenderer.getPaths()).to.include(newPath);
});
});

describe('Test Renderer with config file', () => {
it('Should create a correct file object', () => {
const manager = new FileManager('test.less', 'test.css', process.cwd());
const renderer = new Renderer(manager, [], {});
const lessOptions = renderer.getLessOptions();
const lessRenderer = new LessRenderer(manager, [], {});
const lessOptions = lessRenderer.getLessOptions();
expect(lessOptions).to.be.an('object');
expect(lessOptions).to.have.own.property('paths');
expect(lessOptions).to.have.own.property('plugins');
});

it('Should create a correct file object with paths attributes', () => {
const manager = new FileManager('test.less', 'test.css', process.cwd());
const renderer = new Renderer(manager, [], { paths: ['tested'] });
const lessOptions = renderer.getLessOptions();
const lessRenderer = new LessRenderer(manager, [], { paths: ['tested'] });
const lessOptions = lessRenderer.getLessOptions();
expect(lessOptions.paths)
.to.be.an('array')
.that.includes('tested');
});

it('Should create a correct file object with plugins attributes', () => {
const manager = new FileManager('test.less', 'test.css', process.cwd());
const renderer = new Renderer(manager, [], { plugins: ['tested'] });
const lessOptions = renderer.getLessOptions();
const lessRenderer = new LessRenderer(manager, [], { plugins: ['tested'] });
const lessOptions = lessRenderer.getLessOptions();
expect(lessOptions.plugins)
.to.be.an('array')
.that.includes('tested');
Expand All @@ -73,11 +73,11 @@ describe('Test Renderer with config file', () => {
it('Should throw erros with not correct file object', () => {
const manager = new FileManager('test.less', 'test.css', process.cwd());
expect(() => {
new Renderer(manager, [], { paths: 'tested' });
new LessRenderer(manager, [], { paths: 'tested' });
}).to.throw();

expect(() => {
new Renderer(manager, [], { plugins: 'tested' });
new LessRenderer(manager, [], { plugins: 'tested' });
}).to.throw();
});
});
26 changes: 8 additions & 18 deletions __tests__/utils/utils-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ const expect = require('chai').expect;
const utils = require('../../lib/utils-functions');
const fs = require('fs');
const path = require('path');
const createGraph = require('../../lib/graph/create');
const DirectoryAlreadyExistsException = require('../../lib/exceptions/DirectoryAlreadyExistsException');

const { createLessFile } = require('../test-functions');

const dir = path.resolve(process.cwd(), '__tests__', 'build', 'test');

describe('Test utils functions', () => {
Expand All @@ -28,21 +25,14 @@ describe('Test utils functions', () => {
expect(utils.existsDirectory('notFounded')).to.be.false;
});

/* it('Should create paths for less plugin', () => {
const file = createLessFile();
const graph = createGraph(file.path);
const paths = utils.getPathsForLessPlugin(
graph,
process.cwd(),
'tested.less'
);
expect(paths)
.to.be.an('array')
.that.include('__tests__/build/');
fs.unlinkSync(file.path);
}); */
it('Should handle correct promises functions', async () => {
let [error] = await utils.promiseHandler(Promise.reject(new Error('')));
expect(error).to.be.instanceOf(Error);
[error] = await utils.promiseHandler(Promise.resolve(new Error('')));
expect(error).to.be.instanceOf(Error);
const [, data] = await utils.promiseHandler(Promise.resolve(true));
expect(data).to.be.true;
});

after(() => {
fs.rmdirSync(path.dirname(path.join(dir, 'test')));
Expand Down
124 changes: 10 additions & 114 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
const chokidar = require('chokidar');
const vfs = require('vinyl-fs');
const fs = require('fs');
const through = require('through2').obj;
const argv = require('yargs').argv;
const path = require('path');

const logger = require('./lib/logger');
const GraphStructure = require('./lib/graph/GraphStructure');
const Renderer = require('./lib/compiler/Renderer');
const LessRenderer = require('./lib/compiler/LessRenderer');
const FileManager = require('./lib/compiler/FileManager');
const WatcherQueue = require('./lib/Watcher/WatcherQueue');
const Compiler = require('./lib/compiler/Compiler');
const utils = require('./lib/utils-functions');
const validateArguments = require('./lib/validator/paramsValidator');

Expand All @@ -36,128 +33,27 @@ if (!utils.existsDirectory(manager.getOutputDir())) {
}

const graph = new GraphStructure(manager);
const renderer = new Renderer(
const lessRenderer = new LessRenderer(
manager,
graph.getFoundedPaths(),
config.lessOptions
);

const wQueue = new WatcherQueue(graph.getFiles());

let isFirstBuildValid = false;
let isCompiling = false;
const watcher = chokidar.watch(wQueue.getQueue());

const compileLess = (newFile, isMainFile = false) => {
console.reset();

if (isCompiling) {
return;
}

const stream = vfs.src(newFile);

stream
.pipe(
through(function(file, enc, done) {
let str = file.contents.toString();

const { removedImports, newImports } = graph.getImportStateFromFile(
file.path
);

if (newImports.length) {
addNewFilesToWatch(newImports);
}

if (removedImports.length) {
removedImports
.filter(graph.shouldBeUnWatched.bind(graph))
.forEach(unwatchFile);
}

logger.logInfoBuild(newFile);

isCompiling = true;
renderer
.render(str)
.then(
function({ css }) {
let contents = css;

if (isMainFile) {
file.contents = new Buffer(contents);
manager.setRootFile(file);
isFirstBuildValid = true;
}

const isRootFileEmpty = manager.isRootFileEmpty();

if (!isMainFile && !isRootFileEmpty) {
manager.replaceContentInMainFile(newFile, contents);
}

if (!isRootFileEmpty) {
this.push(manager.getBufferFromRootFile());
}

isCompiling = false;

if (!isFirstBuildValid) {
compileLess(manager.getInputFile(), true);
}

done();
}.bind(this)
)
.catch(err => {
if (isMainFile) {
isFirstBuildValid = false;
}

isCompiling = false;
logger.logErrorBuild(err);
});
})
)
.pipe(fs.createWriteStream(manager.getOutputFile()))
.on('finish', () => {
logger.logSuccessBuild();
});
};

const unwatchFile = filePath => {
const index = wQueue.findIndexPath(filePath);
manager.replaceContentInMainFile(filePath, '');

if (index !== -1) {
wQueue.remove(index);
watcher.unwatch(filePath);
}
};

const addNewFilesToWatch = (paths = []) => {
paths.filter(p => !wQueue.isWatched(p)).forEach(addNewFileToWatch);
};

const addNewFileToWatch = newImport => {
renderer.pushNewPath(path.dirname(newImport));
wQueue.addToWatch(newImport);
watcher.add(newImport);
};
const filesToWatch = graph.getFiles();
const watcher = chokidar.watch(filesToWatch);
const wQueue = new WatcherQueue(filesToWatch, watcher);
const compiler = new Compiler(graph, wQueue, lessRenderer);

watcher
.on('add', filePath => {
const isMain = manager.isMainFile(filePath);
if (isMain) {
compileLess(filePath, isMain);
compiler.compile(filePath, isMain);
}
})
.on('change', filePath => {
compileLess(filePath, manager.isMainFile(filePath));
compiler.compile(filePath, manager.isMainFile(filePath));
})
.on('unlink', filePath => {
unwatchFile(filePath);
compiler.unwatchFile(filePath);
});

module.exports = compileLess;
8 changes: 7 additions & 1 deletion lib/Watcher/WatcherQueue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class WatcherQueue {
constructor(queue = []) {
constructor(queue, chokidarWatcher) {
this.queue = queue;
this.chokidarWatcher = chokidarWatcher;
}

getQueue() {
Expand All @@ -13,6 +14,7 @@ class WatcherQueue {

addToWatch(newFilePath) {
this.queue.push(newFilePath);
this.chokidarWatcher.add(newFilePath);
}

remove(index) {
Expand All @@ -25,6 +27,10 @@ class WatcherQueue {
return false;
}

unwatchFile(filePath) {
this.chokidarWatcher.unwatch(filePath);
}

findIndexPath(filePath) {
return this.queue.findIndex(p => p === filePath);
}
Expand Down
Loading

0 comments on commit 46418bf

Please sign in to comment.