'use strict';

var fs = require('fs');
var chalk = require('chalk');
var path = require('path');
var titaniumifier = require('titaniumifier');

module.exports = function(grunt) {

  grunt.registerMultiTask('titaniumifier', 'Get a Titanium CommonJS Module out of your Node.js package using Grunt!', function() {

    var done = this.async();

    var options = this.options({
      manifest: undefined,
      package: undefined,
      bare: false
    });

    if (this.files.length > 1) {
      grunt.fail.fatal("Too many destinations/sources. Only one can be specified.");
    }

    var file = this.files[0];

    if (!file) {
      file = {
        src: ['.'],
        dest: '.'
      };
    }

    if (file.src.length > 1) {
      grunt.fail.fatal("Too many sources. Only one can be specified");
    }

    var entry = file.src[0];
    var dest = file.dest;
    
    if (!fs.existsSync(dest)) {
      fs.mkdirSync(dest);
    }

    if (!entry) {
      grunt.fail.fatal("Could not find the desired module to build!");
    }

    entry = path.resolve(entry);
    dest = path.resolve(dest);

    if (grunt.file.isFile(entry)) {
      grunt.fail.fatal("Only a directory can be used as source");
    }

    titaniumifier.packer.build({
      entry: entry,
      noDependencies: options.bare,
      manifest: options.manifest,
      package: options.package
    })
    .then(function (zip) {
      return zip.writeModule(dest);
    })
    .done(function () {
      grunt.log.ok("Module zip written in " + chalk.cyan(dest));
      done();
    }, function (err) {
      grunt.fail.fatal(err);
      done(false);
    });

  });

};

@@ -12,32 +12,8 @@
 ';%0A%0A
-var fs = require('fs');%0A
 var 
@@ -834,35 +834,71 @@
 st;%0A
+%0A
     
-%0A    if (!fs
+if (!grunt.file.isDir(dest)) %7B%0A      if (grunt.file
 .exists
-Sync
 (des
@@ -913,27 +913,124 @@
     
+ 
-fs.mkdirSync(dest);
+ grunt.fail.fatal(%22Destination must be a directory!%22);%0A      %7D%0A      else %7B%0A        grunt.file.mkdir(dest);%0A      %7D
 %0A   

Use Grunt APIs instead of fs