Skip to content

Commit c7e23d8

Browse files
committed
Execute npm run format
1 parent a9276e0 commit c7e23d8

16 files changed

+1300
-1207
lines changed

README.md

+65-45
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,37 @@
22
The hashes are propagated upwards, the hash that is returned for a folder is generated over all the hashes of its children.
33
The hashes are generated with the _sha1_ algorithm and returned in _base64_ encoding by default.
44

5-
Each file returns a name and a hash, and each folder returns additionally an array of children (file or folder elements).
5+
Each file returns a name and a hash, and each folder returns additionally an array of children (file or folder elements).
66

7-
## Usage
8-
First, install folder-hash with `npm install --save folder-hash` or `yarn add folder-hash`.
7+
## Usage
8+
9+
First, install folder-hash with `npm install --save folder-hash` or `yarn add folder-hash`.
910

1011
### Simple example
11-
To see differences to the last version of this package, I would create hashes over all *.js* and *.json* files. But ignore everything inside folders starting with a dot, and also from the folders *node_modules*, *test_coverage*. The structure of the options object is documented <a href="#options">below.</a>
12-
This example is also stored in [./examples/readme-example1.js](/examples/readme-example1.js).
1312

13+
To see differences to the last version of this package, I would create hashes over all _.js_ and _.json_ files. But ignore everything inside folders starting with a dot, and also from the folders _node_modules_, _test_coverage_. The structure of the options object is documented <a href="#options">below.</a>
14+
This example is also stored in [./examples/readme-example1.js](/examples/readme-example1.js).
1415

1516
```js
1617
const { hashElement } = require('folder-hash');
1718

1819
const options = {
19-
folders: { exclude: ['.*', 'node_modules', 'test_coverage'] },
20-
files: { include: ['*.js', '*.json'] }
20+
folders: { exclude: ['.*', 'node_modules', 'test_coverage'] },
21+
files: { include: ['*.js', '*.json'] },
2122
};
2223

2324
console.log('Creating a hash over the current folder:');
2425
hashElement('.', options)
25-
.then(hash => {
26-
console.log(hash.toString());
27-
})
28-
.catch(error => {
29-
return console.error('hashing failed:', error);
30-
});
26+
.then(hash => {
27+
console.log(hash.toString());
28+
})
29+
.catch(error => {
30+
return console.error('hashing failed:', error);
31+
});
3132
```
3233

3334
The returned information looks for example like this:
35+
3436
```
3537
Creating a hash over the current folder:
3638
{ name: '.', hash: 'YZOrKDx9LCLd8X39PoFTflXGpRU=,'
@@ -51,25 +53,28 @@ Creating a hash over the current folder:
5153
]}
5254
]}
5355
```
56+
5457
And the structure may be traversed to e.g. create incremental backups.
5558

5659
It is also possible to only match the full path and not the basename. The same configuration could look like this:
57-
_You should be aware that *nix and Windows behave differently, so please use caution._
60+
_You should be aware that \*nix and Windows behave differently, so please use caution._
61+
5862
```js
5963
const options = {
60-
folders: {
61-
exclude: ['.*', '**.*', '**node_modules', '**test_coverage'],
62-
matchBasename: false, matchPath: true
63-
},
64-
files: {
65-
//include: ['**.js', '**.json' ], // Windows
66-
include: ['*.js', '**/*.js', '*.json', '**/*.json'], // *nix
67-
matchBasename: false, matchPath: true
68-
}
64+
folders: {
65+
exclude: ['.*', '**.*', '**node_modules', '**test_coverage'],
66+
matchBasename: false,
67+
matchPath: true,
68+
},
69+
files: {
70+
//include: ['**.js', '**.json' ], // Windows
71+
include: ['*.js', '**/*.js', '*.json', '**/*.json'], // *nix
72+
matchBasename: false,
73+
matchPath: true,
74+
},
6975
};
7076
```
7177

72-
7378
### Parameters for the hashElement function
7479

7580
<table>
@@ -127,7 +132,9 @@ const options = {
127132
</table>
128133

129134
## Options
135+
130136
### Default values
137+
131138
```js
132139
{
133140
algo: 'sha1', // see crypto.getHashes() for options
@@ -236,6 +243,7 @@ const options = {
236243
</table>
237244

238245
#### Rules object properties
246+
239247
<table>
240248
<thead>
241249
<tr>
@@ -329,6 +337,7 @@ const options = {
329337
</table>
330338

331339
### Symlink options
340+
332341
Configure, how symbolic links should be hashed.
333342
To understand how the options can be combined to create a specific behavior, look into [test/symbolic-links.js](https://github.com/marc136/node-folder-hash/blob/master/test/symbolic-links.js).
334343

@@ -396,11 +405,15 @@ To understand how the options can be combined to create a specific behavior, loo
396405
</table>
397406

398407
## Command line usage
408+
399409
After installing it globally via
410+
400411
```
401412
$ npm install -g folder-hash
402413
```
414+
403415
You can use it like this:
416+
404417
```
405418
# local folder
406419
$ folder-hash -c config.json .
@@ -413,15 +426,19 @@ $ folder-hash /user/bin
413426
It also allows to pass an optional JSON configuration file with the `-c` or `--config` flag, which should contain the same configuration as when using the JavaScript API.
414427

415428
You can also use a local version of folder-hash like this:
429+
416430
```
417431
$ npx folder-hash --help
418432
Use folder-hash on cli like this:
419433
folder-hash [--config <json-file>] <file-or-folder>
420434
```
421435

422436
## Examples
437+
423438
### Other examples using promises
424-
See file *./examples/readme-with-promises.js*
439+
440+
See file _./examples/readme-with-promises.js_
441+
425442
```js
426443
const path = require('path');
427444
const { hashElement } = require('folder-hash');
@@ -458,50 +475,51 @@ hashElement(__dirname, options)
458475
```
459476

460477
### Other examples using error-first callbacks
461-
See *./examples/readme-with-callbacks.js*
478+
479+
See _./examples/readme-with-callbacks.js_
462480

463481
```js
464482
const path = require('path');
465483
const { hashElement } = require('folder-hash');
466484

467485
// pass element name and folder path separately
468486
hashElement('test', path.join(__dirname, '..'), (error, hash) => {
469-
if (error) {
470-
return console.error('hashing failed:', error);
471-
} else {
472-
console.log('Result for folder "../test":', hash.toString(), '\n');
473-
}
487+
if (error) {
488+
return console.error('hashing failed:', error);
489+
} else {
490+
console.log('Result for folder "../test":', hash.toString(), '\n');
491+
}
474492
});
475493

476494
// pass element path directly
477495
hashElement(__dirname, (error, hash) => {
478-
if (error) {
479-
return console.error('hashing failed:', error);
480-
} else {
481-
console.log('Result for folder "' + __dirname + '":');
482-
console.log(hash.toString(), '\n');
483-
}
496+
if (error) {
497+
return console.error('hashing failed:', error);
498+
} else {
499+
console.log('Result for folder "' + __dirname + '":');
500+
console.log(hash.toString(), '\n');
501+
}
484502
});
485503

486504
// pass options (example: exclude dotFiles)
487505
const options = { algo: 'md5', files: { exclude: ['.*'], matchBasename: true } };
488506
hashElement(__dirname, options, (error, hash) => {
489-
if (error) {
490-
return console.error('hashing failed:', error);
491-
} else {
492-
console.log('Result for folder "' + __dirname + '":');
493-
console.log(hash.toString());
494-
}
507+
if (error) {
508+
return console.error('hashing failed:', error);
509+
} else {
510+
console.log('Result for folder "' + __dirname + '":');
511+
console.log(hash.toString());
512+
}
495513
});
496514
```
497515

498-
499516
## Behavior
517+
500518
The behavior is documented and verified in the unit tests. Execute `npm test` or `mocha test`, and have a look at the _test_ subfolder.
501519
You can also have a look at the [CircleCI report. ![CircleCI](https://circleci.com/gh/marc136/node-folder-hash/tree/master.svg?style=svg)](https://circleci.com/gh/marc136/node-folder-hash/tree/master)
502520

503-
504521
### Creating hashes over files (with default options)
522+
505523
**The hashes are the same if:**
506524

507525
- A file is checked again
@@ -514,6 +532,7 @@ You can also have a look at the [CircleCI report. ![CircleCI](https://circleci.c
514532
- Two files have the same content but different names
515533

516534
### Creating hashes over folders (with default options)
535+
517536
Content means in this case a folder's children - both the files and the subfolders with their children.
518537

519538
**The hashes are the same if:**
@@ -528,4 +547,5 @@ Content means in this case a folder's children - both the files and the subfolde
528547
- Two folders have the same content but different names
529548

530549
## License
550+
531551
MIT, see LICENSE.txt

cli.js

+50-49
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,75 @@ const fs = require('graceful-fs');
22
const lib = require('./index');
33

44
function program(cliArgs) {
5-
let args;
6-
try {
7-
args = parseArgs(cliArgs);
8-
} catch (ex) {
9-
error(ex);
10-
}
5+
let args;
6+
try {
7+
args = parseArgs(cliArgs);
8+
} catch (ex) {
9+
error(ex);
10+
}
1111

12-
if (args.help) {
13-
printHelp();
14-
process.exit(0);
15-
}
12+
if (args.help) {
13+
printHelp();
14+
process.exit(0);
15+
}
1616

17-
let config;
18-
if (args.config) {
19-
try {
20-
config = JSON.parse(fs.readFileSync(args.config, { encoding: 'utf-8' }));
21-
} catch (err) {
22-
console.error('Could not parse configuration from file ' + args.config);
23-
console.error('Maybe try a JSON config like this instead?\n');
24-
console.error(JSON.stringify(lib.defaults, undefined, 2));
25-
process.exit(1);
26-
}
17+
let config;
18+
if (args.config) {
19+
try {
20+
config = JSON.parse(fs.readFileSync(args.config, { encoding: 'utf-8' }));
21+
} catch (err) {
22+
console.error('Could not parse configuration from file ' + args.config);
23+
console.error('Maybe try a JSON config like this instead?\n');
24+
console.error(JSON.stringify(lib.defaults, undefined, 2));
25+
process.exit(1);
2726
}
27+
}
2828

29-
return lib.hashElement(args.src || process.cwd(), config)
30-
.then(result => console.log(result.toString()))
31-
.catch(error);
29+
return lib
30+
.hashElement(args.src || process.cwd(), config)
31+
.then(result => console.log(result.toString()))
32+
.catch(error);
3233
}
3334

3435
function parseArgs(args) {
35-
let help, config, src
36+
let help, config, src;
3637

37-
for (let index = 0; index < args.length; index++) {
38-
switch (args[index]) {
39-
case '-h':
40-
case '--help':
41-
help = true;
42-
break;
38+
for (let index = 0; index < args.length; index++) {
39+
switch (args[index]) {
40+
case '-h':
41+
case '--help':
42+
help = true;
43+
break;
4344

44-
case '-c':
45-
case '--config':
46-
if (!args[++index]) {
47-
throw new Error(`Need to supply a JSON config file after "${args[index]}"`);
48-
}
49-
config = args[index];
50-
break;
45+
case '-c':
46+
case '--config':
47+
if (!args[++index]) {
48+
throw new Error(`Need to supply a JSON config file after "${args[index]}"`);
49+
}
50+
config = args[index];
51+
break;
5152

52-
default:
53-
if (!src) {
54-
src = args[index];
55-
} else {
56-
console.log(`Ignoring param "${args[index]}"`);
57-
}
58-
break;
53+
default:
54+
if (!src) {
55+
src = args[index];
56+
} else {
57+
console.log(`Ignoring param "${args[index]}"`);
5958
}
59+
break;
6060
}
61+
}
6162

62-
return { help, config, src };
63+
return { help, config, src };
6364
}
6465

6566
function error(err) {
66-
console.error('ERROR:', ex.message || ex.name || ex);
67-
process.exit(1);
67+
console.error('ERROR:', ex.message || ex.name || ex);
68+
process.exit(1);
6869
}
6970

7071
function printHelp() {
71-
console.log('Use folder-hash on cli like this:');
72-
console.log(' folder-hash [--config <json-file>] <file-or-folder>');
72+
console.log('Use folder-hash on cli like this:');
73+
console.log(' folder-hash [--config <json-file>] <file-or-folder>');
7374
}
7475

7576
module.exports = program;

examples/async-example.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
21
const { Volume } = require('memfs'),
3-
prep = volume => require('../index').prep(volume, Promise);
2+
prep = volume => require('../index').prep(volume, Promise);
43

5-
const hashElement = prep(Volume.fromJSON({
4+
const hashElement = prep(
5+
Volume.fromJSON({
66
'abc.txt': 'awesome content',
77
'def/ghi.js': 'awesome content',
8-
}));
8+
}),
9+
);
910

1011
async function example() {
11-
const options = { files: { ignoreRootName: true } };
12+
const options = { files: { ignoreRootName: true } };
1213

13-
const abc = await hashElement('abc.txt', options);
14-
const def = await hashElement('def/ghi.js', options);
14+
const abc = await hashElement('abc.txt', options);
15+
const def = await hashElement('def/ghi.js', options);
1516

16-
console.log(`abc.hash == def.hash ? ${abc.hash === def.hash}`);
17-
console.log(` abc.hash ${abc.hash}\n def.hash ${def.hash}`);
17+
console.log(`abc.hash == def.hash ? ${abc.hash === def.hash}`);
18+
console.log(` abc.hash ${abc.hash}\n def.hash ${def.hash}`);
1819
}
1920

2021
example();

0 commit comments

Comments
 (0)