fix(core): Refactor TsWatch class to improve website execution handling

This commit is contained in:
Philipp Kunz 2024-12-09 19:16:02 +01:00
parent 37f45a4912
commit 340efa5720
6 changed files with 244 additions and 35 deletions

View File

@ -1,5 +1,11 @@
# Changelog
## 2024-12-09 - 2.0.37 - fix(core)
Refactor TsWatch class to improve website execution handling
- Removed unnecessary Smartshell instance creation in TsWatch class.
- Ensured websiteExecution restarts and website bundle reloads in watcher function.
## 2024-12-09 - 2.0.36 - fix(dependencies)
Update @push.rocks/smartshell dependency version

View File

@ -11,7 +11,32 @@
"gitrepo": "tswatch",
"shortDescription": "watch typescript projects during development",
"npmPackagename": "@git.zone/tswatch",
"license": "MIT"
"license": "MIT",
"description": "A development tool for automatically watching and re-compiling TypeScript projects upon detecting file changes, enhancing developer workflows.",
"keywords": [
"TypeScript",
"development",
"watcher",
"automation",
"CLI",
"build",
"npm",
"web development",
"service development",
"project monitoring",
"recompiling",
"testing",
"integration",
"continuous development",
"tooling",
"build automation",
"live reloading",
"node.js",
"development server"
]
}
},
"tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
}
}

View File

@ -2,7 +2,7 @@
"name": "@git.zone/tswatch",
"version": "2.0.36",
"private": false,
"description": "watch typescript projects during development",
"description": "A development tool for automatically watching and re-compiling TypeScript projects upon detecting file changes, enhancing developer workflows.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
@ -50,5 +50,26 @@
],
"browserslist": [
"last 1 chrome versions"
],
"keywords": [
"TypeScript",
"development",
"watcher",
"automation",
"CLI",
"build",
"npm",
"web development",
"service development",
"project monitoring",
"recompiling",
"testing",
"integration",
"continuous development",
"tooling",
"build automation",
"live reloading",
"node.js",
"development server"
]
}
}

213
readme.md
View File

@ -1,39 +1,198 @@
# @git.zone/tswatch
watch typescript projects during development
A development tool for watching and re-compiling TypeScript projects automatically upon detecting changes.
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@git.zone/tswatch)
* [gitlab.com (source)](https://gitlab.com/gitzone/tswatch)
* [github.com (source mirror)](https://github.com/gitzone/tswatch)
* [docs (typedoc)](https://gitzone.gitlab.io/tswatch/)
## Install
## Status for master
To install `@git.zone/tswatch`, ensure that you have a Node.js environment set up with npm. You can install the package globally or locally within a project using npm:
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/gitzone/tswatch/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/gitzone/tswatch/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@git.zone/tswatch)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/gitzone/tswatch)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@git.zone/tswatch)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@git.zone/tswatch)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@git.zone/tswatch)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
```bash
npm install @git.zone/tswatch
```
This command will install the package and enable you to use the `tswatch` command in your terminal to watch TypeScript projects.
## Usage
Use TypeScript for best in class intellisense
`@git.zone/tswatch` is a powerful tool designed to streamline your development workflow by monitoring your TypeScript files and automatically recompiling them when changes are detected. This utility is particularly helpful during the development phase of a TypeScript project, ensuring your project remains up-to-date, reflecting the latest changes seamlessly.
## Contribution
### Getting Started with tswatch
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
To get started, we will explore setting up a basic watcher, integrating tswatch into a Node.js environment, utilizing advanced features, and handling multiple watchers.
For further information read the linked docs at the top of this readme.
### Setting Up a Basic Watcher
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
#### Step 1: Basic Command-Line Interface Usage
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)
After installing `@git.zone/tswatch`, you can start a watcher on your TypeScript project using its command-line interface (CLI). Here's how to initiate a watcher for different project types:
```typescript
import { runCli } from '@git.zone/tswatch';
(async () => {
await runCli();
})();
```
The CLI supports several commands to watch different project types such as `element`, `npm`, `service`, and `website`. Here's how to use them:
- **NPM Projects**: Watch a Node.js NPM project:
```bash
tswatch npm
```
This command will start a watcher for your node module projects. Whenever changes are detected, the specified npm scripts are executed.
- **Element Projects**: Watch an element-based architecture:
```bash
tswatch element
```
This command sets up a development server with live reloading and bundles TypeScript files to a `dist_watch` directory.
- **Service Projects**: Watch a service project:
```bash
tswatch service
```
Watches TypeScript files in a service pattern, restarting the service when changes are detected.
- **Website Projects**: Perfect for full website projects:
```bash
tswatch website
```
Similar to element projects but specifically designed for full websites, recompiling TypeScript files for web deployment scenarios.
#### Step 2: Integration into a Node.js Environment
You can integrate `@git.zone/tswatch` directly into a Node.js project for automatic file watching and recompiling:
```typescript
import { TsWatch } from '@git.zone/tswatch';
const startWatch = async () => {
// Initialize watch instance for a node project
const watchInstance = new TsWatch('node');
await watchInstance.start();
};
// Optionally, stop the watcher gracefully
const stopWatch = async (watchInstance: TsWatch) => {
await watchInstance.stop();
};
```
This setup will establish a node environment watch, automatically recompiling files as changes occur.
### Advanced Usage: Enhancing Workflow with tswatch
#### Starting a TypedServer with Watch Integration
For projects requiring a development server, integrate `typedserver` for handling HTTP server tasks:
```typescript
import { TsWatch } from '@git.zone/tswatch';
const startTypedServer = async () => {
const watchInstance = new TsWatch('element');
await watchInstance.start();
// Serve a local directory using a typedserver with CORS and compression
const server = watchInstance.typedserver;
if (server) {
await server.start();
}
};
```
This code watches for changes and serves the project locally, providing reload capabilities for smooth development.
#### Step 3: Custom Watchers
Beyond built-in commands, you can create custom watchers for detailed control over file monitoring and execution responses:
```typescript
import { Watcher } from '@git.zone/tswatch';
const customWatcherSetup = async () => {
const customWatcher = new Watcher({
filePathToWatch: '/path/to/watch',
commandToExecute: 'npm run custom-script',
});
// Start and stop the custom watcher as needed
await customWatcher.start();
await customWatcher.stop();
};
```
Define specific file paths and custom shell commands with the options provided by the `Watcher` class.
#### Step 4: Handling Multiple Watchers
To handle multiple directories or file sets, use the `ObjectMap` utility, efficiently managing multiple `Watcher` instances within the `TsWatch` framework:
```typescript
import { TsWatch } from '@git.zone/tswatch';
const setupMultipleWatchers = async () => {
const tsWatchInstance = new TsWatch('node');
// View active watchers through instance mapping
tsWatchInstance.watcherMap.forEach(watcher => {
console.log(`Watcher listening on: ${watcher.toString()}`);
});
// Initiate all watchers
await tsWatchInstance.start();
};
```
Incorporate this setup for efficiently managing complex projects with varied sources and parallel build tasks.
#### Handling Timeout and Cleanup
`@git.zone/tswatch` includes functions to manage process exits and timeout scenarios robustly:
```typescript
import { Watcher } from '@git.zone/tswatch';
const watcherWithTimeout = () => {
new Watcher({
filePathToWatch: './src/',
timeout: 10000, // 10 seconds timeout
commandToExecute: 'echo "Task completed"',
}).start();
};
```
The `timeout` option ensures processes don't run indefinitely, aiding development and potential automated testing scenarios.
### Conclusion
By providing flexible configurations, a robust CLI, and deep integration capabilities, `@git.zone/tswatch` serves as a comprehensive solution for automating and optimizing your TypeScript development processes. Whether managing server-side environments or advanced web apps, using this tool will ensure your projects are always ready with the latest changes.
Explore the various features and tailor the tool to fit your unique project requirements, leading to faster and more efficient development workflows.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@git.zone/tswatch',
version: '2.0.36',
description: 'watch typescript projects during development'
version: '2.0.37',
description: 'A development tool for automatically watching and re-compiling TypeScript projects upon detecting file changes, enhancing developer workflows.'
}

View File

@ -128,15 +128,13 @@ export class TsWatch {
tsfolders = tsfolders.filter(
(itemArg) => itemArg.startsWith('ts') && itemArg !== 'ts_web'
);
const smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash',
});
for (const tsfolder of tsfolders) {
this.watcherMap.add(
new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, `./${tsfolder}/`),
functionToCall: async () => {
await websiteExecution.restart();
await bundleAndReloadWebsite();
},
timeout: null,
})