UNPKG

@lcap/nasl

Version:

NetEase Application Specific Language

217 lines 10.9 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getExtensionsMaterial = exports.getExtensionsDeclaration = exports.getPreDeclaration = exports.handleMaterial = exports.genComponentMethodDeclaration = exports.getUILib = exports.handleTsDeclaration = void 0; const lodash_1 = require("lodash"); const translator_1 = require("../../translator"); const naslTypes = __importStar(require("../../concepts")); const utils = __importStar(require("../../utils")); const nasl = __importStar(require("../../index")); function handleTsDeclaration(tsDeclaration) { return tsDeclaration. replace(/\/\/\/ *<reference types="@nasl\/types" *\/>\s*/g, '') .replace(/\{\s+altKey[\s\S]+?\}/g, 'MouseEvent') .replace(/\{\s+cancelBubble[\s\S]+?\}/g, 'FocusEvent') .replace(/\{\s+rawValue[\s\S]+?\}/g, 'ValidateResult') .replace(/\{\s+rawValue[\s\S]+?\}/g, 'ValidateEvent') .replace(/nasl\.core\./g, "") .replace(/nasl\.collection\./g, "") .replace(/Integer/g, "Long"); } exports.handleTsDeclaration = handleTsDeclaration; function getUILib(frontendType) { let jsonCodeMap; let tsCodeMap; /// #if process.env.BUILD_TARGET === 'node' if (globalThis.process) { // For TS build const fs = require('fs'); jsonCodeMap = { pc: fs.readFileSync(require.resolve(`../../../sandbox-natural/stdlib/nasl.ui.pc.json`), 'utf8'), h5: fs.readFileSync(require.resolve(`../../../sandbox-natural/stdlib/nasl.ui.h5.json`), 'utf8'), }; tsCodeMap = { pc: fs.readFileSync(require.resolve(`../../../sandbox-natural/stdlib/nasl.ui.pc.d.ts`), 'utf8'), h5: fs.readFileSync(require.resolve(`../../../sandbox-natural/stdlib/nasl.ui.h5.d.ts`), 'utf8'), }; } /// #endif /// #if process.env.BUILD_TARGET !== 'node' if (globalThis.window) { // For TS build jsonCodeMap = { pc: require('!!raw-loader!../../../sandbox-natural/stdlib/nasl.ui.pc.json').default, h5: require('!!raw-loader!../../../sandbox-natural/stdlib/nasl.ui.h5.json').default, }; tsCodeMap = { pc: require('!!raw-loader!../../../sandbox-natural/stdlib/nasl.ui.pc.d.ts').default, h5: require('!!raw-loader!../../../sandbox-natural/stdlib/nasl.ui.h5.d.ts').default, }; } /// #endif let json = JSON.parse(jsonCodeMap[frontendType]); let tsDeclaration = tsCodeMap[frontendType]; let allNodesAPI = nasl.config.allNodesAPI; return handleMaterial({ json, tsDeclaration, allNodesAPI }); } exports.getUILib = getUILib; function genComponentMethodDeclaration(componentNodesAPI) { let code = ''; const { propsMap, readablePropsMap, name } = componentNodesAPI || {}; const props = Object.keys(propsMap || {}); const readableProps = Object.keys(readablePropsMap || {}); const componentName = utils.kebab2Camel(name); code += `type ${componentName}OptionsSetKeys = `; code += props?.map((p, i) => `'${p}'` + (i < props.length - 1 ? ' | ' : '')).join('') || null; code += ';\n'; code += `type ${componentName}OptionsGetKeys = `; code += readableProps?.map((p, i) => `'${p}'` + (i < readableProps.length - 1 ? ' | ' : '')).join('') || null; code += ';\n'; code += `export class ${componentName}Methods extends ViewComponentMethods {\n`; code += `// 仅支持设置 ${componentName}OptionsSetKeys 中的属性\n`; code += ` setPropValue<K extends ${componentName}OptionsSetKeys>(key: K, value: ${componentName}Options[K]): void;\n`; code += `// 仅支持获取 ${componentName}OptionsGetKeys 中的属性 \n`; code += ` getPropValue<K extends ${componentName}OptionsGetKeys>(key: K): ${componentName}Options[K];\n`; code += `}\n`; return code; } exports.genComponentMethodDeclaration = genComponentMethodDeclaration; function handleMaterial({ json, tsDeclaration, allNodesAPI }) { let newTsDeclaration = handleTsDeclaration(tsDeclaration); const snippets = newTsDeclaration.split('declare namespace nasl.ui {'); const snippetMap = {}; snippets.forEach((code) => { const cap = code.match(/class ([a-zA-Z]+).+?extends ViewComponent/); if (cap) { const kebabName = utils.Camel2kebab(cap[1]); snippetMap[kebabName] = code; } }); json.forEach((item) => { if (item.name === 'UTableView' && !item?.description?.includes('表格中包含 reload')) { item.description += '表格中包含 reload、exportExcel(...) 等 API'; } }); return json.map(({ name, title, description }) => { const kebabName = utils.Camel2kebab(name); let tsDeclaration = 'declare namespace nasl.ui {\n'; tsDeclaration += allNodesAPI?.[kebabName] ? genComponentMethodDeclaration(allNodesAPI?.[kebabName]) : ''; tsDeclaration += snippetMap?.[kebabName]; const camelName = utils.kebab2Camel(name); const item = { name: camelName, title, description, tsDeclaration, }; if (!snippetMap?.[kebabName]) { console.error(`No tsDeclaration: ${kebabName}`); // throw new Error(`No tsDeclaration: ${kebabName}`); } return item; }); } exports.handleMaterial = handleMaterial; function getPreDeclaration() { let code; /// #if process.env.BUILD_TARGET === 'node' if (globalThis.process) { // For TS build const fs = require('fs'); code = fs.readFileSync(require.resolve(`../../../sandbox-natural/stdlib/nasl.ui.d.ts`), 'utf8'); } /// #endif /// #if process.env.BUILD_TARGET !== 'node' if (globalThis.window) { // For TS build code = require('!!raw-loader!../../../sandbox-natural/stdlib/nasl.ui.d.ts').default; } /// #endif return code; } exports.getPreDeclaration = getPreDeclaration; // viewComponent__ 文件未找回,先手转,后续迁移 function getExtensionsDeclaration(viewComponent) { const state = (0, translator_1.createCompilerState)(); const componentName = (0, lodash_1.camelCase)(viewComponent.name); let code = 'declare namespace nasl.ui {\n'; code += `${(0, translator_1.indent)(state.tabSize + 1)}class ${componentName} extends ViewComponent {\n`; // methods viewComponent.methods?.forEach((method) => { code += `${(0, translator_1.indent)(state.tabSize + 2)}${method.name}(${method?.params?.map((param) => `${param?.name}`).join(', ')}): any;\n`; }); code += `${(0, translator_1.indent)(state.tabSize + 2)}constructor(options?: Partial<${componentName}Options>);\n`; code += `${(0, translator_1.indent)(state.tabSize + 1)}}\n`; code += `${(0, translator_1.indent)(state.tabSize + 1)}class ${componentName}Options extends ViewComponentOptions {\n`; // props const props = viewComponent?.props || viewComponent?.attrs || []; props?.forEach((prop) => { code += `${(0, translator_1.indent)(state.tabSize + 2)}${utils.getDescription(prop.title, prop.description)}\n`; code += `${(0, translator_1.indent)(state.tabSize + 2)}${(0, lodash_1.camelCase)(prop.name)}?: ${prop?.type || prop?.tsType};\n`; }); // events const events = viewComponent?.events || []; events?.forEach((event) => { code += `${(0, translator_1.indent)(state.tabSize + 2)}${utils.getDescription(event.title, event.description)}\n`; let tsType = event?.tsType?.replace(/\n/g, ''); if (!tsType) { const eventParams = event?.params || []; eventParams?.forEach((param) => { tsType = `${param?.name?.replace('$', '')}: ${param?.type}`; }); } code += `${(0, translator_1.indent)(state.tabSize + 2)}on${utils.firstUpperCase((0, lodash_1.camelCase)(event.name))}: (${tsType}) => any;\n`; }); // slots const slots = viewComponent?.slots || []; slots?.forEach((slot) => { code += `${(0, translator_1.indent)(state.tabSize + 2)}${utils.getDescription(slot.title, slot.description)}\n`; let tsType = slot?.tsType?.replace(/\n/g, ''); if (!tsType) { tsType = `${slot?.slotProps?.name.replace('$', '')}: ${new naslTypes.TypeAnnotation(slot?.slotProps?.typeAnnotation)?.toNaturalTS()}`; } code += `${(0, translator_1.indent)(state.tabSize + 2)}slot${utils.firstUpperCase((0, lodash_1.camelCase)(slot.name))}: (${tsType}) => any;\n`; }); code += `${(0, translator_1.indent)(state.tabSize + 1)}}\n`; code += `}\n`; return code; } exports.getExtensionsDeclaration = getExtensionsDeclaration; function getExtensionsMaterial(app, frontendType) { const extensions = app?.dependencies?.filter((dependency) => dependency.concept === 'Module' && dependency?.frontends?.length); const state = (0, translator_1.createCompilerState)('', { descriptionComment: true, needNamespace: true }); const components = []; extensions?.forEach((extension) => { const { viewComponents } = extension.frontends.filter((frontend) => frontend.type === frontendType)?.[0] ?? {}; viewComponents?.forEach((viewComponent) => { const { name, title, description } = viewComponent || {}; components.push({ name: (0, lodash_1.camelCase)(name), title, description: description || title, tsDeclaration: viewComponent.concept === 'ViewComponentDeclaration' ? new naslTypes.ViewComponentDeclaration(viewComponent)?.toNaturalTS(state, true) : getExtensionsDeclaration(viewComponent), }); }); }); return components; } exports.getExtensionsMaterial = getExtensionsMaterial; //# sourceMappingURL=getUILib.js.map