tswatch/ts/tswatch.classes.tswatch.ts

208 lines
6.8 KiB
TypeScript
Raw Normal View History

2022-03-14 20:17:36 +00:00
import * as plugins from './tswatch.plugins.js';
import * as paths from './tswatch.paths.js';
import * as interfaces from './interfaces/index.js';
2018-10-28 00:48:43 +00:00
2022-03-14 20:17:36 +00:00
import { Watcher } from './tswatch.classes.watcher.js';
import { logger } from './tswatch.logging.js';
2018-10-28 18:28:08 +00:00
2018-10-28 00:48:43 +00:00
export class TsWatch {
2019-05-08 22:08:40 +00:00
public watchmode: interfaces.TWatchModes;
2020-05-22 07:25:34 +00:00
public watcherMap = new plugins.lik.ObjectMap<Watcher>();
2023-03-31 11:10:29 +00:00
public typedserver: plugins.typedserver.TypedServer;
2018-10-28 00:48:43 +00:00
2019-05-08 22:08:40 +00:00
constructor(watchmodeArg: interfaces.TWatchModes) {
2019-05-13 07:48:35 +00:00
this.watchmode = watchmodeArg;
2018-10-28 00:48:43 +00:00
}
/**
2019-05-08 22:08:40 +00:00
* starts the TsWatch instance
2018-10-28 00:48:43 +00:00
*/
2019-05-13 07:48:35 +00:00
public async start() {
2022-03-18 19:16:58 +00:00
const tsbundle = new plugins.tsbundle.TsBundle();
const htmlHandler = new plugins.tsbundle.HtmlHandler();
2019-05-08 22:08:40 +00:00
switch (this.watchmode) {
case 'test':
/**
* this strategy runs test whenever there is a change in the ts directory
*/
2019-10-12 13:07:44 +00:00
this.watcherMap.add(
new Watcher({
filePathToWatch: paths.cwd,
commandToExecute: 'npm run test2',
2020-07-04 10:35:16 +00:00
timeout: null,
2019-10-12 13:07:44 +00:00
})
);
2019-05-08 22:08:40 +00:00
break;
case 'node':
2019-10-12 13:07:44 +00:00
this.watcherMap.add(
new Watcher({
filePathToWatch: paths.cwd,
commandToExecute: 'npm run test',
2020-07-04 10:35:16 +00:00
timeout: null,
2019-10-12 13:07:44 +00:00
})
);
break;
case 'element':
await (async () => {
/**
* this strategy runs a standard server and bundles the ts files to a dist_watch directory
*/
// lets create a standard server
logger.log(
'info',
'bundling TypeScript files to "dist_watch" Note: This is for development only!'
);
this.typedserver = new plugins.typedserver.TypedServer({
cors: true,
injectReload: true,
serveDir: plugins.path.join(paths.cwd, './dist_watch/'),
port: 3002,
enableCompression: true,
preferredCompressionMethod: 'gzip',
});
const bundleAndReloadElement = async () => {
await tsbundle.build(paths.cwd, './html/index.ts', './dist_watch/bundle.js', {
bundler: 'esbuild',
});
await this.typedserver.reload();
};
this.watcherMap.add(
new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, './ts_web/'),
functionToCall: async () => {
await bundleAndReloadElement();
},
timeout: null,
})
);
2022-08-02 13:38:04 +00:00
// lets get the other ts folders
let tsfolders = await plugins.smartfile.fs.listFolders(paths.cwd);
tsfolders = tsfolders.filter(
(itemArg) => itemArg.startsWith('ts') && itemArg !== 'ts_web'
);
const smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash',
2022-03-18 12:18:15 +00:00
});
for (const tsfolder of tsfolders) {
logger.log('info', `creating watcher for folder ${tsfolder}`);
this.watcherMap.add(
new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, `./${tsfolder}/`),
functionToCall: async () => {
logger.log('info', `building ${tsfolder}`);
2024-12-04 21:44:43 +00:00
await smartshellInstance.exec(`(cd ${paths.cwd} && npm run build)`);
await bundleAndReloadElement();
},
timeout: null,
})
);
}
this.watcherMap.add(
new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, './html/'),
functionToCall: async () => {
await htmlHandler.processHtml({
from: plugins.path.join(paths.cwd, './html/index.html'),
to: plugins.path.join(paths.cwd, './dist_watch/index.html'),
minify: false,
});
await bundleAndReloadElement();
},
timeout: null,
})
);
})();
2019-05-08 22:08:40 +00:00
break;
case 'website':
await (async () => {
const websiteExecution = new plugins.smartshell.SmartExecution('npm run startTs');
const bundleAndReloadWebsite = async () => {
await tsbundle.build(paths.cwd, './ts_web/index.ts', './dist_serve/bundle.js', {
bundler: 'esbuild',
});
};
let tsfolders = await plugins.smartfile.fs.listFolders(paths.cwd);
tsfolders = tsfolders.filter(
(itemArg) => itemArg.startsWith('ts') && itemArg !== 'ts_web'
);
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,
})
);
}
this.watcherMap.add(
new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, './ts_web/'),
functionToCall: async () => {
await bundleAndReloadWebsite();
},
timeout: null,
})
);
this.watcherMap.add(
new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, './html/'),
functionToCall: async () => {
await htmlHandler.processHtml({
from: plugins.path.join(paths.cwd, './html/index.html'),
to: plugins.path.join(paths.cwd, './dist_serve/index.html'),
minify: false,
});
await bundleAndReloadWebsite();
},
timeout: null,
})
);
})();
2020-07-04 10:49:00 +00:00
break;
case 'service':
2020-03-13 17:48:19 +00:00
this.watcherMap.add(
new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, './ts/'),
commandToExecute: 'npm run startTs',
2020-07-04 10:35:16 +00:00
timeout: null,
2020-03-13 17:48:19 +00:00
})
);
break;
case 'echo':
2019-05-13 07:48:35 +00:00
const tsWatchInstanceEchoSomething = new Watcher({
2019-10-12 15:34:05 +00:00
filePathToWatch: plugins.path.join(paths.cwd, './ts'),
2019-05-13 07:48:35 +00:00
commandToExecute: 'npm -v',
2020-07-04 10:35:16 +00:00
timeout: null,
2019-05-08 22:08:40 +00:00
});
2019-05-13 07:48:35 +00:00
this.watcherMap.add(tsWatchInstanceEchoSomething);
2019-05-08 22:08:40 +00:00
break;
default:
break;
2018-10-28 00:48:43 +00:00
}
2020-07-04 10:35:16 +00:00
this.watcherMap.forEach(async (watcher) => {
2019-05-08 22:08:40 +00:00
await watcher.start();
});
2023-03-31 11:10:29 +00:00
if (this.typedserver) {
await this.typedserver.start();
2019-10-14 12:53:55 +00:00
}
2018-10-28 00:48:43 +00:00
}
/**
2019-05-08 22:08:40 +00:00
* stops the execution of any active Watchers
2018-10-28 00:48:43 +00:00
*/
2019-05-13 07:48:35 +00:00
public async stop() {
2023-03-31 11:10:29 +00:00
if (this.typedserver) {
await this.typedserver.stop();
2019-10-14 12:53:55 +00:00
}
2020-07-04 10:35:16 +00:00
this.watcherMap.forEach(async (watcher) => {
2019-05-08 22:08:40 +00:00
await watcher.stop();
2019-05-13 07:48:35 +00:00
});
2018-10-28 00:48:43 +00:00
}
}