Skip to content

Commit

Permalink
using chokidar for better reliabllity
Browse files Browse the repository at this point in the history
  • Loading branch information
NazmusSayad committed Sep 27, 2024
1 parent 97f1ceb commit 05432de
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
33 changes: 31 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "npmize",
"version": "1.1.5",
"version": "1.1.6",
"description": "Let's create an npm package without worrying about anything.",
"bin": "./dist/index.js",
"scripts": {
Expand All @@ -12,6 +12,7 @@
"dependencies": {
"@babel/parser": "^7.25.6",
"ansi-colors": "^4.1.3",
"chokidar": "^4.0.1",
"cross-spawn": "^7.0.3",
"noarg": "^3.1.0",
"typescript": "^5.6.2"
Expand Down
43 changes: 20 additions & 23 deletions src/program/dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import chokidar from 'chokidar'
import tsc from '../scripts/tsc'
import { cleanDir } from '../utils/fs'
import { CompileOptions } from './types.t'
Expand All @@ -26,31 +27,21 @@ function runDev(
) {
const tempOutDir = getNodeModulesTempDir(rootPath, 'dev-' + moduleType)
const finalOutDir = path.resolve(shortOutDir)

cleanDir(tempOutDir)
cleanDir(finalOutDir)

fs.watch(
tempOutDir,
{ recursive: true, persistent: true },
(event, filename) => {
if (event !== 'change' || !filename) return
if (!(filename.endsWith('.js') || filename.endsWith('.ts'))) return

const filePath = path.join(tempOutDir, filename)
if (!(fs.existsSync(filePath) && fs.statSync(filePath).isFile())) return

makeOutput(filePath, {
tempOutDir: tempOutDir,
finalOutDir: finalOutDir,
moduleType: moduleType,
pushNodeCode: options.node,
tsConfig: {
baseUrl: options.tsConfig?.baseUrl,
paths: options.tsConfig?.paths,
},
})
}
)
function updateFile(filePath: string) {
makeOutput(filePath, {
tempOutDir: tempOutDir,
finalOutDir: finalOutDir,
moduleType: moduleType,
pushNodeCode: options.node,
tsConfig: {
baseUrl: options.tsConfig?.baseUrl,
paths: options.tsConfig?.paths,
},
})
}

tsc(
rootPath,
Expand All @@ -67,4 +58,10 @@ function runDev(
stdio: moduleType === options.focus ? 'inherit' : 'ignore',
}
)

chokidar.watch(tempOutDir).on('all', (_, filePath) => {
if (!fs.existsSync(filePath)) return
if (!fs.statSync(filePath).isFile()) return
updateFile(filePath)
})
}

0 comments on commit 05432de

Please sign in to comment.