From 05432de9a52be4da6540c7f03cee7c25a91c2f14 Mon Sep 17 00:00:00 2001 From: Nazmus Sayad <87106526+NazmusSayad@users.noreply.github.com> Date: Fri, 27 Sep 2024 11:16:18 +0600 Subject: [PATCH] using chokidar for better reliabllity --- package-lock.json | 33 +++++++++++++++++++++++++++++++-- package.json | 3 ++- src/program/dev.ts | 43 ++++++++++++++++++++----------------------- 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index 56f797c..b285507 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { "name": "npmize", - "version": "1.1.3", + "version": "1.1.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "npmize", - "version": "1.1.3", + "version": "1.1.5", "license": "ISC", "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" @@ -387,6 +388,21 @@ "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==", "license": "MIT" }, + "node_modules/chokidar": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-4.0.1.tgz", + "integrity": "sha512-n8enUVCED/KVRQlab1hr3MVpcVMvxtZjmEa956u+4YijlmQED223XMSYj2tLuKvr4jcCTzNNMpQDUer72MMmzA==", + "license": "MIT", + "dependencies": { + "readdirp": "^4.0.1" + }, + "engines": { + "node": ">= 14.16.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, "node_modules/cli-table3": { "version": "0.6.5", "resolved": "https://registry.npmjs.org/cli-table3/-/cli-table3-0.6.5.tgz", @@ -527,6 +543,19 @@ "node": ">=8" } }, + "node_modules/readdirp": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-4.0.1.tgz", + "integrity": "sha512-GkMg9uOTpIWWKbSsgwb5fA4EavTR+SG/PMPoAY8hkhHfEEY0/vqljY+XHqtDf2cr2IJtoNRDbrrEpZUiZCkYRw==", + "license": "MIT", + "engines": { + "node": ">= 14.16.0" + }, + "funding": { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + }, "node_modules/safer-buffer": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", diff --git a/package.json b/package.json index 84fcf93..fa9980f 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" diff --git a/src/program/dev.ts b/src/program/dev.ts index d8dd5fb..be660d5 100644 --- a/src/program/dev.ts +++ b/src/program/dev.ts @@ -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' @@ -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, @@ -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) + }) }