Example #1
0
//load configuration file; fallback to global if is not an initialized folder
function loadConfiguration() {
    var confPosition = "";

    if (isInit()) {
        confPosition = "local";
        configuration = jsonReader('./.deploy/configuration.json');
        gConf = jsonReader(__dirname + '/../conf/configuration.json');
    }
    else {
        confPosition = "global";
        configuration = jsonReader(__dirname + '/../conf/configuration.json');
    }
    return confPosition;
}
Example #2
0
function Component(options) {

  // options = {
  //   name: '',
  //   dir: ''
  // }
  assign(this, options);

  // Set the path
  this.path = path.resolve(this.dir, this.name);

  // Get this bower.json
  var bowerFile = isSymlink(this.path) ? 'bower.json' : this.componentJson;
  this.json = json(path.resolve(this.path, bowerFile));

  // Apply the overrides
  assign(this.json, this.overrides[this.name]);

  if (!this.json.main) {
    throw new Error(this.name + ' has no main property');
  }

  // Get the main files
  this.files = mainFiles(this.json.main, this.path);

  // Get the dependencies
  this.dependencies = getDeps(this.json.dependencies, {
    dir: this.dir,
    overrides: this.overrides,
    componentJson: this.componentJson
  });

}
Example #3
0
  it( 'should remove a key from the cache object and persist the change', function () {
    var cache = flatCache.load( 'someId' );
    var data = {
      foo: 'foo',
      bar: 'bar'
    };

    cache.setKey( 'some-key', data );
    expect( cache.getKey( 'some-key' ) ).to.deep.equal( data );
    cache.save();

    cache.removeKey( 'some-key' );
    expect( readJSON( path.resolve( __dirname, '../../.cache/someId' ) )[ 'some-key' ], 'value is still in the persisted storage' ).to.deep.equal( data );

    cache.save();
    expect( readJSON( path.resolve( __dirname, '../../.cache/someId' ) )[ 'some-key' ] ).to.be.undefined
  } );
Example #4
0
 it( 'should remove the key and persist the new state', function () {
   var cache = flatCache.load( 'someId' );
   cache.removeKey( 'foo' );
   cache.save();
   expect( readJSON( path.resolve( __dirname, '../../.cache/someId' ) ) ).to.deep.equal( {
     bar: {
       foo: 'baz'
     }
   } );
 } );
Example #5
0
var readJSONSync = function ( folder, name ) {
  name = path.basename( name, '.js' );
  var filePath = path.join( './specs', folder, name + '.json' );
  var json = { };
  try {
    json = readJSON( filePath );
  } catch (ex) {
    //console.error('error reading file ', name, ex.message);
  }
  return json;
};
Example #6
0
  it( 'should set a key and persist it', function () {
    var cache = flatCache.load( 'someId' );
    var data = {
      foo: 'foo',
      bar: 'bar'
    };

    cache.setKey( 'some-key', data );
    expect( cache.getKey( 'some-key' ) ).to.deep.equal( data );

    cache.save();
    expect( readJSON( path.resolve( __dirname, '../../.cache/someId' ) )[ 'some-key' ] ).to.deep.equal( data );
  } );
function Component(options) {

  options = normalizeOptions(options);

  try {
    this.json = readJson(path.resolve(options.dir, options.json));
  } catch (err) {
    this.json = {};
  }

  this.name = options.isRoot // TODO deprecate 'self' name
    ? 'self'
    : this.json.name || path.basename(options.dir);
  options.overrides = assign({}, this.json.overrides, options.overrides);
  assign(this.json, options.overrides[this.name]);

  this.files = Component.mainFiles(options.dir, this.json.main);

  this.dependencies = getDependencies(this.json.dependencies, options);
  if (options.isRoot) {
    this.devDependencies = getDependencies(this.json.devDependencies, options);
  }

  Object.defineProperties(this, {
    dir: {
      get: util.deprecate(function () {
        return options.dependencyDir;
      }, 'component.dir: access to dir is deprecated')
    },
    path: {
      get: util.deprecate(function () {
        return options.dir;
      }, 'component.path: access to path is deprecated')
    }
  });
}
Example #8
0
  install: function ( args ) {
    var sFormat = require( 'stringformat' );
    var path = require( 'path' );
    var process = require( './process' );
    var options = require( './options' );
    var chalk = require( 'chalk' );

    var readJSON = require( 'read-json-sync' );

    var opts = options.parse( args );

    if ( opts.help ) {
      console.log( options.generateHelp() );
      return;
    }

    if ( opts.version ) {
      console.log( chalk.bold( readJSON( path.resolve( this.dirname, '../package.json' ) ).version ) );
      return;
    }

    var command = opts._.join( '' );
    if ( !command ) {
      console.error( chalk.red( 'Missing command, please indicate "install" or "remove"\n\n' ) );
      console.log( options.generateHelp() );
      return;
    }

    var hook = require( './hook' );

    if ( command === 'install' ) {
      if ( !opts.config ) {
        console.log( chalk.gray( 'config not provided, trying to use the package.json at current working directory' ) );
        opts.config = './package.json';
      }

      var configFile = path.resolve( process.cwd(), opts.config );
      var fs = require( 'fs' );
      var cfg = {};
      if ( fs.existsSync( configFile ) ) {
        cfg.configFile = configFile;
      } else {
        console.error( chalk.red( 'Could not find config file' ), opts.config );
        return;
      }

      try {
        hook.install( cfg ).then( function ( pathToFile ) {
          console.log( chalk.gray( sFormat( '>> Prepush file copied to: {0}', pathToFile ) ) );
          console.log( chalk.yellow( '>> Done!' ) );
        } );
      } catch (ex) {
        console.error( ex.stack );
        throw new Error( sFormat( 'Error installing prepush hook, Error: {0}', ex.message ) );
      }
    }
    if ( command === 'remove' ) {

      try {
        hook.remove().then( function ( pathToFile ) {
          console.log( chalk.yellow( '>> prepush file removed:' ), pathToFile );
        } );
      } catch (ex) {
        console.error( ex.stack );
        throw new Error( sFormat( 'Error installing prepush hook, Error: {0}', ex.message ) );
      }
    }
  }
Example #9
0
    .action(function(dest, options) {
        var working;
        onlyInit();
        var servers = jsonReader('./.deploy/servers.json');

        if (servers[dest] === undefined) {
            sendError('E010', dest);
        }
        print('I will deploy to: ' + dest, 'important', 'date');
        print(spacer22 + 'Selected options:', 'important');
        print(spacer22 + "Disable websockets: " + ((options.nows || configuration.disableWebsockets) ? "true" : "false"), 'important');
        print(spacer22 + "Deploy type: " + configuration.deployType + "\n", 'important');
        process.stdout.write(chalk.inverse.bold(spacer22 + "Do you want to proceed? [Y/n]") + " ");

        process.stdin.on('readable', function() {
            var answer = process.stdin.read();
            if (answer !== null) {
                answer = answer.toString().replace(/\r?\n|\r/g, "");
                if (answer.toLowerCase() !== 'y') process.exit(0);
                else {
                    process.stdout.write("\n");
                    //cache current destination config
                    var serverConf = servers[dest];
                    var sshConnection = "ssh " + serverConf.sshUser + "@" + serverConf.sshAddress;

                    print("Let's start deploy!", "completed", "date");
                    replaceInFiles(serverConf, function() {
                        //creates meteor bundle
                        var buildCommand = "meteor build ./.deploy/tmp";
                        working = spinner();
                        if ((configuration.serverArchitecture !== undefined) && (configuration.serverArchitecture !== "undefined")) buildCommand = buildCommand + " --architecture " + configuration.serverArchitecture;
                        
                        print("creating meteor package...", "important", "spaced22");
                        exec(buildCommand, function(error, stdout, stderr) {
                            if (error) sendProcessError(stdout, stderr);

                            clearInterval(working);
                            process.stdout.write("\r" + spacer22 + "- 100%\n");
                            print("meteor package created", "completed", "date");
                            //copy files to server
                            exec(sshConnection + " mkdir -p " + serverConf.sshUserHome + "/deployTmp", function(error, stdout, stderr) {
                                if (error) sendProcessError(stdout, stderr);

                                print("sending app to " + dest + " server...\n" + spacer22 + "(this will take a while)", "important", "spaced22");
                                working = spinner();
                                exec("scp -r ./.deploy/tmp " + serverConf.sshUser + "@" + serverConf.sshAddress + ":" + serverConf.sshUserHome + "/deployTmp", function(error, stdout, stderr) {
                                    if (error) sendProcessError(stdout, stderr);

                                    clearInterval(working);
                                    process.stdout.write("\r" + spacer22 + "- 100%\n");
                                    print("files copied to server\n", "completed", "date");
                                    
                                    //launch remote script to install everything
                                    print("launching remote installation...", "important", "date");
                                    var depScript = "deploy-" + configuration.deployType + ".sh";
                                    var ws = "";
                                    if ((options.nows) || (configuration.disableWebsockets)) ws = " --nows";
                                    var deployCommand = sshConnection + " chmod +x " + serverConf.sshUserHome + "'/deployTmp/tmp/scripts/" + depScript + " && " + serverConf.sshUserHome + "/deployTmp/tmp/scripts/" + depScript + ws + "'";
                                    exec(deployCommand, function(error, stdout, stderr) {
                                        if (error) sendProcessError(stdout, stderr);

                                        console.log("");
                                        print("--- REMOTE EXECUTION: stdout ---", "stdout");
                                        console.log("");
                                        console.log(cleanStdout(stdout, "meteorDeploy"));
                                        console.log("");
                                        print("--- REMOTE EXECUTION: end ---", "stdout");
                                        console.log("");

                                        print("remote installation done\n", "completed", "date");
                                        print("cleaning...", "important", "spaced22");
                                        //clean remote tmp folder
                                        exec(sshConnection + " rm -Rf " + serverConf.sshUserHome + "/deployTmp", function(error, stdout, stderr) {
                                            if (error) sendProcessError(stdout, stderr);

                                            print("Deploy completed!", "completed", "date");
                                            process.exit(0);
                                        });
                                    });
                                });
                            });
                        });
                    });
                }
            }
        });
    });
Example #10
0
//initialize current directory
function init(type) {
    var files = jsonReader(__dirname + '/files.json');

    if (type !== undefined) files = files[type];
    else {
        //if no runtime override provided, take files from global configuration
        if (gConf !== undefined) files = files[gConf.deployType];  //gConf -> global config file in case of local file already there
        else files = files[configuration.deployType];  //configuration -> global config file in case of first initialization
    }

    if (files === undefined) {
        sendError('E020');
    }

    print("Initializing folder...", 'important', 'date');

    //build assets copy command
    var assetsCpCmd = "mkdir ./.deploy/assets && cp";
    for (i = 0; i < files.assets.length; i++) {
        assetsCpCmd = assetsCpCmd + " " + __dirname + "/../assets/" + files.assets[i];
    }
    assetsCpCmd = assetsCpCmd + " ./.deploy/assets";

    //build scripts copy command
    var scriptsCpCmd = "mkdir ./.deploy/scripts && cp";
    for (i = 0; i < files.scripts.length; i++) {
        scriptsCpCmd = scriptsCpCmd + " " + __dirname + "/../scripts/" + files.scripts[i];
    }
    scriptsCpCmd = scriptsCpCmd + " ./.deploy/scripts";

    //Save the local configuration file
    function writeConfigFile() {
        fs.writeFileSync('./.deploy/configuration.json', JSON.stringify(configuration, null, 4));
        print("local configuration file written", "completed", "date");

        print("folder initialized", 'completed', 'date');
        print("now fill ./deploy/servers.json with the needed parameters", "completed", 'spaced22');
        print("and then run deploy to <destination> command\n", 'completed', 'spaced22');
        process.exit(0);
    }

    //specific type setup for local configuration file
    function specificSetup(type) {
        switch (type) {
            case 'meteor':
            //set default server architecture
            configuration.serverArchitecture = "undefined"; //will use current machine's architecture
            configuration.disableWebsockets = "false";
            break; 
        }
    }

    //do it
    exec('mkdir -p ./.deploy', function(error, stdout, stderr) {
        if (error) sendProcessError(stdout, stderr, revertInit());

        exec(assetsCpCmd, function(error, stdout, stderr) {
            if (error) sendProcessError(stdout, stderr, revertInit());

            exec(scriptsCpCmd, function(error, stdout, stderr) {
                if (error) sendProcessError(stdout, stderr, revertInit());

                exec("cp " + __dirname + "/../conf/* ./.deploy", function(error, stdout, stderr) {
                    if (error) sendProcessError(stdout, stderr, revertInit());

                    exec('mkdir -p ./.deploy/dump', function(error, stdout, stderr) {
                        if (error) sendProcessError(stdout, stderr, revertInit());

                        //if runtime type override is supplied -> overwrite local configuration file (just created from global one)
                        if (type !== undefined) {
                            if ((gConf !== undefined && gConf.deployType !== type) || (gConf === undefined && configuration.deployType !== type)) {
                                configuration.deployType = type;
                            }
                        }
                        specificSetup(type);
                        //get current app's name
                        exec("printf '%s' \"${PWD##*/}\"", function(error, stdout, stderr) {
                            if (error) sendProcessError(stdout, stderr, revertInit());

                            configuration.appName = stdout;
                            writeConfigFile();
                        });
                    });
                });
            });
        });
    });
}
Example #11
0
 it( 'should load an existing cache', function () {
   var cache = flatCache.load( 'someId' );
   expect( readJSON( path.resolve( __dirname, '../../.cache/someId' ) ) ).to.deep.equal( cache._persisted );
 } );