UNPKG

@ant-design/tools

Version:
144 lines (140 loc) 4.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _projectHelper = require("../utils/projectHelper"); var _path = require("path"); var _chalk = _interopRequireDefault(require("chalk")); var _nodeFetch = _interopRequireDefault(require("node-fetch")); var _readline = _interopRequireDefault(require("readline")); var _minimist = _interopRequireDefault(require("minimist")); var _arborist = _interopRequireDefault(require("@npmcli/arborist")); var _npmPacklist = _interopRequireDefault(require("npm-packlist")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const argv = (0, _minimist.default)(process.argv.slice(2)); // Added interface to replace "any" function getMajorVersion(version, specificVersion) { if (specificVersion) { return `@${specificVersion}`; } const match = version && version.match(/^\d+/); if (match) { return `@${match[0]}.x`; } return ''; } function getVersionFromURL(url, name) { const affix = url.slice(url.indexOf(name) + name.length + 1); return affix.slice(0, affix.indexOf('/')); } function _default(packageName, packageVersion, done) { const mergedVersion = getMajorVersion(packageVersion, argv.version); console.log(_chalk.default.cyan(`Fetching latest version file list...${packageName}${mergedVersion}`)); function getLatestVersionFileList() { return (0, _nodeFetch.default)(`https://unpkg.com/${packageName}${mergedVersion}/?meta`).then(res => { const version = getVersionFromURL(res.url, packageName); return res.json().then(json => ({ version, ...json })); }).then(({ version, files: pkgFiles }) => { function flattenPath(files, fileList = []) { (files || []).forEach(({ path, files: subFiles }) => { const realPath = argv.path ? (0, _path.join)(argv.path, path) : path; fileList.push(realPath); flattenPath(subFiles, fileList); }); return fileList; } return { version, fileList: flattenPath(pkgFiles) }; }); } function getLocalVersionFileList() { const arborist = new _arborist.default({ path: (0, _projectHelper.getProjectPath)() }); return arborist.loadActual().then(_npmPacklist.default); } Promise.all([getLocalVersionFileList(), getLatestVersionFileList()]).then(([localFiles, { version, fileList }]) => { const localSet = new Set(localFiles); const remoteSet = new Set(fileList); const missingFiles = []; const addedFiles = []; const allFiles = new Set([...fileList, ...localFiles]); allFiles.forEach(filePath => { if (!localSet.has(filePath)) { missingFiles.push(filePath); } else if (!remoteSet.has(filePath)) { addedFiles.push(filePath); } }); return { missingFiles, addedFiles, version }; }).then(({ missingFiles, addedFiles, version }) => { if (addedFiles.length) { console.log(_chalk.default.yellow(`⚠️ Some file added in current build (last version: ${version}):`)); addedFiles.forEach(filePath => { console.log(` + ${filePath}`); }); // Separator console.log(); console.log(_chalk.default.gray(`-`.repeat(process.stdout.columns || 64))); console.log(); } if (missingFiles.length) { console.log(_chalk.default.red(`⚠️ Some file missing in current build (last version: ${version}):`)); missingFiles.forEach(filePath => { console.log(` - ${filePath}`); }); } const total = missingFiles.length + addedFiles.length; if (total) { return Promise.reject(new Error(`Please double confirm with files. ${missingFiles.length} missing, ${addedFiles.length} added.`)); } console.log(_chalk.default.green('✅ Nothing missing compare to latest version:'), _chalk.default.gray(version)); return 0; }).then(() => done()).catch(err => { console.error(err); console.log(_chalk.default.yellow('\nNeed confirm for file diff:')); function userConfirm() { const rl = _readline.default.createInterface({ input: process.stdin, output: process.stdout }); rl.question(['Type "YES" to confirm it is safe.', 'Type "NO" to exit process.', ''].join('\n'), answer => { rl.close(); if (answer === 'YES') { console.log(_chalk.default.green('✅ Confirm it is OK.')); done(); } else if (answer === 'NO') { console.log(_chalk.default.red('🚫 Aha! Catch you!')); done(new Error('User cancel the process.')); } else { console.log(_chalk.default.yellow('Invalidate input. Type again!')); userConfirm(); } }); } userConfirm(); }); }