Skip to content

Commit

Permalink
Testing effector
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnussio committed Nov 13, 2020
1 parent 15a7f65 commit fdbee5b
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 70 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"chokidar": "3.4.3",
"crocks": "0.12.4",
"effector": "21.6.2",
"esbuild": "0.8.2",
"execa": "4.1.0",
"ora": "5.1.0",
Expand Down
136 changes: 71 additions & 65 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,80 @@
const path = require("path");
const termkit = require("terminal-kit");
const compose = require("crocks/helpers/compose");

const tap = require("crocks/helpers/tap");
const { statSync, existsSync } = require("fs");
const { guard } = require("effector");
const { trace } = require("console");
const fileInput = require("./fileInput");
const createRunner = require("./runner");

const { store, actions, types } = require("./store");

const term = termkit.terminal;

const baseDir = path.resolve(process.argv[2] || process.cwd());

const CHOOSE_FILE = Symbol("CHOOSE_FILE");
const RUNNING = Symbol("RUNNING");

const sleep = async (ms) => new Promise((res) => setTimeout(res, ms));

const main = async (options) => {
const runner = await createRunner();
let currentRunner;

const state = { current: CHOOSE_FILE, running: { file: undefined } };

const header = (opts) => {
term.moveTo(1, 1);
term.bgRed("🔥 Terminal playground 🔥\n\n");
return opts;
};

const menu = (opts) => {
term.bold("Usage\n");
/**
* Layout
* @param {*} opts
*/
const menu = (opts) => {
term.bold("Usage\n");

term.brightBlack(" 🏃 Press").white(" r ").brightBlack("to run file\n");
// term.brightBlack(" 📑 Press").white(" ESC ").brightBlack("back to menu\n");
term.brightBlack(" 🚪 Press").white(" CTRL + C ").brightBlack("to exit\n\n");
return opts;
};

term.brightBlack(" 🏃 Press").white(" r ").brightBlack("to run file\n");
// term.brightBlack(" 📑 Press").white(" ESC ").brightBlack("back to menu\n");
term
.brightBlack(" 🚪 Press")
.white(" CTRL + C ")
.brightBlack("to exit\n\n");
return opts;
};
/**
* Layout
* @param {*} opts
*/
const header = (opts) => {
term.moveTo(1, 1);
term.bgRed("🔥 Terminal playground 🔥\n\n");
return opts;
};

const currentFile = (baseDirPath, currentFilePath, isFile) => (
opts = { running: false, path: undefined }
) => {
// if (opts === true) {
// term.spinner("dotSpinner");
// }
term(isFile ? "Running file: " : "Watching directory: ").bold(
`${currentFilePath.replace(`${baseDirPath}/`, "")}\n\n`
/**
*
* @param {*} baseDirPath
* @param {*} currentFilePath
* @param {*} isFile
*/
const currentFile = (baseDirPath, currentFilePath, isFile) => (
opts = { running: false, path: undefined }
) => {
// if (opts === true) {
// term.spinner("dotSpinner");
// }
term(isFile ? "Running file: " : "Watching directory: ").bold(
`${currentFilePath.replace(`${baseDirPath}/`, "")}\n\n`
);

if (isFile === false && opts.path) {
term("Executed '").bold(opts.path.replace(`${baseDirPath}/`, ""))(
"' file.\n\n"
);
}

if (isFile === false && opts.path) {
term("Executed '").bold(opts.path.replace(`${baseDirPath}/`, ""))(
"' file.\n\n"
);
}
return opts;
};

return opts;
};
/**
*
* @param {{
* baseDir: import("fs").PathLike
* }} options
*/
const main = async (options) => {
const runner = await createRunner();

const chooseFile = (callback) => () => {
const chooseFile = () => {
term("Choose a file or directory: ");

fileInput(
Expand All @@ -74,27 +90,22 @@ const main = async (options) => {
if (error) {
term.red.bold(`An error occurs: ${error}\n`);
} else if (existsSync(input)) {
state.current = RUNNING;
state.running.file = input;
callback();
actions.runningFile(input);
} else {
term("\n\n");
term.red
.bold("File or directory does not exist:\n")
.bold(`💔 ${input}\n`);
await sleep(1000);
callback();
actions.choseFileOperation();
}
}
);
};

const loop = () => {
if (typeof currentRunner === "function") {
currentRunner();
}
const loop = (state) => {
switch (true) {
case state.current === RUNNING:
case state.current === types.RUNNING:
const isFile = statSync(state.running.file).isFile();
const runningScreen = compose(
currentFile(options.baseDir, state.running.file, isFile),
Expand All @@ -103,11 +114,11 @@ const main = async (options) => {
tap(term.clear)
);
runningScreen();
currentRunner = runner(runningScreen, state.running.file);
runner(runningScreen, state.running.file);
return;

case state.current === CHOOSE_FILE:
compose(chooseFile(loop), header, term.clear)();
case state.current === types.CHOOSE_FILE:
compose(chooseFile, header, term.clear)();
return;

default:
Expand All @@ -117,17 +128,14 @@ const main = async (options) => {

term.on("key", (key /* matches, data */) => {
// Running file in watch mode
if (state.current !== CHOOSE_FILE && (key === "r" || key === "R")) {
state.current = CHOOSE_FILE;
loop();
if (
(key === "r" || key === "R") &&
store.getState().current !== types.CHOOSE_FIL
) {
actions.choseFileOperation();
// guard({})
}

// Detect ESCAPE a
// if (key === "ESCAPE") {
// state.current = MENU;
// loop();
// }

// Detect CTRL-C and exit 'manually'
if (key === "CTRL_C") {
term.green("\n😅 Bye bye\n");
Expand All @@ -136,9 +144,7 @@ const main = async (options) => {
});

// term.grabInput(true);

// Start
loop();
store.watch(loop);
};

main({ baseDir });
14 changes: 9 additions & 5 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ const createRunner = async () => {
const service = await startService();
const runner = createInnerRunner();

let watcher;

process.on("SIGTERM", () => service.stop());

const executeFile = (screen) => (path) => {
Expand All @@ -145,7 +147,6 @@ const createRunner = async () => {
loader: "ts",
})
.then((value) => {
console.log(value);
return runner.run(path, screen, `${powerConsole}\n\n${value.code}`);
})
.catch((e) => {
Expand All @@ -155,11 +156,16 @@ const createRunner = async () => {
};

return (screen, paths) => {
if (watcher) {
watcher.close();
}

if (fs.statSync(paths).isFile()) {
executeFile(screen)(paths);
}

const watcher = chokidar.watch(paths);
watcher = chokidar.watch(paths);

try {
// Call transform() many times without the overhead of starting a service
watcher.on("change", executeFile(screen));
Expand All @@ -168,9 +174,7 @@ const createRunner = async () => {
// The child process can be explicitly killed when it's no longer needed
// service.stop();
}
return () => {
watcher.close();
};
return () => {};
};
};

Expand Down
34 changes: 34 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { createStore, createEvent } = require("effector");

const CHOOSE_FILE = Symbol("CHOOSE_FILE");
const RUNNING = Symbol("RUNNING");

const choseFileOperation = createEvent();
const runningFile = createEvent();

const store = createStore({
current: CHOOSE_FILE,
running: { file: undefined },
});

store
// Add reducer
.on(choseFileOperation, (state) => ({
...state,
current: CHOOSE_FILE,
}))
.on(runningFile, (state, file) => ({
...state,
current: RUNNING,
running: { file },
}));

store.watch((state) => {
console.log("state ... → ", state);
});

module.exports = {
store,
actions: { choseFileOperation, runningFile },
types: { CHOOSE_FILE, RUNNING },
};

0 comments on commit fdbee5b

Please sign in to comment.