generated from astrohelm/node-workspace
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
358 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,166 +1,39 @@ | ||
<h1 align="center">Astrohelm default workspace</h1> | ||
|
||
<h2 align="center">Installation guide 🚀</h2> | ||
|
||
### First step: workspace installation | ||
|
||
Use next commands to install and update your workspace | ||
Or use this repository as a template repository, if so - you can skip this step. | ||
|
||
```bash | ||
# Download repository | ||
git clone https://github.com/astrohelm/workspace | ||
rm -rf ./workspace/.git ./workspace/package-lock.json | ||
cd ./workspace | ||
# Update and install dependencies | ||
ncu -u | ||
npm i | ||
# Update node.js (optional() | ||
nvm install latest | ||
nvm use latest | ||
``` | ||
|
||
### Second step: Package personalization | ||
|
||
Update package json, all with prefix <code>your-</code><br/> | ||
If your nodejs version newer than package.json current add <code>|| your-node-version</code>. | ||
|
||
```js | ||
// package.json | ||
{ | ||
"license": "MIT", | ||
"version": "0.0.1", | ||
"type": "commonjs", | ||
"name": "your-package-name", | ||
"homepage": "https://astrohelm.ru", | ||
"description": "your-package-description", | ||
"author": "your-name <your-mail>", | ||
"keywords": ["your-keyword #1", "your-keyword #n"], | ||
|
||
"main": "index.js", | ||
"types": "types/index.d.ts", | ||
"packageManager": "npm@9.6.4", | ||
"readmeFilename": "README.md", | ||
"engines": { "node": "18 || 19 || 20" }, | ||
"browser": {}, | ||
"files": ["/dist", "/lib", "/types"], | ||
|
||
"scripts": { | ||
"test": "node --test && tsc", | ||
"dev": "node index.js", | ||
"prettier:fix": "prettier --write \"**/*.{js,ts,json,html,cjs,md,yaml}\"", | ||
"eslint:fix": "eslint --fix \"**/*.{js,ts}\"" | ||
}, | ||
|
||
"repository": { "type": "git", "url": "git+https://github.com/astrohelm/your-package-name.git" }, | ||
"bugs": { "url": "https://github.com/astrohelm/your-package-name/issues", "email": "your-mail" }, | ||
|
||
"devDependencies": { | ||
"@types/node": "^18.15.10", | ||
"eslint": "^8.40.0", | ||
"eslint-config-astrohelm": "^1.0.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"prettier": "^2.8.8", | ||
"typescript": "^5.0.2" | ||
} | ||
} | ||
``` | ||
|
||
### Third step: About files | ||
|
||
Go to CHANGELOG.md and update it for your package. | ||
_WARNING !_ Don't fotget about date (xxxx-xx-xx). | ||
|
||
```md | ||
<!-- CHANGELOG.md --> | ||
# Changelog | ||
|
||
## [Unreleased][unreleased] | ||
|
||
## [0.0.1][] - xxxx-xx-xx | ||
|
||
- Stable release version | ||
- Repository created | ||
|
||
[unreleased]: https://github.com/astrohelm/your-package-name/compare/v0.0.1...HEAD | ||
[0.0.1]: https://github.com/astrohelm/your-package-name/releases/tag/v0.0.1 | ||
``` | ||
|
||
Update AUTHORS | ||
|
||
```md | ||
<!-- AUTHORS --> | ||
your-name <your-mail> | ||
``` | ||
|
||
### Almost last step: Update README.md | ||
|
||
Replace your README.md with next information and change Your-package-name to actual. | ||
|
||
```md | ||
<!-- README.md --> | ||
<h1 align="center">Your-package-name v0.0.1</h1> | ||
|
||
<h2 align="center">Initial release 🚀</h2> | ||
|
||
<h2 align="center">Copyright & contributors</h2> | ||
<h1 align="center">Astrowatch - File system watcher</h1> | ||
|
||
<p align="center"> | ||
Copyright © 2023 <a href="https://github.com/astrohelm/Your-package-name/graphs/contributors">Astrohelm contributors</a>. | ||
Your-package-name is <a href="./LICENSE">MIT licensed</a>.<br/> | ||
Your-package-name is part of <a href="https://github.com/astrohelm">Astrohelm ecosystem</a>. | ||
Watch specific files, directories, deeply nested directories <br/> | ||
Rebuild recursive when new directories found or old directories remove <br/> | ||
Deduplicate events with debounce <br/> | ||
</p> | ||
``` | ||
|
||
### Last step: Save results | ||
|
||
_WARNING !_ Update this file before moving throw this step. | ||
|
||
Create a new package in [organization][https://github.com/astrohelm/] repository. | ||
Use next commands to save you package. | ||
<h2 align="center">Installation</h2> | ||
|
||
```bash | ||
git init | ||
git remote add origin your-package-location | ||
# Start from here, if you used template | ||
git branch -M main # if your default branch is not main | ||
git commit -am "Repository init" | ||
git tag v0.0.1 | ||
git push origin main | ||
git push origin v0.0.1 | ||
git checkout -b dev | ||
git push origin dev | ||
npm i leadwatch --save | ||
``` | ||
|
||
Return to your organization repository and do: | ||
|
||
- Add keywords | ||
- Update description | ||
- Draft release with `v0.0.1` tag and `v0.0.1` as a title and updated README file as description. | ||
|
||
> If you creating library you may publish it now to npm with `npm publish` command. | ||
<h2 align="center">Usage</h2> | ||
|
||
Congratulations, package initialized 🚀 | ||
|
||
## About files & structure | ||
|
||
This workspace have commonjs in use by default. You can switch it in package.json if you want. | ||
|
||
- `dist` directory used for fronted package analog. You can use it if your package is multi-platform based. | ||
- `eslint` astrohelm eslint rules | ||
- `types` .d.ts library types exports | ||
- `CHANGELOG.md` in use for project history documentation | ||
- `Makefile` ultimate commands shortcuts creator | ||
- `tests` here you can put all test coverage of your package | ||
- `.github` github ci pipeline by default | ||
- `lib` folder should contain all you library logic, _WARNING !_ Remove if you not writing library. Replace with src folder. | ||
```js | ||
const DirectoryWatcher = require('astrowatch'); | ||
const watcher = new DirectoryWatcher({ | ||
timeout: 200, // Events debouncing for queue | ||
ignore: [new RegExp(/[\D\d]+\.ignore\D*/)], // Ignore files and directories | ||
deep: false, // Include nested directories | ||
home: process.cwd(), // Removes root path from emits, Warning: ignore will work on full paths | ||
}); | ||
watcher.watch('/home/sashapop10/Downloads'); | ||
watcher.watch('/home/sashapop10/Documents'); | ||
watcher.on('before', updates => console.log({ before: updates })); | ||
watcher.on('change', path => console.log({ changed: path })); | ||
watcher.on('delete', path => console.log({ deleted: path })); | ||
watcher.on('after', updates => console.log({ after: updates })); | ||
``` | ||
|
||
<h2 align="center">Copyright & contributors</h2> | ||
|
||
<p align="center"> | ||
Copyright © 2023 <a href="https://github.com/astrohelm/workspace/graphs/contributors">Astrohelm contributors</a>. | ||
Workspace is <a href="./LICENSE">MIT licensed</a>.<br/> | ||
Workspace is part of <a href="https://github.com/astrohelm">Astrohelm ecosystem</a>. | ||
Copyright © 2023 <a href="https://github.com/astrohelm/astrowatch/graphs/contributors">Astrohelm contributors</a>. | ||
Astrowatch is <a href="./LICENSE">MIT licensed</a>.<br/> | ||
Astrowatch is part of <a href="https://github.com/astrohelm">Astrohelm ecosystem</a>. | ||
</p> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
'use strict'; | ||
|
||
const lib = require('./lib'); | ||
const fs = require('node:fs'); | ||
const { join, sep } = require('node:path'); | ||
const { access: accessible, schedular } = require('./lib'); | ||
const { EventEmitter } = require('node:events'); | ||
|
||
module.exports = { lib }; | ||
module.exports = function (options) { | ||
const [watchers, bridge] = [new Map(), new EventEmitter()]; | ||
const emit = schedular(options?.timeout, bridge.emit.bind(bridge)); | ||
const access = accessible.bind(null, options?.ignore ?? []); | ||
const watch = (path, f) => access(f.name) && f.isDirectory() && bridge.watch(join(path, f.name)); | ||
const watchFiles = (path, files, handler = watch.bind(null, path)) => files.forEach(handler); | ||
|
||
const setWatcher = path => { | ||
if (watchers.has(path)) return; | ||
const watcher = fs.watch(path, (_, filename) => { | ||
const target = path.endsWith(sep + filename) ? path : join(path, filename); | ||
if (!access(target)) return; | ||
fs.stat(target, (err, stats) => { | ||
const parsed = options?.home ? target.replace(options.home, '') : target; | ||
if (err) return void (bridge.unwatch(target), emit('delete', parsed)); | ||
stats.isDirectory() && options?.deep && bridge.watch(target), emit('change', parsed); | ||
return void 0; | ||
}); | ||
}); | ||
watchers.set(path, watcher); | ||
}; | ||
|
||
bridge.close = () => void (bridge.clear(), bridge.removeAllListeners()); | ||
bridge.clear = () => void (watchers.forEach(watcher => watcher.close()), watchers.clear()); | ||
bridge.unwatch = path => void (watchers.get(path)?.close(), watchers.delete(path)); | ||
bridge.watch = path => { | ||
if (watchers.has(path)) return; | ||
fs.stat(path, (err, stats) => { | ||
if (err) return; | ||
setWatcher(path); | ||
if (!stats.isDirectory() || !options?.deep) return; | ||
fs.readdir(path, { withFileTypes: true }, (err, files) => !err && watchFiles(path, files)); | ||
}); | ||
}; | ||
|
||
return bridge; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,29 @@ | ||
'use strict'; | ||
|
||
module.exports = {}; | ||
const TIMEOUT = 200; | ||
|
||
const access = (ignore, path) => | ||
ignore.reduce((acc, cur) => acc & !new RegExp(cur).test(path), true); | ||
|
||
const schedular = (timeout = TIMEOUT, emit) => { | ||
let timer = null; | ||
const queue = new Map(); | ||
|
||
const sendQueue = () => { | ||
if (!timer) return; | ||
timer = (clearTimeout(timer), null); | ||
const packet = [...queue.entries()]; | ||
queue.clear(); | ||
emit('before', packet); | ||
for (const [path, event] of packet) emit(event, path); | ||
emit('after', packet); | ||
}; | ||
|
||
return (event, path) => { | ||
if (timer) clearTimeout(timer); | ||
timer = setTimeout(sendQueue, timeout); | ||
queue.set(path, event); | ||
}; | ||
}; | ||
|
||
module.exports = { access, schedular }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.