function configurePackageEnvironments()

in lib/painlessConfigAsCode.js [30:73]


function configurePackageEnvironments(providers, environmentModules, environment, appName) {
  let environmentInstances = [];
  for (let i = 0; i < environmentModules.length; i++) {
    // CONSIDER: Should the name strip any @ after the first slash, in case it is a version-appended version?
    const npmName = environmentModules[i].trim();
    if (!npmName) {
      continue;
    }

    let environmentPackage = null;
    try {
      environmentPackage = require(npmName);
    } catch (packageRequireError) {
      const packageMissing = new Error(`Unable to require the "${npmName}" environment package for the "${environment}" environment`);
      packageMissing.innerError = packageRequireError;
      throw packageMissing;
    }
    if (!environmentPackage) {
      continue;
    }

    let values = null;
    if (typeof(environmentPackage) === 'function') {
      environmentInstances.push(environmentPackage);
      try {
        values = environmentPackage(environment);
      } catch (problemCalling) {
        const asText = problemCalling.toString();
        const error = new Error(`While calling the environment package "${npmName}" for the "${environment}" environment an error was thrown: ${asText}`);
        error.innerError = problemCalling;
        throw error;
      }
    } else if (typeof(environmentPackage) === 'object') {
      values = environmentPackage;
    }

    if (!values) {
      throw new Error(`Could not determine what to do with the environment package "${npmName}" for the "${environment}" environment (no values or unexpected type)`);
    }
    providers.push(objectProvider(values, appName));

    return environmentInstances;
  }
}