it('should load from filesystem', () => {
      const SUtils = new Utils();
      const serverlessYml = {
        service: 'new-service',
        provider: 'aws',
        defaults: {
          stage: 'dev',
          region: 'us-east-1',
          variableSyntax: '\\${{([\\s\\S]+?)}}',
        },
        plugins: ['testPlugin'],
        functions: {
          functionA: {},
        },
        resources: {
          aws: {
            resourcesProp: 'value',
          },
          azure: {},
          google: {},
        },
        package: {
          exclude: ['exclude-me.js'],
          artifact: 'some/path/foo.zip',
        },
      };

      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
        YAML.dump(serverlessYml));

      const serverless = new Serverless();
      serverless.init();
      serverless.config.update({ servicePath: tmpDirPath });
      serviceInstance = new Service(serverless);

      return serviceInstance.load().then(() => {
        expect(serviceInstance.service).to.be.equal('new-service');
        expect(serviceInstance.provider.name).to.deep.equal('aws');
        expect(serviceInstance.defaults.variableSyntax).to.equal('\\${{([\\s\\S]+?)}}');
        expect(serviceInstance.plugins).to.deep.equal(['testPlugin']);
        expect(serviceInstance.resources.aws).to.deep.equal({ resourcesProp: 'value' });
        expect(serviceInstance.resources.azure).to.deep.equal({});
        expect(serviceInstance.resources.google).to.deep.equal({});
        expect(serviceInstance.package.exclude.length).to.equal(1);
        expect(serviceInstance.package.exclude[0]).to.equal('exclude-me.js');
        expect(serviceInstance.package.artifact).to.equal('some/path/foo.zip');
      });
    });
Example #2
0
    it('should reject when the service name is missing', () => {
      const SUtils = new Utils();
      const serverlessYaml = {
        service: {},
        provider: 'aws',
      };

      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yaml'),
        YAML.dump(serverlessYaml));

      const serverless = new Serverless({ servicePath: tmpDirPath });
      serviceInstance = new Service(serverless);

      return expect(serviceInstance.load()).to.eventually.be
        .rejectedWith('"service" is missing the "name" property in');
    });
    it('should not throw error if functions property is missing', () => {
      const SUtils = new Utils();
      const serverlessYml = {
        service: 'service-name',
        provider: 'aws',
      };
      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
        YAML.dump(serverlessYml));

      const serverless = new Serverless({ servicePath: tmpDirPath });
      serviceInstance = new Service(serverless);

      return serviceInstance.load().then(() => {
        expect(serverless.service.functions).to.deep.equal({});
      });
    });
Example #4
0
    it('should reject if provider property is invalid', () => {
      const SUtils = new Utils();
      const serverlessYml = {
        service: 'service-name',
        provider: 'invalid',
        functions: {},
      };

      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
        YAML.dump(serverlessYml));

      const serverless = new Serverless({ servicePath: tmpDirPath });
      serviceInstance = new Service(serverless);

      return expect(serviceInstance.load()).to.eventually.be
        .rejectedWith('Provider "invalid" is not supported.');
    });
Example #5
0
  cli.fs.registerSerializer('yaml', function (obj, opts) {
    opts = opts || {}

    var copy = {}

    Object.getOwnPropertyNames(obj).forEach(function (prop) {
      if (typeof obj[prop] !== 'undefined') {
        copy[prop] = obj[prop]
      }
    })

    if (opts.unsafe) {
      return yaml.dump(copy, opts)
    } else {
      return yaml.safeDump(copy, opts)
    }
  })
Example #6
0
  var write = function(locale) {
    var stats, target, tmp;

    // don't write new locale information to disk if updateFiles isn't true
    if (!updateFiles) {
      return;
    }

    // creating directory if necessary
    try {
      stats = fs.lstatSync(directory);
    } catch (e) {
      logDebug('creating locales dir in: ' + directory);
      fs.mkdirSync(directory, directoryPermissions);
    }

    // first time init has an empty file
    if (!locales[locale]) {
      locales[locale] = {};
    }

    // writing to tmp and rename on success
    try {
      target = getStorageFilePath(locale);
      tmp = target + '.tmp';
      var fileContents = '';
      switch (extension) {
        case '.yml':
          fileContents = jsyaml.dump(locales[locale]);
            break;
        default:
          fileContents = JSON.stringify(locales[locale], null, indent);
      }
      fs.writeFileSync(tmp, fileContents, "utf8");
      stats = fs.statSync(tmp);
      if (stats.isFile()) {
        fs.renameSync(tmp, target);
      } else {
        logError('unable to write locales to file (either ' +
          tmp + ' or ' + target + ' are not writeable?): ');
      }
    } catch (e) {
      logError('unexpected error writing files (either ' +
        tmp + ' or ' + target + ' are not writeable?): ', e);
    }
  };
Example #7
0
    it('should load YAML in favor of JSON', () => {
      const SUtils = new Utils();
      const serverlessJSON = {
        provider: {
          name: 'aws',
          stage: 'dev',
          region: 'us-east-1',
          variableSyntax: '\\${{([\\s\\S]+?)}}',
        },
        plugins: ['testPlugin'],
        functions: {
          functionA: {},
        },
        resources: {
          aws: {
            resourcesProp: 'value',
          },
          azure: {},
          google: {},
        },
        package: {
          exclude: ['exclude-me'],
          include: ['include-me'],
          artifact: 'some/path/foo.zip',
        },
      };

      serverlessJSON.service = 'JSON service';
      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.json'),
        JSON.stringify(serverlessJSON));

      serverlessJSON.service = 'YAML service';
      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yaml'),
        YAML.dump(serverlessJSON));

      const serverless = new Serverless();
      serverless.init();
      serverless.config.update({ servicePath: tmpDirPath });
      serviceInstance = new Service(serverless);

      return expect(serviceInstance.load()).to.eventually.be.fulfilled
      .then(() => {
        // YAML should have been loaded instead of JSON
        expect(serviceInstance.service).to.be.equal('YAML service');
      });
    });
    it('should push the plugin to the service files plugin array if present', () => {
      // serverless.yml
      const serverlessYml = {
        service: 'plugin-service',
        provider: 'aws',
        plugins: ['serverless-existing-plugin'], // one plugin was already added
      };
      serverless.utils
        .writeFileSync(serverlessYmlFilePath, YAML.dump(serverlessYml));

      pluginInstall.options.pluginName = 'serverless-plugin-1';

      return expect(pluginInstall.addPluginToServerlessFile()).to.be.fulfilled.then(() => {
        expect(serverless.utils.readFileSync(serverlessYmlFilePath, 'utf8').plugins)
          .to.deep.equal(['serverless-existing-plugin', 'serverless-plugin-1']);
      });
    });
    it('should add the plugin to the service file if plugins array is not present', () => {
      // serverless.yml
      const serverlessYml = {
        service: 'plugin-service',
        provider: 'aws',
        // no plugins array here
      };
      serverless.utils
        .writeFileSync(serverlessYmlFilePath, YAML.dump(serverlessYml));

      pluginInstall.options.pluginName = 'serverless-plugin-1';

      return expect(pluginInstall.addPluginToServerlessFile()).to.be.fulfilled.then(() => {
        expect(serverless.utils.readFileSync(serverlessYmlFilePath, 'utf8').plugins)
          .to.deep.equal(['serverless-plugin-1']);
      });
    });
Example #10
0
File: build.js Project: Vanuan/iD
function generatePresets() {
    var presets = {};
    glob.sync(__dirname + '/data/presets/presets/**/*.json').forEach(function(file) {
        var preset = read(file),
            id = file.match(/presets\/presets\/([^.]*)\.json/)[1];

        validate(file, preset, presetSchema);

        translations.presets[id] = {
            name: preset.name,
            terms: (preset.terms || []).join(',')
        };

        presets[id] = preset;
    });
    fs.writeFileSync('data/presets/presets.json', stringify(presets));
    fs.writeFileSync('data/presets.yaml', YAML.dump({en: {presets: translations}}));
}
Example #11
0
  writeFileSync(filePath, conts) {
    let contents = conts || '';

    fse.mkdirsSync(path.dirname(filePath));

    if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
      contents = JSON.stringify(contents, null, 2);
    }

    const yamlFileExists = (filePath.indexOf('.yaml') !== -1);
    const ymlFileExists = (filePath.indexOf('.yml') !== -1);

    if ((yamlFileExists || ymlFileExists) && typeof contents !== 'string') {
      contents = YAML.dump(contents);
    }

    return fse.writeFileSync(filePath, contents);
  }
Example #12
0
    .then(() => {
      if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
        if (cycles) {
          contents = jc.stringify(contents, null, 2);
        } else {
          contents = JSON.stringify(contents, null, 2);
        }
      }

      const yamlFileExists = (filePath.indexOf('.yaml') !== -1);
      const ymlFileExists = (filePath.indexOf('.yml') !== -1);

      if ((yamlFileExists || ymlFileExists) && typeof contents !== 'string') {
        contents = YAML.dump(contents);
      }

      return fse.writeFileAsync(filePath, contents);
    });
Example #13
0
    it('should pass if frameworkVersion is satisfied', () => {
      const SUtils = new Utils();
      const serverlessYml = {
        service: 'service-name',
        frameworkVersion: '>=1.0.0',
        provider: 'aws',
        functions: {},
      };
      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
        YAML.dump(serverlessYml));

      const serverless = new Serverless({ servicePath: tmpDirPath });
      const getVersion = sinon.stub(serverless.utils, 'getVersion');
      getVersion.returns('1.2.2');
      serviceInstance = new Service(serverless);

      return expect(serviceInstance.load()).to.eventually.be.fulfilled;
    });
Example #14
0
    it('should reject if frameworkVersion is not satisfied', () => {
      const SUtils = new Utils();
      const serverlessYml = {
        service: 'service-name',
        frameworkVersion: '=1.0.0',
        provider: 'aws',
        functions: {},
      };
      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
        YAML.dump(serverlessYml));

      const serverless = new Serverless({ servicePath: tmpDirPath });
      const getVersion = sinon.stub(serverless.utils, 'getVersion');
      getVersion.returns('1.0.2');
      serviceInstance = new Service(serverless);

      return expect(serviceInstance.load()).to.eventually.be
        .rejectedWith(/version \(1\.0\.2\).*"frameworkVersion" \(=1\.0\.0\)/);
    });
Example #15
0
  onTapAssert: function(data) {
    log.info(data);
    if (data.skip) {
      return;
    }

    if (data.id === undefined) {
      return;
    }

    var test = {
      passed: 0,
      failed: 0,
      total: 1,
      id: data.id,
      name: data.name ? data.name.trim() : '',
      items: []
    };

    if (!data.ok) {
      var stack;
      if (data.diag) {
        stack = data.diag.stack || data.diag.at;
      }
      if (stack) {
        stack = yaml.dump(stack);
      }
      if (!stack) {
        stack = this.stack.length > 0 ? this.stack.join('\n') : null;
      }
      data = extend(data, data.diag);
      test.items.push(extend(data, {
        passed: false,
        stack: stack
      }));
      test.failed++;
    } else {
      test.passed++;
    }
    this.stack = [];
    this.emit('test-result', test);
  },
Example #16
0
    it('should make sure function name contains the default stage', () => {
      const SUtils = new Utils();
      const serverlessYml = {
        service: 'new-service',
        provider: {
          name: 'aws',
          stage: 'dev',
          region: 'us-east-1',
          variableSyntax: '\\${{([\\s\\S]+?)}}',
        },
        plugins: ['testPlugin'],
        functions: {
          functionA: {},
        },
        resources: {
          aws: {
            resourcesProp: 'value',
          },
          azure: {},
          google: {},
        },
        package: {
          exclude: ['exclude-me'],
          include: ['include-me'],
          artifact: 'some/path/foo.zip',
        },
      };

      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
        YAML.dump(serverlessYml));

      const serverless = new Serverless();
      serverless.init();
      serverless.config.update({ servicePath: tmpDirPath });
      serviceInstance = new Service(serverless);

      return expect(serviceInstance.load()).to.eventually.be.fulfilled
      .then(() => {
        serviceInstance.setFunctionNames();
        expect(serviceInstance.functions.functionA.name).to.be.equal('new-service-dev-functionA');
      });
    });
Example #17
0
    it('should not install the plugin if it can not be found in the registry', () => {
      // serverless.yml
      const serverlessYml = {
        service: 'plugin-service',
        provider: 'aws',
      };
      serverless.utils
        .writeFileSync(serverlessYmlFilePath, YAML.dump(serverlessYml));

      pluginInstall.options.name = 'serverless-not-available-plugin';
      return expect(pluginInstall.install()).to.be.rejected.then(() => {
        expect(validateStub.calledOnce).to.equal(true);
        expect(getPluginsStub.calledOnce).to.equal(true);
        expect(pluginInstallStub.calledOnce).to.equal(false);
        expect(consoleLogStub.called).to.equal(false);
        expect(serverlessErrorStub.calledOnce).to.equal(true);
        expect(addPluginToServerlessFileStub.calledOnce).to.equal(false);
        expect(installPeerDependenciesStub.calledOnce).to.equal(false);
      });
    });
Example #18
0
    it('should throw error if functions property is missing', () => {
      const SUtils = new Utils();
      const serverlessYml = {
        service: 'service-name',
        provider: 'aws',
      };
      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
        YAML.dump(serverlessYml));

      const serverless = new Serverless({ servicePath: tmpDirPath });
      serviceInstance = new Service(serverless);

      return serviceInstance.load().then(() => {
        // if we reach this, then no error was thrown as expected
        // so make assertion fail intentionally to let us know something is wrong
        expect(1).to.equal(2);
      }).catch(e => {
        expect(e.name).to.be.equal('ServerlessError');
      });
    });
    it('should pass if frameworkVersion is satisfied', () => {
      const SUtils = new Utils();
      const serverlessYml = {
        service: 'service-name',
        frameworkVersion: '>=1.0.0',
        provider: 'aws',
        functions: {},
      };
      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
        YAML.dump(serverlessYml));

      const serverless = new Serverless({ servicePath: tmpDirPath });
      const getVersion = sinon.stub(serverless.utils, 'getVersion');
      getVersion.returns('1.2.2');
      serviceInstance = new Service(serverless);

      return serviceInstance.load().then(() => {
        // if this callbar is reached, then no error was thrown as expected
        expect(1).to.equal(1);
      });
    });
    it('should populate an entire variable file', () => {
      const serverless = new Serverless();
      const SUtils = new Utils();
      const tmpDirPath = testUtils.getTmpDirPath();
      const configYml = {
        test: 1,
        test2: 'test2',
        testObj: {
          sub: 2,
          prob: 'prob',
        },
      };

      SUtils.writeFileSync(path.join(tmpDirPath, 'config.yml'),
        YAML.dump(configYml));

      serverless.config.update({ servicePath: tmpDirPath });

      const valueToPopulate = serverless.variables.getValueFromFile('file(./config.yml)');
      expect(valueToPopulate).to.deep.equal(configYml);
    });
    it('should throw error if not using ":" syntax', () => {
      const serverless = new Serverless();
      const SUtils = new Utils();
      const tmpDirPath = testUtils.getTmpDirPath();
      const configYml = {
        test: 1,
        test2: 'test2',
        testObj: {
          sub: 2,
          prob: 'prob',
        },
      };

      SUtils.writeFileSync(path.join(tmpDirPath, 'config.yml'),
        YAML.dump(configYml));

      serverless.config.update({ servicePath: tmpDirPath });

      expect(() => serverless.variables
        .getValueFromFile('file(./config.yml).testObj.sub')).to.throw(Error);
    });
Example #22
0
/**
 * Loads a profile rule file, resolving all its includes
 * All the included rule files are expected to be in the directory
 * as the rule file.
 * Cyclic includes (e.g. file A includes file B which includes file C which includes file A) are not supported.
 *
 * @param ruleFilePath
 * @returns a merged rule profile object of the profile and all its includes
 */
function load(ruleFilePath) {
    //First get the base rules
    var baseRuleLocation = fs.readFileSync(config.BASE_RULES, 'UTF-8');
    var rules = yamlParser.safeLoad(baseRuleLocation);
    if (!ruleFilePath) {
        ruleFilePath = config.DEFAULT_RULES;
    }
	var includedRuleProfiles = [];
        var ruleDirectory = path.dirname(ruleFilePath); //require all rules to be in same dir
        var ruleFileName = path.basename(ruleFilePath);
        logger.debug("Rule directory is " + ruleDirectory);
        loadRuleFile(ruleFileName, ruleDirectory, includedRuleProfiles);
	logger.debug("Number of rule files found for " + ruleFileName + " " + includedRuleProfiles.length);
        includedRuleProfiles.reverse();
        includedRuleProfiles.forEach(function (profile) {
            delete profile.sourceFilename; // no longer needed part of hack!
            extend(rules, profile);
        });
        logger.debug("Effective rule set is :\n" + yamlParser.dump(rules));
        return rules;
}
 setAppsYaml: function() {
     this.appsYaml = [];
     for (var i = 0; i < this.appsFolders.length; i++) {
         var parentConfiguration = {};
         var path = this.destinationPath(this.directoryPath + this.appsFolders[i]);
         // Add application configuration
         var yaml = jsyaml.load(this.fs.read(path + '/src/main/docker/app.yml'));
         var yamlConfig = yaml.services[this.appConfigs[i].baseName.toLowerCase() + '-app'];
         parentConfiguration[this.appConfigs[i].baseName.toLowerCase() + '-app'] = yamlConfig;
         // Add database configuration
         var database = this.appConfigs[i].prodDatabaseType;
         if (database != 'no') {
             var databaseYaml = jsyaml.load(this.fs.read(path + '/src/main/docker/' + database + '.yml'));
             var databaseYamlConfig = databaseYaml.services[this.appConfigs[i].baseName.toLowerCase() + '-' + database];
             delete databaseYamlConfig.ports;
             if(this.appConfigs[i].devDatabaseType === 'cassandra') {
                 var relativePath = pathjs.relative(this.destinationRoot(), path + '/src/main/docker');
                 databaseYamlConfig.build.context = relativePath;
             }
             parentConfiguration[this.appConfigs[i].baseName.toLowerCase() + '-' + database] = databaseYamlConfig;
         }
         // Add search engine configuration
         var searchEngine = this.appConfigs[i].searchEngine;
         if (searchEngine != 'no') {
             var searchEngineYaml = jsyaml.load(this.fs.read(path + '/src/main/docker/' + searchEngine + '.yml'));
             var searchEngineConfig = searchEngineYaml.services[this.appConfigs[i].baseName.toLowerCase() + '-' + searchEngine];
             delete searchEngineConfig.ports;
             parentConfiguration[this.appConfigs[i].baseName.toLowerCase() + '-' + searchEngine] = searchEngineConfig;
         }
         // Dump the file
         var stringYaml = jsyaml.dump(parentConfiguration, {indent: 4});
         var array = stringYaml.split("\n");
         for (var j = 0; j < array.length; j++) {
             array[j] = "    " + array[j];
             array[j] = array[j].replace(/\'/g, '');
         }
         stringYaml = array.join("\n");
         this.appsYaml.push(stringYaml);
     }
 },
Example #24
0
            stage contains only alphanumeric, underscore and hyphen`, () => {
        const SUtils = new Utils();
        const serverlessYml = {
          service: 'new-service',
          provider: {
            name: 'aws',
            stage: 'xyz-101_abc-123',
          },
          functions: {
            first: {
              events: [],
            },
          },
        };
        SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
          YAML.dump(serverlessYml));

        const serverless = new Serverless({ servicePath: tmpDirPath });
        return expect(simulateRun(serverless)).to.eventually.be.fulfilled.then(() => {
          expect(() => serverless.service.validate()).to.not.throw(serverless.classes.Error);
        });
      });
Example #25
0
    source.toXML(data, function(err, xml) {
        if (err) return callback(err);
        if (!perm) return source.refresh(data, xml, callback);

        var files = [];
        var filtered = tm.filterkeys(data, defaults);
        filtered.Layer = filtered.Layer.map(function(l) { return tm.filterkeys(l, deflayer) });
        files.push({
            basename: 'data.yml',
            data: yaml.dump(tm.sortkeys(filtered), null, 2)
        });
        files.push({ basename: 'data.xml', data: xml });

        tm.writefiles(uri.dirname, files, function(err) {
            if (err) return callback(err);
            source.refresh(data, xml, function(err, p) {
                if (err) return callback(err);
                source.thumbSave(id, path.join(uri.dirname,'.thumb.png'));
                callback(null, p);
            });
        });
    });
Example #26
0
 path.join(process.cwd(), 'manifest.yml'), function(err, content) {
   if(err) {
     cb(err);
     return;
   }
   var yml = yaml.load(content);
   var app = yml.applications[0];
   if(app) {
     app.name = name;
     app.host = name;
     if(instances)
       app.instances = parseInt(instances);
     app.path = '../' + app.path;
     if(conf) {
       if(!app.env) app.env = {};
       app.env.CONF = conf;
     }
   }
   fs.writeFile(
     path.join('.cfpush', [name, 'manifest.yml'].join('-')),
     yaml.dump(yml), cb);
 });
Example #27
0
    function done(err, loaded) {
        if (err) return callback(err);
        var data = loaded.data;
        var xml = loaded.xml;

        // Save thumb optimistically (save completes even if thumb errors out).
        source.thumbSave(id, path.join(uri.dirname,'.thumb.png'));

        var files = [];
        var filtered = tm.filterkeys(data, defaults);
        filtered.Layer = filtered.Layer.map(function(l) { return tm.filterkeys(l, deflayer) });
        files.push({
            basename: 'data.yml',
            data: yaml.dump(tm.sortkeys(filtered), null, 2)
        });
        files.push({ basename: 'data.xml', data: xml });

        tm.writefiles(uri.dirname, files, function(err) {
            if (err) return callback(err);
            callback(null, loaded);
        });
    }
Example #28
0
    it('should support service objects', () => {
      const SUtils = new Utils();
      const serverlessYaml = {
        service: {
          name: 'my-service',
          foo: 'bar',
        },
        provider: 'aws',
      };

      SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yaml'),
        YAML.dump(serverlessYaml));

      const serverless = new Serverless({ servicePath: tmpDirPath });
      serviceInstance = new Service(serverless);

      return expect(serviceInstance.load()).to.eventually.be.fulfilled
      .then(() => {
        expect(serviceInstance.service).to.equal('my-service');
        expect(serviceInstance.serviceObject).to.deep.equal(serverlessYaml.service);
      });
    });
Example #29
0
    fs.readFile(templateFile, 'utf8', function(err, content) {
      if (err)
        throw err;

      const templateYml = yaml.load(content);

      for (let credentialsKey in credentials)
        if (credentials.hasOwnProperty(credentialsKey))
          replaceValues(templateYml.applications, credentials, credentialsKey);

      const templatePath = path.dirname(templateFile);
      const templateBaseName = path.basename(templateFile);
      const manifestBaseName = templateBaseName.replace(/\.template/g, '');
      const manifestFile = path.join(templatePath, manifestBaseName);

      const manifestContent = yaml.dump(templateYml);
      fs.writeFile(manifestFile, manifestContent, 'utf8', (err) => {
        if (err)
          throw err;
      });
      console.log('   %s', manifestFile);
    });
Example #30
0
module.exports = function(app, engine) {

    return {
        name: 'format',
        filter(obj, format) {
            format = (format || 'json').toLowerCase();
            if (_.isString(obj)) {
                return obj;
            }
            if (obj instanceof Buffer) {
                return obj.toString('UTF-8');
            }
            if (format === 'yaml' || format === 'yml') {
                return yaml.dump(obj);
            }
            if (format === 'json') {
                return JSON.stringify(obj, null, 2);
            }
            throw new Error(`Unknown format: ${format}`)
        }
    }

};