3

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?

SimplGy
  • 20,079
  • 15
  • 107
  • 144

1 Answers1

4

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
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80