BREAKING CHANGE(tswatch): refactor tswatch to a config-driven design (load config from npmextra.json) and add interactive init wizard; change TsWatch public API and enhance Watcher behavior
This commit is contained in:
185
ts/tswatch.classes.confighandler.ts
Normal file
185
ts/tswatch.classes.confighandler.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import * as plugins from './tswatch.plugins.js';
|
||||
import * as paths from './tswatch.paths.js';
|
||||
import * as interfaces from './interfaces/index.js';
|
||||
|
||||
const CONFIG_KEY = '@git.zone/tswatch';
|
||||
|
||||
/**
|
||||
* Preset configurations matching legacy watch modes
|
||||
*/
|
||||
const presets: Record<string, interfaces.ITswatchConfig> = {
|
||||
npm: {
|
||||
watchers: [
|
||||
{
|
||||
name: 'npm-test',
|
||||
watch: ['./ts/**/*', './test/**/*'],
|
||||
command: 'npm run test',
|
||||
restart: true,
|
||||
debounce: 300,
|
||||
runOnStart: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
test: {
|
||||
watchers: [
|
||||
{
|
||||
name: 'test2',
|
||||
watch: ['./ts/**/*', './test/**/*'],
|
||||
command: 'npm run test2',
|
||||
restart: true,
|
||||
debounce: 300,
|
||||
runOnStart: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
service: {
|
||||
watchers: [
|
||||
{
|
||||
name: 'service',
|
||||
watch: './ts/**/*',
|
||||
command: 'npm run startTs',
|
||||
restart: true,
|
||||
debounce: 300,
|
||||
runOnStart: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
element: {
|
||||
server: {
|
||||
enabled: true,
|
||||
port: 3002,
|
||||
serveDir: './dist_watch/',
|
||||
liveReload: true,
|
||||
},
|
||||
bundles: [
|
||||
{
|
||||
name: 'element-bundle',
|
||||
from: './html/index.ts',
|
||||
to: './dist_watch/bundle.js',
|
||||
watchPatterns: ['./ts_web/**/*'],
|
||||
triggerReload: true,
|
||||
},
|
||||
{
|
||||
name: 'html',
|
||||
from: './html/index.html',
|
||||
to: './dist_watch/index.html',
|
||||
watchPatterns: ['./html/**/*'],
|
||||
triggerReload: true,
|
||||
},
|
||||
],
|
||||
watchers: [
|
||||
{
|
||||
name: 'ts-build',
|
||||
watch: './ts/**/*',
|
||||
command: 'npm run build',
|
||||
restart: false,
|
||||
debounce: 300,
|
||||
runOnStart: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
website: {
|
||||
bundles: [
|
||||
{
|
||||
name: 'website-bundle',
|
||||
from: './ts_web/index.ts',
|
||||
to: './dist_serve/bundle.js',
|
||||
watchPatterns: ['./ts_web/**/*'],
|
||||
triggerReload: false,
|
||||
},
|
||||
{
|
||||
name: 'html',
|
||||
from: './html/index.html',
|
||||
to: './dist_serve/index.html',
|
||||
watchPatterns: ['./html/**/*'],
|
||||
triggerReload: false,
|
||||
},
|
||||
{
|
||||
name: 'assets',
|
||||
from: './assets/',
|
||||
to: './dist_serve/assets/',
|
||||
watchPatterns: ['./assets/**/*'],
|
||||
triggerReload: false,
|
||||
},
|
||||
],
|
||||
watchers: [
|
||||
{
|
||||
name: 'backend',
|
||||
watch: './ts/**/*',
|
||||
command: 'npm run startTs',
|
||||
restart: true,
|
||||
debounce: 300,
|
||||
runOnStart: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles loading and managing tswatch configuration
|
||||
*/
|
||||
export class ConfigHandler {
|
||||
private npmextra: plugins.npmextra.Npmextra;
|
||||
private cwd: string;
|
||||
|
||||
constructor(cwdArg?: string) {
|
||||
this.cwd = cwdArg || paths.cwd;
|
||||
this.npmextra = new plugins.npmextra.Npmextra(this.cwd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a tswatch configuration exists
|
||||
*/
|
||||
public hasConfig(): boolean {
|
||||
const config = this.npmextra.dataFor<interfaces.ITswatchConfig>(CONFIG_KEY, null);
|
||||
return config !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load configuration from npmextra.json
|
||||
* If a preset is specified, merge preset defaults with user overrides
|
||||
*/
|
||||
public loadConfig(): interfaces.ITswatchConfig | null {
|
||||
const config = this.npmextra.dataFor<interfaces.ITswatchConfig>(CONFIG_KEY, null);
|
||||
|
||||
if (!config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// If a preset is specified, merge it with user config
|
||||
if (config.preset && presets[config.preset]) {
|
||||
const preset = presets[config.preset];
|
||||
return {
|
||||
...preset,
|
||||
...config,
|
||||
// Merge arrays instead of replacing
|
||||
watchers: config.watchers || preset.watchers,
|
||||
bundles: config.bundles || preset.bundles,
|
||||
server: config.server !== undefined ? config.server : preset.server,
|
||||
};
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a preset configuration by name
|
||||
*/
|
||||
public getPreset(presetName: string): interfaces.ITswatchConfig | null {
|
||||
return presets[presetName] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available preset names
|
||||
*/
|
||||
public getPresetNames(): string[] {
|
||||
return Object.keys(presets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the config key for npmextra.json
|
||||
*/
|
||||
public getConfigKey(): string {
|
||||
return CONFIG_KEY;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user