├── test ├── empty ├── simple1 ├── simple2 ├── folder │ ├── a.js │ └── b.js └── test.js ├── .travis.yml ├── tsconfig.json ├── bin ├── concat └── concat.ts ├── .gitignore ├── README.md ├── LICENSE ├── package.json ├── index.ts └── index.js /test/empty: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/simple1: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test/simple2: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /test/folder/a.js: -------------------------------------------------------------------------------- 1 | function A() {}; 2 | -------------------------------------------------------------------------------- /test/folder/b.js: -------------------------------------------------------------------------------- 1 | function B() {}; 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - "7" 5 | - "6" 6 | - "node" 7 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "noImplicitAny": true, 5 | "target": "es6", 6 | "types" : [ "node", "core-js" ] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /bin/concat: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | const app = require('commander'); 4 | const cfg = require('../package.json'); 5 | const concat = require('../index'); 6 | const path = require('path'); 7 | app.version(cfg.version) 8 | .option('-o, --output ', 'output file') 9 | .description(cfg.description); 10 | app.parse(process.argv); 11 | let err = (err) => console.log(err); 12 | let output = (o) => { 13 | if (!app.output) { 14 | console.log(o); 15 | } 16 | }; 17 | if (app.args.length) { 18 | concat(app.args, app.output).then(output).catch(err); 19 | } 20 | else 21 | throw new Error('no files specified'); 22 | -------------------------------------------------------------------------------- /bin/concat.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 'use strict'; 4 | 5 | const app = require('commander') 6 | const cfg = require('../package.json'); 7 | const concat = require('../index'); 8 | const path = require('path'); 9 | 10 | app.version(cfg.version) 11 | .option('-o, --output ', 'output file') 12 | .description(cfg.description) 13 | 14 | app.parse(process.argv); 15 | 16 | let err = (err: Error) => console.log(err); 17 | let output = (o: string) => { 18 | if (!app.output) { 19 | console.log(o); 20 | } 21 | }; 22 | 23 | if(app.args.length) { 24 | concat(app.args, app.output).then(output).catch(err) 25 | } else throw new Error('no files specified') 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Concat 2 | 3 | [![Build Status](https://travis-ci.org/gko/concat.svg?branch=master)](https://travis-ci.org/gko/concat) 4 | 5 | Concatenate multiple files 6 | 7 | ## Usage 8 | 9 | ```bash 10 | Usage: concat [options] 11 | 12 | concatenate multiple files 13 | 14 | Options: 15 | -h, --help output usage information 16 | -V, --version output the version number 17 | -o, --output output file 18 | ``` 19 | 20 | examples: 21 | ```bash 22 | concat -o output.css ./1.css ./2.css ./3.css 23 | ``` 24 | 25 | You can also use it from node: 26 | 27 | ```javascript 28 | const concat = require('concat'); 29 | 30 | concat([file1, file2, file3]).then(result => console.log(result)) 31 | 32 | // or 33 | concat([file1, file2, file3], outputFile) 34 | ``` 35 | 36 | ## Tests 37 | 38 | To run tests you simply need to do: 39 | ```bash 40 | npm run test 41 | ``` 42 | 43 | ## Like it? 44 | 45 | :star: this repo 46 | 47 | ## License 48 | 49 | [MIT](http://opensource.org/licenses/MIT) 50 | 51 | Copyright (c) 2017 Konstantin Gorodinskiy 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Konstantin Gorodinskii 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "concat", 3 | "version": "1.0.3", 4 | "description": "concatenate multiple files", 5 | "main": "index.js", 6 | "scripts": { 7 | "bin": "tsc -t ES6 --moduleResolution node ./bin/concat.ts --outFile ./bin/concat", 8 | "build": "tsc -t ES6 --module commonjs --moduleResolution node index.ts && npm run bin", 9 | "test": "npm run build && _mocha" 10 | }, 11 | "bin": "./bin/concat", 12 | "engines": { 13 | "node": ">=6" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/gko/concat" 18 | }, 19 | "keywords": [ 20 | "concatenate", 21 | "files", 22 | "concat", 23 | "join" 24 | ], 25 | "author": "Konstantin Gorodinskiy ", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/gko/concat/issues" 29 | }, 30 | "homepage": "https://github.com/gko/concat#readme", 31 | "devDependencies": { 32 | "@types/core-js": "^0.9.41", 33 | "@types/node": "^7.0.0", 34 | "core-js": "^2.4.1", 35 | "mocha": "^3.2.0", 36 | "typescript": "^2.1.5" 37 | }, 38 | "dependencies": { 39 | "commander": "^2.9.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | import {readFile, readdir, writeFile, statSync} from 'fs' 2 | import {resolve, join} from 'path' 3 | 4 | const log = (err: Error) => console.log(err); 5 | 6 | const isFile = (f: string) => statSync(f).isFile(); 7 | 8 | const write = (fName: string, str: string) => new Promise((res, rej) => { 9 | writeFile(resolve(fName), str, (err: Error) => { 10 | if (err) return rej(err) 11 | 12 | return res(str) 13 | }) 14 | }); 15 | 16 | const readFolder = (folder: string) => new Promise((res, rej) => { 17 | readdir(resolve(folder), (err, files) => { 18 | if (err) rej(err) 19 | 20 | const fileList = files.map(f => join(folder, f)); 21 | res(fileList.filter(isFile)); 22 | }) 23 | }); 24 | 25 | 26 | const read = (fName: string) => new Promise((res, rej) => { 27 | readFile(resolve(fName), (err, str) => { 28 | if (err) rej(err) 29 | 30 | res(str) 31 | }) 32 | }); 33 | 34 | const concat = (files: string[]) => new Promise((res, rej) => { 35 | return Promise.all(files.map(read)) 36 | .then(src => res(src.join('\n'))) 37 | .catch(rej); 38 | }); 39 | 40 | export = (folder: string[] | string, outFile?: string) => new Promise((res, rej) => { 41 | let concatenated; 42 | 43 | if(typeof folder === 'string') { 44 | concatenated = readFolder(folder) 45 | .then(concat); 46 | } else { 47 | concatenated = concat(folder); 48 | } 49 | 50 | if (outFile) { 51 | concatenated.then((out: string) => write(outFile, out) 52 | .then(res) 53 | .catch(rej) 54 | ).catch(rej); 55 | } else { 56 | concatenated.then(res).catch(rej); 57 | } 58 | }); 59 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | const fs_1 = require("fs"); 3 | const path_1 = require("path"); 4 | const log = (err) => console.log(err); 5 | const isFile = (f) => fs_1.statSync(f).isFile(); 6 | const write = (fName, str) => new Promise((res, rej) => { 7 | fs_1.writeFile(path_1.resolve(fName), str, (err) => { 8 | if (err) 9 | return rej(err); 10 | return res(str); 11 | }); 12 | }); 13 | const readFolder = (folder) => new Promise((res, rej) => { 14 | fs_1.readdir(path_1.resolve(folder), (err, files) => { 15 | if (err) 16 | rej(err); 17 | const fileList = files.map(f => path_1.join(folder, f)); 18 | res(fileList.filter(isFile)); 19 | }); 20 | }); 21 | const read = (fName) => new Promise((res, rej) => { 22 | fs_1.readFile(path_1.resolve(fName), (err, str) => { 23 | if (err) 24 | rej(err); 25 | res(str); 26 | }); 27 | }); 28 | const concat = (files) => new Promise((res, rej) => { 29 | return Promise.all(files.map(read)) 30 | .then(src => res(src.join('\n'))) 31 | .catch(rej); 32 | }); 33 | module.exports = (folder, outFile) => new Promise((res, rej) => { 34 | let concatenated; 35 | if (typeof folder === 'string') { 36 | concatenated = readFolder(folder) 37 | .then(concat); 38 | } 39 | else { 40 | concatenated = concat(folder); 41 | } 42 | if (outFile) { 43 | concatenated.then((out) => write(outFile, out) 44 | .then(res) 45 | .catch(rej)).catch(rej); 46 | } 47 | else { 48 | concatenated.then(res).catch(rej); 49 | } 50 | }); 51 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const concat = require('../index') 2 | const assert = require('assert'); 3 | const read = require('fs').readFileSync; 4 | const exec = require('child_process').execSync; 5 | 6 | const err = err => console.log(err) 7 | 8 | describe('Concatenate', () => { 9 | it('empty files', done => { 10 | concat([__dirname + "/empty", __dirname + "/empty"]).then(f => { 11 | assert.equal(f, '\n') 12 | done() 13 | }).catch(err) 14 | }) 15 | 16 | it('simple concatenation', done => { 17 | concat([__dirname + "/simple1", __dirname + "/simple2"]).then(f => { 18 | assert.equal(f, '1\n\n2\n') 19 | done() 20 | }).catch(err) 21 | }) 22 | 23 | it('multiple files concatenation', done => { 24 | concat([ 25 | __dirname + "/simple1", 26 | __dirname + "/simple2", 27 | __dirname + "/simple1", 28 | __dirname + "/simple2", 29 | __dirname + "/simple1", 30 | __dirname + "/simple2", 31 | __dirname + "/simple1", 32 | __dirname + "/simple2" 33 | ]).then(f => { 34 | assert.equal(f, '1\n\n2\n\n1\n\n2\n\n1\n\n2\n\n1\n\n2\n') 35 | done() 36 | }).catch(err) 37 | }) 38 | 39 | it('should concatenate folder', done => { 40 | concat(__dirname + "/folder").then(f => { 41 | assert.equal(f.trim(), 'function A() {};\n\nfunction B() {};') 42 | done() 43 | }).catch(err) 44 | }) 45 | }) 46 | 47 | describe('cli', () => { 48 | it('should write to output when no file specified', () => { 49 | assert.equal(exec(`node ./bin/concat ${__dirname + '/simple1'} ${__dirname + '/simple2'}`).toString(), '1\n\n2\n\n') 50 | }) 51 | 52 | it('should write to file', () => { 53 | assert.equal(exec(`node ./bin/concat -o ${__dirname + '/test'} ${__dirname + '/simple1'} ${__dirname + '/simple2'} && cat ${__dirname + '/test'}`).toString(), '1\n\n2\n') 54 | }) 55 | }) 56 | --------------------------------------------------------------------------------