1 | # Cool things about fs-jetpack in examples
|
2 | **Note:** All examples here are synchronous for simplicity. You can easily make them asynchronous just by adding 'Async' to method names and expecting promise to be returned instead of ready value.
|
3 |
|
4 | ## Every jetpack instance has its internal CWD
|
5 | You can create many jetpack objects with different internal working directories (which are independent of `process.cwd()`) and work on directories in a little more object-oriented manner.
|
6 | ```js
|
7 | var src = jetpack.cwd('path/to/source');
|
8 | var dest = jetpack.cwd('path/to/destination');
|
9 | src.copy('foo.txt', dest.path('bar.txt'));
|
10 | ```
|
11 |
|
12 | ## JSON is a first class citizen
|
13 | You can write JavaScript object directly to disk and it will be transformed to JSON automatically.
|
14 | ```js
|
15 | var obj = { greet: "Hello World!" };
|
16 | jetpack.write('file.json', obj);
|
17 | ```
|
18 | Then you can get your object back just by telling read method that it's a JSON.
|
19 | ```js
|
20 | var obj = jetpack.read('file.json', 'json');
|
21 | ```
|
22 |
|
23 | ## Jetpack throws errors at you as the last resort
|
24 | Everyone who did something with files for sure seen (and probably hates) *"ENOENT, no such file or directory"* error. Jetpack tries to recover from that error if possible.
|
25 | 1. For write/creation operations, if any of parent directories doesn't exist jetpack will just create lacking directories.
|
26 | 2. For read/inspect operations, if file or directory doesn't exist `undefined` is returned instead of throwing.
|
27 |
|
28 | ## Jetpack is great for build scripts
|
29 | ```js
|
30 | var src = jetpack.cwd('path/to/source');
|
31 | var dest = jetpack.dir('path/to/destination', { empty: true });
|
32 |
|
33 | src.copy('.', dest.path(), {
|
34 | matching: ['./vendor/**', '*.html', '*.png', '*.jpg']
|
35 | });
|
36 |
|
37 | var config = src.read('config.json', 'json');
|
38 | config.env = 'production';
|
39 | dest.write('config.json', config);
|
40 | ```
|
41 |
|
42 | ## All methods play nicely with each other
|
43 | Let's say you want to create folder structure:
|
44 | ```
|
45 | .
|
46 | |- greets
|
47 | |- greet.txt
|
48 | |- greet.json
|
49 | |- greets-i18n
|
50 | |- polish.txt
|
51 | ```
|
52 | Peace of cake with jetpack!
|
53 | ```js
|
54 | jetpack
|
55 | .dir('greets')
|
56 | .file('greet.txt', { content: 'Hello world!' })
|
57 | .file('greet.json', { content: { greet: 'Hello world!' } })
|
58 | .cwd('..')
|
59 | .dir('greets-i18n')
|
60 | .file('polish.txt', { content: 'Witaj świecie!' });
|
61 | ```
|
62 |
|
63 | ## Find and delete all `.tmp` files inside `my-dir`
|
64 | ```js
|
65 | jetpack.find('my-dir', {
|
66 | matching: '*.tmp'
|
67 | })
|
68 | .forEach(jetpack.remove);
|
69 | ```
|
70 |
|
71 | ## Check if two files have the same content
|
72 | ```js
|
73 | var file1 = jetpack.inspect('file1', { checksum: 'md5' });
|
74 | var file2 = jetpack.inspect('file2', { checksum: 'md5' });
|
75 | var areTheSame = (file1.md5 === file2.md5);
|
76 | ```
|
77 |
|
78 | ## More secure writes to disk
|
79 | For essential data you might consider "atomic write" feature. To read more about "why" and "how" please see: [Transactionally writing files in Node.js](http://stackoverflow.com/questions/17047994/transactionally-writing-files-in-node-js) Jetpack implements this simple trick and makes it available as an option.
|
80 | ```js
|
81 | jetpack.write('important_config.json', { atomic: true });
|
82 | ```
|