7.8.153 • Published 1 year ago

@firanorg/ut-officiis-et v7.8.153

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@firanorg/ut-officiis-et Latest version npm downloads

Find and parse tsconfig.json files.

Features

  • Zero dependency (not even TypeScript)
  • Tested against TypeScript for correctness
  • Supports comments & dangling commas in tsconfig.json
  • Resolves extends
  • Fully typed tsconfig.json
  • Validates and throws parsing errors
  • Tiny! 7 kB Minified + Gzipped

Install

npm install @firanorg/ut-officiis-et

Why?

For TypeScript related tooling to correctly parse tsconfig.json file without depending on TypeScript.

API

getTsconfig(searchPath?, configName?, cache?)

Searches for a tsconfig.json file and parses it. Returns null if a config file cannot be found, or an object containing the path and parsed TSConfig object if found.

Returns:

type TsconfigResult = {

    /**
     * The path to the tsconfig.json file
     */
    path: string

    /**
     * The resolved tsconfig.json file
     */
    config: TsConfigJsonResolved
}

searchPath

Type: string

Default: process.cwd()

Accepts a path to a file or directory to search up for a tsconfig.json file.

configName

Type: string

Default: tsconfig.json

The file name of the TypeScript config file.

cache

Type: Map<string, any>

Default: new Map()

Optional cache for fs operations.

Example

import { getTsconfig } from '@firanorg/ut-officiis-et'

// Searches for tsconfig.json starting in the current directory
console.log(getTsconfig())

// Find tsconfig.json from a TypeScript file path
console.log(getTsconfig('./path/to/index.ts'))

// Find tsconfig.json from a directory file path
console.log(getTsconfig('./path/to/directory'))

// Explicitly pass in tsconfig.json path
console.log(getTsconfig('./path/to/tsconfig.json'))

// Search for jsconfig.json - https://code.visualstudio.com/docs/languages/jsconfig
console.log(getTsconfig('.', 'jsconfig.json'))

parseTsconfig(tsconfigPath, cache?)

The tsconfig.json parser used internally by getTsconfig. Returns the parsed tsconfig as TsConfigJsonResolved.

tsconfigPath

Type: string

Required path to the tsconfig file.

cache

Type: Map<string, any>

Default: new Map()

Optional cache for fs operations.

Example

import { parseTsconfig } from '@firanorg/ut-officiis-et'

// Must pass in a path to an existing tsconfig.json file
console.log(parseTsconfig('./path/to/tsconfig.custom.json'))


createFileMatcher(tsconfig: TsconfigResult, caseSensitivePaths?: boolean)

Given a tsconfig.json file, it returns a file-matcher function that determines whether it should apply to a file path.

type FileMatcher = (filePath: string) => TsconfigResult['config'] | undefined

tsconfig

Type: TsconfigResult

Pass in the return value from getTsconfig, or a TsconfigResult object.

caseSensitivePaths

Type: boolean

By default, it uses is-fs-case-sensitive to detect whether the file-system is case-sensitive.

Pass in true to make it case-sensitive.

Example

For example, if it's called with a tsconfig.json file that has include/exclude/files defined, the file-matcher will return the config for files that match include/files, and return undefined for files that don't match or match exclude.

const tsconfig = getTsconfig()
const fileMatcher = tsconfig && createFileMatcher(tsconfig)

/*
 * Returns tsconfig.json if it matches the file,
 * undefined if not
 */
const configForFile = fileMatcher?.('/path/to/file.ts')
const distCode = compileTypescript({
    code: sourceCode,
    tsconfig: configForFile
})

createPathsMatcher(tsconfig: TsconfigResult)

Given a tsconfig with compilerOptions.paths defined, it returns a matcher function.

The matcher function accepts an import specifier (the path to resolve), checks it against compilerOptions.paths, and returns an array of possible paths to check:

function pathsMatcher(specifier: string): string[]

This function only returns possible paths and doesn't actually do any resolution. This helps increase compatibility wtih file/build systems which usually have their own resolvers.

Example

import { getTsconfig, createPathsMatcher } from '@firanorg/ut-officiis-et'

const tsconfig = getTsconfig()
const pathsMatcher = createPathsMatcher(tsconfig)

const exampleResolver = (request: string) => {
    if (pathsMatcher) {
        const tryPaths = pathsMatcher(request)

        // Check if paths in `tryPaths` exist
    }
}

FAQ

How can I use TypeScript to parse tsconfig.json?

This package is a re-implementation of TypeScript's tsconfig.json parser.

However, if you already have TypeScript as a dependency, you can simply use it's API:

import {
    sys as tsSys,
    findConfigFile,
    readConfigFile,
    parseJsonConfigFileContent
} from 'typescript'

// Find tsconfig.json file
const tsconfigPath = findConfigFile(process.cwd(), tsSys.fileExists, 'tsconfig.json')

// Read tsconfig.json file
const tsconfigFile = readConfigFile(tsconfigPath, tsSys.readFile)

// Resolve extends
const parsedTsconfig = parseJsonConfigFileContent(
    tsconfigFile.config,
    tsSys,
    path.dirname(tsconfigPath)
)

Sponsors

Object.assignserializeprototypecryptoArray.prototype.findLastIndexartnegativemulti-packageenvtouchpreprocessorHyBitrimStartcollection.es6metadatasimpledbprotobufimportexportflattenTypeScriptlogtestingECMAScript 7monoreporequirees-shimssuperagentdiffinstallsyntaxclassnamel10nwatches2018owninferencemobileisConcatSpreadablebannerequalityiamstringifiercss-in-jsminimalUint8ArraycolorsbyteLengthpasswordprettypushclassnameswalkcontainsweaksetmakeimportes-abstracthas-ownschemaaccessorawesomesaucefastcollectionInt16Arrayfastcloneconcatless mixinsdeterministicarrayshotpromisesprunecss lessArray.prototype.includesawaitruntimeObject.entriesmodulesnpmcoercibleboundprocessswfmime-dbclass-validatorvestutilitiesmimebootstrap csstc39esimmutableES2022cjkECMAScript 5namesbufferReactiveXauthconfigurableansiES3hashloadbalancingpackage.jsona11yexpressiondependency managercryptentrieschinese_.extendbundlingpackagesES2021copyeventEmitterparseredactbusystringifyeast-asian-widthterminalpropertieseventDispatcherfileECMAScript 2018workflowmacoswgetArray.prototype.findLastkinesisproxyreact-hook-formtermsigintbreakcompile lessES8ES5limitbundlerrandomtapeinstallernameformupES2017sideoffseteslintpluginponyfillObject.valueses7ECMAScript 2020matchWebSockets3jsenumerableerrordependencieslinuxreuseUint32Arraycloudfrontroute53columnssuperstructttyPromiseTypedArrayes8es5JSONregular expressionregularcomputed-typeses-shim APIrestfulnumbertasktstoArrayenvironmentjsdomsymbolsESjsdiffeslintsyntaxerrorES2015cloudsearchYAMLzodhttpsfindupeslintconfighigher-orderroutingredux-toolkitelmcheckstateObservableclonedataViewslicesinatrafastifybrowserliststyleESnextgradients cssargsargumentexpressnested cssurlmkdirpchaikarmaencryptionmixinsinternal slotsharedsequencetypesrecursiverateprefixstylingURLrouterloggingpluginpreserve-symlinksgroupwordbreakdom-testing-libraryfscachefastcopyreal-timecorehardlinkswatchingsigtermtypesafewritableidutilitygradients css3ajaxtestquerystringparentspostcssbootstrap lessbrowsershimsortappquerynodejsprotocol-buffersSetawstostringtaglockfilebatchgenericsassignslotebsdeepcensordescriptiondeepcopyhasOwnlrulistenersfolderinvariantyupfunctionsinputcore-jsfindLastIndexxtermshamdeletegetterfpsprivatefilterthrottlejavascriptdefineArrayBufferformattingagentidlelookpostcss-pluginmatchAllArray.prototype.flatcirculariteratecomparedebuggerwriteprivate dataiteratorconfigflagslook-upfluxdeep-clonerfc4122Array.prototype.flattenSymbolspecRegExp.prototype.flagsframeworkutil.inspectcallbounddomweakmapestreeWebSocketspatchpipejwtpropertydeep-copyObject.isiterationrm -frlinewrapsymlinkspolyfillexecUint16ArrayargparsefullloggerasyncmatchesextraECMAScript 2022directorygdpropentaketoolsexitendpointjQuerywarningstyled-componentsvariables in cssrdsfindlimitedrmdir-0hasOwnPropertytypedflaghooksoptimistcharactersignalszeroBigUint64ArrayES7queueMicrotaskparentfull-widthamazonelectronpromiseresolveless.jses2015weberror-handlingRxinternalqueuestyleguideprotoeveryoncecommand-linestoragegatewaystreamreducecode pointsmruString.prototype.trimdotenvregular expressionsvisual[[Prototype]]stdlibloadingvaluethroat0ES6symbolUint8ClampedArraylazyguidelbwaittypeofsymlinktypanionfast-copyimmerecmascriptbddsomewatchFiletextformatviewescapecall-bindes2017momentbrowserslistconsumebuffersvalidlesskoreantddpicomatchES2020walkingArrayhandlerssnsextendRFC-6455efficientremoveJSON-SchemaArray.prototype.filtercolorassertionincludestapbluebirdeventsObjecttoStringTagpnpm9extensionnativejestcorsjapanesehascloudtraildataviewfseventsrestomitinterruptshelpersexit-codeMicrosoftsetPrototypeOfaccessibilityindicatoremojiworker
7.8.153

1 year ago

7.8.152

1 year ago

7.7.138

1 year ago

7.7.135

1 year ago

7.7.134

1 year ago

7.7.137

1 year ago

7.7.136

1 year ago

7.8.150

1 year ago

7.8.149

1 year ago

7.8.148

1 year ago

7.8.147

1 year ago

7.8.146

1 year ago

7.8.145

1 year ago

7.8.144

1 year ago

7.8.143

1 year ago

7.8.142

1 year ago

7.8.141

1 year ago

7.8.140

1 year ago

7.8.151

1 year ago

7.8.139

1 year ago

7.8.138

1 year ago

7.7.133

1 year ago

7.7.132

1 year ago

7.7.131

1 year ago

7.7.130

1 year ago

7.7.129

1 year ago

7.7.128

1 year ago

7.7.127

1 year ago

7.7.124

1 year ago

7.7.123

1 year ago

7.7.126

1 year ago

7.7.125

1 year ago

7.7.122

1 year ago

7.7.121

1 year ago

7.7.120

1 year ago

7.7.119

1 year ago

7.7.117

1 year ago

7.7.116

1 year ago

7.7.118

1 year ago

7.7.115

1 year ago

7.6.115

1 year ago

7.6.114

1 year ago

7.6.113

1 year ago

7.6.112

1 year ago

7.6.111

1 year ago

7.6.110

1 year ago

7.6.109

1 year ago

7.6.108

1 year ago

7.6.107

1 year ago

7.6.106

1 year ago

7.6.105

1 year ago

7.5.78

1 year ago

7.5.79

1 year ago

4.1.37

1 year ago

7.5.84

1 year ago

7.5.85

1 year ago

7.5.86

1 year ago

7.5.87

1 year ago

7.5.80

1 year ago

7.5.81

1 year ago

7.5.82

1 year ago

7.5.83

1 year ago

7.5.88

1 year ago

7.5.89

1 year ago

7.6.101

1 year ago

5.2.46

1 year ago

7.6.100

1 year ago

5.2.45

1 year ago

5.2.44

1 year ago

5.2.43

1 year ago

5.2.42

1 year ago

7.6.104

1 year ago

5.2.41

1 year ago

7.6.103

1 year ago

5.2.40

1 year ago

7.6.102

1 year ago

3.1.34

1 year ago

3.1.33

1 year ago

3.1.36

1 year ago

7.5.90

1 year ago

3.1.35

1 year ago

3.1.37

1 year ago

7.5.95

1 year ago

7.5.96

1 year ago

7.5.97

1 year ago

7.5.98

1 year ago

7.5.91

1 year ago

7.5.92

1 year ago

7.5.93

1 year ago

7.5.94

1 year ago

6.4.61

1 year ago

5.2.39

1 year ago

6.4.60

1 year ago

6.4.63

1 year ago

6.4.62

1 year ago

6.4.54

1 year ago

6.4.53

1 year ago

6.4.56

1 year ago

6.4.55

1 year ago

6.4.58

1 year ago

6.4.57

1 year ago

6.4.59

1 year ago

6.4.52

1 year ago

7.4.63

1 year ago

7.4.64

1 year ago

7.4.69

1 year ago

7.4.65

1 year ago

7.4.66

1 year ago

7.4.67

1 year ago

7.4.68

1 year ago

7.4.72

1 year ago

7.4.73

1 year ago

7.4.74

1 year ago

7.4.75

1 year ago

7.6.99

1 year ago

7.6.98

1 year ago

7.4.70

1 year ago

7.4.71

1 year ago

7.4.76

1 year ago

7.4.77

1 year ago

7.4.78

1 year ago

4.2.39

1 year ago

4.2.37

1 year ago

4.2.38

1 year ago

3.0.23

1 year ago

3.0.24

1 year ago

3.0.21

1 year ago

3.0.22

1 year ago

3.0.27

1 year ago

3.0.28

1 year ago

3.0.25

1 year ago

3.0.26

1 year ago

6.3.52

1 year ago

3.0.20

1 year ago

6.3.50

1 year ago

6.3.51

1 year ago

5.3.48

1 year ago

5.3.47

1 year ago

5.3.46

1 year ago

3.0.18

1 year ago

3.0.19

1 year ago

6.3.49

1 year ago

3.0.32

1 year ago

3.0.33

1 year ago

3.0.30

1 year ago

6.3.48

1 year ago

3.0.31

1 year ago

3.0.29

1 year ago

3.0.17

1 year ago

3.0.16

1 year ago

3.0.14

1 year ago

3.0.15

1 year ago

2.0.14

1 year ago

2.0.13

1 year ago

2.0.12

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

2 years ago