mirror of
https://github.com/openharmony/developtools_ace-ets2bundle.git
synced 2026-07-20 19:47:44 -04:00
Fix ut tests
Signed-off-by: hufeng <hufeng20@huawei.com> Change-Id: Ibba3d45b78bf6d10981d034a5e6e61834f8812df
This commit is contained in:
@@ -112,14 +112,8 @@ export function createLanguageService(rootFileNames: string[]): ts.LanguageServi
|
||||
|
||||
function getOhmUrlFile(moduleName: string): {modulePath: string, suffix: string} {
|
||||
let modulePath = resolveSourceFile(moduleName);
|
||||
let suffix: string;
|
||||
if (modulePath.endsWith('.ets')) {
|
||||
suffix = '.ets';
|
||||
} else if (modulePath.endsWith('.ts')) {
|
||||
suffix = '.ts';
|
||||
} else if (modulePath.endsWith('.js')) {
|
||||
suffix = '.js';
|
||||
} else {
|
||||
let suffix: string = path.extname(modulePath);
|
||||
if (suffix === 'ts' && modulePath.endsWith('.d.ts')) {
|
||||
suffix = '.d.ts';
|
||||
}
|
||||
|
||||
@@ -140,7 +134,7 @@ function resolveModuleNames(moduleNames: string[], containingFile: string): ts.R
|
||||
if (result.resolvedModule) {
|
||||
resolvedModules.push(result.resolvedModule);
|
||||
} else if (/^@bundle:/.test(moduleName.trim())) {
|
||||
const module = getOhmUrlFile(moduleName.trim());
|
||||
const module: {modulePath: string, suffix: string} = getOhmUrlFile(moduleName.trim());
|
||||
if (ts.sys.fileExists(module.modulePath)) {
|
||||
resolvedModules.push(getResolveModule(module.modulePath, module.suffix));
|
||||
} else {
|
||||
|
||||
@@ -1,59 +1,69 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { logger } from './compile_info';
|
||||
const { projectConfig } = require('../main');
|
||||
|
||||
const red: string = '\u001b[31m';
|
||||
const reset: string = '\u001b[39m';
|
||||
|
||||
const REG_OHM_URL = /^@bundle:(\S+)\/(\S+)\/(ets|js)\/(\S+)$/;
|
||||
|
||||
export class OHMResolverPlugin {
|
||||
private source: any;
|
||||
private target: any;
|
||||
|
||||
constructor(source = 'resolve', target = 'resolve') {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
apply(resolver) {
|
||||
const target = resolver.ensureHook(this.target);
|
||||
resolver.getHook(this.source).tapAsync("OHMResolverPlugin", (request, resolveContext, callback) => {
|
||||
if (isOhmUrl(request.request)) {
|
||||
var resolvedSourceFile: string = resolveSourceFile(request.request);
|
||||
var obj = Object.assign({}, request, {
|
||||
request: resolvedSourceFile
|
||||
});
|
||||
return resolver.doResolve(target, obj, null, resolveContext, callback);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function isOhmUrl(moduleRequest: string) {
|
||||
return /^@(\S+):/.test(moduleRequest) ? true : false;
|
||||
}
|
||||
|
||||
export function resolveSourceFile(ohmUrl: string): string {
|
||||
const result = ohmUrl.match(REG_OHM_URL);
|
||||
let moduleName = result[2];
|
||||
let srcKind = result[3];
|
||||
let file = path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main', srcKind, result[4]);
|
||||
if (fs.existsSync(file + '.ets') && fs.statSync(file + '.ets').isFile()) {
|
||||
file += '.ets';
|
||||
} else if (fs.existsSync(file + '.ts') && fs.statSync(file + '.ts').isFile()) {
|
||||
file += '.ts';
|
||||
} else if (fs.existsSync(file + '.js') && fs.statSync(file + '.js').isFile()) {
|
||||
file += '.js';
|
||||
} else {
|
||||
file += '.d.ts';
|
||||
}
|
||||
|
||||
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
|
||||
logger.error(red, `ETS:ERROR Failed to resolve existed file by this ohm url ${ohmUrl} `, reset);
|
||||
}
|
||||
|
||||
return file;
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { logger } from './compile_info';
|
||||
const { projectConfig } = require('../main');
|
||||
|
||||
const red: string = '\u001b[31m';
|
||||
const reset: string = '\u001b[39m';
|
||||
|
||||
const REG_OHM_URL: RegExp = /^@bundle:(\S+)\/(\S+)\/(ets|js)\/(\S+)$/;
|
||||
|
||||
export class OHMResolverPlugin {
|
||||
private source: any;
|
||||
private target: any;
|
||||
|
||||
constructor(source = 'resolve', target = 'resolve') {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
apply(resolver) {
|
||||
const target = resolver.ensureHook(this.target);
|
||||
resolver.getHook(this.source).tapAsync("OHMResolverPlugin", (request, resolveContext, callback) => {
|
||||
if (isOhmUrl(request.request)) {
|
||||
var resolvedSourceFile: string = resolveSourceFile(request.request);
|
||||
var obj = Object.assign({}, request, {
|
||||
request: resolvedSourceFile
|
||||
});
|
||||
return resolver.doResolve(target, obj, null, resolveContext, callback);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function isOhmUrl(moduleRequest: string): boolean {
|
||||
return /^@(\S+):/.test(moduleRequest) ? true : false;
|
||||
}
|
||||
|
||||
function addExtension(file: string): string {
|
||||
if (path.extname(file) != '') {
|
||||
return file;
|
||||
}
|
||||
|
||||
let extension: string = '.d.ts';
|
||||
if (fs.existsSync(file + '.ets') && fs.statSync(file + '.ets').isFile()) {
|
||||
extension = '.ets';
|
||||
}
|
||||
if (fs.existsSync(file + '.ts') && fs.statSync(file + '.ts').isFile()) {
|
||||
extension = '.ts';
|
||||
}
|
||||
if (fs.existsSync(file + '.js') && fs.statSync(file + '.js').isFile()) {
|
||||
extension = '.js';
|
||||
}
|
||||
return file + extension;
|
||||
}
|
||||
|
||||
export function resolveSourceFile(ohmUrl: string): string {
|
||||
const result = ohmUrl.match(REG_OHM_URL);
|
||||
let moduleName = result[2];
|
||||
let srcKind = result[3];
|
||||
let file = path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main', srcKind, result[4]);
|
||||
file = addExtension(file);
|
||||
|
||||
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
|
||||
logger.error(red, `ETS:ERROR Failed to resolve existed file by this ohm url ${ohmUrl} `, reset);
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
@@ -844,7 +844,7 @@ function getPackageInfo(configFile: string): Array<string> {
|
||||
return [bundleName, moduleName];
|
||||
}
|
||||
|
||||
function replaceSystemApi(item: string, systemValue: string, moduleType: string, systemKey: string) {
|
||||
function replaceSystemApi(item: string, systemValue: string, moduleType: string, systemKey: string): string {
|
||||
moduleCollection.add(`${moduleType}.${systemKey}`);
|
||||
if (NATIVE_MODULE.has(`${moduleType}.${systemKey}`)) {
|
||||
item = `var ${systemValue} = globalThis.requireNativeModule('${moduleType}.${systemKey}')`;
|
||||
@@ -860,7 +860,7 @@ function replaceSystemApi(item: string, systemValue: string, moduleType: string,
|
||||
return item;
|
||||
}
|
||||
|
||||
function replaceLibSo(importValue: string, libSoKey: string, sourcePath: string = null) {
|
||||
function replaceLibSo(importValue: string, libSoKey: string, sourcePath: string = null): string {
|
||||
if (sourcePath) {
|
||||
useOSFiles.add(sourcePath);
|
||||
}
|
||||
@@ -936,16 +936,18 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin
|
||||
}
|
||||
|
||||
function replaceRelativePath(item:string, moduleRequest: string, sourcePath: string) {
|
||||
let filePath = path.resolve(path.dirname(sourcePath), moduleRequest);
|
||||
let result = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/);
|
||||
if (result) {
|
||||
const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath :
|
||||
path.join(result[1], 'src/main/config.json');
|
||||
let packageInfo = getPackageInfo(configJsonFile);
|
||||
let bundleName = packageInfo[0];
|
||||
let moduleName = packageInfo[1];
|
||||
moduleRequest = `@bundle:${bundleName}/${moduleName}/${result[5]}/${toUnixPath(result[7])}`;
|
||||
item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"');
|
||||
if (sourcePath) {
|
||||
let filePath = path.resolve(path.dirname(sourcePath), moduleRequest);
|
||||
let result = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/);
|
||||
if (result) {
|
||||
const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath :
|
||||
path.join(result[1], 'src/main/config.json');
|
||||
let packageInfo = getPackageInfo(configJsonFile);
|
||||
let bundleName = packageInfo[0];
|
||||
let moduleName = packageInfo[1];
|
||||
moduleRequest = `@bundle:${bundleName}/${moduleName}/${result[5]}/${toUnixPath(result[7])}`;
|
||||
item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"');
|
||||
}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user