#!/usr/bin/env node
var replace = require('replace-in-file');
var fs = require('fs');
var isStop = process.argv.slice(2).indexOf('--stop') !== -1;

// path to angular-meteor-data package
var path = 'packages/angular-meteor-data/';
// package definition
var packageFile = path + 'package.js';
// angular-meteor file
var npmFile = '.npm/package/node_modules/angular-meteor/dist/angular-meteor.js';
var devFile = 'angular-meteor.js';

// replace options
var options = {
  files: packageFile,
  replace: isStop ? devFile : npmFile,
  with: isStop ? npmFile : devFile
};

// clean up first
try {
  if (fs.accessSync(path + devFile, fs.F_OK)) {
    fs.unlinkSync(path + devFile);
  }
} catch(e) {}

// make sure package uses proper file
replace(options);

@@ -78,16 +78,106 @@
 e('fs');
+%0A%0Afunction wrapInQuotes(file) %7B%0A  return '%5C'' + file + '%5C'';%0A%7D%0A%0A// look up for --stop flag
 %0Avar isS
@@ -583,16 +583,29 @@
 eplace: 
+wrapInQuotes(
 isStop ?
@@ -618,24 +618,25 @@
 le : npmFile
+)
 ,%0A  with: is
@@ -633,16 +633,29 @@
   with: 
+wrapInQuotes(
 isStop ?
@@ -672,16 +672,17 @@
  devFile
+)
 %0A%7D;%0A%0A// 

improve patterns in scripts/dev