diff --git a/package-lock.json b/package-lock.json index abeb2dc..ed8f033 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "case": "1.6.3", "chalk": "4.1.2", "commander": "9.4.1", + "comment-json": "^4.2.4", "eslint": "8.24.0", "eslint-plugin-simple-import-sort": "^8.0.0", "execa": "5.1.1", @@ -2786,6 +2787,11 @@ "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" }, + "node_modules/array-timsort": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/array-timsort/-/array-timsort-1.0.3.tgz", + "integrity": "sha512-/+3GRL7dDAGEfM6TseQk/U+mi18TU2Ms9I3UlLdUMhz2hbvGNTKdj9xniwXfUqgYhHxRx0+8UnKkvlNwVU+cWQ==" + }, "node_modules/array-union": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz", @@ -3164,6 +3170,21 @@ "node": "^12.20.0 || >=14" } }, + "node_modules/comment-json": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/comment-json/-/comment-json-4.2.4.tgz", + "integrity": "sha512-E5AjpSW+O+N5T2GsOQMHLLsJvrYw6G/AFt9GvU6NguEAfzKShh7hRiLtVo6S9KbRpFMGqE5ojo0/hE+sdteWvQ==", + "dependencies": { + "array-timsort": "^1.0.3", + "core-util-is": "^1.0.3", + "esprima": "^4.0.1", + "has-own-prop": "^2.0.0", + "repeat-string": "^1.6.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/commondir": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", @@ -3194,6 +3215,11 @@ "url": "https://opencollective.com/core-js" } }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -3921,6 +3947,14 @@ "node": ">=8" } }, + "node_modules/has-own-prop": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-own-prop/-/has-own-prop-2.0.0.tgz", + "integrity": "sha512-Pq0h+hvsVm6dDEa8x82GnLSYHOzNDt7f0ddFa3FqcQlgzEiptPqL+XrOJNavjOzSYiYWIrgeVYYgGlLmnxwilQ==", + "engines": { + "node": ">=8" + } + }, "node_modules/has-property-descriptors": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.0.tgz", @@ -5552,6 +5586,14 @@ "jsesc": "bin/jsesc" } }, + "node_modules/repeat-string": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz", + "integrity": "sha512-PV0dzCYDNfRi1jCDbJzpW7jNNDRuCOG/jI5ctQcGKt/clZD+YcPS3yIlWuTJMmESC8aevCFmWJy5wjAFgNqN6w==", + "engines": { + "node": ">=0.10" + } + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", diff --git a/package.json b/package.json index 54b9e9b..501f8b8 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "case": "1.6.3", "chalk": "4.1.2", "commander": "9.4.1", + "comment-json": "^4.2.4", "eslint": "8.24.0", "eslint-plugin-simple-import-sort": "^8.0.0", "execa": "5.1.1", diff --git a/src/build/gen-types.ts b/src/build/gen-types.ts index 54273af..3bb599e 100644 --- a/src/build/gen-types.ts +++ b/src/build/gen-types.ts @@ -2,6 +2,7 @@ import execa from 'execa' import { join, relative } from 'path' import { SOURCE_FOLDER } from '../consts' +import { readTSConfig } from './utils' export const typesDirectoryName = '_types' @@ -15,13 +16,16 @@ export function getDTSPath(srcScript: string, distPath: string, packageDirectory } export async function generateTypes(outputDirectories: string[]) { + const config = await readTSConfig(process.cwd()) + const target = config?.compilerOptions?.target || 'es5' + for (let i = 0; i < outputDirectories.length; i++) { const outputDirectory = outputDirectories[i] await execa('tsc', [ '-d', '--pretty', - '--target', 'es5', + '--target', target, '--outDir', join(outputDirectory, typesDirectoryName), '--skipLibCheck', '--declarationMap', diff --git a/src/build/utils.ts b/src/build/utils.ts index ce79bb4..599c351 100644 --- a/src/build/utils.ts +++ b/src/build/utils.ts @@ -1,3 +1,8 @@ +import { parse } from 'comment-json' +import fs from 'fs' +import { join } from 'path' +import { CompilerOptions, ScriptTarget } from 'typescript' + let verbose = false export function setVerbose(enabled: boolean) { @@ -14,3 +19,21 @@ export async function safeExec(func: () => Promise, failMessage: string, e return error } } + +interface TSConfig { + compilerOptions?: Omit & { + target?: keyof typeof ScriptTarget + } +} + +export async function readTSConfig(cwd: string): Promise { + const configPath = join(cwd, 'tsconfig.json') + const exists = await fs.promises.access(configPath).then(() => true).catch(() => false) + + if (!exists) return null + + const file = await fs.promises.readFile(configPath, 'utf-8') + const config = parse(file) as TSConfig + + return config +}