Adam
Adam

Reputation: 495

How do you run a bash script in metalsmith

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()?

Upvotes: 1

Views: 264

Answers (2)

pcothenet
pcothenet

Reputation: 401

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.

Upvotes: 0

James Khoury
James Khoury

Reputation: 22339

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);

Upvotes: 0

Related Questions