1/* 2 * Copyright (c) 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 * as path from 'node:path' 18import { exit } from 'node:process' 19 20const STANDARD_LIBRARIES = [ 21 'lib.d.ts', 22 'lib.dom.iterable.d.ts', 23 'lib.dom.d.ts', 24 'lib.es2015.symbol.wellknown.d.ts', 25 'lib.es2015.symbol.d.ts', 26 'lib.es2015.reflect.d.ts', 27 'lib.es2015.proxy.d.ts', 28 'lib.es2015.promise.d.ts', 29 'lib.es2015.iterable.d.ts', 30 'lib.es2015.generator.d.ts', 31 'lib.es2015.d.ts', 32 'lib.es2015.core.d.ts', 33 'lib.es2015.collection.d.ts', 34 'lib.es2016.full.d.ts', 35 'lib.es2016.d.ts', 36 'lib.es2016.array.include.d.ts', 37 'lib.es2017.typedarrays.d.ts', 38 'lib.es2017.string.d.ts', 39 'lib.es2017.sharedmemory.d.ts', 40 'lib.es2017.object.d.ts', 41 'lib.es2017.intl.d.ts', 42 'lib.es2017.full.d.ts', 43 'lib.es2017.d.ts', 44 'lib.es2018.regexp.d.ts', 45 'lib.es2018.promise.d.ts', 46 'lib.es2018.intl.d.ts', 47 'lib.es2018.full.d.ts', 48 'lib.es2018.d.ts', 49 'lib.es2018.asynciterable.d.ts', 50 'lib.es2018.asyncgenerator.d.ts', 51 'lib.es2019.symbol.d.ts', 52 'lib.es2019.string.d.ts', 53 'lib.es2019.object.d.ts', 54 'lib.es2019.intl.d.ts', 55 'lib.es2019.full.d.ts', 56 'lib.es2019.d.ts', 57 'lib.es2019.array.d.ts', 58 'lib.es2020.symbol.wellknown.d.ts', 59 'lib.es2020.string.d.ts', 60 'lib.es2020.sharedmemory.d.ts', 61 'lib.es2020.promise.d.ts', 62 'lib.es2020.number.d.ts', 63 'lib.es2020.intl.d.ts', 64 'lib.es2020.full.d.ts', 65 'lib.es2020.date.d.ts', 66 'lib.es2020.d.ts', 67 'lib.es2020.bigint.d.ts', 68 'lib.es2021.weakref.d.ts', 69 'lib.es2021.string.d.ts', 70 'lib.es2021.promise.d.ts', 71 'lib.es2021.intl.d.ts', 72 'lib.es2021.full.d.ts', 73 'lib.es2021.d.ts', 74 'lib.es2022.string.d.ts', 75 'lib.es2022.sharedmemory.d.ts', 76 'lib.es2022.object.d.ts', 77 'lib.es2022.intl.d.ts', 78 'lib.es2022.full.d.ts', 79 'lib.es2022.error.d.ts', 80 'lib.es2022.d.ts', 81 'lib.es2022.array.d.ts', 82 'lib.es6.d.ts', 83 'lib.es5.d.ts', 84 'lib.esnext.weakref.d.ts', 85 'lib.esnext.string.d.ts', 86 'lib.esnext.promise.d.ts', 87 'lib.esnext.intl.d.ts', 88 'lib.esnext.full.d.ts', 89 'lib.esnext.d.ts', 90 'lib.scripthost.d.ts', 91 'lib.webworker.iterable.d.ts', 92 'lib.webworker.importscripts.d.ts', 93 'lib.webworker.d.ts' 94]; 95 96function reportErrorAndExit(msg) { 97 console.log(msg); 98 exit(1); 99} 100 101function copyDirectorySync(srcPath, destPath, options) { 102 const entries = fs.readdirSync(srcPath, { withFileTypes: true }); 103 104 if (!fs.existsSync(destPath)) { 105 fs.mkdirSync(destPath, { recursive: true }); 106 } 107 108 for (const entry of entries) { 109 const srcEntry = path.join(srcPath, entry.name); 110 const destEntry = path.join(destPath, entry.name); 111 112 if (typeof options.filter === 'function' && !options.filter(srcEntry, destEntry)) { 113 continue; 114 } 115 116 if (entry.isDirectory()) { 117 if (options.recursive) { 118 copyDirectorySync(srcEntry, destEntry, options); 119 } 120 } else { 121 fs.copyFileSync(srcEntry, destEntry); 122 } 123 } 124} 125 126function copyTypescriptLibDeclarationsToDist() { 127 const typescript_lib = path.join('node_modules', 'typescript', 'lib'); 128 const dist = path.join('dist'); 129 130 if (!fs.existsSync(typescript_lib)) { 131 reportErrorAndExit('Failed to locate ' + typescript_lib + ' directory'); 132 } 133 if (!fs.existsSync(dist)) { 134 reportErrorAndExit('Failed to locate ' + dist + ' directory'); 135 } 136 137 const srcPath = path.resolve(typescript_lib); 138 const distPath = path.resolve(dist); 139 140 copyDirectorySync(srcPath, distPath, { 141 recursive: true, 142 filter: (src, dest) => { 143 if (!src?.length) return false; 144 const stats = fs.statSync(src); 145 if (stats.isDirectory()) { 146 const normalizedSrc = path.resolve(src); 147 return normalizedSrc === path.resolve(srcPath) || normalizedSrc.endsWith(srcPath); 148 } 149 return STANDARD_LIBRARIES.includes(path.basename(src)); 150 } 151 }); 152} 153 154copyTypescriptLibDeclarationsToDist();