• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import * as fs from 'node:fs'
17import os from 'node:os'
18import { exit } from 'node:process'
19import { dirname } from 'path'
20import shell from 'shelljs'
21import { fileURLToPath } from 'url'
22
23// waitTime in ms
24function sleep(waitTime) {
25    const start = new Date().getTime();
26    const end = start + waitTime;
27    while (new Date().getTime() < end) {
28        // wait
29    }
30    return;
31}
32
33function detectOS() {
34    let windowsPlatforms = ['win32', 'win64', 'windows', 'wince']
35    let linuxPlatforms = ['linux']
36    let detectedOS = null
37    const opetaringSystemName = os.platform().toLowerCase()
38
39    if (windowsPlatforms.indexOf(opetaringSystemName) !== -1) {
40        detectedOS = 'Windows'
41    } else if (linuxPlatforms.indexOf(opetaringSystemName) !== -1) {
42        detectedOS = 'Linux'
43    }
44
45    return detectedOS
46}
47
48function getTypescript(detectedOS) {
49    const __filename = fileURLToPath(import.meta.url);
50    const __dirname = dirname(__filename);
51
52    const linter = __dirname + '/..'
53    const third_party = __dirname + '/../third_party'
54    const typescript_dir = third_party + '/third_party_typescript'
55
56    if (!fs.existsSync(third_party)) {
57        fs.mkdirSync(third_party);
58    }
59
60    let branch = process.env.TYPESCRIPT_BRANCH ?? 'master'
61
62    if (detectedOS === 'Linux') {
63        let timeToWait = 5000
64        const iterations = 4
65        if (!fs.existsSync(typescript_dir)) {
66            for (let i = 0; i <= iterations; i++) {
67                shell.exec(`git clone --depth=1 https://gitee.com/openharmony/third_party_typescript.git ${typescript_dir}`, { stdio: 'ignore', fatal: true } )
68                if (fs.existsSync(typescript_dir) || i === iterations) {
69                    break;
70                }
71                console.log(`Typescript download failed wait ${timeToWait}ms to restart`)
72                sleep(timeToWait)
73                timeToWait *= 2
74            }
75        }
76        if (!fs.existsSync(typescript_dir)) {
77            console.log(`Failed to download Typescript`)
78            exit(1)
79        }
80
81        shell.cd(typescript_dir)
82        shell.exec(`git checkout ${branch}`)
83        shell.exec('git pull')
84    } else if (detectedOS === 'Windows') {
85        if (fs.existsSync(typescript_dir)) {
86            fs.rmSync(typescript_dir, {recursive: true, force: true})
87        }
88        for (let i = 0; i < 5; i++) {
89            shell.exec(`git clone --depth=1 --branch=${branch} https://gitee.com/openharmony/third_party_typescript.git ${typescript_dir}`)
90            if (fs.existsSync(typescript_dir)) {
91                break;
92            }
93        }
94        if (!fs.existsSync(typescript_dir)) {
95            exit(1)
96        }
97        shell.cd(typescript_dir)
98
99        shell.exec('git config core.protectNTFS false')
100        shell.exec('git checkout')
101    } else {
102        console.log('OS was detected, but was not expected')
103        exit(1)
104    }
105
106    const npm_package = shell.exec('npm pack').stdout
107    shell.cd(linter)
108    shell.exec(`npm install --no-save ${typescript_dir}/${npm_package}`)
109    shell.rm(`${typescript_dir}/${npm_package}`)
110
111    const node_modules = linter + '/node_modules'
112
113    fs.rmSync(node_modules + '/typescript', {recursive: true, force: true})
114    fs.cpSync(node_modules + '/ohos-typescript', node_modules + '/typescript', {recursive: true})
115}
116
117const detectedOS = detectOS()
118if (!detectedOS) {
119    console.log('OS was not detected')
120    exit(1)
121}
122getTypescript(detectedOS)
123
124