@sanity/pkg-utils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/bin/pkg-utils +2 -0
  4. package/dist/_build-3336bcb3.cjs +5432 -0
  5. package/dist/_build-3336bcb3.cjs.map +1 -0
  6. package/dist/cli.cjs +42 -0
  7. package/dist/cli.cjs.map +1 -0
  8. package/dist/index.cjs +37 -0
  9. package/dist/index.cjs.map +1 -0
  10. package/dist/index.d.ts +136 -0
  11. package/dist/index.js +5412 -0
  12. package/dist/index.js.map +1 -0
  13. package/package.json +107 -0
  14. package/src/build/_parseTasks.ts +85 -0
  15. package/src/build/_types.ts +25 -0
  16. package/src/build/build.ts +87 -0
  17. package/src/build/index.ts +2 -0
  18. package/src/build/tasks/_types.ts +8 -0
  19. package/src/build/tasks/dts/_buildTypes.ts +62 -0
  20. package/src/build/tasks/dts/_createApiExtractorConfig.ts +88 -0
  21. package/src/build/tasks/dts/_createTSDocConfig.ts +57 -0
  22. package/src/build/tasks/dts/_extractTypes.ts +101 -0
  23. package/src/build/tasks/dts/_loadTSConfig.ts +18 -0
  24. package/src/build/tasks/dts/dts.ts +60 -0
  25. package/src/build/tasks/dts/index.ts +1 -0
  26. package/src/build/tasks/extract/_extractTask.ts +49 -0
  27. package/src/build/tasks/extract/index.ts +1 -0
  28. package/src/build/tasks/index.ts +4 -0
  29. package/src/build/tasks/rollup/_resolveRollupConfig.ts +95 -0
  30. package/src/build/tasks/rollup/index.ts +1 -0
  31. package/src/build/tasks/rollup/rollup.ts +96 -0
  32. package/src/cli.ts +25 -0
  33. package/src/core/_isRecord.ts +6 -0
  34. package/src/core/config/_loadConfig.ts +30 -0
  35. package/src/core/config/_resolveConfigProperty.ts +12 -0
  36. package/src/core/config/defineConfig.ts +8 -0
  37. package/src/core/config/index.ts +4 -0
  38. package/src/core/config/types.ts +48 -0
  39. package/src/core/index.ts +3 -0
  40. package/src/core/pkg/_loadPkg.ts +13 -0
  41. package/src/core/pkg/_parseExports.ts +41 -0
  42. package/src/core/pkg/_types.ts +18 -0
  43. package/src/core/pkg/index.ts +3 -0
  44. package/src/index.ts +2 -0
package/package.json ADDED
@@ -0,0 +1,107 @@
1
+ {
2
+ "name": "@sanity/pkg-utils",
3
+ "version": "1.0.0",
4
+ "author": "Sanity.io <hello@sanity.io>",
5
+ "license": "MIT",
6
+ "description": "Simple utilities for modern npm packages.",
7
+ "keywords": [
8
+ "sanity-io",
9
+ "sanity",
10
+ "npm",
11
+ "package",
12
+ "utilities",
13
+ "build",
14
+ "bundling"
15
+ ],
16
+ "source": "./src/index.ts",
17
+ "main": "./dist/index.cjs",
18
+ "module": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "default": "./dist/index.js",
23
+ "require": "./dist/index.cjs",
24
+ "types": "./dist/index.d.ts"
25
+ }
26
+ },
27
+ "bin": {
28
+ "pkg-utils": "./bin/pkg-utils"
29
+ },
30
+ "files": [
31
+ "src",
32
+ "dist"
33
+ ],
34
+ "scripts": {
35
+ "build": "node -r esbuild-register src/cli.ts build --extract --tsconfig tsconfig.dist.json",
36
+ "format": "prettier --write --cache --ignore-unknown .",
37
+ "lint": "eslint . --ext .cjs,.js,.ts",
38
+ "prebuild": "rimraf dist",
39
+ "prepublishOnly": "pnpm run build",
40
+ "release": "semantic-release",
41
+ "test": "vitest run",
42
+ "type-check": "tsc --noEmit"
43
+ },
44
+ "dependencies": {
45
+ "@microsoft/api-extractor": "^7.29.5",
46
+ "@microsoft/tsdoc-config": "^0.16.1",
47
+ "@rollup/plugin-commonjs": "^22.0.2",
48
+ "@rollup/plugin-json": "^4.1.0",
49
+ "@rollup/plugin-node-resolve": "^13.3.0",
50
+ "@sanity/tsdoc-to-portable-text": "^0.4.1",
51
+ "cac": "^6.7.12",
52
+ "chalk": "^4.1.2",
53
+ "esbuild": "^0.15.5",
54
+ "esbuild-register": "^3.3.3",
55
+ "jsonc-parser": "^3.1.0",
56
+ "mkdirp": "^1.0.4",
57
+ "pkg-up": "^3.1.0",
58
+ "rimraf": "^3.0.2",
59
+ "rollup": "^2.78.1",
60
+ "rollup-plugin-esbuild": "^4.9.3"
61
+ },
62
+ "devDependencies": {
63
+ "@commitlint/cli": "^17.0.3",
64
+ "@commitlint/config-conventional": "^17.0.3",
65
+ "@sanity/semantic-release-preset": "^2.0.0",
66
+ "@types/cpx": "^1.5.2",
67
+ "@types/mkdirp": "^1.0.2",
68
+ "@types/node": "^18.7.13",
69
+ "@types/rimraf": "^3.0.2",
70
+ "@types/tmp": "^0.2.3",
71
+ "@typescript-eslint/eslint-plugin": "^5.35.1",
72
+ "@typescript-eslint/parser": "^5.35.1",
73
+ "commitizen": "^4.2.5",
74
+ "cpx": "^1.5.0",
75
+ "eslint": "^8.23.0",
76
+ "eslint-config-prettier": "^8.5.0",
77
+ "eslint-plugin-import": "^2.26.0",
78
+ "eslint-plugin-prettier": "^4.2.1",
79
+ "husky": "^8.0.1",
80
+ "lint-staged": "^12.5.0",
81
+ "prettier": "^2.7.1",
82
+ "semantic-release": "^19.0.5",
83
+ "tmp": "^0.2.1",
84
+ "ts-node": "^10.9.1",
85
+ "typescript": "^4.7.4",
86
+ "vitest": "^0.22.1"
87
+ },
88
+ "peerDependencies": {
89
+ "typescript": "^4.7"
90
+ },
91
+ "engines": {
92
+ "node": ">=12.0.0"
93
+ },
94
+ "lint-staged": {
95
+ "*": [
96
+ "prettier --write --cache --ignore-unknown"
97
+ ]
98
+ },
99
+ "repository": {
100
+ "type": "git",
101
+ "url": "git+ssh://git@github.com/sanity-io/pkg-utils.git"
102
+ },
103
+ "bugs": {
104
+ "url": "https://github.com/sanity-io/pkg-utils/issues"
105
+ },
106
+ "homepage": "https://github.com/sanity-io/pkg-utils#readme"
107
+ }
@@ -0,0 +1,85 @@
1
+ import path from 'path'
2
+ import {PackageSubPathExportEntry} from '../core'
3
+ import {_BuildContext} from './_types'
4
+ import {_RollupTask, _BuildTask, _DtsTask} from './tasks'
5
+
6
+ /**
7
+ * @internal
8
+ */
9
+ export function _parseTasks(ctx: _BuildContext): _BuildTask[] {
10
+ const {pkg} = ctx
11
+
12
+ const entries = Object.entries(ctx.exports || {}).map(
13
+ ([path, entry]) => ({path, ...entry} as PackageSubPathExportEntry & {path: string})
14
+ )
15
+
16
+ const dtsTasks: Record<string, _DtsTask> = {}
17
+ const rollupTasks: Record<string, _RollupTask> = {}
18
+
19
+ // Parse `dts` tasks
20
+ for (const entry of entries) {
21
+ const exportId = path.join(pkg.name, entry.path)
22
+
23
+ if (entry.types) {
24
+ if (!dtsTasks[exportId]) {
25
+ dtsTasks[exportId] = {
26
+ type: 'dts',
27
+ exportId,
28
+ exportPath: entry.path === '.' ? './index' : entry.path,
29
+ source: entry.source,
30
+ output: entry.types,
31
+ }
32
+ }
33
+ }
34
+ }
35
+
36
+ // Parse `rollup` tasks
37
+ for (const entry of entries) {
38
+ const format: ('commonjs' | 'esm')[] = []
39
+ const target: string[] = []
40
+
41
+ if (entry.require) {
42
+ format.push('commonjs')
43
+ }
44
+
45
+ if (entry.default) {
46
+ format.push('esm')
47
+ }
48
+
49
+ if (entry.runtime === 'node') {
50
+ target.push('node12')
51
+ }
52
+
53
+ if (entry.runtime === 'web') {
54
+ target.push('chrome104')
55
+ }
56
+
57
+ for (const f of format) {
58
+ const buildId = `${entry.runtime}:${f}:${target.join(',')}`
59
+
60
+ if (!rollupTasks[buildId]) {
61
+ rollupTasks[buildId] = {
62
+ type: 'rollup',
63
+ buildId,
64
+ entries: [entry],
65
+ runtime: entry.runtime,
66
+ format: f,
67
+ target,
68
+ }
69
+ } else {
70
+ rollupTasks[buildId].entries.push(entry)
71
+ }
72
+ }
73
+ }
74
+
75
+ const tasks: _BuildTask[] = [...Object.values(dtsTasks), ...Object.values(rollupTasks)]
76
+
77
+ if (ctx.extract && pkg.types) {
78
+ tasks.push({
79
+ type: 'extract',
80
+ mainEntryPointPath: pkg.types,
81
+ })
82
+ }
83
+
84
+ return tasks
85
+ }
@@ -0,0 +1,25 @@
1
+ import {PackageExports, PkgConfigOptions, _PackageJSON} from '../core'
2
+
3
+ /**
4
+ * @internal
5
+ */
6
+ export interface _BuildFile {
7
+ type: 'asset' | 'chunk' | 'types'
8
+ path: string
9
+ }
10
+
11
+ /**
12
+ * @internal
13
+ */
14
+ export interface _BuildContext {
15
+ config?: PkgConfigOptions
16
+ cwd: string
17
+ exports: PackageExports | undefined
18
+ external: string[]
19
+ extract: boolean
20
+ files: _BuildFile[]
21
+ dist: string
22
+ pkg: _PackageJSON
23
+ src: string
24
+ tsconfig?: string
25
+ }
@@ -0,0 +1,87 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import path from 'path'
4
+ import chalk from 'chalk'
5
+ import {PackageExports, _loadConfig, _loadPkg, _parseExports, _resolveConfigProperty} from '../core'
6
+ import {_parseTasks} from './_parseTasks'
7
+ import {_BuildContext} from './_types'
8
+ import {_dtsTask, _extractTask, _rollupTask} from './tasks'
9
+
10
+ /**
11
+ * @public
12
+ */
13
+ export async function build(options: {
14
+ cwd: string
15
+ extract?: boolean
16
+ tsconfig?: string
17
+ }): Promise<void> {
18
+ const {cwd, extract = false, tsconfig = 'tsconfig.json'} = options
19
+ const pkg = await _loadPkg({cwd})
20
+ const config = await _loadConfig({cwd})
21
+
22
+ // if (!config) {
23
+ // console.log('no configuration found')
24
+ // return
25
+ // }
26
+
27
+ const parsedExports = _parseExports(pkg).reduce<PackageExports>((acc, x) => {
28
+ const {path: exportPath, ...exportEntry} = x
29
+
30
+ return {...acc, [exportPath]: exportEntry}
31
+ }, {})
32
+
33
+ const exports = config?.exports
34
+ ? _resolveConfigProperty(config.exports, parsedExports)
35
+ : parsedExports
36
+
37
+ const initialExternal = [
38
+ ...(pkg.dependencies ? Object.keys(pkg.dependencies) : []),
39
+ ...(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : []),
40
+ ]
41
+
42
+ const external = config?.external
43
+ ? _resolveConfigProperty(config.external, initialExternal)
44
+ : initialExternal
45
+
46
+ const srcPath = path.resolve(cwd, config?.src || 'src')
47
+ const distPath = path.resolve(cwd, config?.dist || 'dist')
48
+
49
+ const context: _BuildContext = {
50
+ config,
51
+ cwd,
52
+ exports,
53
+ extract,
54
+ files: [],
55
+ external,
56
+ dist: path.relative(cwd, distPath),
57
+ pkg,
58
+ src: path.relative(cwd, srcPath),
59
+ tsconfig,
60
+ }
61
+
62
+ console.log(chalk.gray('cwd '), context.cwd)
63
+ // console.log('external ', context.external)
64
+ console.log(chalk.gray('src '), context.src)
65
+ console.log(chalk.gray('dist '), context.dist)
66
+ console.log(chalk.gray('tsconfig '), context.tsconfig)
67
+
68
+ if (exports) {
69
+ const buildTasks = _parseTasks(context)
70
+
71
+ for (const task of buildTasks) {
72
+ if (task.type === 'dts') {
73
+ await _dtsTask(context, task)
74
+ }
75
+
76
+ if (task.type === 'extract') {
77
+ await _extractTask(context, task)
78
+ }
79
+
80
+ if (task.type === 'rollup') {
81
+ await _rollupTask(context, task)
82
+ }
83
+ }
84
+ } else {
85
+ throw new Error('no exports')
86
+ }
87
+ }
@@ -0,0 +1,2 @@
1
+ export * from './_types'
2
+ export * from './build'
@@ -0,0 +1,8 @@
1
+ import {_DtsTask} from './dts'
2
+ import {_ExtractTask} from './extract'
3
+ import {_RollupTask} from './rollup'
4
+
5
+ /**
6
+ * @internal
7
+ */
8
+ export type _BuildTask = _DtsTask | _ExtractTask | _RollupTask
@@ -0,0 +1,62 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import path from 'path'
4
+ import ts from 'typescript'
5
+ import {_loadTSConfig} from './_loadTSConfig'
6
+
7
+ export const formatHost: ts.FormatDiagnosticsHost = {
8
+ getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
9
+ getNewLine: () => ts.sys.newLine,
10
+ getCanonicalFileName: ts.sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase(),
11
+ }
12
+
13
+ /**
14
+ * @internal
15
+ */
16
+ export async function _buildTypes(options: {
17
+ cwd: string
18
+ outDir: string
19
+ sourcePath: string
20
+ tsconfigPath: string
21
+ }): Promise<{path: string}> {
22
+ const {cwd, outDir, sourcePath, tsconfigPath} = options
23
+
24
+ const config = await _loadTSConfig({cwd, tsconfigPath})
25
+
26
+ const compilerOptions: ts.CompilerOptions = {
27
+ ...config.options,
28
+ emitDeclarationOnly: true,
29
+ outDir,
30
+ }
31
+
32
+ const program = ts.createProgram([path.resolve(cwd, sourcePath)], compilerOptions)
33
+
34
+ const emitResult = program.emit()
35
+
36
+ const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics)
37
+
38
+ allDiagnostics.forEach((diagnostic) => {
39
+ if (diagnostic.file) {
40
+ const {line, character} = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!)
41
+ const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
42
+
43
+ console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`)
44
+ } else {
45
+ console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
46
+ }
47
+ })
48
+
49
+ if (emitResult.emitSkipped) {
50
+ const errors = allDiagnostics.filter((diag) => diag.category === ts.DiagnosticCategory.Error)
51
+
52
+ if (errors.length) {
53
+ console.error(ts.formatDiagnostics(errors, formatHost))
54
+
55
+ throw new Error('failed to compile')
56
+ }
57
+ }
58
+
59
+ return {
60
+ path: path.resolve(outDir, sourcePath.replace(/\.ts$/, '.d.ts')),
61
+ }
62
+ }
@@ -0,0 +1,88 @@
1
+ import path from 'path'
2
+ import {ExtractorLogLevel, IConfigFile, IExtractorMessagesConfig} from '@microsoft/api-extractor'
3
+
4
+ export const DEFAULT_MESSAGES_CONFIG: IExtractorMessagesConfig = {
5
+ compilerMessageReporting: {
6
+ default: {
7
+ logLevel: 'warning' as ExtractorLogLevel,
8
+ },
9
+ },
10
+
11
+ extractorMessageReporting: {
12
+ default: {
13
+ logLevel: 'warning' as ExtractorLogLevel,
14
+ addToApiReportFile: false,
15
+ },
16
+
17
+ // 'ae-extra-release-tag': {
18
+ // logLevel: 'error' as ExtractorLogLevel,
19
+ // addToApiReportFile: false
20
+ // },
21
+
22
+ // 'ae-forgotten-export': {
23
+ // logLevel: 'error' as ExtractorLogLevel,
24
+ // addToApiReportFile: false
25
+ // }
26
+ },
27
+
28
+ tsdocMessageReporting: {
29
+ default: {
30
+ logLevel: 'warning' as ExtractorLogLevel,
31
+ addToApiReportFile: false,
32
+ },
33
+
34
+ // 'tsdoc-link-tag-unescaped-text': {
35
+ // logLevel: 'warning' as ExtractorLogLevel,
36
+ // addToApiReportFile: false
37
+ // },
38
+
39
+ // 'tsdoc-unsupported-tag': {
40
+ // logLevel: 'none' as ExtractorLogLevel,
41
+ // addToApiReportFile: false
42
+ // },
43
+
44
+ // 'tsdoc-undefined-tag': {
45
+ // logLevel: 'none' as ExtractorLogLevel,
46
+ // addToApiReportFile: false
47
+ // }
48
+ },
49
+ }
50
+
51
+ export function _createApiExtractorConfig(options: {
52
+ exportPath: string
53
+ projectPath: string
54
+ sourcePath: string
55
+ tsconfigPath: string
56
+ distPath: string
57
+ }): IConfigFile {
58
+ const {exportPath, projectPath, sourcePath, tsconfigPath, distPath} = options
59
+
60
+ return {
61
+ apiReport: {
62
+ enabled: false,
63
+ reportFileName: '<unscopedPackageName>.api.md',
64
+ // reportFileName: path.resolve(distPath, `${exportPath}.api.md`)
65
+ },
66
+ // bundledPackages: [],
67
+ compiler: {
68
+ tsconfigFilePath: tsconfigPath,
69
+ },
70
+ docModel: {
71
+ enabled: false,
72
+ // apiJsonFilePath: '<unscopedPackageName>.api.json'
73
+ apiJsonFilePath: path.resolve(distPath, `${exportPath}.api.json`),
74
+ },
75
+ dtsRollup: {
76
+ enabled: true,
77
+ untrimmedFilePath: path.resolve(distPath, `${exportPath}.d.ts`),
78
+ // betaTrimmedFilePath: path.resolve(distPath, `${exportPath}-beta.d.ts`),
79
+ // publicTrimmedFilePath: path.resolve(distPath, `${exportPath}-public.d.ts`)
80
+ },
81
+ tsdocMetadata: {
82
+ enabled: false,
83
+ },
84
+ messages: DEFAULT_MESSAGES_CONFIG,
85
+ mainEntryPointFilePath: sourcePath,
86
+ projectFolder: projectPath,
87
+ }
88
+ }
@@ -0,0 +1,57 @@
1
+ import {readFile} from 'fs/promises'
2
+ import {TSDocConfigFile} from '@microsoft/tsdoc-config'
3
+ import {parse} from 'jsonc-parser'
4
+
5
+ /**
6
+ * @public
7
+ */
8
+ export interface TSDocCustomTag {
9
+ name: string
10
+ syntaxKind: 'block' | 'modifier'
11
+ allowMultiple?: boolean
12
+ }
13
+
14
+ /**
15
+ * @internal
16
+ */
17
+ export async function _createTSDocConfig(opts: {
18
+ customTags: TSDocCustomTag[]
19
+ }): Promise<TSDocConfigFile | undefined> {
20
+ const {customTags} = opts
21
+
22
+ if (customTags.length === 0) {
23
+ return undefined
24
+ }
25
+
26
+ const tsDocBaseBuf = await readFile(
27
+ require.resolve('@microsoft/api-extractor/extends/tsdoc-base.json')
28
+ )
29
+
30
+ // Include the definitions that are required for API Extractor
31
+ // extends: ['@microsoft/api-extractor/extends/tsdoc-base.json'],
32
+ const tsDocBaseConfig = parse(tsDocBaseBuf.toString())
33
+
34
+ // Define custom tags and specify how they should be parsed
35
+ const tagDefinitions = (tsDocBaseConfig.tagDefinitions || []).concat(
36
+ customTags.map((t) => ({
37
+ tagName: `@${t.name}`,
38
+ syntaxKind: t.syntaxKind,
39
+ allowMultiple: t.allowMultiple,
40
+ }))
41
+ )
42
+
43
+ // Indicate that custom tags are supported by your tooling.
44
+ // (Without this, warnings may be reported saying that a tag is unsupported.)
45
+ const supportForTags = {...tsDocBaseConfig.supportForTags}
46
+
47
+ for (const customTag of customTags) {
48
+ supportForTags[`@${customTag.name}`] = true
49
+ }
50
+
51
+ return TSDocConfigFile.loadFromObject({
52
+ ...tsDocBaseConfig,
53
+ noStandardTags: false,
54
+ tagDefinitions,
55
+ supportForTags,
56
+ })
57
+ }
@@ -0,0 +1,101 @@
1
+ import path from 'path'
2
+ import {
3
+ Extractor,
4
+ ExtractorConfig,
5
+ ExtractorMessage,
6
+ ExtractorResult,
7
+ } from '@microsoft/api-extractor'
8
+ import chalk from 'chalk'
9
+ import {_BuildFile} from '../../_types'
10
+ import {_createApiExtractorConfig} from './_createApiExtractorConfig'
11
+ import {_createTSDocConfig} from './_createTSDocConfig'
12
+
13
+ export async function _extractTypes(options: {
14
+ cwd: string
15
+ distPath: string
16
+ // exportId: string
17
+ exportPath: string
18
+ files: _BuildFile[]
19
+ projectPath: string
20
+ quiet?: boolean
21
+ sourcePath: string
22
+ tsconfigPath: string
23
+ }): Promise<{extractorResult: ExtractorResult; messages: ExtractorMessage[]}> {
24
+ const {cwd, distPath, exportPath, files, projectPath, quiet, sourcePath, tsconfigPath} = options
25
+
26
+ const tsdocConfigFile = await _createTSDocConfig({
27
+ customTags: [],
28
+ })
29
+
30
+ const extractorConfig: ExtractorConfig = ExtractorConfig.prepare({
31
+ configObject: _createApiExtractorConfig({
32
+ exportPath,
33
+ projectPath,
34
+ sourcePath,
35
+ distPath,
36
+ tsconfigPath,
37
+ }),
38
+ configObjectFullPath: undefined,
39
+ tsdocConfigFile,
40
+ packageJsonFullPath: path.resolve(projectPath, 'package.json'),
41
+ })
42
+
43
+ const messages: ExtractorMessage[] = []
44
+
45
+ // Invoke API Extractor
46
+ const extractorResult = Extractor.invoke(extractorConfig, {
47
+ // Equivalent to the "--local" command-line parameter
48
+ localBuild: true,
49
+ // Equivalent to the "--verbose" command-line parameter
50
+ showVerboseMessages: true,
51
+ // handle messages
52
+ messageCallback(message: ExtractorMessage) {
53
+ messages.push(message)
54
+ message.handled = true
55
+ },
56
+ })
57
+
58
+ if (!quiet) {
59
+ const warnings: ExtractorMessage[] = messages.filter((msg) => msg.logLevel === 'warning')
60
+
61
+ for (const msg of warnings) {
62
+ const sourceFilePath = msg.sourceFilePath && path.relative(cwd, msg.sourceFilePath)
63
+
64
+ // eslint-disable-next-line no-console
65
+ console.log(
66
+ [
67
+ `${chalk.cyan(sourceFilePath || '?')}`,
68
+ `:${chalk.yellow(msg.sourceFileLine)}:${chalk.yellow(msg.sourceFileColumn)}`,
69
+ ` - ${chalk.yellow('warning')} ${chalk.gray(msg.messageId)}\n`,
70
+ msg.text,
71
+ '\n',
72
+ ].join('')
73
+ )
74
+ }
75
+ }
76
+
77
+ const errors: ExtractorMessage[] = messages.filter((msg) => msg.logLevel === 'error')
78
+
79
+ for (const msg of errors) {
80
+ const sourceFilePath = msg.sourceFilePath && path.relative(cwd, msg.sourceFilePath)
81
+
82
+ // eslint-disable-next-line no-console
83
+ console.log(
84
+ [
85
+ `${chalk.cyan(sourceFilePath || '?')}`,
86
+ `:${chalk.yellow(msg.sourceFileLine)}:${chalk.yellow(msg.sourceFileColumn)}`,
87
+ ` - ${chalk.red('error')} ${chalk.gray(msg.messageId)}\n`,
88
+ msg.text,
89
+ '\n',
90
+ ].join('')
91
+ )
92
+ }
93
+
94
+ // Add to `files` in context
95
+ files.push({
96
+ type: 'types',
97
+ path: path.resolve(distPath, `${exportPath}.d.ts`),
98
+ })
99
+
100
+ return {extractorResult, messages}
101
+ }
@@ -0,0 +1,18 @@
1
+ import ts from 'typescript'
2
+
3
+ export async function _loadTSConfig(options: {
4
+ cwd: string
5
+ tsconfigPath: string
6
+ }): Promise<ts.ParsedCommandLine> {
7
+ const {cwd, tsconfigPath} = options
8
+
9
+ const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath)
10
+
11
+ if (!configPath) {
12
+ throw new Error(`could not find a valid '${tsconfigPath}'`)
13
+ }
14
+
15
+ const configFile = ts.readConfigFile(configPath, ts.sys.readFile)
16
+
17
+ return ts.parseJsonConfigFileContent(configFile.config, ts.sys, cwd)
18
+ }