mirror of
https://github.com/openharmony/tools_previewer.git
synced 2026-07-19 22:33:34 -04:00
@@ -81,13 +81,9 @@ const configJSAPIMockOutput = {
|
||||
|
||||
rollup.rollup(configJSAPIMockInput).then(bundle => {
|
||||
bundle.write(configJSAPIMockOutput).then(() => {
|
||||
console.log(configJSAPIMockOutput.file)
|
||||
countSize(configJSAPIMockOutput.file);
|
||||
const fileContent = fs.readFileSync(configJSAPIMockOutput.file, 'utf-8');
|
||||
fs.writeFileSync(configJSAPIMockOutput.file,
|
||||
fileContent
|
||||
.replace(/\$\d*/g, '')
|
||||
, 'utf-8')
|
||||
fs.writeFileSync(configJSAPIMockOutput.file, fileContent.replace(/\$\d*/g, ''), 'utf-8');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
MethodSignature, ModifiersArray, ModuleDeclaration, NodeArray, ParameterDeclaration, PropertyName, SourceFile
|
||||
} from 'typescript';
|
||||
import fs from 'fs';
|
||||
import { ImportElementEntity } from '../declaration-node/importAndExportDeclaration';
|
||||
import type { ImportElementEntity } from '../declaration-node/importAndExportDeclaration';
|
||||
|
||||
|
||||
const paramIndex = 2;
|
||||
@@ -332,10 +332,10 @@ export function getJsSdkDir(): string {
|
||||
* @returns
|
||||
*/
|
||||
export function hasBeenImported(importDeclarations: ImportElementEntity[], typeName: string): boolean {
|
||||
if (!typeName.trim()){
|
||||
return;
|
||||
if (!typeName.trim()) {
|
||||
return true;
|
||||
}
|
||||
if (isFirstCharLowerCase(typeName)){
|
||||
if (isFirstCharLowerCase(typeName)) {
|
||||
return true;
|
||||
}
|
||||
return importDeclarations.some(importDeclaration => importDeclaration.importElements.includes(typeName));
|
||||
@@ -358,4 +358,4 @@ export const specialFiles = [
|
||||
'@internal/component/ets/enums.d.ts',
|
||||
'@internal/component/ets/alert_dialog.d.ts',
|
||||
'@internal/component/ets/ability_component.d.ts'
|
||||
]
|
||||
];
|
||||
@@ -40,10 +40,10 @@ export function generateClassDeclaration(rootName: string, classEntity: ClassEnt
|
||||
|
||||
const className = firstCharacterToUppercase(classEntity.className);
|
||||
let classBody = '';
|
||||
if ((classEntity.exportModifiers.includes(SyntaxKind.ExportKeyword)
|
||||
|| classEntity.exportModifiers.includes(SyntaxKind.DeclareKeyword))
|
||||
&& !isInnerMockFunction) {
|
||||
classBody += `export const ${className} = class ${className} `;
|
||||
if ((classEntity.exportModifiers.includes(SyntaxKind.ExportKeyword) ||
|
||||
classEntity.exportModifiers.includes(SyntaxKind.DeclareKeyword)) &&
|
||||
!isInnerMockFunction) {
|
||||
classBody += `export const ${className} = class ${className} `;
|
||||
} else {
|
||||
classBody += `const ${className} = class ${className} `;
|
||||
}
|
||||
|
||||
@@ -14,14 +14,15 @@
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { SourceFile } from 'typescript';
|
||||
import { InterfaceEntity } from '../declaration-node/interfaceDeclaration';
|
||||
import { generateCommonMethodSignature } from './generateCommonMethodSignature';
|
||||
import { generateIndexSignature } from './generateIndexSignature';
|
||||
import { generatePropertySignatureDeclaration } from './generatePropertySignatureDeclaration';
|
||||
import { dtsFileList, getApiInputPath, hasBeenImported, specialFiles } from '../common/commonUtils'
|
||||
import { ImportElementEntity } from '../declaration-node/importAndExportDeclaration';
|
||||
import path from 'path';
|
||||
import { dtsFileList, getApiInputPath, hasBeenImported, specialFiles } from '../common/commonUtils';
|
||||
import type { ImportElementEntity } from '../declaration-node/importAndExportDeclaration';
|
||||
import type { PropertySignatureEntity } from '../declaration-node/propertySignatureDeclaration';
|
||||
|
||||
/**
|
||||
* generate interface
|
||||
@@ -107,25 +108,37 @@ function generateHeritageInterface(interfaceEntity: InterfaceEntity, sourceFile:
|
||||
return interfaceBody;
|
||||
}
|
||||
|
||||
function addExtraImport(extraImport: string[], importDeclarations: ImportElementEntity[], sourceFile: SourceFile, value) {
|
||||
if (extraImport && importDeclarations){
|
||||
/**
|
||||
*
|
||||
* @param extraImport
|
||||
* @param importDeclarations
|
||||
* @param sourceFile
|
||||
* @param value
|
||||
* @returns
|
||||
*/
|
||||
function addExtraImport(
|
||||
extraImport: string[],
|
||||
importDeclarations: ImportElementEntity[],
|
||||
sourceFile: SourceFile,
|
||||
value: PropertySignatureEntity): void {
|
||||
if (extraImport && importDeclarations) {
|
||||
const propertyTypeName = value.propertyTypeName.split('.')[0].split('|')[0].split('&')[0].replace(/"'/g, '').trim();
|
||||
if (propertyTypeName.includes('/')){
|
||||
if (propertyTypeName.includes('/')) {
|
||||
return;
|
||||
}
|
||||
if (hasBeenImported(importDeclarations, propertyTypeName)) {
|
||||
return;
|
||||
}
|
||||
const specialFilesList = [...specialFiles.map(specialFile=>path.join(getApiInputPath(), ...specialFile.split('/')))];
|
||||
if (!specialFilesList.includes(sourceFile.fileName)){
|
||||
if (!specialFilesList.includes(sourceFile.fileName)) {
|
||||
specialFilesList.unshift(sourceFile.fileName);
|
||||
}
|
||||
for (let i=0; i < specialFilesList.length; i++) {
|
||||
for (let i = 0; i < specialFilesList.length; i++) {
|
||||
const specialFilePath = specialFilesList[i];
|
||||
const specialFileContent = fs.readFileSync(specialFilePath, 'utf-8');
|
||||
const regex = new RegExp(`\\s${propertyTypeName}\\s({|=|extends)`);
|
||||
const results = specialFileContent.match(regex)
|
||||
if (!results){
|
||||
const results = specialFileContent.match(regex);
|
||||
if (!results) {
|
||||
continue;
|
||||
}
|
||||
if (sourceFile.fileName === specialFilePath) {
|
||||
@@ -135,24 +148,24 @@ function addExtraImport(extraImport: string[], importDeclarations: ImportElement
|
||||
if (!specialFileRelatePath.startsWith('./') && !specialFileRelatePath.startsWith('../')) {
|
||||
specialFileRelatePath = './' + specialFileRelatePath;
|
||||
}
|
||||
if (!dtsFileList.includes(specialFilePath)){
|
||||
dtsFileList.push(specialFilePath)
|
||||
if (!dtsFileList.includes(specialFilePath)) {
|
||||
dtsFileList.push(specialFilePath);
|
||||
}
|
||||
specialFileRelatePath = specialFileRelatePath.split(path.sep).join('/');
|
||||
const importStr = `import {${propertyTypeName}} from '${
|
||||
specialFileRelatePath}${
|
||||
specialFileRelatePath.endsWith('/') ? '': '/'}${
|
||||
path.basename(specialFilePath).replace('.d.ts', '').replace('.d.ets', '')}'\n`;
|
||||
if (extraImport.includes(importStr)){
|
||||
specialFileRelatePath.endsWith('/') ? '' : '/'}${
|
||||
path.basename(specialFilePath).replace('.d.ts', '').replace('.d.ets', '')}'\n`;
|
||||
if (extraImport.includes(importStr)) {
|
||||
return;
|
||||
}
|
||||
extraImport.push(importStr);
|
||||
return;
|
||||
}
|
||||
if (propertyTypeName.includes('<')
|
||||
|| propertyTypeName.includes('[')){
|
||||
if (propertyTypeName.includes('<') || propertyTypeName.includes('[')) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
console.log(sourceFile.fileName, 'propertyTypeName', propertyTypeName);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,7 @@ export function generateSourceFileElements(rootName: string, sourceFileEntity: S
|
||||
if (sourceFileEntity.interfaceDeclarations.length > 0) {
|
||||
sourceFileEntity.interfaceDeclarations.forEach(value => {
|
||||
mockApi += generateInterfaceDeclaration('', value, sourceFile, true, sourceFileEntity.interfaceDeclarations,
|
||||
sourceFileEntity.importDeclarations, extraImport) + '\n';
|
||||
sourceFileEntity.importDeclarations, extraImport) + '\n';
|
||||
mockFunctionElements.push({ elementName: value.interfaceName, type: 'interface' });
|
||||
});
|
||||
}
|
||||
@@ -148,7 +148,7 @@ export function generateSourceFileElements(rootName: string, sourceFileEntity: S
|
||||
}
|
||||
});
|
||||
}
|
||||
mockApi = extraImport.join('') + mockApi
|
||||
mockApi = extraImport.join('') + mockApi;
|
||||
return mockApi;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ import { generateSystemIndex } from './generate/generateSystemIndex';
|
||||
*/
|
||||
function getAllDtsFile(dir: string): Array<string> {
|
||||
const arr = fs.readdirSync(dir);
|
||||
if (!dir.toString().includes('node_modules') && !dir.toString().includes(path.join('@internal','component'))) {
|
||||
if (!dir.toString().includes('node_modules') && !dir.toString().includes(path.join('@internal', 'component'))) {
|
||||
arr.forEach(value => {
|
||||
const fullPath = path.join(dir, value);
|
||||
const stats = fs.statSync(fullPath);
|
||||
|
||||
Reference in New Issue
Block a user