'use strict';

/**
 * 查看当前包版本是否改过，如果没改过先抛错提醒，避免每次build完才发现
 */

const { join } = require('path');
const { execSync } = require('child_process');
const { EOL } = require('os');
const rootDir = join(__dirname, '../..');
const { name, version } = require(join(rootDir, 'package.json'));

const RESET = '\x1b[0m';
const RED = '\x1b[31m';

console.log('使用tnpm检测包的最新版本，请确保：1）已安装tnpm。2）已连接内网。');

const lines = execSync(`tnpm info ${name} time`).toString().split(EOL);
for (const line of lines) {
  const existVersion = line.split(':')[0].replace(/[\s\']/g, '');
  if (existVersion === version) {
    console.error(`${RED}${name}@${version}已发布，请更新版本号！${RESET}`);
    process.exit(1);
  }
}
