Giter Club home page Giter Club logo

Comments (13)

wickstargazer avatar wickstargazer commented on May 24, 2024 3

Ah ok, yes we can fix that by moving the sequence around a bit

from nx-plugins.

wickstargazer avatar wickstargazer commented on May 24, 2024 1

Hi! ... was wonderng is there an error message somewhere? because i check the serverless code, and it seems to be included, but i am having a hunch about the file path that might be screwing up ...

Below is their function ... If you can provide an error or any warning messages? If not I will debug into their code. If you could help out it would be awesome .. 😄

D:\<your-project>\node_modules\serverless\lib\classes\Variables.js

getValueFromFile(variableString) {
    const matchedFileRefString = variableString.match(this.fileRefSyntax)[0];
    const referencedFileRelativePath = matchedFileRefString
      .replace(this.fileRefSyntax, (match, varName) => varName.trim())
      .replace('~', os.homedir());

    let referencedFileFullPath = path.isAbsolute(referencedFileRelativePath)
      ? referencedFileRelativePath
      : path.join(this.serverless.config.servicePath, referencedFileRelativePath);

    // Get real path to handle potential symlinks (but don't fatal error)
    referencedFileFullPath = fse.existsSync(referencedFileFullPath)
      ? fse.realpathSync(referencedFileFullPath)
      : referencedFileFullPath;

    let fileExtension = referencedFileRelativePath.split('.');
    fileExtension = fileExtension[fileExtension.length - 1];
    // Validate file exists
    if (!this.serverless.utils.fileExistsSync(referencedFileFullPath)) {
      return BbPromise.resolve(undefined);
    }

    let valueToPopulate;

    // Process JS files
    if (fileExtension === 'js') {
      // eslint-disable-next-line global-require, import/no-dynamic-require
      const jsFile = require(referencedFileFullPath);
      const variableArray = variableString.split(':');
      let returnValue;
      if (variableArray[1]) {
        let jsModule = variableArray[1];
        jsModule = jsModule.split('.')[0];
        returnValue = jsFile[jsModule];
      } else {
        returnValue = jsFile;
      }

      if (typeof returnValue === 'function') {
        valueToPopulate = returnValue.call(jsFile, this.serverless);
      } else {
        valueToPopulate = returnValue;
      }

      return BbPromise.resolve(valueToPopulate).then(valueToPopulateResolved => {
        let deepProperties = variableString.replace(matchedFileRefString, '');
        deepProperties = deepProperties.slice(1).split('.');
        deepProperties.splice(0, 1);
        return this.getDeeperValue(deepProperties, valueToPopulateResolved).then(
          deepValueToPopulateResolved => {
            if (unsupportedJsTypes.has(typeof deepValueToPopulateResolved)) {
              const errorMessage = [
                'Invalid variable syntax when referencing',
                ` file "${referencedFileRelativePath}".`,
                ' Check if your javascript is returning the correct data.',
              ].join('');
              return BbPromise.reject(new this.serverless.classes.Error(errorMessage));
            }
            return BbPromise.resolve(deepValueToPopulateResolved);
          }
        );
      });
    }

    // Process everything except JS
    if (fileExtension !== 'js') {
      valueToPopulate = this.serverless.utils.readFileSync(referencedFileFullPath);
      if (matchedFileRefString !== variableString) {
        let deepProperties = variableString.replace(matchedFileRefString, '');
        if (deepProperties.substring(0, 1) !== ':') {
          const errorMessage = [
            'Invalid variable syntax when referencing',
            ` file "${referencedFileRelativePath}" sub properties`,
            ' Please use ":" to reference sub properties.',
          ].join('');
          return BbPromise.reject(new this.serverless.classes.Error(errorMessage));
        }
        deepProperties = deepProperties.slice(1).split('.');
        return this.getDeeperValue(deepProperties, valueToPopulate);
      }
    }
    return BbPromise.resolve(valueToPopulate);
  }

I am thinking this would be the culprit.

if (!this.serverless.utils.fileExistsSync(referencedFileFullPath)) {
      return BbPromise.resolve(undefined);
    }

from nx-plugins.

wickstargazer avatar wickstargazer commented on May 24, 2024 1

@hiepinnomizetech so, did you have to add some new package to the package.json? If so lets fix it to be a depedency needed for the lib :)

from nx-plugins.

wickstargazer avatar wickstargazer commented on May 24, 2024 1

i am looking at the project this week and next. We will have an update within first week jan and again first week feb 👍🏽

from nx-plugins.

wickstargazer avatar wickstargazer commented on May 24, 2024 1

following this issue for the fix --> serverless/serverless#2997

from nx-plugins.

wickstargazer avatar wickstargazer commented on May 24, 2024

Could you help paste a reference to serverless importing yml as function? I dont think i considered this feature when building it as we don't use it. But I would be happy to in-corporate it into the library! 😄

from nx-plugins.

hiepinnomizetech avatar hiepinnomizetech commented on May 24, 2024

I have created an example at https://github.com/hiepinnomizetech/nx-serverless
In this example, I separate AWS cloud formation to 3 files serverless.config.yml, hello-world.yml and serverless.yml. But I think file() function isn't supported by this plugin (https://www.serverless.com/framework/docs/providers/aws/guide/variables#reference-variables-in-other-files).

I hope you can include this feature in the plugin. My current project has a large serverless.yml. If this plugin can separate serverless.yml, it is so great.
Thank you very much!

from nx-plugins.

wickstargazer avatar wickstargazer commented on May 24, 2024

Hi @hiepinnomizetech I haven't forgot this, will be looking into it this working week 😄

from nx-plugins.

hiepinnomizetech avatar hiepinnomizetech commented on May 24, 2024

@wickstargazer , i have checked and I see serverless has included other files by using file() function. Thank so much!

from nx-plugins.

wickstargazer avatar wickstargazer commented on May 24, 2024

@hiepinnomizetech So should i add some new dependency for the file() funciton? i still dint get the answer 😄

from nx-plugins.

hiepinnomizetech avatar hiepinnomizetech commented on May 24, 2024

@wickstargazer thanks!
I only tested file() function can load another file data, but I didn't test why it doesn't work in Demo project (I think it is caused by include URL). So I will close issue. Thank you very much!

from nx-plugins.

hiepinnomizetech avatar hiepinnomizetech commented on May 24, 2024

@wickstargazer , I have found root cause.
If I use file() function on Lambda function section:
functions: hello-world: ${file(hello-world.yml)}
hello-world.yml will not be loaded because file() function doesn't run.
But if i use file() function on custom section:
custom: config: ${file(serverless.config.yml)}
serverless.config.yml will be loaded.

image01

from nx-plugins.

nhammond101 avatar nhammond101 commented on May 24, 2024

Any update @wickstargazer?

Our project's growing to a size where splitting into separate files would be useful

from nx-plugins.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.