From 28fb419475674345e3a1a6dbcc4a0d875444866d Mon Sep 17 00:00:00 2001 From: fkereki Date: Wed, 11 Apr 2018 15:29:50 -0300 Subject: [PATCH] Minor changes --- chapter03/package.json | 37 ++++++++---------------------- chapter03/src/process_exec.js | 26 +++++---------------- chapter03/src/process_fork_dir.js | 8 +++---- chapter03/src/process_spawn.js | 5 ++-- chapter03/src/process_spawn_dir.js | 7 +++--- 5 files changed, 24 insertions(+), 59 deletions(-) diff --git a/chapter03/package.json b/chapter03/package.json index 4cbd70a..3e9f6b6 100644 --- a/chapter03/package.json +++ b/chapter03/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "build": "flow-remove-types src/ -d out/", - "buildWithMaps": "flow-remove-types src/ -d out/ --pretty --sourcemaps", + "buildWithMaps": + "flow-remove-types src/ -d out/ --pretty --sourcemaps", "start": "npm run build && node out/doroundmath.js", "start-db": "npm run build && node out/dbaccess.js", "nodemon": "nodemon --watch src --delay 1 --exec npm start", @@ -19,10 +20,7 @@ "author": "Federico Kereki", "license": "ISC", "babel": { - "presets": [ - "env", - "flow" - ] + "presets": ["env", "flow"] }, "eslintConfig": { "parserOptions": { @@ -31,41 +29,24 @@ }, "parser": "babel-eslint", "env": { - "browser": true, "node": true, "es6": true }, - "extends": [ - "eslint:recommended", - "plugin:flowtype/recommended" - ], - "plugins": [ - "babel", - "flowtype" - ], + "extends": ["eslint:recommended", "plugin:flowtype/recommended"], + "plugins": ["babel", "flowtype"], "rules": { "no-console": "off", "no-var": "error", "prefer-const": "error" } }, - "eslintIgnore": [ - "**/out/*.js" - ], + "eslintIgnore": ["**/out/*.js"], "flow-coverage-report": { "concurrentFiles": 1, - "excludeGlob": [ - "node_modules/**" - ], - "includeGlob": [ - "src/**/*.js" - ], + "excludeGlob": ["node_modules/**"], + "includeGlob": ["src/**/*.js"], "threshold": 90, - "type": [ - "text", - "html", - "json" - ] + "type": ["text", "html", "json"] }, "prettier": { "tabWidth": 4, diff --git a/chapter03/src/process_exec.js b/chapter03/src/process_exec.js index 998765b..c14ec78 100644 --- a/chapter03/src/process_exec.js +++ b/chapter03/src/process_exec.js @@ -1,22 +1,15 @@ /* @flow */ +"use strict"; const child_process = require("child_process"); const { promisify } = require("util"); child_process.exec = promisify(child_process.exec); -async function getDirectory(path: ?string) { +async function getDirectoryJs(path: string) { try { const cmd = "ls -ld -1 *.js"; - - let stdout; - if (typeof path === "undefined") { - ({ stdout } = await child_process.exec(cmd)); - } else { - // $FlowFixMe: Flow doesn't understand "promisify()" - ({ stdout } = await child_process.exec(cmd, { cwd: path })); - } - + const stdout = await child_process.exec(cmd, { cwd: path }); console.log("OUT", path || ""); console.log(stdout); } catch (e) { @@ -24,19 +17,12 @@ async function getDirectory(path: ?string) { } } -getDirectory(); +getDirectoryJs("/boot"); /* -OUT --rw-r--r-- 1 fkereki users 3336 Apr 10 13:12 dbaccess.js --rw-r--r-- 1 fkereki users 319 Apr 10 13:12 doroundmath.js --rw-r--r-- 1 fkereki users 890 Apr 10 13:12 flowcomments.js --rw-r--r-- 1 fkereki users 293 Apr 10 13:12 miniserver.js --rw-r--r-- 1 fkereki users 751 Apr 10 13:12 process_exec.js --rw-r--r-- 1 fkereki users 1311 Apr 10 13:12 promisify.js --rw-r--r-- 1 fkereki users 828 Apr 10 13:12 roundmath.js +ERR ls: cannot access '*.js': No such file or directory */ -getDirectory("/home/fkereki/MODERNJS/chapter03/flow-typed/npm"); +getDirectoryJs("/home/fkereki/MODERNJS/chapter03/flow-typed/npm"); /* OUT /home/fkereki/MODERNJS/chapter03/flow-typed/npm -rw-r--r-- 1 fkereki users 4791 Apr 9 12:52 axios_v0.18.x.js diff --git a/chapter03/src/process_fork_dir.js b/chapter03/src/process_fork_dir.js index 7301e56..467c0ee 100644 --- a/chapter03/src/process_fork_dir.js +++ b/chapter03/src/process_fork_dir.js @@ -1,14 +1,12 @@ +"use strict"; + const fs = require("fs"); process.on("message", obj => { - console.log("GOT!", obj); - // Received a path to process fs .readdirSync(obj.path) .sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())) .filter(file => !file.startsWith(".")) - .forEach(file => { - process.send(file); - }); + .forEach(file => process.send(file)); }); diff --git a/chapter03/src/process_spawn.js b/chapter03/src/process_spawn.js index d011e83..2bcc21a 100644 --- a/chapter03/src/process_spawn.js +++ b/chapter03/src/process_spawn.js @@ -1,12 +1,11 @@ /* @flow */ +"use strict"; const path = require("path"); const { spawn } = require("child_process"); const child = spawn("node", [path.resolve("out/process_spawn_dir.js")]); -const filePath = "/home/fkereki"; - -child.stdin.write(filePath); +child.stdin.write("/home/fkereki"); child.stdout.on("data", data => { console.log(String(data)); diff --git a/chapter03/src/process_spawn_dir.js b/chapter03/src/process_spawn_dir.js index 22c66cf..53d705c 100644 --- a/chapter03/src/process_spawn_dir.js +++ b/chapter03/src/process_spawn_dir.js @@ -1,3 +1,6 @@ +/* @flow */ +"use strict"; + const fs = require("fs"); process.stdin.resume(); @@ -8,9 +11,7 @@ process.stdin.on("data", path => { .readdirSync(path) .sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())) .filter(file => !file.startsWith(".")) - .forEach(file => { - process.stdout.write(file + "\n"); - }); + .forEach(file => process.stdout.write(file + "\n")); process.stdout.end(); });