UNPKG

3 kBMarkdownView Raw
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
5You 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
7var src = jetpack.cwd('path/to/source');
8var dest = jetpack.cwd('path/to/destination');
9src.copy('foo.txt', dest.path('bar.txt'));
10```
11
12## JSON is a first class citizen
13You can write JavaScript object directly to disk and it will be transformed to JSON automatically.
14```js
15var obj = { greet: "Hello World!" };
16jetpack.write('file.json', obj);
17```
18Then you can get your object back just by telling read method that it's a JSON.
19```js
20var obj = jetpack.read('file.json', 'json');
21```
22
23## Jetpack throws errors at you as the last resort
24Everyone 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.
251. For write/creation operations, if any of parent directories doesn't exist jetpack will just create lacking directories.
262. 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
30var src = jetpack.cwd('path/to/source');
31var dest = jetpack.dir('path/to/destination', { empty: true });
32
33src.copy('.', dest.path(), {
34 matching: ['./vendor/**', '*.html', '*.png', '*.jpg']
35});
36
37var config = src.read('config.json', 'json');
38config.env = 'production';
39dest.write('config.json', config);
40```
41
42## All methods play nicely with each other
43Let'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```
52Peace of cake with jetpack!
53```js
54jetpack
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
65jetpack.find('my-dir', {
66 matching: '*.tmp'
67})
68.forEach(jetpack.remove);
69```
70
71## Check if two files have the same content
72```js
73var file1 = jetpack.inspect('file1', { checksum: 'md5' });
74var file2 = jetpack.inspect('file2', { checksum: 'md5' });
75var areTheSame = (file1.md5 === file2.md5);
76```
77
78## More secure writes to disk
79For 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
81jetpack.write('important_config.json', { atomic: true });
82```