'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var envVariable_envVariable = require('./env-variable.js');
var fs_fs = require('../fs/fs.js');
var nodejs_path = require('../nodejs/path.js');
require('../nodejs/fs.js');
require('../nodejs/crypto.js');
require('../nodejs/os.js');

var npmTokenRoot = process.cwd();
function getNpmToken() {
  var NPM_TOKEN = 'NPM_TOKEN';
  var ENV_FILE = '.env.local';
  var envFile = nodejs_path.getPath().resolve(npmTokenRoot, ENV_FILE);
  var token = envVariable_envVariable.readEnvVariable(NPM_TOKEN, envFile);
  if (!token) {
    console.log("".concat(NPM_TOKEN, " \u4E0D\u5B58\u5728"));
    process.exit(1);
  }
  return token;
}
/**
 * 将 .env.local 中 NPM_TOKEN 的值写入到 .npmrc 中
 * @example
 * ```ts
 * // 在发版脚本中使用：
 * // 先在项目根目录的 .env.local 写入 NPM_TOKEN=xxx
 * // 然后调用此方法生成 .npmrc，便于 npm publish 进行授权
 * writeEnvTokenToNpmRC();
 * ```
 */
function writeEnvTokenToNpmRC() {
  var NPM_RC_TPL = "registry=https://registry.npmjs.org/\n//registry.npmjs.org/:always-auth=true\n//registry.npmjs.org/:_authToken={{TOKEN}}";
  var token = getNpmToken();
  var content = NPM_RC_TPL.replace('{{TOKEN}}', token);
  var npmRCFile = nodejs_path.getPath().resolve(npmTokenRoot, '.npmrc');
  fs_fs.writeFileSync(npmRCFile, content);
}

exports.writeEnvTokenToNpmRC = writeEnvTokenToNpmRC;
