From dd2bd2b2b148715f4052010fab928afdd96408c4 Mon Sep 17 00:00:00 2001 From: mmorozov Date: Tue, 13 Feb 2024 13:47:00 +0300 Subject: [PATCH] Add node script to use ohos ts instead of ts Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I927NJ Tests: were not affected Signed-off-by: mmorozov --- .gitignore | 1 + ets2panda/linter/.eslintignore | 1 + ets2panda/linter/package.json | 10 +- .../scripts/install-ohos-typescript.mjs | 107 ++++++++++++++++++ 4 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 ets2panda/linter/scripts/install-ohos-typescript.mjs diff --git a/.gitignore b/.gitignore index c8ed6fcdfe..7fa73d7786 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ ets2panda/linter*/**/package-lock.json **/compile_commands.json .cache .vscode +third_party/ diff --git a/ets2panda/linter/.eslintignore b/ets2panda/linter/.eslintignore index d828d086af..4921dafdff 100644 --- a/ets2panda/linter/.eslintignore +++ b/ets2panda/linter/.eslintignore @@ -20,6 +20,7 @@ /docs/** /node_modules/** /scripts/** +/third_party/** /test/** /test_extended_features/** /test_rules/** diff --git a/ets2panda/linter/package.json b/ets2panda/linter/package.json index f1239d5ea7..bdf6d26383 100644 --- a/ets2panda/linter/package.json +++ b/ets2panda/linter/package.json @@ -14,7 +14,7 @@ "clean": "rimraf build dist bundle", "compile": "npm run tsc", "build": "npm run clean && npm run compile && npm run webpack && npm run pack:linter", - "postinstall": "npm run build", + "postinstall": "node scripts/install-ohos-typescript.mjs && npm run build", "pack:linter": "rimraf bundle && mkdir bundle && npm pack --pack-destination bundle", "pretest": "npm run eslint-check", "test": "npm run test_main && npm run test_rules && npm run test_regression", @@ -29,8 +29,7 @@ }, "dependencies": { "commander": "9.4.0", - "log4js": "6.4.0", - "typescript": "4.9.5" + "log4js": "6.4.0" }, "devDependencies": { "@stylistic/eslint-plugin": "latest", @@ -38,13 +37,16 @@ "@typescript-eslint/eslint-plugin": "latest", "@typescript-eslint/parser": "latest", "eslint": "latest", + "eslint-plugin-import": "^2.29.1", + "eslint-plugin-jsdoc": "^48.0.6", + "eslint-plugin-no-null": "^1.0.2", "prettier": "latest", "rimraf": "^3.0.2", + "shelljs": "^0.8.5", "webpack": "^5.75.0", "webpack-cli": "^5.0.1" }, "bundledDependencies": [ - "typescript", "log4js", "commander" ] diff --git a/ets2panda/linter/scripts/install-ohos-typescript.mjs b/ets2panda/linter/scripts/install-ohos-typescript.mjs new file mode 100644 index 0000000000..421193bb23 --- /dev/null +++ b/ets2panda/linter/scripts/install-ohos-typescript.mjs @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2023-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. + */ + +import * as fs from 'node:fs' +import { fileURLToPath } from 'url' +import { dirname } from 'path' +import { exit } from 'node:process' +import os from 'node:os' +import shell from 'shelljs' + +function detectOS() { + let windowsPlatforms = ['win32', 'win64', 'windows', 'wince'] + let linuxPlatforms = ['linux'] + let detectedOS = null + const opetaringSystemName = os.platform().toLowerCase() + + if (windowsPlatforms.indexOf(opetaringSystemName) !== -1) { + detectedOS = 'Windows' + } else if (linuxPlatforms.indexOf(opetaringSystemName) !== -1) { + detectedOS = 'Linux' + } + + return detectedOS +} + +function getTypescript(detectedOS) { + const __filename = fileURLToPath(import.meta.url); + const __dirname = dirname(__filename); + + const linter = __dirname + '/..' + const third_party = __dirname + '/../third_party' + const typescript_dir = third_party + '/third_party_typescript' + + if (!fs.existsSync(third_party)) { + fs.mkdirSync(third_party); + } + + let branch = process.env.TYPESCRIPT_BRANCH ?? 'master' + + if (detectedOS === 'Linux') { + if (!fs.existsSync(typescript_dir)) { + for (let i = 0; i < 5; i++) { + shell.exec(`git clone --depth=1 https://gitee.com/openharmony/third_party_typescript.git ${typescript_dir}`) + if (fs.existsSync(typescript_dir)) { + break; + } + } + } + if (!fs.existsSync(typescript_dir)) { + exit(1) + } + + shell.cd(typescript_dir) + shell.exec(`git checkout ${branch}`) + shell.exec('git pull') + } else if (detectedOS === 'Windows') { + if (fs.existsSync(typescript_dir)) { + fs.rmSync(typescript_dir, {recursive: true, force: true}) + } + for (let i = 0; i < 5; i++) { + shell.exec(`git clone --depth=1 --branch=${branch} https://gitee.com/openharmony/third_party_typescript.git ${typescript_dir}`) + if (fs.existsSync(typescript_dir)) { + break; + } + } + if (!fs.existsSync(typescript_dir)) { + exit(1) + } + shell.cd(typescript_dir) + + shell.exec('git config core.protectNTFS false') + shell.exec('git checkout') + } else { + console.log('OS was detected, but was not expected') + exit(1) + } + + const npm_package = shell.exec('npm pack').stdout + shell.cd(linter) + shell.exec(`npm install --no-save ${typescript_dir}/${npm_package}`) + shell.rm(`${typescript_dir}/${npm_package}`) + + const node_modules = linter + '/node_modules' + + fs.rmSync(node_modules + '/typescript', {recursive: true, force: true}) + fs.cpSync(node_modules + '/ohos-typescript', node_modules + '/typescript', {recursive: true}) +} + +const detectedOS = detectOS() +if (!detectedOS) { + console.log('OS was not detected') + exit(1) +} +getTypescript(detectedOS) + -- Gitee