UNPKG

5.42 kBPlain TextView Raw
1/*
2本测试用于对华为原子化服务的配置文件ability.xml进行校验。
3ability.xml所需声明内容包括:
41、ability用于声明应用内服务,intent属性用于叫起该服务;
52、display为可选项,用于声明服务页面展示信息;
63、service用于声明提供对应服务的页面;
74、parameter用于声明启动服务所需要的参数信息。
8
9文件格式样式举例如下:
10*******************************************************************************************************************
11<?xml version="1.0" encoding="utf-8"?>
12<aml version="1"
13 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ability.xsd">
14 <ability intent="ability.intent.ORDER_MOVIE">
15 <display name="电影票预定" icon="Common/ticket.png" description="电影票预定"/>
16 <service link="hap://app/com.huawei.www/movie/order">
17 <parameter linkParameter="movieName" intentParameter="movieName" testValue="狮子王" urlEncoding="false"/>
18 </service>
19 </ability>
20 <ability intent="ability.intents.ORDER_TAXI">
21 <display name="打车" icon="Common/taxi.png" description="打车"/>
22 <service link="hap://app/com.huawei.www/taxi/order">
23 <parameter linkParameter="dstLocation" intentParameter="dstLocation" testValue="南京站" urlEncoding="false"/>
24 </service>
25 </ability>
26</aml>
27******************************************************************************************************************
28
29详细配置规范请参考华为开发者网站:developer.huawei.com
30*/
31
32import * as _ from 'lodash/fp'
33import * as fs from 'fs-extra'
34import * as path from 'path'
35import { chalk } from '@tarojs/helper'
36
37import { IErrorLine } from './interface'
38
39const xml2js = require('xml2js')
40
41const ABILITYXML = ['ability.xml']
42
43export default async function ({ appPath }) {
44 const PROJECT_PACKAGE_PATH = path.join(appPath, 'package.json')
45 const PROJECT_FOLDER_FILES = fs.readdirSync('./')
46 if (!fs.existsSync(PROJECT_PACKAGE_PATH)) {
47 console.log(chalk.red(`找不到${PROJECT_PACKAGE_PATH},请确定当前目录是Taro项目根目录!`))
48 process.exit(1)
49 }
50
51 const inProjectFolder = filenames => (_.intersectionBy(_.toLower, PROJECT_FOLDER_FILES, filenames)).length > 0
52 const hasAbilityXML = inProjectFolder(ABILITYXML)
53
54 const errorLines: IErrorLine[] = []
55
56 if (!hasAbilityXML) {
57 errorLines.push({
58 desc: '没有检查到 ability.xml, 编写 ability.xml可以让其他应用方便地叫起应用内服务',
59 valid: true
60 })
61 } else {
62 const PROJECT_ABILITY_FILE_PATH = path.join(appPath, 'ability.xml')
63 const data = fs.readFileSync(PROJECT_ABILITY_FILE_PATH)
64
65 xml2js.parseString(data, function(err, result){
66 // 解析ability.xml配置文件
67 if (err) {
68 errorLines.push({
69 desc: 'ability.xml 解析错误,请检查文件格式!',
70 valid: true
71 })
72 } else {
73 // 检查ability配置
74 const abilities = result.aml.ability;
75 if (abilities == null) {
76 errorLines.push({
77 desc: '没有发现 ability声明, 请检查是否定义完整',
78 valid: true
79 })
80 } else {
81 // 检查intent声明合法性
82 const intentRegex = /ability.intent.[0-9A-Z_]+$/ // intent匹配规则
83 const parameterRegex = /^[A-Za-z0-9_]+$/ // parameter匹配规则
84
85 for(const x in abilities){
86 const abilityIndex = parseInt(x, 10) + 1
87
88 // 检查intent声明
89 if(!intentRegex.test(abilities[x].$.intent)){
90 errorLines.push({
91 desc: '第'+abilityIndex+'个ability的intent格式错误',
92 valid: true,
93 solution: 'intent 必须声明为格式为ability.intent.xxx的字符串,xxx可以包含数字,大写字母和下划线'
94 })
95 }
96
97 // 检查service的parameter声明合法性
98 const services = abilities[x].service;
99
100 if (services == null) {
101 errorLines.push({
102 desc: '第'+abilityIndex+'个ability没有发现 service声明, 请检查是否定义完整',
103 valid: true
104 })
105 } else {
106 for (const y in services) {
107 const serviceIndex = parseInt(y, 10) + 1
108
109 // 校验linkParameter
110 if(!parameterRegex.test(services[y].parameter[0].$.linkParameter)){
111 errorLines.push({
112 desc: '第'+abilityIndex+'个ability的第'+serviceIndex+'个service linkParameter 格式错误',
113 valid: true,
114 solution: 'linkParameter 只能包含数字,大写字母,小写字母和下划线'
115 })
116 }
117
118 // 校验intentParameter
119 if(!parameterRegex.test(services[y].parameter[0].$.intentParameter)){
120 errorLines.push({
121 desc: '第'+abilityIndex+'个ability的第'+serviceIndex+'个service intentParameter 格式错误',
122 valid: true,
123 solution: 'intentParameter 只能包含数字,大写字母,小写字母和下划线'
124 })
125 }
126 }
127 }
128 }
129 }
130 }
131 })
132 }
133
134 return {
135 desc: '检查原子化服务规范',
136 lines: errorLines
137 }
138}