# harmony-app-parser 

1. 解析鸿蒙安装包[.app/.hap/.hsp/.har文件的pack.info/module.json]的信息
2. 支持  browser 和 node 环境
3. 支持读取包中任何文本文件内容，支持入参指定文件名

## 安装
```js
    npm i harmony-app-parser
```
## API 
> 宝子们注意下啊， 浏览器和node环境入参类型有差异，个人觉得更实用一点

### 浏览器API    
```js 
 harmonyParser(file:File, fileList?: Array<FileInfo>):Promise
 ```
### nodeAPI   
```js
harmonyParser(filePath:String, fileList?: Array<FileInfo>):Promise
```
### 公共API
```js 
isHarmonyAPP(fileName: String):Boolean  // 文件名是否以.app结尾
```

## 类型
### FileList (指定读取包中的哪些文本文件的内容，默认读取pack.info 和 module.json)

```js 
    interface FileInfo {key: string, fileName: string}

    // 默认设置的入参，不可覆盖且不可重名
    [
        {
            key: 'packInfo',
            fileName: 'pack.info',
        },
        {
            key: 'moduleInfo',
            fileName: 'module.json'
        }
    ]
```

## 使用 
### 浏览器
+ 注入widnow
```html
<body>
    <input type="file" name="file" id="file">
    <script src="./node_modules/harmony-app-parser/dist/harmonyParser-browser.js"></script>
    <script>
        let inputFile = document.querySelector('input');
        inputFile.onchange = function () {
            harmonyParser(this.files[0]).then(res => {
                console.log('res:', res);
            }).catch(err => {
                console.log('err:', err)
            })
        }
    </script>
</body>
```
+ esmodule
```html 
<body>
    <input type="file" name="file" id="file">
    <script type="module">
        import { harmonyParser } from '../dist/harmonyParser-esm.js'
        let inputFile = document.querySelector('input');
        inputFile.onchange = function () {
            harmonyParser(this.files[0]).then(res => {
                console.log('res:', res);
            }).catch(err => {
                console.log('err:', err)
            })
        }
    </script>
</body>
```
+ 或者

```js
import { harmonyParser } from 'harmony-app-parser';

function parserApp(e){
  let file = document.querySelector('input').files[0];
  harmonyParser(file, [{ fileName: 'map.json', key: 'mapInfo'}]).then(res=> {
    console.log('res:', res)
  }).catch(err=> {
    console.log('err:', err);
  })
}

```


### nodejs 
```js
const { isHarmonyApp, harmonyParser } = require('harmony-app-parser')
const path = require('path');

harmonyParser(path.join(__dirname, './module-router-harmony-default-signed.app')).then(res => {
    console.log('res:', res)
}).catch(err => {
    console.log('err:', err)
})
```

###  联系方式：
邮箱： 724228803@qq.com 