Skip to content

Commit

Permalink
Merge pull request #18 from blackflux/dev
Browse files Browse the repository at this point in the history
[Gally]: master <- dev
  • Loading branch information
simlu committed Mar 12, 2019
2 parents f8dced3 + 6e0b3e6 commit f01cbc8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ Abstraction Layer for File Management.

## Functions

To ignore file extension and force treat the file as a certain type, you can pass the option `treatAs` as e.g. `json`.
### guessFile(filepath)

Extends and returns filepath with the file extension as appropriate. Returns `null` if no good match was found.

A few notes:
- Only extends extensions, not partial file names
- Will prefer to match the exact file
- Will return `null` when multiple possible extensions are found

### smartRead(filepath, options = { treatAs = null })

Expand All @@ -32,6 +39,8 @@ The following extensions are handled in order:

Note that the [required cache](https://nodejs.org/api/modules.html#modules_require_cache) is not automatically invalidated when loading cached `.js` files.

To ignore file extension and force treat the file as a certain type, you can pass the option `treatAs` as e.g. `json`.

### smartWrite(filepath. content, options = { treatAs = null, mergeStrategy = (existing, changeset) => changeset })

Serialize and write content to file based on file extension.
Expand All @@ -47,6 +56,8 @@ The following extension are handled in order:
- `.yml` and `.yaml`: Serialize uses [yaml-boost](https://github.com/blackflux/yaml-boost).
- `.*`: Expects content as array and serializes by joining array using new line character.

To ignore file extension and force treat the file as a certain type, you can pass the option `treatAs` as e.g. `json`.

The `mergeStrategy` option can be used to customize how the new content is merged if the target file already exists.
By default the file is simply overwritten.

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

module.exports.guessFile = require('./logic/guess-file');

module.exports.smartRead = require('./logic/smart-read');
module.exports.smartWrite = require('./logic/smart-write');
17 changes: 17 additions & 0 deletions src/logic/guess-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs');
const path = require('path');

module.exports = (filepath) => {
const dirname = path.dirname(filepath);
const basename = path.basename(filepath);
const relevantFiles = fs
.readdirSync(dirname)
.filter(f => f === basename || f.startsWith(`${basename}.`));
if (relevantFiles.includes(basename)) {
return filepath;
}
if (relevantFiles.length === 1) {
return path.join(dirname, relevantFiles[0]);
}
return null;
};
2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ const sfs = require('../src/index');

describe('Testing Integration', () => {
it('Testing Exported Functions', () => {
expect(Object.keys(sfs)).to.deep.equal(['smartRead', 'smartWrite']);
expect(Object.keys(sfs)).to.deep.equal(['guessFile', 'smartRead', 'smartWrite']);
});
});
37 changes: 37 additions & 0 deletions test/logic/guess-file.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require('fs');
const path = require('path');
const expect = require('chai').expect;
const tmp = require('tmp');
const guessFile = require('../../src/logic/guess-file');

describe('Testing guessFile', () => {
let dir;
beforeEach(() => {
dir = tmp.dirSync({ keep: false, unsafeCleanup: true }).name;
});

const executeTest = (files, input, expected) => {
files.forEach(f => fs.writeFileSync(path.join(dir, f), ''));
const filepath = path.join(dir, input);
expect(guessFile(filepath)).to.equal(typeof expected === 'string' ? path.join(dir, expected) : expected);
};

it('Testing exact multi-match', () => {
executeTest(['file', 'file.json'], 'file', 'file');
});

it('Testing partial match', () => {
executeTest(['file.json'], 'file', 'file.json');
});

it('Testing partial non-match', () => {
const files = ['file.json'];
executeTest(files, 'fil', null);
executeTest(files, 'file.', null);
executeTest(files, 'file.j', null);
});

it('Testing partial multi non-match', () => {
executeTest(['file.json', 'file.yml'], 'file', null);
});
});

0 comments on commit f01cbc8

Please sign in to comment.