Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
shuritch committed Aug 25, 2023
1 parent 9386867 commit 2701e13
Show file tree
Hide file tree
Showing 10 changed files with 358 additions and 205 deletions.
29 changes: 8 additions & 21 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@

## [Unreleased][unreleased]

## [1.1.1][] - 2023-08-23
## [1.0.0][] - 2023-08-25

- Astrohelm config update
- Packages update
- Moved from Leadfisher
- New <code>.close</code> and <code>close</code> methods
- New option <code>home</code>
- New Tests
- JSDoc

## [1.1.0][] - 2023-08-02

- Prettier row length 120 -> 100
- Makefile git commands
- Grep command
- Replace command
- Search between files command
- Search between rows command

## [1.0.0][] - 2023-07-31

- Stable release version
- Repository created

[unreleased]: https://github.com/astrohelm/workspace/compare/v1.1.1...HEAD
[1.1.1]: https://github.com/astrohelm/workspace/compare/v1.1.0...v1.1.1
[1.1.0]: https://github.com/astrohelm/workspace/compare/release...v1.1.0
[1.0.0]: https://github.com/astrohelm/workspace/releases/tag/release
[unreleased]: https://github.com/astrohelm/astrowatch/compare/v1.0.0...HEAD
[1.0.0]: https://github.com/astrohelm/astrowatch/releases/tag/v1.0.0
177 changes: 25 additions & 152 deletions README.md
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>
5 changes: 0 additions & 5 deletions dist/.eslintrc

This file was deleted.

43 changes: 41 additions & 2 deletions index.js
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;
};
28 changes: 27 additions & 1 deletion lib/index.js
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 };
4 changes: 2 additions & 2 deletions package-lock.json

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

31 changes: 13 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
{
"license": "MIT",
"version": "1.1.1",
"version": "1.0.0",
"type": "commonjs",
"name": "astrohelm-workspace",
"name": "astrowatch",
"homepage": "https://astrohelm.ru",
"description": "Astrohelm workspace example",
"description": "Astrohelm file system watcher",
"author": "Alexander Ivanov <sashapop101@gmail.com>",
"keywords": [
"nodejs",
"zero-dependencies",
"prettier",
"eslint",
"ts",
"workspace",
"example",
"preset",
"starter-kit",
"astrohelm",
"node.js",
"javascript",
"typescript"
"astrohelm",
"fs",
"watcher",
"filesystem",
"zero-dependencies"
],

"main": "index.js",
Expand All @@ -27,18 +22,18 @@
"readmeFilename": "README.md",
"engines": { "node": "18 || 19 || 20" },
"browser": {},
"files": ["/dist", "/lib", "/types"],
"files": ["/lib", "/types"],

"scripts": {
"test": "node --test && tsc",
"test": "node --test",
"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/workspace.git" },
"repository": { "type": "git", "url": "git+https://github.com/astrohelm/astrowatcher.git" },
"bugs": {
"url": "https://github.com/astrohelm/workspace/issues",
"url": "https://github.com/astrohelm/astrowatcher/issues",
"email": "sashapop101@gmail.com"
},

Expand Down
Loading

0 comments on commit 2701e13

Please sign in to comment.