Skip to content

Commit fc88c20

Browse files
committed
lint: add prettier config and format all files
1 parent 6e6bcea commit fc88c20

18 files changed

+299
-177
lines changed

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
**/*.js
1+
/bin

prettier.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
trailingComma: 'none',
3+
singleQuote: true
4+
};

src/actions/install.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ const verifyAndPlaceBinary = require('../assets/binary');
77
* Select a resource handling strategy based on given options.
88
*/
99
function getStrategy({ url }) {
10-
1110
if (url.endsWith('.tar.gz')) {
12-
return require('../assets/untar');
11+
return require('../assets/untar');
1312
} else if (url.endsWith('.zip')) {
14-
return require('../assets/unzip');
13+
return require('../assets/unzip');
1514
} else {
16-
return require('../assets/move');
15+
return require('../assets/move');
1716
}
1817
}
1918

@@ -26,7 +25,6 @@ function getStrategy({ url }) {
2625
* See: https://docs.npmjs.com/files/package.json#bin
2726
*/
2827
function install(callback) {
29-
3028
const opts = parsePackageJson();
3129
if (!opts) return callback('Invalid inputs');
3230

@@ -38,16 +36,20 @@ function install(callback) {
3836

3937
req.on('error', () => callback('Error downloading from URL: ' + opts.url));
4038
req.on('response', (res) => {
41-
if (res.statusCode !== 200) return callback('Error downloading binary. HTTP Status Code: ' + res.statusCode);
42-
43-
const strategy = getStrategy(opts);
44-
45-
strategy({
46-
opts,
47-
req,
48-
onSuccess: () => verifyAndPlaceBinary(opts.binName, opts.binPath, callback),
49-
onError: callback
50-
});
39+
if (res.statusCode !== 200)
40+
return callback(
41+
'Error downloading binary. HTTP Status Code: ' + res.statusCode
42+
);
43+
44+
const strategy = getStrategy(opts);
45+
46+
strategy({
47+
opts,
48+
req,
49+
onSuccess: () =>
50+
verifyAndPlaceBinary(opts.binName, opts.binPath, callback),
51+
onError: callback
52+
});
5153
});
5254
}
5355

src/actions/install.spec.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ jest.mock('../../src/assets/unzip');
1717
jest.mock('../../src/assets/binary');
1818

1919
describe('install()', () => {
20-
2120
let callback, requestEvents;
2221

2322
beforeEach(() => {
24-
2523
callback = jest.fn();
2624

2725
requestEvents = new EventEmitter();
2826
});
2927

30-
it('should call callback with error if package.json did not return value' , () => {
28+
it('should call callback with error if package.json did not return value', () => {
3129
common.parsePackageJson.mockReturnValueOnce(undefined);
3230

3331
install(callback);
@@ -43,7 +41,9 @@ describe('install()', () => {
4341

4442
requestEvents.emit('error');
4543

46-
expect(callback).toHaveBeenCalledWith('Error downloading from URL: http://url');
44+
expect(callback).toHaveBeenCalledWith(
45+
'Error downloading from URL: http://url'
46+
);
4747
});
4848

4949
it('should call callback with error on response with status code different than 200', () => {
@@ -54,7 +54,9 @@ describe('install()', () => {
5454

5555
requestEvents.emit('response', { statusCode: 404 });
5656

57-
expect(callback).toHaveBeenCalledWith('Error downloading binary. HTTP Status Code: 404');
57+
expect(callback).toHaveBeenCalledWith(
58+
'Error downloading binary. HTTP Status Code: 404'
59+
);
5860
});
5961

6062
it('should pick move strategy if url is an uncompressed binary', () => {
@@ -92,13 +94,21 @@ describe('install()', () => {
9294

9395
it('should call verifyAndPlaceCallback on success', () => {
9496
request.mockReturnValueOnce(requestEvents);
95-
common.parsePackageJson.mockReturnValueOnce({ url: 'http://url', binName: 'command', binPath: './bin' });
97+
common.parsePackageJson.mockReturnValueOnce({
98+
url: 'http://url',
99+
binName: 'command',
100+
binPath: './bin'
101+
});
96102
move.mockImplementationOnce(({ onSuccess }) => onSuccess());
97103

98104
install(callback);
99105

100106
requestEvents.emit('response', { statusCode: 200 });
101107

102-
expect(verifyAndPlaceCallback).toHaveBeenCalledWith('command', './bin', callback);
108+
expect(verifyAndPlaceCallback).toHaveBeenCalledWith(
109+
'command',
110+
'./bin',
111+
callback
112+
);
103113
});
104114
});

src/actions/uninstall.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ const { unlinkSync } = require('fs');
33
const { parsePackageJson, getInstallationPath } = require('../common');
44

55
function uninstall(callback) {
6-
76
const { binName } = parsePackageJson();
87

98
getInstallationPath((err, installationPath) => {
10-
if (err) {
11-
return callback(err);
12-
}
9+
if (err) {
10+
return callback(err);
11+
}
1312

14-
try {
15-
unlinkSync(join(installationPath, binName));
16-
} catch(ex) {
17-
// Ignore errors when deleting the file.
18-
}
13+
try {
14+
unlinkSync(join(installationPath, binName));
15+
} catch (ex) {
16+
// Ignore errors when deleting the file.
17+
}
1918

20-
return callback(null);
19+
return callback(null);
2120
});
2221
}
2322

src/actions/uninstall.spec.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ jest.mock('fs');
77
jest.mock('../../src/common');
88

99
describe('uninstall()', () => {
10-
1110
let callback;
1211

1312
beforeEach(() => {
14-
1513
callback = jest.fn();
1614

1715
common.parsePackageJson.mockReturnValueOnce({ binName: 'command' });
@@ -28,26 +26,29 @@ describe('uninstall()', () => {
2826
});
2927

3028
it('should call unlinkSync with binary and installation path', () => {
31-
32-
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, './bin'));
29+
common.getInstallationPath.mockImplementationOnce((cb) =>
30+
cb(null, './bin')
31+
);
3332

3433
uninstall(callback);
3534

3635
expect(fs.unlinkSync).toHaveBeenCalledWith(path.join('bin', 'command'));
3736
});
3837

3938
it('should call callback on success', () => {
40-
41-
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, './bin'));
39+
common.getInstallationPath.mockImplementationOnce((cb) =>
40+
cb(null, './bin')
41+
);
4242

4343
uninstall(callback);
4444

4545
expect(callback).toHaveBeenCalledWith(null);
4646
});
4747

4848
it('should call callback regardless of errors on unlink', () => {
49-
50-
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, './bin'));
49+
common.getInstallationPath.mockImplementationOnce((cb) =>
50+
cb(null, './bin')
51+
);
5152

5253
fs.unlinkSync.mockImplementationOnce(() => {
5354
throw new Error();

src/assets/binary.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ const { getInstallationPath } = require('../common');
44

55
function verifyAndPlaceBinary(binName, binPath, callback) {
66
if (!existsSync(join(binPath, binName))) {
7-
return callback(`Downloaded binary does not contain the binary specified in configuration - ${binName}`);
7+
return callback(
8+
`Downloaded binary does not contain the binary specified in configuration - ${binName}`
9+
);
810
}
911

1012
getInstallationPath((err, installationPath) => {
11-
if (err) {
12-
return callback(err);
13-
}
13+
if (err) {
14+
return callback(err);
15+
}
1416

15-
// Move the binary file and make sure it is executable
16-
copyFileSync(join(binPath, binName), join(installationPath, binName));
17-
unlinkSync(join(binPath, binName));
18-
chmodSync(join(installationPath, binName), '755');
17+
// Move the binary file and make sure it is executable
18+
copyFileSync(join(binPath, binName), join(installationPath, binName));
19+
unlinkSync(join(binPath, binName));
20+
chmodSync(join(installationPath, binName), '755');
1921

20-
console.log('Placed binary on', join(installationPath, binName));
22+
console.log('Placed binary on', join(installationPath, binName));
2123

22-
callback(null);
24+
callback(null);
2325
});
2426
}
2527

src/assets/binary.spec.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ describe('verifyAndPlaceBinary()', () => {
1818

1919
verifyAndPlaceBinary('command', './bin', callback);
2020

21-
expect(callback).toHaveBeenCalledWith('Downloaded binary does not contain the binary specified in configuration - command');
21+
expect(callback).toHaveBeenCalledWith(
22+
'Downloaded binary does not contain the binary specified in configuration - command'
23+
);
2224
});
2325

2426
it('should call callback with error if installation path cannot be found', () => {
@@ -34,7 +36,9 @@ describe('verifyAndPlaceBinary()', () => {
3436

3537
it('should call callback with null on success', () => {
3638
fs.existsSync.mockReturnValueOnce(true);
37-
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, path.sep + path.join('usr', 'local', 'bin')));
39+
common.getInstallationPath.mockImplementationOnce((cb) =>
40+
cb(null, path.sep + path.join('usr', 'local', 'bin'))
41+
);
3842

3943
verifyAndPlaceBinary('command', './bin', callback);
4044

@@ -43,10 +47,15 @@ describe('verifyAndPlaceBinary()', () => {
4347

4448
it('should move the binary to installation directory', () => {
4549
fs.existsSync.mockReturnValueOnce(true);
46-
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, path.sep + path.join('usr', 'local', 'bin')));
50+
common.getInstallationPath.mockImplementationOnce((cb) =>
51+
cb(null, path.sep + path.join('usr', 'local', 'bin'))
52+
);
4753

4854
verifyAndPlaceBinary('command', './bin', callback);
4955

50-
expect(fs.copyFileSync).toHaveBeenCalledWith(path.join('bin', 'command'), path.sep + path.join('usr', 'local', 'bin', 'command'));
56+
expect(fs.copyFileSync).toHaveBeenCalledWith(
57+
path.join('bin', 'command'),
58+
path.sep + path.join('usr', 'local', 'bin', 'command')
59+
);
5160
});
5261
});

src/assets/move.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const { createWriteStream } = require('fs');
55
* Move strategy for binary resources without compression.
66
*/
77
function move({ opts, req, onSuccess, onError }) {
8-
98
const stream = createWriteStream(join(opts.binPath, opts.binName));
109

1110
stream.on('error', onError);

src/assets/move.spec.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ const move = require('../../src/assets/move');
66
jest.mock('fs');
77

88
describe('move()', () => {
9-
109
let streamEvents, pipe, onSuccess, onError;
1110

1211
beforeEach(() => {
13-
1412
streamEvents = new EventEmitter();
1513

1614
pipe = jest.fn();
@@ -22,26 +20,40 @@ describe('move()', () => {
2220
});
2321

2422
it('should download resource to given binPath', () => {
25-
26-
move({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
27-
28-
expect(fs.createWriteStream).toHaveBeenCalledWith(path.join("bin", "command"));
23+
move({
24+
opts: { binPath: './bin', binName: 'command' },
25+
req: { pipe },
26+
onSuccess,
27+
onError
28+
});
29+
30+
expect(fs.createWriteStream).toHaveBeenCalledWith(
31+
path.join('bin', 'command')
32+
);
2933
});
3034

3135
it('should call onSuccess on stream closed', () => {
32-
33-
move({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
36+
move({
37+
opts: { binPath: './bin', binName: 'command' },
38+
req: { pipe },
39+
onSuccess,
40+
onError
41+
});
3442

3543
streamEvents.emit('close');
3644

3745
expect(onSuccess).toHaveBeenCalled();
3846
});
3947

4048
it('should call onError with error on write stream error', () => {
41-
4249
const error = new Error();
4350

44-
move({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
51+
move({
52+
opts: { binPath: './bin', binName: 'command' },
53+
req: { pipe },
54+
onSuccess,
55+
onError
56+
});
4557

4658
streamEvents.emit('error', error);
4759

0 commit comments

Comments
 (0)