Skip to content

Commit

Permalink
File & dir runner
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnussio committed Nov 4, 2020
1 parent d00ebd3 commit 35b6a32
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
29 changes: 17 additions & 12 deletions src/fileInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
SOFTWARE.
*/
const termkit = require("terminal-kit");
const autoComplete = termkit.autoComplete;

const { autoComplete } = termkit;
const term = termkit.terminal;
const fs = require("fs");
const path = require("path");
const { resolve } = require("path");

/*
/!\ Document that!!! /!\
Expand All @@ -42,8 +42,8 @@ function fileInput(options, callback) {
options = {};
}

var baseDir;
var promise = Promise.resolve();
let baseDir;
const promise = Promise.resolve();

if (options.baseDir) {
baseDir = path.resolve(options.baseDir);
Expand Down Expand Up @@ -79,7 +79,7 @@ function fileInput(options, callback) {
);
});

return Promise;
return promise;
}
} else {
baseDir = process.cwd();
Expand All @@ -89,15 +89,19 @@ function fileInput(options, callback) {
baseDir += "/";
}

var autoCompleter = async function autoCompleter(inputString) {
var inputDir, inputFile, currentDir, files, completion;
const autoCompleter = async function autoCompleter(inputString) {
let inputDir;
let inputFile;
let currentDir;
let files;
let completion;

if (inputString[inputString.length - 1] === "/") {
inputDir = inputString;
inputFile = "";
} else {
inputDir = path.dirname(inputString);
inputDir = inputDir === "." ? "" : inputDir + "/";
inputDir = inputDir === "." ? "" : `${inputDir}/`;
inputFile = path.basename(inputString);
}

Expand All @@ -108,7 +112,7 @@ function fileInput(options, callback) {
currentDir = baseDir + inputDir;
}

//console.error( "### '" + inputDir +"' '"+ inputFile +"' '"+ currentDir + "'" ) ;
// console.error( "### '" + inputDir +"' '"+ inputFile +"' '"+ currentDir + "'" ) ;
try {
files = await readdir(currentDir, options);
} catch (error) {
Expand All @@ -132,11 +136,12 @@ function fileInput(options, callback) {
};

// Transmit options to inputField()
options = Object.assign({}, options, {
options = {
...options,
autoComplete: autoCompleter,
autoCompleteMenu: true,
minLength: 1,
});
};

term.inputField(options).promise.then(
(input) => {
Expand Down Expand Up @@ -177,7 +182,7 @@ function readdir(dir, options) {
}

const filteredDirs =
files.filter((f) => f.isDirectory()).map((f) => f.name + "/") || [];
files.filter((f) => f.isDirectory()).map((f) => `${f.name}/`) || [];

const filteredFiles =
files
Expand Down
8 changes: 4 additions & 4 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function createInnerRunner() {
let result = "";
let err = "";
try {
screen(true);
screen({ running: true, path });
subprocess = execa("node", ["-e", code], {
cwd: dirname(path),
});
Expand All @@ -110,8 +110,7 @@ function createInnerRunner() {
result += str;
});
subprocess.stdout.on("end", () => {
term.clear();
screen(false);
screen({ running: false, path });
term(`${result}\n`);
});
subprocess.stderr.on("data", (str) => {
Expand All @@ -121,7 +120,7 @@ function createInnerRunner() {
await subprocess;
} catch (error) {
if (!isDisabled) {
screen(false);
screen({ running: false, path });
term.red.bold("Error:\n\n");
term.red(`- ${err}\n\n`);
// eslint-disable-next-line no-await-in-loop
Expand Down Expand Up @@ -152,6 +151,7 @@ const createRunner = async () => {
loader: "ts",
})
.then((value) => {
console.log(value);
return runner.run(path, screen, `${powerConsole}\n\n${value.code}`);
})
.catch((e) => {
Expand Down
22 changes: 17 additions & 5 deletions src/term.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require("path");
const termkit = require("terminal-kit");
const compose = require("crocks/helpers/compose");
const tap = require("crocks/helpers/tap");
const { statSync } = require("fs");
const fileInput = require("./fileInput");
const createRunner = require("./runner");

Expand All @@ -21,7 +22,7 @@ const main = async (options) => {

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

Expand All @@ -37,18 +38,27 @@ const main = async (options) => {
return opts;
};

const currentFile = (baseDirPath, currentFilePath) => (opts) => {
const currentFile = (baseDirPath, currentFilePath, isFile) => (
opts = { running: false, path: undefined }
) => {
// if (opts === true) {
// term.spinner("dotSpinner");
// }
term("Running file: ").bold(
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"
);
}

return opts;
};

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

fileInput(
{
Expand Down Expand Up @@ -76,12 +86,14 @@ const main = async (options) => {
}
switch (true) {
case state.current === RUNNING:
const isFile = statSync(state.running.file).isFile();
const runningScreen = compose(
currentFile(options.baseDir, state.running.file),
currentFile(options.baseDir, state.running.file, isFile),
menu,
header,
tap(term.clear)
);
runningScreen();
currentRunner = runner(runningScreen, state.running.file);
return;

Expand Down

0 comments on commit 35b6a32

Please sign in to comment.