npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@poppinss/chokidar-ts

v4.1.4

Published

File watcher that creates a file watcher after parsing tsconfig.json file

Downloads

102,779

Readme

Chokidar TS

A thin wrapper on top of chokidar file watcher that relies on the tsconfig.json file to distinguish between the TypeScript source files and other files.

gh-workflow-image typescript-image npm-image license-image

Why does this package exists?

When running a Node.js backend development server with a file watcher, we need to know whether a newly added or changed file is part of our TypeScript project.

The best way to establish if a file is part of a TypeScript project is to rely on the tsconfig.json file.

This is precisely what this package does. It will create a file watcher using chokidar and then uses the includes and excludes patterns from the tsconfig.json file to know if a changed file is part of a TypeScript project.

Setup

Install the package from the npm packages registry. In addition, the package has a peer dependency on the typescript package, so make sure to install that as well.

npm i @poppinss/chokidar-ts@next

And use it as follows.

import typescript from 'typescript'
import { ConfigParser, Watcher } from '@poppinss/chokidar-ts'

const projectRoot = new URL('./', import.meta.url)
const configFileName = 'tsconfig.json'

const { config } = new ConfigParser(
  projectRoot,
  configFileName,
  typescript,
).parse()

if (config) {
  const watcher = new Watcher(projectRoot, config)
  watcher.watch(['.'])
}

Listening for events

The Watcher class emits the following events. Events prefixed with source refers to files included by the tsconfig.json file, and other events refer to non-typescript or files excluded by the tsconfig.json file.

  • add: A new file has been added. The file is either not a TypeScript file or is excluded by the tsconfig.json file.
  • source:add: A new TypeScript source file has been added.
  • change: An existing file has been updated. The file is either not a TypeScript file or is excluded by the tsconfig.json file.
  • source:change: An existing TypeScript source file has been changed.
  • unlink: An existing file has been deleted. The file is not a TypeScript source file.
  • source:unlink: An existing TypeScript source file has been deleted.
const watcher = new Watcher(projectRoot, config)

watcher.on('add', (file) => {
  console.log(file.absPath)
  console.log(file.relativePath)
})

watcher.on('source:add', (file) => {
  console.log(file.absPath)
  console.log(file.relativePath)
})

watcher.on('change', (file) => {
  console.log(file.absPath)
  console.log(file.relativePath)
})

watcher.on('source:change', (file) => {
  console.log(file.absPath)
  console.log(file.relativePath)
})

watcher.on('unlink', (file) => {
  console.log(file.absPath)
  console.log(file.relativePath)
})

watcher.on('source:unlink', (file) => {
  console.log(file.absPath)
  console.log(file.relativePath)
})

watcher.watch(['.'])

Handling config parser errors

Parsing the tsconfig.json file can produce errors, and you can display them using the TypeScript compiler as follows.

import typescript from 'typescript'
const { error, config } = new ConfigParser(
  projectRoot,
  configFileName,
  typescript,
).parse()

if (error) {
  const compilerHost = typescript.createCompilerHost({})
  console.log(
    typescript.formatDiagnosticsWithColorAndContext([error], compilerHost)
  )

  return
}

if (!config) {
  return
}

if (config.errors) {
  const compilerHost = typescript.createCompilerHost({})
  console.log(
    typescript.formatDiagnosticsWithColorAndContext(config.errors, compilerHost)
  )

  return
}