无服务器YML toUpperCase

时间:2018-01-12 12:53:24

标签: yaml serverless

我想在不同的环境(dev,test,prod)中重用我的[org.joda/joda-convert "1.9.2"]

在配置中我有:

serverless.yml

现在,该值将为provider: name: aws stage: ${opt:stage, 'dev'} environment: NODE_ENV: ${self:provider.stage} devtest(全部为小写)。

有没有办法将输入转换为UpCase(),输入和prod保持原样(即小写),但NODE_ENV的值将是大写?

3 个答案:

答案 0 :(得分:10)

AFAIK,YAML中没有这样的功能。

通过使用小写和大写名称之间的映射,您可以实现您想要的效果。

custom:
  environments:
    dev: DEV
    test: TEST
    prod: PROD

provider:
  name: aws
  stage: ${opt:stage, 'dev'}
  environment:
    NODE_ENV: ${self:custom.environments.${self:provider.stage}}

答案 1 :(得分:0)

您可以使用提供的Title功能来达到此目的。

以您的示例为例,这应该可以工作(假设您正在支持现代语法的node.js环境中运行)

serverless.yml

const clientId = 'KnUCjsgC_SwsKbyWhoY_KQ';
const secret = 'H26mxyT3NsVNCTCLf21NjPtPI0PRWKZUIWbkyycSf1Pzv7xPOjh7wCz1YyiSEoaO';
let accessToken;

//object
const Yelp ={
    getAccessToken(){
        if(accessToken){
            return new Promise(resolve =>resolve(accessToken));
        } 
        return fetch(`https://cors-anywhere.herokuapp.com/https://api.yelp.com/oauth2/token?grant_type=client_credentials& client_id=${clientId}&client_secret=${secret}`,
        {method:'POST'
        }).then(response =>{
        return response.json();
        }).then(jsonResponse => {accessToken = jsonResponse.access_token;
      }); 
    },


    search(term, location, sortBy) {
    return Yelp.getAccessToken().then(() => {
      return fetch(`https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}&sort_by=${sortBy}`, {
        headers: {
          Authorization: `Bearer ${accessToken}`
        }
      });
    }).then(response => {
      return response.json();
    }).then(jsonResponse => {
      if (jsonResponse.businesses) {
        console.log(jsonResponse.businesses);
        return jsonResponse.businesses.map(business => ({
          id: business.id,
          imageSrc: business.image_url,
          name: business.name,
          address: business.location.address1,
          city: business.location.city,
          state: business.location.state,
          zipCode: business.location.zip_code,
          category: business.categories[0].title,
          rating: business.rating,
          reviewCount: business.review_count
        }));
      }
    });
  }
};
export default Yelp;

yml-helpers.js (与serverless.yml相邻)

    myClient.Post(url,JsonConvert.SerializeObject(new {
        param1: z,
        param2: y,
        param3: z
        }), 
    authkey);

答案 2 :(得分:0)

通过阅读一些源代码和控制台记录整个 serverless 对象,我找到了一些可行的方法。此示例将辅助函数应用于标题大小写某些输入选项值(根据需要改为应用 str.toUpperCase())。解析无服务器对象中已经可用的输入选项的结果。

// serverless-helpers.js

function toTitleCase(word) {
  console.log("input word:  " + word);
  let lower = word.toLowerCase();
  let title = lower.replace(lower[0], lower[0].toUpperCase());
  console.log("output word: " + title);
  return title;
}

module.exports.dynamic = function(serverless) {
  // The `serverless` argument contains all the information in
  // the serverless.yaml file
  // serverless.cli.consoleLog('Use Serverless config and methods as well!');

  // this is useful for discovery of what is available:
  // serverless.cli.consoleLog(serverless);

  const input_options = serverless.processedInput.options;
  return {
    part1Title: toTitleCase(input_options.part1),
    part2Title: toTitleCase(input_options.part2)
  };
};
# serverless.yaml snippet

custom:

  part1: ${opt:part1}
  part2: ${opt:part2}

  dynamicOpts: ${file(./serverless-helpers.js):dynamic}
  combined: prefix${self:custom.dynamicOpts.part1Title}${self:custom.dynamicOpts.part2Title}Suffix

这个简单的例子假设输入选项是 --part1={value}--part2={value},但概括是遍历 serverless.processedInput.options 的属性并将任何自定义帮助程序应用于这些值。