'use strict';
import { readFile, writeFile, rm } from 'node:fs/promises';
import { EOL } from 'node:os';
import _0x589480 from 'process';
import { stringEncodingNames } from '../../../base-repo/app-constants/internal-system-constants.js';
import { files } from '../../../base-repo/app-constants/project-paths.js';
import { environmentVariables } from '../constants/ci-constants.js';
import { npmRegistry } from '../constants/npm-commands.js';
import { cliLogError, cliLogInfo } from '../../../fd-toolbox/logging/cli-logger.js';
import { getStringEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';

var __defProp=Object['defineProperty'],__name=(_0x1cd977,_0x13a326)=>__defProp(_0x1cd977,'name',{'value':_0x13a326,'configurable':!![]});async function setupNpmAuth(_0x513b71){const _0x49b05f=getStringEnvSetting(environmentVariables['ciNpmToken']);!_0x49b05f&&(cliLogError('Critical:\x20\x27'+environmentVariables['ciNpmToken']+'\x27\x20is\x20not\x20set.'),_0x589480['exit'](0xdfd*0x1+-0x15d*-0xd+0x1fb5*-0x1));const _0x3fb487=_0x513b71+'/'+files['npmConfig'],_0x158b5b='//'+npmRegistry['url']+'/:'+npmRegistry['authTokenKey']+'='+_0x49b05f;let _0x19f892;try{_0x19f892=await readFile(_0x3fb487,stringEncodingNames['utfDash8']),await writeFile(_0x3fb487,''+_0x19f892+EOL+_0x158b5b);}catch{await writeFile(_0x3fb487,_0x158b5b);}return cliLogInfo('Created\x20temporary\x20\x27'+files['npmConfig']+'\x27\x20at\x20\x27'+_0x3fb487+'\x27.'),{'npmrcPath':_0x3fb487,'originalContent':_0x19f892};}__name(setupNpmAuth,'setupNpmAuth');async function cleanupNpmAuth(_0x1571a0,_0x5d3fb4){_0x5d3fb4?(cliLogInfo('Restoring\x20original\x20content\x20of\x20\x27'+_0x1571a0+'\x27...'),await writeFile(_0x1571a0,_0x5d3fb4),cliLogInfo('Original\x20\x27'+_0x1571a0+'\x27\x20file\x20has\x20been\x20restored.')):(cliLogInfo('Removing\x20temporary\x20\x27'+_0x1571a0+'\x27...'),await rm(_0x1571a0,{'force':!![]}),cliLogInfo('Temporary\x20\x27'+_0x1571a0+'\x27\x20file\x20has\x20been\x20removed.'));}__name(cleanupNpmAuth,'cleanupNpmAuth');

export { cleanupNpmAuth, setupNpmAuth };
