Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"license": "ISC",
"dependencies": {
"@aws-sdk/client-s3": "3.420.0",
"chokidar": "3",
"commander": "10.0.1",
"jszip": "3.10.1",
"typescript": "^5.0.4",
Expand Down
115 changes: 115 additions & 0 deletions src/classes/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import Logger from '../utils/logger';
import { deepCloneMap } from '../utils/functions';
import { configManagerInstance } from '../config';
import { PipeConfigArray } from '../types/ConfigTypes';
import { watch as chokidarWatch } from 'chokidar';
import * as path from 'path';
import { ROOT_PATH } from '../utils/constants';

export default class Runner {
project = '';
Expand Down Expand Up @@ -38,6 +41,8 @@ export default class Runner {

coloredOutput = true;

watchMode = false;

static workspace = new Map<string, Set<string>>();

constructor(...args: readonly string[]) {
Expand All @@ -56,6 +61,7 @@ export default class Runner {
.option('-nc, --noCache', 'default: false. If true, will skip the cache and execute the target.')
.option('-np, --noPipe', 'default: false. If true, will skip the pipe and execute the target.')
.option('-co, --coloredOutput <color>', 'default: true. If false, will disable colors in the console.', 'true')
.option('-w, --watch', 'default: false. If true, will watch for file changes and rebuild automatically.')
.addOption(
new Option(
'-l, --logLevel <logLevel>',
Expand Down Expand Up @@ -112,6 +118,9 @@ export default class Runner {
ZENITH_READ_ONLY: true
});
}
if (options.watch) {
this.watchMode = true;
}
this.debugLocation = options.debugLocation;
this.worker = options.worker;
this.pipe = options.noPipe ? [] : ConfigHelperInstance.pipe;
Expand All @@ -121,6 +130,10 @@ export default class Runner {
}

async runWrapper(): Promise<void> {
if (this.watchMode) {
await this.runWatch();
return;
}
if (this.pipe.length === 0) {
await this.run(this.command);
return;
Expand Down Expand Up @@ -169,4 +182,106 @@ export default class Runner {
Logger.log(2, Builder.outputColor, `Zenith ${command} started.`);
await Builder.build();
}

async runWatch(): Promise<void> {
console.log('');
console.log('👁️ Watch mode enabled');
console.log('═══════════════════════════════════════');
console.log(` Target: ${this.command}`);
console.log(` Project filter: ${this.project}`);
console.log('');

// Get the list of projects to watch
const projectPaths: string[] = [];
const projects = ConfigHelperInstance.projects;

for (const [projectName, projectPath] of Object.entries(projects)) {
if (this.project === 'all' || this.project === projectName) {
const fullPath = path.join(ROOT_PATH, projectPath);
projectPaths.push(fullPath);
console.log(` Watching: ${projectPath}`);
}
}

if (projectPaths.length === 0) {
console.log(' ⚠️ No projects to watch');
return;
}

console.log('');
console.log(' Press Ctrl+C to stop watching.');
console.log('');

// Run initial build
console.log('🔨 Running initial build...');
console.log('');
await this.run(this.command);

// Set up file watcher
const ignorePatterns = ConfigHelperInstance.ignoreFiles || [];
const ignored = [
'**/node_modules/**',
'**/.git/**',
'**/dist/**',
'**/build/**',
'**/.zenith-cache/**',
...ignorePatterns.map(p => `**/${p}/**`)
];

let isBuilding = false;
let pendingBuild = false;

const watcher = chokidarWatch(projectPaths, {
ignored,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 100,
pollInterval: 100
}
});

const triggerBuild = async (filePath: string) => {
const relativePath = path.relative(ROOT_PATH, filePath);

if (isBuilding) {
pendingBuild = true;
return;
}

isBuilding = true;
console.log('');
console.log(`📝 File changed: ${relativePath}`);
console.log('🔨 Rebuilding...');
console.log('');

try {
// Reset workspace to detect changes
Runner.workspace = new Map();
await this.run(this.command);
console.log('');
console.log('✅ Build completed. Watching for changes...');
} catch (error) {
console.error('');
console.error('❌ Build failed:', error instanceof Error ? error.message : error);
console.log('');
console.log('👁️ Watching for changes...');
}

isBuilding = false;

if (pendingBuild) {
pendingBuild = false;
await triggerBuild(filePath);
}
};

watcher
.on('change', triggerBuild)
.on('add', triggerBuild)
.on('unlink', triggerBuild);

// Keep the process running
await new Promise(() => {});
}
}
46 changes: 40 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,7 @@ ansi-styles@^5.0.0:
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-5.2.0.tgz#07449690ad45777d1924ac2abb2fc8895dba836b"
integrity sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==

anymatch@^3.0.3:
anymatch@^3.0.3, anymatch@~3.1.2:
version "3.1.3"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.3.tgz#790c58b19ba1720a84205b57c618d5ad8524973e"
integrity sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==
Expand Down Expand Up @@ -2195,6 +2195,11 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==

binary-extensions@^2.0.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.3.0.tgz#f6e14a97858d327252200242d4ccfe522c445522"
integrity sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==

bowser@^2.11.0:
version "2.11.0"
resolved "https://registry.yarnpkg.com/bowser/-/bowser-2.11.0.tgz#5ca3c35757a7aa5771500c70a73a9f91ef420a8f"
Expand All @@ -2208,7 +2213,7 @@ brace-expansion@^1.1.7:
balanced-match "^1.0.0"
concat-map "0.0.1"

braces@^3.0.3:
braces@^3.0.3, braces@~3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.3.tgz#490332f40919452272d55a8480adc0c441358789"
integrity sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==
Expand Down Expand Up @@ -2292,6 +2297,21 @@ char-regex@^1.0.2:
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
integrity sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==

chokidar@3:
version "3.6.0"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.6.0.tgz#197c6cc669ef2a8dc5e7b4d97ee4e092c3eb0d5b"
integrity sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==
dependencies:
anymatch "~3.1.2"
braces "~3.0.2"
glob-parent "~5.1.2"
is-binary-path "~2.1.0"
is-glob "~4.0.1"
normalize-path "~3.0.0"
readdirp "~3.6.0"
optionalDependencies:
fsevents "~2.3.2"

ci-info@^3.2.0:
version "3.8.0"
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-3.8.0.tgz#81408265a5380c929f0bc665d62256628ce9ef91"
Expand Down Expand Up @@ -2994,7 +3014,7 @@ get-symbol-description@^1.0.0:
call-bind "^1.0.2"
get-intrinsic "^1.1.1"

glob-parent@^5.1.2:
glob-parent@^5.1.2, glob-parent@~5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
integrity sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==
Expand Down Expand Up @@ -3241,6 +3261,13 @@ is-bigint@^1.0.1:
dependencies:
has-bigints "^1.0.1"

is-binary-path@~2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-binary-path/-/is-binary-path-2.1.0.tgz#ea1f7f3b80f064236e83470f86c09c254fb45b09"
integrity sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==
dependencies:
binary-extensions "^2.0.0"

is-boolean-object@^1.1.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/is-boolean-object/-/is-boolean-object-1.1.2.tgz#5c6dc200246dd9321ae4b885a114bb1f75f63719"
Expand Down Expand Up @@ -3283,7 +3310,7 @@ is-generator-fn@^2.0.0:
resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-2.1.0.tgz#7d140adc389aaf3011a8f2a2a4cfa6faadffb118"
integrity sha512-cTIB4yPYL/Grw0EaSzASzg6bBy9gqCofvWN8okThAYIxKJZC+udlRAmGbM0XLeniEJSs8uEgHPGuHSe1XsOLSQ==

is-glob@^4.0.0, is-glob@^4.0.1, is-glob@^4.0.3:
is-glob@^4.0.0, is-glob@^4.0.1, is-glob@^4.0.3, is-glob@~4.0.1:
version "4.0.3"
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.3.tgz#64f61e42cbbb2eec2071a9dac0b28ba1e65d5084"
integrity sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==
Expand Down Expand Up @@ -4103,7 +4130,7 @@ node-releases@^2.0.13:
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.13.tgz#d5ed1627c23e3461e819b02e57b75e4899b1c81d"
integrity sha512-uYr7J37ae/ORWdZeQ1xxMJe3NtdmqMC/JZK+geofDrkLUApKRHPd18/TxtBOJ4A0/+uUIliorNrfYV6s1b02eQ==

normalize-path@^3.0.0:
normalize-path@^3.0.0, normalize-path@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==
Expand Down Expand Up @@ -4274,7 +4301,7 @@ picocolors@^1.0.0:
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c"
integrity sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==

picomatch@^2.0.4, picomatch@^2.2.3, picomatch@^2.3.1:
picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3, picomatch@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==
Expand Down Expand Up @@ -4356,6 +4383,13 @@ readable-stream@~2.3.6:
string_decoder "~1.1.1"
util-deprecate "~1.0.1"

readdirp@~3.6.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.6.0.tgz#74a370bd857116e245b29cc97340cd431a02a6c7"
integrity sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==
dependencies:
picomatch "^2.2.1"

regexp.prototype.flags@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.5.1.tgz#90ce989138db209f81492edd734183ce99f9677e"
Expand Down