16a23e08bSopenharmony_ci/* 26a23e08bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 36a23e08bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46a23e08bSopenharmony_ci * you may not use this file except in compliance with the License. 56a23e08bSopenharmony_ci * You may obtain a copy of the License at 66a23e08bSopenharmony_ci * 76a23e08bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86a23e08bSopenharmony_ci * 96a23e08bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106a23e08bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116a23e08bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126a23e08bSopenharmony_ci * See the License for the specific language governing permissions and 136a23e08bSopenharmony_ci * limitations under the License. 146a23e08bSopenharmony_ci */ 156a23e08bSopenharmony_ci 166a23e08bSopenharmony_ciconst path = require('path'); 176a23e08bSopenharmony_ciconst fs = require('fs'); 186a23e08bSopenharmony_ciconst os = require('os'); 196a23e08bSopenharmony_ciconst isType = require('./lite-utils'); 206a23e08bSopenharmony_ci/** 216a23e08bSopenharmony_ci * Check if the custom file exists.If it does not exist, follow the normal process. 226a23e08bSopenharmony_ci * If it exists, get the file content. 236a23e08bSopenharmony_ci */ 246a23e08bSopenharmony_cifunction checkFilePath() { 256a23e08bSopenharmony_ci const rulePath = path.resolve(os.userInfo().homedir, '.literc.js'); 266a23e08bSopenharmony_ci if (fs.existsSync(rulePath)) { 276a23e08bSopenharmony_ci process.env.RULE_PATH = rulePath; 286a23e08bSopenharmony_ci const customTag = require(rulePath); 296a23e08bSopenharmony_ci checkContent(customTag); 306a23e08bSopenharmony_ci } 316a23e08bSopenharmony_ci} 326a23e08bSopenharmony_ci/** 336a23e08bSopenharmony_ci * Check if the object thrown by the file is correct. 346a23e08bSopenharmony_ci * @param {Object} customTag User defined custom file content. 356a23e08bSopenharmony_ci */ 366a23e08bSopenharmony_cifunction checkContent(customTag) { 376a23e08bSopenharmony_ci throwError( 386a23e08bSopenharmony_ci !isType.isObject(customTag), 396a23e08bSopenharmony_ci `The configuration in the '.literc.js' file is incorrect.(it should be an object.)`, 406a23e08bSopenharmony_ci ); 416a23e08bSopenharmony_ci throwError( 426a23e08bSopenharmony_ci isType.isUndefined(customTag.rules), 436a23e08bSopenharmony_ci `You must write the 'rules' attribute in '.literc.js' file`, 446a23e08bSopenharmony_ci ); 456a23e08bSopenharmony_ci throwError( 466a23e08bSopenharmony_ci !isType.isObject(customTag.rules), 476a23e08bSopenharmony_ci `The value of 'rules' in '.literc.js' file is incorrect.(it should be an object)`, 486a23e08bSopenharmony_ci ); 496a23e08bSopenharmony_ci if (customTag.extends == 'recommended') { 506a23e08bSopenharmony_ci validatorCustomTag(customTag.rules); 516a23e08bSopenharmony_ci } 526a23e08bSopenharmony_ci} 536a23e08bSopenharmony_ci 546a23e08bSopenharmony_ci/** 556a23e08bSopenharmony_ci * Check whether the user-defined rules are correct. 566a23e08bSopenharmony_ci * @param {Object} rules User defined custom file content. 576a23e08bSopenharmony_ci */ 586a23e08bSopenharmony_cifunction validatorCustomTag(rules) { 596a23e08bSopenharmony_ci const keys = Object.keys(rules); 606a23e08bSopenharmony_ci for (let i = 0; i < keys.length; i++) { 616a23e08bSopenharmony_ci const key = keys[i]; 626a23e08bSopenharmony_ci const value = rules[key]; 636a23e08bSopenharmony_ci throwError( 646a23e08bSopenharmony_ci !isType.isObject(value), 656a23e08bSopenharmony_ci `The value of '${key}' is incorrect, it should be an object.`, 666a23e08bSopenharmony_ci ); 676a23e08bSopenharmony_ci const children = Object.keys(value); 686a23e08bSopenharmony_ci for (let j = 0; j < children.length; j++) { 696a23e08bSopenharmony_ci const child = children[j]; 706a23e08bSopenharmony_ci throwError( 716a23e08bSopenharmony_ci child != 'attrs', 726a23e08bSopenharmony_ci `'${key}' object can only contain 'attrs' attributes`, 736a23e08bSopenharmony_ci ); 746a23e08bSopenharmony_ci } 756a23e08bSopenharmony_ci } 766a23e08bSopenharmony_ci} 776a23e08bSopenharmony_ci/** 786a23e08bSopenharmony_ci * Tool method, if the condition is true, throw an exception. 796a23e08bSopenharmony_ci * @param {Boolean} condition Analyzing conditions. 806a23e08bSopenharmony_ci * @param {String} reason Output wrong information. 816a23e08bSopenharmony_ci */ 826a23e08bSopenharmony_cifunction throwError(condition, reason) { 836a23e08bSopenharmony_ci if (condition) { 846a23e08bSopenharmony_ci throw Error(`\u001b[31mError: ${reason} \u001b[39m`).message; 856a23e08bSopenharmony_ci } 866a23e08bSopenharmony_ci} 876a23e08bSopenharmony_ciexports.checkFilePath = checkFilePath; 88