Skip to content

Commit

Permalink
Use cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnussio committed Mar 23, 2021
1 parent ba04131 commit 1fac45c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 49 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"effector": "^21.8.4",
"esbuild": "^0.8.51",
"execa": "^5.0.0",
"false": "0.0.4",
"ora": "^5.3.0",
"ramda": "^0.27.1",
"terminal-kit": "^2.0.3"
},
"devDependencies": {
Expand Down
41 changes: 41 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const isNil = (x) => x === undefined || x === null;

const when = (pred, f) => (x) => (pred(x) ? f(x) : x);

const hasInspect = (x) => !isNil(x) && typeof x.inspect === "function";

const inspect = when(hasInspect, (x) => x.inspect());

// eslint-disable-next-line no-global-assign
console = ((clog) => ({
...clog,
log(...args) {
const writeLog = (arg) => {
if (typeof arg === "string") {
return `\x1b[1;33m${arg}\x1b[0m`;
}
if (typeof arg === "number") {
return `\x1b[1;34m${arg}\x1b[0m`;
}
if (typeof arg === "boolean") {
return `\x1b[1;36m${arg}\x1b[0m`;
}
if (hasInspect(arg)) {
return `\x1b[1;35m${inspect(arg)}\x1b[0m`;
}
if (typeof args === "object") {
return JSON.stringify(arg, null, 2);
}
return args;
};
if (!args.length) {
clog.log(undefined);
} else if (args.length > 1) {
clog.log(
args.reduce((acc, curr) => acc.concat(writeLog(curr)), []).join(" ")
);
} else {
clog.log(writeLog(args[0]));
}
},
}))(console);
58 changes: 9 additions & 49 deletions src/runner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require("fs");
const path = require("path");
const { dirname } = require("path");
const chokidar = require("chokidar");
const term = require("terminal-kit").terminal;
Expand All @@ -8,51 +9,7 @@ const { startService } = require("esbuild");
const { store } = require("./store");
// const ora = require("ora");

const powerConsole = `
const isNil =
x => (x === undefined || x === null)
const when =
(pred, f) => x => !!pred(x) ? f(x) : x
const hasInspect =
x => !isNil(x) && typeof x.inspect === 'function'
const inspect =
when(hasInspect, x => x.inspect())
console = ((clog) => {
return {
...clog,
log(...args) {
const writeLog = (arg) => {
if (typeof arg === 'string') {
return '\x1b[1;33m' + arg + '\x1b[0m';
} else if (typeof arg === 'number') {
return '\x1b[1;34m' + arg + '\x1b[0m';
} else if (typeof arg === 'boolean') {
return '\x1b[1;36m' + arg + '\x1b[0m';
} else if (hasInspect(arg)) {
return '\x1b[1;35m' + inspect(arg) + '\x1b[0m';
} else if (typeof args === 'object') {
return JSON.stringify(arg, null, 2)
} else {
return args;
}
}
if(!args.length) {
clog.log(undefined);
}
else if(args.length > 1) {
clog.log(args.reduce((acc, curr) => acc.concat(writeLog(curr)), []).join(' '));
} else {
clog.log(writeLog(args[0]))
}
}
}
})(console)
`;
const powerConsole = fs.readFileSync(path.join(__dirname, "log.js"));

const installMissingDeps = async (err) =>
new Promise((res) => {
Expand Down Expand Up @@ -101,7 +58,11 @@ function createInnerRunner() {
screen({ running: true, path });
subprocess = execa(
"node",
["--experimental-modules", "--input-type=module", "-e", code],
[
// "--experimental-modules", "--input-type=module",
"-e",
code,
],
{
cwd: dirname(path),
}
Expand Down Expand Up @@ -150,7 +111,7 @@ const createRunner = async () => {
service
.transform(data.toString(), {
target: "node12",
format: "esm",
format: "cjs",
loader: "ts",
})
.then((value) => {
Expand All @@ -177,10 +138,9 @@ const createRunner = async () => {
try {
// Call transform() many times without the overhead of starting a service
watcher.on("change", executeFile(screen));
// console.log([a.js, b.js, c.js]);
} finally {
// The child process can be explicitly killed when it's no longer needed
// service.stop();
service.stop();
}
return () => {};
};
Expand Down

0 comments on commit 1fac45c

Please sign in to comment.