From a985a01f15eb53d0eeb8c520e1a291ed777e8199 Mon Sep 17 00:00:00 2001 From: jerryzcx Date: Mon, 7 Jul 2025 10:55:38 +0800 Subject: [PATCH] fix npm plugin using --- customPlugin/hvigor/hvigor-config.json5 | 1 + customPlugin/library/Index.ets | 2 +- customPlugin/library/oh-package.json5 | 2 +- .../src/main/ets/components/MainPage.ets | 13 ++++ customPlugin/npm_plugin/index.ts | 1 + customPlugin/npm_plugin/package.json | 14 ++++ .../pure_plugin.ts} | 74 +++---------------- customPlugin/npm_plugin/tsconfig.json | 10 +++ 8 files changed, 53 insertions(+), 64 deletions(-) create mode 100644 customPlugin/library/src/main/ets/components/MainPage.ets create mode 100644 customPlugin/npm_plugin/index.ts create mode 100644 customPlugin/npm_plugin/package.json rename customPlugin/{library/src/main/ets/components/customPlugin.ts => npm_plugin/pure_plugin.ts} (32%) create mode 100644 customPlugin/npm_plugin/tsconfig.json diff --git a/customPlugin/hvigor/hvigor-config.json5 b/customPlugin/hvigor/hvigor-config.json5 index 5bebc975..9b908cb5 100644 --- a/customPlugin/hvigor/hvigor-config.json5 +++ b/customPlugin/hvigor/hvigor-config.json5 @@ -1,6 +1,7 @@ { "modelVersion": "5.0.5", "dependencies": { +// "npm_plugin": "file:D:\\npm_plugin.tgz" }, "execution": { // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ diff --git a/customPlugin/library/Index.ets b/customPlugin/library/Index.ets index 6e1aba4a..42bf4161 100644 --- a/customPlugin/library/Index.ets +++ b/customPlugin/library/Index.ets @@ -1 +1 @@ -export { renameHarTask } from './src/main/ets/components/customPlugin'; +export { MainPage } from './src/main/ets/components/MainPage'; diff --git a/customPlugin/library/oh-package.json5 b/customPlugin/library/oh-package.json5 index b087f575..b14c1b57 100644 --- a/customPlugin/library/oh-package.json5 +++ b/customPlugin/library/oh-package.json5 @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "Please describe the basic information.", "main": "Index.ets", - "author": "", + "author": "TestName", "license": "Apache-2.0", "dependencies": {} } diff --git a/customPlugin/library/src/main/ets/components/MainPage.ets b/customPlugin/library/src/main/ets/components/MainPage.ets new file mode 100644 index 00000000..563edbf0 --- /dev/null +++ b/customPlugin/library/src/main/ets/components/MainPage.ets @@ -0,0 +1,13 @@ +@Preview +@Component +export struct MainPage { + @State message: string = 'Hello World'; + + build() { + Row() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + }.height('100%') + } +} \ No newline at end of file diff --git a/customPlugin/npm_plugin/index.ts b/customPlugin/npm_plugin/index.ts new file mode 100644 index 00000000..d5b88657 --- /dev/null +++ b/customPlugin/npm_plugin/index.ts @@ -0,0 +1 @@ +export { renameHarTask } from './pure_plugin' \ No newline at end of file diff --git a/customPlugin/npm_plugin/package.json b/customPlugin/npm_plugin/package.json new file mode 100644 index 00000000..b4306eb8 --- /dev/null +++ b/customPlugin/npm_plugin/package.json @@ -0,0 +1,14 @@ +{ + "name": "npm_plugin", + "version": "1.0.0", + "description": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "1.0.0", + "license": "ISC", + "devDependencies": { + "@ohos/hvigor": "5.2.2" + } +} \ No newline at end of file diff --git a/customPlugin/library/src/main/ets/components/customPlugin.ts b/customPlugin/npm_plugin/pure_plugin.ts similarity index 32% rename from customPlugin/library/src/main/ets/components/customPlugin.ts rename to customPlugin/npm_plugin/pure_plugin.ts index 1e36ecef..98677b6b 100644 --- a/customPlugin/library/src/main/ets/components/customPlugin.ts +++ b/customPlugin/npm_plugin/pure_plugin.ts @@ -1,25 +1,4 @@ -/* -* Copyright (C) 2024 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -/** - * - *最佳实践: 定制hvigor插件开发实践 - */ - -// [Start custom_plugin] -import { fileIo as fs } from '@kit.CoreFileKit'; -import { util } from '@kit.ArkTS'; +import fs from 'fs'; interface OhPackage { name: string; @@ -28,30 +7,6 @@ interface OhPackage { author: string; } -function readFileSync(filePath: string): string { - try { - const fd = fs.openSync(filePath, fs.OpenMode.READ_ONLY).fd; - const stat = fs.statSync(filePath); - - const buffer = new ArrayBuffer(stat.size); - const bufferView = new Uint8Array(buffer); - - const bytesRead = fs.readSync( - fd, - bufferView, - { length: stat.size } - ); - - fs.closeSync(fd); - - const textDecoder = new util.TextDecoder('utf-8'); - return textDecoder.decodeToString(bufferView); - } catch (err) { - console.error('Read file failed:', err); - throw err; // Re-throw the error for upper-level handling - } -} - export function renameHarTask(str?: string) { return { pluginId: 'RenameHarTaskID', @@ -61,28 +16,24 @@ export function renameHarTask(str?: string) { name: 'renameHarTask', run: (taskContext) => { // Read oh-package.json5 and parse the version - const packageFile = taskContext.modulePath+'\\oh-package.json5'; - console.log('file: ', packageFile); - let fileContent = this.readFileSync(packageFile, 'utf8'); - console.log(fileContent); + const packageFile = taskContext.modulePath + '\\oh-package.json5'; + let fileContent = fs.readFileSync(packageFile, 'utf8'); const content: OhPackage = JSON.parse(fileContent); const version = content.version; const author = content.author; - - console.log('renameHarTask: ', taskContext.moduleName, taskContext.modulePath); - const sourceFile = taskContext.modulePath + '\\build\\default\\outputs\\default\\' + taskContext.moduleName + '.har'; + const sourceFile = + taskContext.modulePath + '\\build\\default\\outputs\\default\\' + taskContext.moduleName + '.har'; const targetPath = taskContext.modulePath + '\\build\\default\\outputs\\target\\'; const targetFile = targetPath - + taskContext.moduleName + '-' + version + '-' + author +'.har'; - console.log('renameHarTask: sourceFile: ', sourceFile); - console.log('renameHarTask: targetFile: ', targetFile); + + taskContext.moduleName + '-' + version + '-' + author + '.har'; // Create Directory - fs.mkdir(targetPath, true, (err) => { - if (err) throw err; + fs.mkdir(targetPath, { recursive: true }, (err) => { + if (err) { + throw err; + } // Move and modify product file names - fs.rename(sourceFile, targetFile, (err)=> { - console.log('err: ' + err); + fs.rename(sourceFile, targetFile, (err) => { }); }); }, @@ -92,5 +43,4 @@ export function renameHarTask(str?: string) { }) } } -} -// [End custom_plugin] \ No newline at end of file +} \ No newline at end of file diff --git a/customPlugin/npm_plugin/tsconfig.json b/customPlugin/npm_plugin/tsconfig.json new file mode 100644 index 00000000..5df481cd --- /dev/null +++ b/customPlugin/npm_plugin/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "es2016", + "module": "commonjs", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true + } +} \ No newline at end of file -- Gitee