Skip to content

Commit

Permalink
Fix getting default sources path (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpman authored Nov 20, 2017
1 parent b809f4b commit f6aa271
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/__tests__/opml.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('path');

const opml = require('../opml');

jest.mock('fs');
Expand Down Expand Up @@ -31,6 +33,15 @@ describe('#opml', () => {
expect(fs.readFileSync).toBeCalledWith('/testonly/awesome.opml');
});

it('should use default file path', async () => {
fs.existsSync.mockReturnValue(false);
const opmlpath = undefined;
await opml(opmlpath);
expect(fs.readFileSync).toBeCalledWith(
path.join(__dirname, '..', './sources.opml')
);
});

it('should parse correctly', async () => {
const opmlpath = 'awesome.opml';
const result = await opml(opmlpath);
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const main = async argv => {
process.exit(0);
}

const opmlPath = argv.o ? argv.o : './sources.opml';
const sources = await parseOpml(opmlPath);
const customOpmlPath = argv.o;
const sources = await parseOpml(customOpmlPath);

let source;
let pageSize;
Expand Down
13 changes: 10 additions & 3 deletions src/opml.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ const parseOpml = thenify(require('node-opml-parser'));

module.exports = async opmlPath => {
let filePath;
if (fs.existsSync(opmlPath)) {
filePath = opmlPath;

if (opmlPath) {
if (fs.existsSync(opmlPath)) {
filePath = opmlPath;
} else {
filePath = path.join(process.cwd(), opmlPath);
}
} else {
filePath = path.join(process.cwd(), opmlPath);
// default sources
filePath = path.join(__dirname, './sources.opml');
}

const opmlFile = fs.readFileSync(filePath);
const result = await parseOpml(opmlFile.toString());
return result;
Expand Down

0 comments on commit f6aa271

Please sign in to comment.