Project watcher
Watch on project files and filesystem events.
Easily automate some routines.
Use case:
Full TypeScript support.
Uses chokidar
for fs observing.
For all path rules minimatch
used.
While still in development, use --save-exact
to prevent updating with breaking changes.
Integration
Install
npm i project-watcher --save-exact
Create watcher script
const path = ;const ProjectWatcher = ; // project pathsconst projectSource = path;const projectBlockTemplate = path;const projectBlockFileTemplate = path; projectSource watcher: {} paths: "**/blocks/*": autoIndex: 'ts' excludeIndex: '*.scss' '*.scss.d.ts' newDirTemplate: projectBlockTemplate "**/blocks/*.tsx": newFileTemplate: projectBlockFileTemplate ;
Now run watcher script with development environment.
Every time you create folder in any blocks/
folder, it will be filled with template.
Every time you create file in blocks/
, it will be filled with template from file.
Every time you create/delete/rename something in blocks/
, index.ts
file will be auto-updated.
API
ProjectWatcher
constructor
constructorrootPath: string | string, opts: ProjectWatcherOptions, logger: ILogger = DefaultLogger.instance;
rootPath
- Path or paths to project's root directories that will be watched.opts
- Configuration (see below).logger
- ILogger instance that will be used for all log messages.
ProjectWatcherOptions
watcher
- Chokidar watch options (chokidar.WatchOptions
).paths
- Map of paths & it's processing options.
ProjectWatcherOptions
is
You can observe any of filesystem events.
Callbacks are async and if you return Promise
from it, project-watcher will wait until promise will be resolved.
break
- Will stop event propogation for other similar paths (doesnt effect custom handlers).breakCustomEvents
- Will stop event propogation for custom handlers.autoIndex
- Auto update exports in index files. Usejs
orts
to specify index's extension. (ts
by default).dontCreateIndex
- IfautoIndex=true
dont create index file if it is not exists.excludeIndex
- Excludes some paths or file names from indexing inautoIndex
.newDirTemplate
- Path to directory with template code. If new dir is created on matched path, it will be populated with this template. Usereplace
&replaceFileName
to tweak this template.newFileTemplate
- Path to file with template code. If new dir is created on matched path, it will be populated with this template. Usereplace
&replaceFileName
to tweak this template.replace
- Replace text in templates (see more info below).replaceFileName
- Rename files of directory template (see more info below).
ProjectWatcherOptions.replaceFileName
Replacement map for template's files.
Applied before ProjectWatcherOptions.replace
.
'from' is matching fully filenames in templates directory, without extensions (when populating new directory with template code).
Example
When creating dir like 'blocks/myBlock'
And template file copied from 'template/NAME.js'
with:
replaceFileName:
File will be copied as 'blocks/myBlock/myBlock.js'
ProjectWatcherOptions.replace
Replacement map to text in templates.
Applied after ProjectWatcherOptions.replaceFileName
.
When from is matched, replaces to string or 'transformer' func.
!! replace
used after replaceFileName
, so filePath & fileName may be transformed. !!
Example
new ProjectWatcherprojectSource, ;
Now when you create new .tsx
file or new directory in blocks
,
Exports will be auto-updated, and in all file COMPONENT_NAME
will be replaced to file name or directory name.