const fs = require('fs');
const path = require('path');
const paserConfig = require('./parser');
const r = paserConfig();
const data = fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8');
if (!data) {
  console.error('未找到package.json文件');
}
const packageJson = JSON.parse(data);
const _config = packageJson.module_config || {};

let resourceCode;
try {
  const env = process.argv[2] || 'production';
  const dotenv = require('dotenv').config({
    path: path.join(__dirname, '../.env.' + env),
  });
  resourceCode = dotenv.parsed.VUE_APP_RESOURCE_CODE;
} catch (e) {
  console.log(e, '获取resourceCode失败');
}

packageJson.module_config = {
  ..._config,
  resourceCode: resourceCode,
  pages: r,
};
const packageString = JSON.stringify(packageJson, null, 2);
fs.writeFileSync(
  path.join(__dirname, '../package.json'),
  packageString,
  'utf8',
);
