function trimNpmrcFileLines()

in common/scripts/install-run.js [119:185]


function trimNpmrcFileLines(npmrcFileLines, env, supportEnvVarFallbackSyntax) {
    var _a;
    const resultLines = [];
    // This finds environment variable tokens that look like "${VAR_NAME}"
    const expansionRegExp = /\$\{([^\}]+)\}/g;
    // Comment lines start with "#" or ";"
    const commentRegExp = /^\s*[#;]/;
    // Trim out lines that reference environment variables that aren't defined
    for (let line of npmrcFileLines) {
        let lineShouldBeTrimmed = false;
        //remove spaces before or after key and value
        line = line
            .split('=')
            .map((lineToTrim) => lineToTrim.trim())
            .join('=');
        // Ignore comment lines
        if (!commentRegExp.test(line)) {
            const environmentVariables = line.match(expansionRegExp);
            if (environmentVariables) {
                for (const token of environmentVariables) {
                    /**
                     * Remove the leading "${" and the trailing "}" from the token
                     *
                     * ${nameString}                  -> nameString
                     * ${nameString-fallbackString}   -> name-fallbackString
                     * ${nameString:-fallbackString}  -> name:-fallbackString
                     */
                    const nameWithFallback = token.substring(2, token.length - 1);
                    let environmentVariableName;
                    let fallback;
                    if (supportEnvVarFallbackSyntax) {
                        /**
                         * Get the environment variable name and fallback value.
                         *
                         *                                name          fallback
                         * nameString                 ->  nameString    undefined
                         * nameString-fallbackString  ->  nameString    fallbackString
                         * nameString:-fallbackString ->  nameString    fallbackString
                         */
                        const matched = nameWithFallback.match(/^([^:-]+)(?:\:?-(.+))?$/);
                        // matched: [originStr, variableName, fallback]
                        environmentVariableName = (_a = matched === null || matched === void 0 ? void 0 : matched[1]) !== null && _a !== void 0 ? _a : nameWithFallback;
                        fallback = matched === null || matched === void 0 ? void 0 : matched[2];
                    }
                    else {
                        environmentVariableName = nameWithFallback;
                    }
                    // Is the environment variable and fallback value defined.
                    if (!env[environmentVariableName] && !fallback) {
                        // No, so trim this line
                        lineShouldBeTrimmed = true;
                        break;
                    }
                }
            }
        }
        if (lineShouldBeTrimmed) {
            // Example output:
            // "; MISSING ENVIRONMENT VARIABLE: //my-registry.com/npm/:_authToken=${MY_AUTH_TOKEN}"
            resultLines.push('; MISSING ENVIRONMENT VARIABLE: ' + line);
        }
        else {
            resultLines.push(line);
        }
    }
    return resultLines;
}