aspnetcore-react

    0

    0

    The code requires the fs module and the path module.

    The code first retrieves the baseFolder from the environment variable APPDATA if it is not set to an empty string, or from the user's home directory if APPDATA is not set. The code then checks to see if the certificate name is valid. If the certificate name is invalid, the script outputs an error and exits. Otherwise, the code writes the certificate files to the .env.development.local directory.

    const fs = require('fs');
    const path = require('path');
    
    const baseFolder =
      process.env.APPDATA !== undefined && process.env.APPDATA !== ''
        ? `${process.env.APPDATA}/ASP.NET/https`
        : `${process.env.HOME}/.aspnet/https`;
    
    const certificateArg = process.argv.map((arg) => arg.match(/--name=(?<value>.+)/i)).filter(Boolean)[0];
    const certificateName = certificateArg ? certificateArg.groups.value : process.env.npm_package_name;
    
    if (!certificateName) {
      console.error(
        'Invalid certificate name. Run this script in the context of an npm/yarn script or pass --name=<<app>> explicitly.',
      );
      process.exit(-1);
    }
    
    const certFilePath = path.join(baseFolder, `${certificateName}.pem`);
    const keyFilePath = path.join(baseFolder, `${certificateName}.key`);
    
    if (!fs.existsSync('.env.development.local')) {
      fs.writeFileSync(
        '.env.development.local',
        `SSL_CRT_FILE=${certFilePath}
    SSL_KEY_FILE=${keyFilePath}`,
      );
    } else {
      let lines = fs.readFileSync('.env.development.local').toString().split('\n');
    
      let hasCert,
        hasCertKey = false;
      for (const line of lines) {
        if (/SSL_CRT_FILE=.*/i.test(line)) {
          hasCert = true;
        }
        if (/SSL_KEY_FILE=.*/i.test(line)) {
          hasCertKey = true;
        }
      }
      if (!hasCert) {
        fs.appendFileSync('.env.development.local', `\nSSL_CRT_FILE=${certFilePath}`);
      }
      if (!hasCertKey) {
        fs.appendFileSync('.env.development.local', `\nSSL_KEY_FILE=${keyFilePath}`);
      }
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.