tswatch/dist_ts/tswatch.classes.tswatch.js

169 lines
15 KiB
JavaScript
Raw Normal View History

2022-03-14 21:04:11 +00:00
import * as plugins from './tswatch.plugins.js';
import * as paths from './tswatch.paths.js';
2023-08-26 12:56:09 +00:00
import * as interfaces from './interfaces/index.js';
2022-03-14 21:04:11 +00:00
import { Watcher } from './tswatch.classes.watcher.js';
export class TsWatch {
2020-05-22 07:25:34 +00:00
constructor(watchmodeArg) {
this.watcherMap = new plugins.lik.ObjectMap();
this.watchmode = watchmodeArg;
}
/**
* starts the TsWatch instance
*/
async start() {
2022-04-21 21:41:45 +00:00
const tsbundle = new plugins.tsbundle.TsBundle();
const htmlHandler = new plugins.tsbundle.HtmlHandler();
2020-05-22 07:25:34 +00:00
switch (this.watchmode) {
case 'test':
/**
* this strategy runs test whenever there is a change in the ts directory
*/
2022-03-14 21:04:11 +00:00
this.watcherMap.add(new Watcher({
2020-05-22 07:25:34 +00:00
filePathToWatch: paths.cwd,
commandToExecute: 'npm run test2',
2021-03-08 00:26:44 +00:00
timeout: null,
2020-05-22 07:25:34 +00:00
}));
break;
case 'node':
2022-03-14 21:04:11 +00:00
this.watcherMap.add(new Watcher({
2020-05-22 07:25:34 +00:00
filePathToWatch: paths.cwd,
commandToExecute: 'npm run test',
2021-03-08 00:26:44 +00:00
timeout: null,
2020-05-22 07:25:34 +00:00
}));
break;
case 'element':
(async () => {
/**
* this strategy runs a standard server and bundles the ts files to a dist_watch directory
*/
// lets create a standard server
console.log('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',
2022-03-18 15:36:13 +00:00
});
const bundleAndReloadElement = async () => {
await tsbundle.build(paths.cwd, './html/index.ts', './dist_watch/bundle.js', {
bundler: 'esbuild',
2022-08-02 13:38:04 +00:00
});
await this.typedserver.reload();
};
this.watcherMap.add(new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, './ts_web/'),
functionToCall: async () => {
await bundleAndReloadElement();
},
timeout: null,
}));
// 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-04-21 21:41:45 +00:00
});
for (const tsfolder of tsfolders) {
this.watcherMap.add(new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, `./${tsfolder}/`),
functionToCall: async () => {
await smartshellInstance.exec(`npm run build`);
await this.typedserver.reload();
},
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,
}));
})();
break;
case 'website':
(async () => {
const bundleAndReloadWebsite = async () => {
await tsbundle.build(paths.cwd, './ts_web/index.ts', './dist_serve/bundle.js', {
bundler: 'esbuild',
2022-08-02 13:38:04 +00:00
});
};
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',
});
for (const tsfolder of tsfolders) {
this.watcherMap.add(new Watcher({
filePathToWatch: plugins.path.join(paths.cwd, `./${tsfolder}/`),
commandToExecute: `npm run startTs`,
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-05-22 07:25:34 +00:00
break;
case 'service':
2022-03-14 21:04:11 +00:00
this.watcherMap.add(new Watcher({
2020-05-22 07:25:34 +00:00
filePathToWatch: plugins.path.join(paths.cwd, './ts/'),
commandToExecute: 'npm run startTs',
2021-03-08 00:26:44 +00:00
timeout: null,
2020-05-22 07:25:34 +00:00
}));
break;
case 'echo':
2022-03-14 21:04:11 +00:00
const tsWatchInstanceEchoSomething = new Watcher({
2020-05-22 07:25:34 +00:00
filePathToWatch: plugins.path.join(paths.cwd, './ts'),
commandToExecute: 'npm -v',
2021-03-08 00:26:44 +00:00
timeout: null,
2020-05-22 07:25:34 +00:00
});
this.watcherMap.add(tsWatchInstanceEchoSomething);
break;
default:
break;
}
this.watcherMap.forEach(async (watcher) => {
await watcher.start();
});
2023-03-31 11:10:29 +00:00
if (this.typedserver) {
await this.typedserver.start();
2020-05-22 07:25:34 +00:00
}
}
/**
* stops the execution of any active Watchers
*/
async stop() {
2023-03-31 11:10:29 +00:00
if (this.typedserver) {
await this.typedserver.stop();
2020-05-22 07:25:34 +00:00
}
this.watcherMap.forEach(async (watcher) => {
await watcher.stop();
});
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHN3YXRjaC5jbGFzc2VzLnRzd2F0Y2guanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90c3dhdGNoLmNsYXNzZXMudHN3YXRjaC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssT0FBTyxNQUFNLHNCQUFzQixDQUFDO0FBQ2hELE9BQU8sS0FBSyxLQUFLLE1BQU0sb0JBQW9CLENBQUM7QUFDNUMsT0FBTyxLQUFLLFVBQVUsTUFBTSx1QkFBdUIsQ0FBQztBQUVwRCxPQUFPLEVBQUUsT0FBTyxFQUFF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