Skip to content

Instantly share code, notes, and snippets.

@Alex-D
Created June 13, 2021 12:46
Show Gist options
  • Save Alex-D/c4ba21206ed4fce8e2b165ac97c0fcc0 to your computer and use it in GitHub Desktop.
Save Alex-D/c4ba21206ed4fce8e2b165ac97c0fcc0 to your computer and use it in GitHub Desktop.
Over complicated way, replaced by rollup-plugin-dts
import {Extractor, ExtractorConfig} from '@microsoft/api-extractor'
import * as fs from 'fs'
import {replaceTscAliasPaths} from 'tsc-alias'
import * as ts from 'typescript'
import {CompilerOptions} from 'typescript'
import * as util from 'util'
function compile(fileNames: string[], options: ts.CompilerOptions): void {
const program = ts.createProgram(fileNames, options)
const emitResult = program.emit()
const allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics)
allDiagnostics.forEach(diagnostic => {
if (diagnostic.file) {
const {line, character} = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!)
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
console.error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`)
return
}
console.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
})
if (allDiagnostics.length !== 0) {
console.error('[ERROR] Compile failed')
process.exit(allDiagnostics.length)
}
}
const genDts = async (entryPointPath: string) => {
const directoryPath = 'node_modules/.@alex-d_gen-dts_tmp'
const entryPointFileName = entryPointPath.replace('.ts', '')
const tsConfigPath = ts.findConfigFile(
'./',
ts.sys.fileExists,
'tsconfig.json',
)
if (tsConfigPath === undefined) {
process.exit(1)
}
const {config} = ts.readConfigFile(tsConfigPath, ts.sys.readFile)
const {options} = ts.parseJsonConfigFileContent(config, ts.sys, process.cwd())
const typesCompilerOptions = {
declaration: true,
emitDeclarationOnly: true,
outDir: directoryPath,
}
const compilerOptions: CompilerOptions = {
...options,
...typesCompilerOptions,
}
compile([`./${entryPointFileName}.ts`], compilerOptions)
const typesTsConfig = {
...config,
}
typesTsConfig.compilerOptions = {
...typesTsConfig.compilerOptions,
...typesCompilerOptions,
}
const configTypesPath = tsConfigPath.replace('.json', '.__tmp-types__.json')
fs.writeFileSync(configTypesPath, JSON.stringify(typesTsConfig))
replaceTscAliasPaths({
configFile: configTypesPath,
})
await util.promisify(fs.rm)(configTypesPath)
// TODO: loop to find the right folder path into entryPointFileName
const apiExtractorConfig = {
projectFolder: typesTsConfig.compilerOptions.baseUrl,
mainEntryPointFilePath: `${directoryPath}/${entryPointFileName.replace(/.*\//, '')}.d.ts`,
dtsRollup: {
enabled: true,
},
apiReport: {
enabled: false,
},
docModel: {
enabled: false,
},
tsdocMetadata: {
enabled: false,
},
newlineKind: 'lf',
}
const apiExtractorJsonPath = tsConfigPath.replace('.json', '.__api-extractor__.json')
fs.writeFileSync(apiExtractorJsonPath, JSON.stringify(apiExtractorConfig))
const extractorConfig: ExtractorConfig = ExtractorConfig.loadFileAndPrepare(apiExtractorJsonPath)
Extractor.invoke(extractorConfig)
await util.promisify(fs.rm)(apiExtractorJsonPath)
await util.promisify(fs.rm)(directoryPath, {recursive: true, force: true})
}
genDts('src/try/entry.ts')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment