1

How do you get metalsmith to run a bash script? Can you even do that?

My build.js is pretty simple, but I want to delete something from the build folder after everything is compiled.

var Metalsmith = require('metalsmith'),
    copy       = require('metalsmith-copy'),
    define     = require('metalsmith-define'),
    markdown   = require('metalsmith-markdown'),
    permalinks = require('metalsmith-permalinks'),
    static     = require('metalsmith-static'),
    templates  = require('metalsmith-templates');



Metalsmith(__dirname)
    .source('./pages')
    .use(static(require('./config/assets')))
    .use(static(require('./config/rootFiles')))
    .use(define(require('./config/define')))
    .use(markdown())
    .use(permalinks())
    .use(templates(require('./config/templates')))
    .destination('./build')
    .build(function (err) {
        if (err) {
          throw err
        }
    })

So if I keep a bash script in config/cleanup.sh, how do I execute it after the .build()?

Adam
  • 475
  • 5
  • 18
  • Is there a reason to use a bash script over Node.js File IO ? – James Khoury Mar 24 '15 at 03:39
  • only that I am a greenhorn at node and I don't know how to use node.js file IO. I am just trying to delete a few folders in the build. Thanks for your response James – Adam Mar 24 '15 at 10:24
  • As much as I don't like not answering your question directly I really think that it might be a better approach. Could you come back and post an answer if you find a way around it? – James Khoury Mar 25 '15 at 01:09
  • It would be great if you could post the answer -- I posted here as I am stuck, not a naughty school child – Adam Mar 25 '15 at 12:09
  • 1
    usually I'd suggest you need to do your own research: http://stackoverflow.com/questions/5315138/node-js-remove-file – James Khoury Mar 25 '15 at 23:40
  • 1
    I think [rimraf](https://github.com/isaacs/rimraf) would be the easiest solution. Doing this from the shell is a lot more work when you want to trigger the action from a node script, rimraf (and fs) can do this for you. –  Jun 30 '16 at 13:03

2 Answers2

0

Node js fs utils

If you're just looking to delete files and folders then Node.js has filesystem utils that can do it for you:

var fs = require('fs');

// file
fs.unlink('/path/to/filename.txt', callback);
// directory
fs.rmdir('/path/to/dirname', callback);

Execute bash script (or other commands)

If you really want to run a bash script then child_process.exec might help you.
(example from: http://www.dzone.com/snippets/execute-unix-command-nodejs)

var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }

exec('./bash_script.sh', puts);
James Khoury
  • 21,330
  • 4
  • 34
  • 65
0

You could use https://github.com/pjeby/gulpsmith and use another gulp plugin (e.g. https://www.npmjs.com/package/gulp-clean) to delete your files.

pcothenet
  • 381
  • 3
  • 12