• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { exec, Debouncer } from "./utils.mjs";
2import { resolve } from "path";
3import { findUpRoot } from "./findUpDir.mjs";
4import cmdLineOptions from "./options.mjs";
5
6class ProjectQueue {
7    /**
8     * @param {(projects: string[]) => Promise<any>} action
9     */
10    constructor(action) {
11        /** @type {string[] | undefined} */
12        this._projects = undefined;
13        this._debouncer = new Debouncer(100, async () => {
14            const projects = this._projects;
15            if (projects) {
16                this._projects = undefined;
17                await action(projects);
18            }
19        });
20    }
21
22    /**
23     * @param {string} project
24     */
25    enqueue(project) {
26        if (!this._projects) this._projects = [];
27        this._projects.push(project);
28        return this._debouncer.enqueue();
29    }
30}
31
32const execTsc = (/** @type {string[]} */ ...args) =>
33    exec(process.execPath,
34         [resolve(findUpRoot(), cmdLineOptions.lkg ? "./lib/tsc.js" : "./built/local/tsc.js"),
35          "-b", ...args],
36         { hidePrompt: true });
37
38const projectBuilder = new ProjectQueue((projects) => execTsc(...projects));
39
40/**
41 * @param {string} project
42 */
43export const buildProject = (project) => projectBuilder.enqueue(project);
44
45const projectCleaner = new ProjectQueue((projects) => execTsc("--clean", ...projects));
46
47/**
48 * @param {string} project
49 */
50 export const cleanProject = (project) => projectCleaner.enqueue(project);
51
52const projectWatcher = new ProjectQueue((projects) => execTsc("--watch", "--preserveWatchOutput", ...projects));
53
54/**
55 * @param {string} project
56 */
57export const watchProject = (project) => projectWatcher.enqueue(project);
58