text: #!/usr/bin/env node
  // Generated by CoffeeScript 1.12.7
  var argv, baseDir, config, configPath, cson, datePrefix, defaultEndpoint, e, fs, i, len, loadConfig, needle, path, ref, ref1, script, updateScript, updateStoreInfo, validate, watch, watchOptions;
  
  fs = require('fs');
  
  path = require('path');
  
  cson = require('cson');
  
  needle = require('needle');
  
  watch = require('node-watch');
  
  validate = require('is-my-json-valid')(require('./configuration-schema.json'));
  
  argv = require('yargs').argv;
  
  defaultEndpoint = 'https://phantombuster.com/api/v1';
  
  configPath = argv.c || 'phantombuster.cson';
  
  datePrefix = function() {
    return (new Date).toLocaleTimeString() + ' - ';
  };
  
  loadConfig = function(configPath) {
    var account, config, envVar, i, len;
    config = cson.load(configPath);
    if (validate(config)) {
      for (i = 0, len = config.length; i < len; i++) {
        account = config[i];
        if (account.apiKey.indexOf("ENV:") === 0) {
          envVar = account.apiKey.replace("ENV:", "");
          account.apiKey = process.env[envVar];
          if ((typeof account.apiKey !== "string") || (account.apiKey.length < 10) || (account.apiKey.length > 50)) {
            console.log(account.name + ": Environment variable \"" + envVar + "\" does not contain a valid API key");
            process.exit(1);
          }
        }
      }
      return config;
    } else {
      console.log("" + (datePrefix()) + configPath + " is not a correct SDK configuration file");
      console.log(JSON.stringify(validate.errors));
      return process.exit(1);
    }
  };
  
  try {
    configPath = fs.realpathSync(configPath);
    baseDir = path.dirname(configPath);
    config = loadConfig(configPath);
    updateStoreInfo = function(updatedPath, fileType) {
      var account, i, jsonFile, len, linkedScriptCoffee, linkedScriptJs, localScript, mdFile, pbScript, ref, ref1, upload;
      linkedScriptJs = updatedPath.replace(new RegExp(fileType + '$'), 'js');
      linkedScriptCoffee = updatedPath.replace(new RegExp(fileType + '$'), 'coffee');
      mdFile = updatedPath.replace(new RegExp(fileType + '$'), 'md');
      jsonFile = updatedPath.replace(new RegExp(fileType + '$'), 'json');
      upload = function(account, pbScript, localScript) {
        return fs.readFile(jsonFile, function(err, jsonText) {
          var e, jsonErr;
          if (err && (err.code !== 'ENOENT')) {
            return console.log("" + (datePrefix()) + account.name + ": [API store settings] " + jsonFile + ": " + (err.toString()));
          } else {
            if (err) {
              jsonText = '';
            } else {
              try {
                JSON.parse(jsonText);
              } catch (error) {
                e = error;
                jsonErr = e;
              }
            }
            if (jsonErr) {
              return console.log("" + (datePrefix()) + account.name + ": [API store settings] " + jsonFile + ": " + (jsonErr.toString()));
            } else {
              return fs.readFile(mdFile, function(err, mdText) {
                var options, payload;
                if (err && (err.code !== 'ENOENT')) {
                  return console.log("" + (datePrefix()) + account.name + ": [API store settings] " + mdFile + ": " + (err.toString()));
                } else {
                  if (err) {
                    mdText = '';
                  }
                  options = {
                    headers: {
                      'X-Phantombuster-Key-1': account.apiKey
                    }
                  };
                  payload = {
                    infoString: jsonText.toString(),
                    markdown: mdText.toString()
                  };
                  return needle.post((account.endpoint || defaultEndpoint) + "/store-info/by-name/" + pbScript, payload, options, function(err, res) {
                    var ref, ref1, ref2;
                    if (err) {
                      return console.log("" + (datePrefix()) + account.name + ": [API store settings] " + localScript + ": " + (err.toString()));
                    } else {
                      if (((ref = res.body) != null ? ref.status : void 0) === 'success') {
                        return console.log("" + (datePrefix()) + account.name + ": [API store settings] " + localScript + " -> " + pbScript);
                      } else {
                        return console.log("" + (datePrefix()) + account.name + ": [API store settings] " + localScript + ": " + (((ref1 = res.body) != null ? ref1.status : void 0) != null ? res.body.status : "Error") + ": " + (((ref2 = res.body) != null ? ref2.message : void 0) != null ? res.body.message : "HTTP " + res.statusCode));
                      }
                    }
                  });
                }
              });
            }
          }
        });
      };
      for (i = 0, len = config.length; i < len; i++) {
        account = config[i];
        ref = account.scripts;
        for (pbScript in ref) {
          localScript = ref[pbScript];
          if ((ref1 = path.join(baseDir, localScript)) === linkedScriptJs || ref1 === linkedScriptCoffee) {
            upload(account, pbScript, localScript);
            return true;
          }
        }
      }
      return false;
    };
    updateScript = function(updatedPath) {
      var account, fileExt, i, len, localScript, pbScript, ref, upload;
      fileExt = path.extname(updatedPath);
      if (fileExt === '.md' || fileExt === '.json') {
        return updateStoreInfo(updatedPath, fileExt.replace('.', ''));
      } else {
        upload = function(account, pbScript, localScript, updatedPath) {
          return fs.readFile(updatedPath, function(err, text) {
            var options, payload;
            if (err) {
              return console.log("" + (datePrefix()) + account.name + ": " + localScript + ": " + (err.toString()));
            } else {
              options = {
                headers: {
                  'X-Phantombuster-Key-1': account.apiKey
                }
              };
              payload = {
                text: text.toString(),
                source: 'sdk'
              };
              return needle.post((account.endpoint || defaultEndpoint) + "/script/" + pbScript, payload, options, function(err, res) {
                var ref, ref1, ref2;
                if (err) {
                  return console.log("" + (datePrefix()) + account.name + ": " + localScript + ": " + (err.toString()));
                } else {
                  if (((ref = res.body) != null ? ref.status : void 0) === 'success') {
                    return console.log("" + (datePrefix()) + account.name + ": " + localScript + " -> " + pbScript + (typeof res.body.data === 'number' ? ' (new script created)' : ''));
                  } else {
                    return console.log("" + (datePrefix()) + account.name + ": " + localScript + ": " + (((ref1 = res.body) != null ? ref1.status : void 0) != null ? res.body.status : "Error") + ": " + (((ref2 = res.body) != null ? ref2.message : void 0) != null ? res.body.message : "HTTP " + res.statusCode));
                  }
                }
              });
            }
          });
        };
        for (i = 0, len = config.length; i < len; i++) {
          account = config[i];
          ref = account.scripts;
          for (pbScript in ref) {
            localScript = ref[pbScript];
            if (path.join(baseDir, localScript) === updatedPath) {
              upload(account, pbScript, localScript, updatedPath);
              return true;
            }
          }
        }
        return false;
      }
    };
    if ((ref = argv._) != null ? ref.length : void 0) {
      ref1 = argv._;
      for (i = 0, len = ref1.length; i < len; i++) {
        script = ref1[i];
        if (!updateScript(fs.realpathSync(script))) {
          console.log("" + (datePrefix()) + script + ": Not found in configuration");
        }
      }
    } else {
      watchOptions = {
        recursive: true,
        filter: (function(f) {
          return !/node_modules/.test(f);
        })
      };
      watch(baseDir, watchOptions, function(event, updatedPath) {
        if (event === "update") {
          if (updatedPath === configPath) {
            config = loadConfig(updatedPath);
            return console.log("" + (datePrefix()) + updatedPath + ": Configuration reloaded");
          } else {
            return updateScript(updatedPath);
          }
        }
      });
    }
  } catch (error) {
    e = error;
    console.log(e.toString());
    process.exit(1);
  }
  