<commit_before>import { Bundle } from "./core/Bundle";
import { ChildProcess, spawn } from "child_process";

export class FuseProcess {
    public node: ChildProcess;
    public filePath: string;
    constructor(public bundle: Bundle) { }

    public setFilePath(filePath: string) {
        this.filePath = filePath;
    }
    /** Kills a process if exists */
    public kill() {
        if (this.node) {
            this.node.kill();
        }
    }
    /** Starts a process (for example express server) */
    public start(): FuseProcess {
        this.kill();
        this.exec();
        return this;
    }

    /** Spawns another proces */
    public exec(): FuseProcess {
        const node = spawn("node", [this.filePath], {
            stdio: "inherit",
        });
        node.on("close", (code) => {
            if (code === 8) {
                console.error("Error detected, waiting for changes...");
            }
        });
        this.node = node;
        return this;
    }
}<commit_msg>proc.require() (#521)<commit_after>import { Bundle } from "./core/Bundle";
import { ChildProcess, spawn } from "child_process";
declare function require(name:string);

export class FuseProcess {
    public node: ChildProcess;
    public filePath: string;
    constructor(public bundle: Bundle) { }

    public setFilePath(filePath: string) {
        this.filePath = filePath;
    }
    /** Kills a process if exists */
    public kill() {
        if (this.node) {
            this.node.kill();
        }
    }
    /** Starts a process (for example express server) */
    public start(): FuseProcess {
        this.kill();
        this.exec();
        return this;
    }

		require(close?: ((exports: any) => void)|(() => void)) {
			function getMainExport(mdl) {
				return mdl && mdl.FuseBox && mdl.FuseBox.mainFile ?
						mdl.FuseBox.import(mdl.FuseBox.mainFile) :
						mdl;
			}
			return new Promise((resolve, reject)=> {
				var cache = (<any>require).cache, cached = cache[this.filePath], closePromise;
				if(cached) {
					cached = getMainExport(cached.exports);
					if(cached) {
						try {
							closePromise = close ? (<(exps: any) => void>close)(cached) :	//if a close function is given in parameter
								cached.close ? cached.close() : //if a `close` function is exported by the bundle
								cached.default && cached.default.close ?
									cached.default.close() : //if a `close` function is exported by the default export
								console.warn(`Bundle ${this.bundle.name} doesn't export a close() function and no close was given`);
						} catch(x) {
							console.error(`Exception while closing bundle ${this.bundle.name}.`);
							reject(x);
						}
					} else if(close) {
						closePromise = (<() => void>close)();
					}
					delete cache[this.filePath];
				}
				if(!(closePromise instanceof Promise))
					closePromise = Promise.resolve(true);
				closePromise.then(
					() => {
						var exps = false;
						try { exps = getMainExport(require(this.filePath)); }
						catch(x) { reject(x); }
						if(exps) resolve(exps);
					},
					x=> reject(x)
				);
			});
		}
    /** Spawns another proces */
    public exec(): FuseProcess {
        const node = spawn("node", [this.filePath], {
            stdio: "inherit",
        });
        node.on("close", (code) => {
            if (code === 8) {
                console.error("Error detected, waiting for changes...");
            }
        });
        this.node = node;
        return this;
    }
}
{'fraction_non_alphanumeric': 0.14432989690721648, 'fraction_numerical': 0.0014318442153493699, 'mean_word_length': 2.1696914700544463, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4967108', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1013, 'n_words': 264}