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 copyTypescriptLibDeclarationsToDist() { 102 const typescript_lib = path.join('node_modules', 'typescript', 'lib'); 103 const dist = path.join('dist'); 104 105 if (!fs.existsSync(typescript_lib)) { 106 reportErrorAndExit('Failed to locate ' + typescript_lib + ' directory'); 107 } 108 if (!fs.existsSync(dist)) { 109 reportErrorAndExit('Failed to locate ' + dist + ' directory'); 110 } 111 112 const srcPath = path.resolve(typescript_lib); 113 const distPath = path.resolve(dist); 114 115 fs.cpSync(srcPath, distPath, { 116 force: true, 117 recursive: true, 118 filter: (src, dest) => { 119 if (!src?.length) { 120 return false; 121 } 122 if (fs.statSync(src).isDirectory()) { 123 124 /* 125 * Return true only for the input directory in order to copy its context. Ignore other sub-directories. 126 * NOTE: On windows, Node appends special prefix for long paths ("\\?\") to path values. In such case, 127 * compare the path using the 'endsWith' method. 128 */ 129 const normalizedSrc = path.resolve(src); 130 return normalizedSrc === srcPath || normalizedSrc.endsWith(srcPath); 131 } 132 return STANDARD_LIBRARIES.includes(path.basename(src)); 133 } 134 }); 135} 136 137copyTypescriptLibDeclarationsToDist();