Skip to content
This repository was archived by the owner on Jan 10, 2020. It is now read-only.

Commit b371e0a

Browse files
boneskullmhdawson
authored andcommitted
initial draft of tooling initiatives doc (#70)
PR-URL: #70 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 09db99b commit b371e0a

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

groups/tooling/README.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Tooling Group Initiatives
2+
3+
> This "living document" outlines the initiatives that have come out of User Feedback Sessions for the Tooling group.
4+
5+
<!-- TOC depthFrom:2 -->
6+
7+
- [Add Common Filesystem Operations to Core](#add-common-filesystem-operations-to-core)
8+
- [Summary](#summary)
9+
- [Motivation](#motivation)
10+
- [Example 1: The Bundler](#example-1-the-bundler)
11+
- [Example 2: The Test Case](#example-2-the-test-case)
12+
- [Notes](#notes)
13+
14+
<!-- /TOC -->
15+
16+
## Add Common Filesystem Operations to Core
17+
18+
### Summary
19+
20+
Two (2) filesystem operations should be added to Node.js Core:
21+
22+
1. Recursively create a directory (e.g., `mkdir -p foo/bar`)
23+
2. Recursively remove a directory (e.g., `rm -rf foo`)
24+
25+
The popular userland modules [mkdirp](https://npm.im/mkdirp) and [rimraf](https://npm.im/rimraf) respectively provide this functionality.
26+
27+
### Motivation
28+
29+
Keeping Node.js' core API small is a commendable goal. Additions to Node.js' core API should not be taken lightly! Yet over time, Node.js' users have found some functionality that truly seems "missing". **These two operations are frequently-cited examples of userland modules which should be built-in to Node.js core.**
30+
31+
Use cases are *extremely common*, especially amongst Node.js CLI applications. From bundlers to scaffolding tools to package managers; many need to recursively create and destroy directory structures. It's inconvenient for users, as many new to Node.js expect this functionality to be part of the "standard library". New and experienced Node.js users are incredulous that external modules must be installed to do these common tasks.
32+
33+
### Example 1: The Bundler
34+
35+
It's 2018, and developer P.J. needs to bundle a web application. P.J. has written a bundling tool, `wabpeck`, written in Node.js. `wabpeck` allows the user to configure an "output" directory using the command-line option, `--output <dir>`.
36+
37+
P.J. chooses the output directory, and runs `wabpeck input.js --output /var/www/sites/pj/staging/alpha/static/dist`. `wabpeck` fails:
38+
39+
```plain
40+
fs.js:143
41+
throw err;
42+
^
43+
44+
Error: ENOENT: no such file or directory, mkdir '/var/www/sites/pj/staging/alpha/static/dist'
45+
at Object.fs.mkdirSync (fs.js:872:3)
46+
```
47+
48+
P.J. is confused. P.J. thinks, "Of course it doesn't exist--why do you think I'm trying to create it?!" P.J. investigates further, and by random chance, chooses to list the contents of `/var/www/sites/pj/staging/alpha`:
49+
50+
```plain
51+
total 16
52+
-rw-r--r-- 1 pj wheel 5979 Jun 8 11:01 anarchist_cookbook.txt
53+
```
54+
55+
P.J. (somehow) realizes that `/var/www/sites/pj/staging/alpha/static` doesn't exist, so `fs.mkdirSync` can't create `/var/www/sites/pj/staging/alpha/static/dist`.
56+
57+
Because `fs` contains no functionality to do this, P.J. now has two choices: modify `wabpeck` to use [mkdirp](https://npm.im/mkdirp), or re-invent a wheel.
58+
59+
### Example 2: The Test Case
60+
61+
P.J. has made the change to use [mkdirp](https://npm.im/mkdirp), but wants to ensure it's correct. Like a good developer, P.J. writes an integration test, careful to clean up afterwards:
62+
63+
```js
64+
const {tmpdir} = require('os');
65+
const {join} = require('path');
66+
const {existsSync, rmdirSync} = require('fs');
67+
const assert = require('assert');
68+
69+
describe('--output', function() {
70+
it('should create a directory recursively', function() {
71+
const recursive1dir = join(tmpdir(), 'recursive-1');
72+
const recursive2dir = join(recursive1dir, 'recursive-2');
73+
// minified dummy output bundle
74+
const expected = join(recursive2dir, 'input-fixture.min.js');
75+
const w = wabpeck({
76+
input: join(__dirname, 'input-fixture.js') // dummy input file
77+
output: recursive2dir
78+
});
79+
try {
80+
w.bundle();
81+
assert.ok(existsSync(expected));
82+
} finally {
83+
rmdirSync(recursive1dir);
84+
}
85+
});
86+
});
87+
```
88+
89+
Accidentally practicing TDD, P.J. runs the test and notes it's throwing an exception on the call to `rmdirSync`.
90+
91+
P.J. realizes `rmdirSync` fails for a similar reason; `recursive1dir` isn't empty. Again, P.J. has two choices: modify the tests to use [rimraf](https://npm.im/rimraf), or re-invent another wheel.
92+
93+
### Notes
94+
95+
Assuming methods would be added to `fs`, this would not (as far as the author knows) require breaking changes which would violate the `fs` module's "Stable" stability status.
96+
97+
There is precedent for these out-of-the-box in other languages, such as Python (`os.makedirs()` and `shutil.rmtree()`).

0 commit comments

Comments
 (0)