Skip to content

Commit

Permalink
no-save opt
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnussio committed Mar 16, 2021
1 parent 4a530df commit ce6203a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
30 changes: 21 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ 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(" s ")
.brightBlack("Toggle npm install no save option: ")
.white(`npm install${opts.npmInstallNoSave ? " --no-save" : ""}\n`);
if (opts.npmInstallNoSave) {
term
.red(
" ⚠️ with --no-save you can import only one package at time not included in package.json\n"
)
.brightBlack(" More https://github.com/npm/cli/issues/1460\n");
}
term.brightBlack(" 🚪 Press").white(" CTRL + C ").brightBlack("to exit\n\n");
return opts;
};
Expand All @@ -48,9 +59,6 @@ const header = (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`
);
Expand All @@ -71,6 +79,7 @@ const currentFile = (baseDirPath, currentFilePath, isFile) => (
* }} options
*/
const main = async (options) => {
term.clear();
const runner = await createRunner();

const chooseFile = () => {
Expand Down Expand Up @@ -102,6 +111,7 @@ const main = async (options) => {
};

const loop = (state) => {
term.clear();
switch (true) {
case state.current === types.RUNNING:
const isFile = statSync(state.running.file).isFile();
Expand All @@ -111,7 +121,7 @@ const main = async (options) => {
header,
tap(term.clear)
);
runningScreen();
runningScreen(state);
runner(runningScreen, state.running.file);
return;

Expand All @@ -126,14 +136,16 @@ const main = async (options) => {

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

if (["s", "S"].includes(key) && notChoosingFile) {
actions.toggleNpmInstallSaveOpt();
}

// Detect CTRL-C and exit 'manually'
if (key === "CTRL_C") {
term.green("\n😅 Bye bye\n");
Expand Down
6 changes: 4 additions & 2 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const chokidar = require("chokidar");
const term = require("terminal-kit").terminal;
const execa = require("execa");
const { startService } = require("esbuild");

const { store } = require("./store");
// const ora = require("ora");

const powerConsole = `
Expand Down Expand Up @@ -57,14 +59,14 @@ const installMissingDeps = async (err) =>
const m = err.match(/Cannot find module '(.+)'/);
if (m && m.length > 1) {
const missingModuleName = m[1].replace(/\/.*$/, "");
term("Do you want install '").bold(missingModuleName)("'? [Y|n]\n");
term(`Do you want install `).bold(missingModuleName)("'? [Y|n]\n");
term.yesOrNo({ yes: ["y", "ENTER"], no: ["n"] }, (error, result) => {
if (result) {
// const spinner = ora("Installing missing dependency...\n").start();
term("Installing missing dependency...\n\n");
const subprocess = execa("npm", [
"install",
// "--no-save", // https://github.com/npm/cli/issues/1460
store.getState().npmInstallNoSave && "--no-save", // https://github.com/npm/cli/issues/1460
missingModuleName,
]);
// subprocess.stdout.on("data", term);
Expand Down
8 changes: 7 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ const RUNNING = Symbol("RUNNING");

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

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

store
Expand All @@ -21,10 +23,14 @@ store
...state,
current: RUNNING,
running: { file },
}))
.on(toggleNpmInstallSaveOpt, (state) => ({
...state,
npmInstallNoSave: !state.npmInstallNoSave,
}));

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

0 comments on commit ce6203a

Please sign in to comment.