Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat in npm using a glob input (no Grunt or Gulp)

Tags:

node.js

npm

build

I am experimenting with using npm as a build tool.

I'd like to do something simple, just concat all the css files together. I can't find an npm module that does only that. there is concat, but it has no CLI interface. There are lots of gulp/grunt plugins that do it, but I don't want those dependencies for this experiement. I also understand I can use unix's cat:

cat a.css b.css > all.css

But this doesn't support globbing. I'd like to be able to do this:

concat app/**.css > dist/all.css

Is this possible?

like image 508
SimplGy Avatar asked Dec 03 '25 01:12

SimplGy


1 Answers

If you're on linux, you can use cat.

cat app/*.css > all.css

If you want to search the folders recursively, use find.

find app -name "*.css" -exec cat {} \; > all.css

Since you wanted something node-specific, you could use glob.

Save the following as concat.js

var fs = require('fs'),
    glob = require('glob'),
    args = process.argv.splice(2);

if(args.length !== 2)
    return console.log('Incorrect usage. "node concat [glob] [output file]"');

if(fs.exists(args[1]))
    fs.unlinkSync(args[1]);
glob.sync(args[0]).forEach(function(file) {
    fs.appendFileSync(args[1], fs.readFileSync(file, 'utf-8'));
});

Then you'd use

node concat app/**/*.css all.css

Or since you're using build scripts, have the following in package.json

"scripts": {
    "build": "node concat app/**/*.css all.css",
}

and

npm build
like image 152
Ben Fortune Avatar answered Dec 04 '25 14:12

Ben Fortune



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!