1// @ts-check 2const { exec, Debouncer } = require("./utils"); 3 4class ProjectQueue { 5 /** 6 * @param {(projects: string[], lkg: boolean, force: boolean) => Promise<any>} action 7 */ 8 constructor(action) { 9 /** @type {{ lkg: boolean, force: boolean, projects?: string[], debouncer: Debouncer }[]} */ 10 this._debouncers = []; 11 this._action = action; 12 } 13 14 /** 15 * @param {string} project 16 * @param {object} options 17 */ 18 enqueue(project, { lkg = true, force = false } = {}) { 19 let entry = this._debouncers.find(entry => entry.lkg === lkg && entry.force === force); 20 if (!entry) { 21 const debouncer = new Debouncer(100, async () => { 22 const projects = entry.projects; 23 if (projects) { 24 entry.projects = undefined; 25 await this._action(projects, lkg, force); 26 } 27 }); 28 this._debouncers.push(entry = { lkg, force, debouncer }); 29 } 30 if (!entry.projects) entry.projects = []; 31 entry.projects.push(project); 32 return entry.debouncer.enqueue(); 33 } 34} 35 36const projectBuilder = new ProjectQueue((projects, lkg, force) => exec(process.execPath, [lkg ? "./lib/tsc" : "./built/local/tsc", "-b", ...(force ? ["--force"] : []), ...projects], { hidePrompt: true })); 37 38/** 39 * @param {string} project 40 * @param {object} [options] 41 * @param {boolean} [options.lkg=true] 42 * @param {boolean} [options.force=false] 43 */ 44exports.buildProject = (project, { lkg, force } = {}) => projectBuilder.enqueue(project, { lkg, force }); 45 46const projectCleaner = new ProjectQueue((projects, lkg) => exec(process.execPath, [lkg ? "./lib/tsc" : "./built/local/tsc", "-b", "--clean", ...projects], { hidePrompt: true })); 47 48/** 49 * @param {string} project 50 */ 51exports.cleanProject = (project) => projectCleaner.enqueue(project); 52 53const projectWatcher = new ProjectQueue((projects) => exec(process.execPath, ["./lib/tsc", "-b", "--watch", ...projects], { hidePrompt: true })); 54 55/** 56 * @param {string} project 57 * @param {object} [options] 58 * @param {boolean} [options.lkg=true] 59 */ 60exports.watchProject = (project, { lkg } = {}) => projectWatcher.enqueue(project, { lkg }); 61