#!/usr/bin/env node

'use strict';

var fs = require('fs'),
    astdiff = require('./astdiff'),
    shell = require('shelljs'),
    astii = require('commander');

astii
    .version('0.0.1');

astii
    .command('patch <file1> <patchfile>')
    .description('apply an astii-generated diff file to an original in an AST-aware way')
    .option('-P, --preserve', 'preserve code formatting (exprimental)', 'public')
    .action(function(file1, patchfile, cmd) {
        console.log(file1 + ' ' + patchfile);
        var source1 = fs.readFileSync(file1);
        var patch = fs.readFileSync(patchfile).toString();
        if (cmd.preserve) {
            console.log('preserve');
        } else {
            console.log(astdiff.patch(source1, patch));
        }
    });

astii
    .command('diff <file1> <file2>')
    .description('compare AST-neutral representations of two JavaScript files line by line')
    .action(function(file1, file2) {
        console.log(file1 + ' ' + file2);
        var source1 = fs.readFileSync(file1);
        var source2 = fs.readFileSync(file2);
        console.log(astdiff.diff(source1, source2));
    });

astii
    .command('git-diff <file1> <SHA>')
    .description('compare AST-neutral representations of a JavaScript files against its specified git revision')
    .action(function(file1, SHA) {
        var source1 = fs.readFileSync(file1);
        var source2 = getFileForSha(file1, SHA);
        console.log(astdiff.diff(source1, source2, file1));
    });

astii
    .command('git-diff-version <file1> <SHA1> <SHA2>')
    .description('compare AST-neutral representations of a JavaScript file between two git revisions')
    .action(function(file1, SHA1, SHA2) {
        var source1 = getFileForSha(file1, SHA1);
        var source2 = getFileForSha(file1, SHA2);
        console.log(astdiff.diff(source1, source2, file1));
    });

var getFileForSha = function(filename, sha) {
    checkGit();
    return shell.exec('git show  ' + sha + ':' + filename, {
        silent: true
    }).output;
};

var checkGit = function() {
    if (!shell.which('git')) {
        shell.echo('Sorry, this command requires git');
        shell.exit(1);
    }
};

astii.parse(process.argv);

@@ -325,90 +325,36 @@
  way
-')%0A    .option('-P, --preserve', 'preserve code formatting (exprimental)', 'public
+, losing original formatting
 ')%0A 
@@ -402,54 +402,8 @@
 ) %7B%0A
-        console.log(file1 + ' ' + patchfile);%0A
     
@@ -515,86 +515,389 @@
     
-if (cmd.preserve) %7B%0A            console.log('preserve
+console.log(astdiff.patch(source1, patch));%0A    %7D);%0Aastii%0A    .command('patchPreserve %3Cfile1%3E %3Cpatchfile%3E')%0A    .description('apply an astii-generated diff file to an original in an AST-aware way, preserving original formatting
 ')
-;
 %0A    
-    %7D else %7B%0A    
+.action(function(file1, patchfile, cmd) %7B%0A        var source1 = fs.readFileSync(file1);%0A        var patch = fs.readFileSync(patchfile).toString();%0A
     
@@ -917,32 +917,40 @@
 og(astdiff.patch
+Preserve
 (source1, patch)
@@ -956,26 +956,16 @@
 ));%0A
-        %7D%0A
     %7D);%0A
 %0Aast
@@ -952,33 +952,32 @@
 atch));%0A    %7D);%0A
-%0A
 astii%0A    .comma
@@ -1137,50 +1137,8 @@
 ) %7B%0A
-        console.log(file1 + ' ' + file2);%0A
     

Add patchPreserve command to cli