├── .gitignore ├── README.md ├── concatFunctions └── index.js ├── index.js ├── package.json └── test ├── concat.unit.test.js ├── dest └── bundle.css ├── files ├── a.css ├── b.css └── c.css └── index.integration.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | npm-debug.log -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## concat-cli 2 | 3 | Just a simple wrapper around the [concat](https://www.npmjs.org/package/concat) module, to concatenate files via the command line 4 | 5 | 6 | 7 | ## Install 8 | 9 | ```bash 10 | $ npm install -g concat-cli 11 | ``` 12 | 13 | 14 | 15 | ## Usage 16 | 17 | Pass the files to concatenate (-f or --files parameter) to the tool, and optionally and destination file (-o or --output parameter): 18 | 19 | ```bash 20 | $ concat-cli -f *.js -o bundle.js 21 | ``` 22 | Concat-cli will create the bundle.js file, if it doesn't exists, and dump the content of all the passed files into that one. If you don't provide an output file, the tool will concatenate everything into a file called 'all', with the correct extension. 23 | 24 | 25 | ## Test 26 | 27 | ```bash 28 | $ npm test 29 | ``` -------------------------------------------------------------------------------- /concatFunctions/index.js: -------------------------------------------------------------------------------- 1 | 2 | var concat = require('concat'); 3 | var chalk = require('chalk'); 4 | 5 | function getFileExtension(file) { 6 | return file.substr((~-file.lastIndexOf(".") >>> 0) + 2); 7 | } 8 | 9 | function errorHandler(error) { 10 | if(error) { 11 | throw new Error(error); 12 | } 13 | } 14 | 15 | function getDestination(output, files) { 16 | if(!Array.isArray(files)) errorHandler('Files should be an Array'); 17 | return (output === 'all') ? output + '.' + getFileExtension(files[0]) : output; 18 | } 19 | 20 | function concatFiles(files, output) { 21 | var destination = getDestination(output, files); 22 | concat(files, destination, function(concatError) { 23 | errorHandler(concatError); 24 | console.log(chalk.green('Files concatenated!')); 25 | }); 26 | } 27 | 28 | module.exports = { 29 | concatFiles: concatFiles, 30 | getDestination: getDestination, 31 | errorHandler: errorHandler, 32 | getFileExtension: getFileExtension 33 | }; -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var exampleTxt = 'This will concatenate all the js files in the current directory into a bundle.js'; 4 | var argv = require('yargs') 5 | .option('f', { 6 | alias: 'files', 7 | demand: true, 8 | array: true, 9 | describe: 'files or glob/wildcard to be matched and concatenated', 10 | type: 'string' 11 | }) 12 | .option('o', { 13 | alias: 'output', 14 | default: 'all', 15 | describe: 'the resulting file of the concatenation', 16 | type: 'string' 17 | }) 18 | .usage('$0 concat-cli -f string -o string') 19 | .example('concat-cli -f *.js -o bundle.js', exampleTxt) 20 | .help('help') 21 | .argv; 22 | 23 | var concatFunctions = require('./concatFunctions'); 24 | 25 | concatFunctions.concatFiles(argv.f, argv.o); 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "concat-cli", 3 | "version": "4.0.0", 4 | "description": "Command line implementation of concat module", 5 | "main": "index.js", 6 | "keywords": [ 7 | "concatenate", 8 | "concat", 9 | "cli" 10 | ], 11 | "repository": { 12 | "url": "git@github.com:jalba/concat-cli.git", 13 | "type": "git" 14 | }, 15 | "engines": { 16 | "node": ">=0.10" 17 | }, 18 | "scripts": { 19 | "test": "node ./node_modules/mocha/bin/mocha --reporter list" 20 | }, 21 | "preferGlogal": "true", 22 | "author": "Javier Alba", 23 | "license": "ISC", 24 | "bin": { 25 | "concat-cli": "./index.js" 26 | }, 27 | "dependencies": { 28 | "chalk": "^1.1.1", 29 | "concat": "^1.0.0", 30 | "yargs": "^3.30.0" 31 | }, 32 | "bugs": { 33 | "url": "https://github.com/jalba/concat-cli/issues" 34 | }, 35 | "homepage": "https://github.com/jalba/concat-cli", 36 | "devDependencies": { 37 | "chai": "^3.4.1", 38 | "mocha": "^2.3.3", 39 | "sinon": "^1.17.2" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/concat.unit.test.js: -------------------------------------------------------------------------------- 1 | var concatFunctions = require('./../concatFunctions'); 2 | 3 | var expect = require('chai').expect; 4 | var sinon = require('sinon'); 5 | var fs = require('fs'); 6 | var path = require('path'); 7 | 8 | describe('The concatFunctions module', function() { 9 | beforeEach(function() { 10 | sinon.spy(console, 'log'); 11 | }); 12 | afterEach(function() { 13 | console.log.restore(); 14 | }); 15 | it('should expose an errorHandler method that will throw any error provided', function() { 16 | var error = 'this is an error'; 17 | expect(function() { concatFunctions.errorHandler(error) }).to.throw(Error, /this is an error/); 18 | }); 19 | it('should expose an getFileExtension method that returns the file extension of a file name', function() { 20 | var fileNameCSS = 'file.css'; 21 | var fileNameJS = 'file.js'; 22 | var gitignore = '.gitignore'; 23 | expect(concatFunctions.getFileExtension(fileNameCSS)).to.equal('css'); 24 | expect(concatFunctions.getFileExtension(fileNameJS)).to.equal('js'); 25 | expect(concatFunctions.getFileExtension(gitignore)).to.equal(''); 26 | }); 27 | it('should expose an getDestination method that returns the output file string', function() { 28 | var dest = 'bundle.js'; 29 | var files = ['file.js']; 30 | expect(concatFunctions.getDestination('all', files)).to.equal('all.js'); 31 | expect(concatFunctions.getDestination(dest, files)).to.equal(dest); 32 | expect(function() { concatFunctions.getDestination('all', '') }).to.throw(Error); 33 | }); 34 | it('should expose an concatFiles method that concatenates the files passed to it', function(done) { 35 | var dest = 'bundle.js'; 36 | var files = ['file.js']; 37 | var filesCSS = [path.join(__dirname, '/files/a.css'), 38 | path.join(__dirname, '/files/b.css'), 39 | path.join(__dirname, '/files/c.css')]; 40 | var destCSS = path.join(__dirname, '/dest/bundle.css'); 41 | 42 | if (fs.existsSync(destCSS)) { 43 | fs.unlinkSync(destCSS); 44 | } 45 | concatFunctions.concatFiles(filesCSS, destCSS); 46 | setTimeout(function() { 47 | var calledArgs = console.log.getCall(0).args[0]; 48 | var condition = calledArgs.indexOf('Files concatenated!') > -1; 49 | expect(function() { concatFunctions.concatFiles('', 'all') }).to.throw(Error); 50 | expect(condition).to.equal(true); 51 | done(); 52 | }, 400); 53 | }); 54 | }); -------------------------------------------------------------------------------- /test/dest/bundle.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jalba/concat-cli/604c157129d515df63de4623a8412ae2110ef8bd/test/dest/bundle.css -------------------------------------------------------------------------------- /test/files/a.css: -------------------------------------------------------------------------------- 1 | a -------------------------------------------------------------------------------- /test/files/b.css: -------------------------------------------------------------------------------- 1 | b -------------------------------------------------------------------------------- /test/files/c.css: -------------------------------------------------------------------------------- 1 | c -------------------------------------------------------------------------------- /test/index.integration.test.js: -------------------------------------------------------------------------------- 1 | var child_process = require('child_process'); 2 | var expect = require('chai').expect; 3 | var path = require('path'); 4 | var fs = require('fs'); 5 | var destCSS = path.join(__dirname, '/dest/bundle.css'); 6 | var filesCSS = [path.join(__dirname, '/files/a.css'), 7 | path.join(__dirname, '/files/b.css'), 8 | path.join(__dirname, '/files/c.css')]; 9 | 10 | describe('concat-cli module', function() { 11 | it('should concatenate files when provided with the correct arguments', function(done) { 12 | if (fs.existsSync(destCSS)) { 13 | fs.unlinkSync(destCSS); 14 | } 15 | var spawn = child_process.spawn; 16 | var concatProcess = spawn('node', [path.join(__dirname, '/../index.js'), 17 | '-f', 18 | filesCSS, 19 | '-o', 20 | destCSS]); 21 | concatProcess.stdout.on('end', function() { 22 | expect(fs.existsSync(destCSS)).to.equal(true); 23 | done(); 24 | }); 25 | }); 26 | }); --------------------------------------------------------------------------------