"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBlockList = exports.createBlockLog = exports.DEFAULT_RESOURCES = exports.getFilesTreeData = exports.getFolderTreeData = void 0; function _react() { const data = _interopRequireDefault(require("react")); _react = function _react() { return data; }; return data; } function _fs() { const data = _interopRequireDefault(require("fs")); _fs = function _fs() { return data; }; return data; } function _chalk() { const data = _interopRequireDefault(require("chalk")); _chalk = function _chalk() { return data; }; return data; } function _path() { const data = require("path"); _path = function _path() { return data; }; return data; } function _umi() { const data = require("umi"); _umi = function _umi() { return data; }; return data; } function _blockSdk() { const data = require("@umijs/block-sdk"); _blockSdk = function _blockSdk() { return data; }; return data; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } const winPath = _umi().utils.winPath; /** * 遍历文件地址 * @param path */ const getFolderTreeData = (path, parentPath = '/', depth = 0) => { const files = _fs().default.readdirSync(winPath(path)); return files.map(fileName => { const status = _fs().default.statSync((0, _path().join)(path, fileName)); // 是文件夹 并且不已 . 开头, 且最深三层 if (status.isDirectory() && fileName.indexOf('.') !== 0 && depth < 3) { const absPath = winPath((0, _path().join)(path, fileName)); const absPagePath = winPath((0, _path().join)(parentPath, fileName)); const children = getFolderTreeData(absPath, absPagePath, depth + 1); if (children && children.length > 0) { return { key: absPagePath, title: fileName, value: absPagePath, children }; } return { title: fileName, value: absPagePath, key: absPagePath }; } return undefined; }).filter(obj => obj); }; /** * 遍历文件地址 * 包含文件 * @param path */ exports.getFolderTreeData = getFolderTreeData; const getFilesTreeData = (path, parentPath = '/', depth = 0) => { const files = _fs().default.readdirSync(winPath(path)); return files.map(fileName => { const status = _fs().default.statSync((0, _path().join)(path, fileName)); const isDirectory = status.isDirectory(); // 是文件夹 并且不已 . 开头, 且最深五层 if (fileName.indexOf('.') !== 0 && depth < 5) { if (!isDirectory && !fileName.includes('.tsx') && !fileName.includes('.jsx') && !fileName.includes('.js') && !fileName.includes('.ts')) { return undefined; } const absPath = winPath((0, _path().join)(path, fileName)); const absPagePath = winPath((0, _path().join)(parentPath, fileName)); const children = isDirectory ? getFilesTreeData(absPath, absPagePath, depth + 1) : []; return { selectable: !isDirectory, key: absPagePath, title: fileName, value: absPagePath, children }; } return undefined; }).filter(obj => obj); }; exports.getFilesTreeData = getFilesTreeData; const DEFAULT_RESOURCES = [{ id: 'ant-design-pro', name: 'Ant Design Pro', resourceType: 'custom', description: '基于 antd 的中台模板。', blockType: 'template', icon: 'https://img.alicdn.com/tfs/TB1e8gomAL0gK0jSZFAXXcA9pXa-64-64.png', getData: () => (0, _blockSdk().fetchCDNBlocks)({ pkg: 'pro-blocks', summary: 'umi-block.json', version: '^1.0.0' }) }, { id: 'ant-design-blocks', name: 'Ant Design', resourceType: 'custom', description: '来自 antd 的 Demo 区块', blockType: 'block', icon: 'https://img.alicdn.com/tfs/TB1e8gomAL0gK0jSZFAXXcA9pXa-64-64.png', getData: () => (0, _blockSdk().fetchCDNBlocks)({ pkg: 'ant-design-blocks', summary: 'umi-block.json', version: '^1.0.0' }) }]; // 日志带 block 前缀 exports.DEFAULT_RESOURCES = DEFAULT_RESOURCES; const createBlockLog = log => (logType, info) => log(logType, `${_chalk().default.hex('#40a9ff')('block:')} ${info}`); /** * 从 resource 中获取数据源 */ exports.createBlockLog = createBlockLog; const getBlockList = /*#__PURE__*/function () { var _ref = _asyncToGenerator(function* (resourceId, list) { const resource = list.find(item => item.id === resourceId); if (resource) { if (resource.resourceType === 'custom') { const _yield$resource$getDa = yield resource.getData(), data = _yield$resource$getDa.data; return data; } } return []; }); return function getBlockList(_x, _x2) { return _ref.apply(this, arguments); }; }(); exports.getBlockList = getBlockList;