Skip to content
This repository was archived by the owner on Jul 5, 2019. It is now read-only.

Commit 89e9838

Browse files
committed
Initial commit
0 parents  commit 89e9838

9 files changed

+175
-0
lines changed

.editorconfig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 2

.eslintrc.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es6": true,
5+
"node": true
6+
},
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"sourceType": "module"
10+
},
11+
"rules": {
12+
"indent": [
13+
"error",
14+
2
15+
],
16+
"linebreak-style": [
17+
"error",
18+
"unix"
19+
],
20+
"quotes": [
21+
"error",
22+
"single"
23+
],
24+
"semi": [
25+
"error",
26+
"always"
27+
]
28+
}
29+
}

index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Export all helpers
3+
* @type {Object}
4+
*/
5+
module.exports = {
6+
hashString: require('./lib/hashString'),
7+
isFile: require('./lib/isFile'),
8+
readJsonFile: require('./lib/readJsonFile'),
9+
removeFile: require('./lib/removeFile'),
10+
writeFile: require('./lib/writeFile')
11+
};

lib/hashString.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Convert string to 32bit integer
3+
* @param {String} string String that should be hashed
4+
* @return {String} Converted 32bit integer
5+
*/
6+
module.exports = (string) => {
7+
let hash = 0;
8+
let length = string.length;
9+
let char;
10+
11+
if (length === 0) {
12+
return hash;
13+
}
14+
15+
for (let i = 0; i < length; i++) {
16+
char = string.charCodeAt(i);
17+
hash = ((hash << 5) - hash) + char;
18+
hash |= 0; // Convert to 32bit integer
19+
}
20+
21+
return hash;
22+
};

lib/isFile.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Check if a path is a file
3+
*/
4+
const fs = require('fs');
5+
6+
/**
7+
* Check if a given path is a file
8+
* @param {String} filePath File path to check
9+
* @return {Boolean}
10+
*/
11+
module.exports = (filePath) => {
12+
if (!fs.existsSync(filePath)) {
13+
return false;
14+
}
15+
16+
const stats = fs.lstatSync(filePath);
17+
18+
if (stats.isFile()) {
19+
return true;
20+
}
21+
22+
return false;
23+
};

lib/readJsonFile.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Read JSON files from filesystem, Promise wrapper
3+
*/
4+
const fs = require('fs');
5+
6+
/**
7+
* Read the JSON file from the filesystem
8+
* @param {String} filepath Filepath of the JSON file that should be read
9+
* @return {Promise} Resolves with parsed JSON of the file
10+
*/
11+
module.exports = (filepath) => {
12+
return new Promise((resolve, reject) => {
13+
return fs.readFile(filepath, 'utf-8', (error, data) => {
14+
if (error) {
15+
return reject(error);
16+
}
17+
18+
if (!data) {
19+
reject(new Error('Empty file'));
20+
}
21+
else {
22+
data = JSON.parse(data);
23+
}
24+
25+
return resolve(data);
26+
});
27+
28+
});
29+
};

lib/removeFile.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Delete a file from filesystem, Promise wrapper
3+
*/
4+
const fs = require('fs');
5+
6+
/**
7+
* Delete the file from the filesystem
8+
* @param {String} name Filepath
9+
* @return {Promise} Resolves when file was deleted
10+
*/
11+
module.exports = (name) => {
12+
return new Promise((resolve, reject) => {
13+
return fs.unlink(name, (error) => {
14+
if (error) {
15+
return reject(error);
16+
}
17+
18+
return resolve();
19+
});
20+
21+
});
22+
};

lib/writeFile.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Write files to the filesystem, Promise wrapper
3+
*/
4+
const fs = require('fs');
5+
6+
/**
7+
* Write the file to the filesystem
8+
* @param {String} name Filepath to where the file should be written
9+
* @param {String} data Date that should be written
10+
* @return {Promise} Resolves when file was written
11+
*/
12+
module.exports = (name, data) => {
13+
return new Promise((resolve, reject) => {
14+
return fs.writeFile(name, data, (error) => {
15+
if (error) {
16+
return reject(error);
17+
}
18+
19+
return resolve();
20+
});
21+
22+
});
23+
};

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "hapi-init-node-helper",
3+
"version": "1.0.0",
4+
"description": "Helper library for node applications",
5+
"main": "index.js",
6+
"repository": "[email protected]:use-init/hapi-init-node-helper.git",
7+
"author": "Hans Christian Reinl <[email protected]>",
8+
"license": "MIT"
9+
}

0 commit comments

Comments
 (0)