Skip to content

Commit 5de73a1

Browse files
Initial commit
0 parents  commit 5de73a1

11 files changed

+310
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
package-lock.json
3+
yarn.lock
4+
.nyc_output/
5+
documents/

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
3+
node_js:
4+
- "8"
5+
- "10"
6+
- "12"
7+
- "13"
8+
9+
script:
10+
- npm run lint
11+
- npm run test:cov
12+
13+
after_script:
14+
- npm install coveralls
15+
- nyc report --reporter=text-lcov | coveralls

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 dailyrandomphoto
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# DEV-BLOG-DIRECTORY: save-json-cli
2+
3+
[![NPM Version][npm-version-image]][npm-url]
4+
[![LICENSE][license-image]][license-url]
5+
[![Build Status][travis-image]][travis-url]
6+
[![Coverage Status][coveralls-image]][coveralls-url]
7+
[![dependencies Status][dependencies-image]][dependencies-url]
8+
[![devDependencies Status][devDependencies-image]][devDependencies-url]
9+
10+
A CLI for saves the JSON format blog list to `documents/*.yml`.
11+
12+
## Installation
13+
14+
```sh
15+
npm install dev-blog-directory-save-json-cli
16+
```
17+
18+
## Usages
19+
20+
```sh
21+
$ npx save-blog-json [--merge] JSONFILE
22+
```
23+
24+
**Sample of JSON file:**
25+
26+
- single blog
27+
28+
```json
29+
{
30+
"name": "Daily Random Photo",
31+
"url": "https://www.dailyrandomphoto.com/",
32+
"author": "dailyrandomphoto"
33+
}
34+
```
35+
36+
- multi blogs
37+
38+
```json
39+
[
40+
{
41+
"name": "Daily Random Photo",
42+
"url": "https://www.dailyrandomphoto.com/",
43+
"author": "dailyrandomphoto"
44+
},
45+
{
46+
"name": "Daily Random Photo",
47+
"url": "https://dailyrandomphoto.web.app/",
48+
"author": "dailyrandomphoto"
49+
}
50+
]
51+
```
52+
53+
## Related
54+
55+
- [dev-blog-directory](https://github.com/dailyrandomphoto/dev-blog-directory) - A Developer Blog Directory.
56+
- [dev-blog-directory-raw](https://github.com/dailyrandomphoto/dev-blog-directory-raw) - Raw data storage of [Developer Blog Directory](https://github.com/dailyrandomphoto/dev-blog-directory).
57+
- [dev-blog-directory-save](https://github.com/dailyrandomphoto/dev-blog-directory-save) - API for save new blogs to [dev-blog-directory-raw](https://github.com/dailyrandomphoto/dev-blog-directory-raw).
58+
- [dev-blog-directory-save-yaml-cli](https://github.com/dailyrandomphoto/dev-blog-directory-save-yaml-cli) - A CLI for saves the YAML format blog list to `documents/*.yml`.
59+
60+
## License
61+
Copyright (c) 2019 [dailyrandomphoto][my-url]. Licensed under the [MIT license][license-url].
62+
63+
[my-url]: https://github.com/dailyrandomphoto
64+
[npm-url]: https://www.npmjs.com/package/dev-blog-directory-save-json-cli
65+
[travis-url]: https://travis-ci.org/dailyrandomphoto/dev-blog-directory-save-json-cli
66+
[coveralls-url]: https://coveralls.io/github/dailyrandomphoto/dev-blog-directory-save-json-cli?branch=master
67+
[license-url]: LICENSE
68+
[dependencies-url]: https://david-dm.org/dailyrandomphoto/dev-blog-directory-save-json-cli
69+
[devDependencies-url]: https://david-dm.org/dailyrandomphoto/dev-blog-directory-save-json-cli?type=dev
70+
71+
[npm-downloads-image]: https://img.shields.io/npm/dm/dev-blog-directory-save-json-cli
72+
[npm-version-image]: https://img.shields.io/npm/v/dev-blog-directory-save-json-cli
73+
[license-image]: https://img.shields.io/npm/l/dev-blog-directory-save-json-cli
74+
[travis-image]: https://img.shields.io/travis/dailyrandomphoto/dev-blog-directory-save-json-cli
75+
[coveralls-image]: https://img.shields.io/coveralls/github/dailyrandomphoto/dev-blog-directory-save-json-cli
76+
[dependencies-image]: https://img.shields.io/david/dailyrandomphoto/dev-blog-directory-save-json-cli
77+
[devDependencies-image]: https://img.shields.io/david/dev/dailyrandomphoto/dev-blog-directory-save-json-cli

index.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const {resolve} = require('path');
6+
const {readJson} = require('fs-extra');
7+
const {save, saveAll} = require('dev-blog-directory-save');
8+
9+
function main(filepath, options) {
10+
return readJson(filepath)
11+
.then(docs => {
12+
if (!docs) {
13+
return;
14+
}
15+
16+
if (Array.isArray(docs)) {
17+
docs = docs.filter(doc => doc && typeof doc === 'object');
18+
return saveAll(docs, options);
19+
}
20+
21+
return save(docs, options);
22+
});
23+
}
24+
25+
function exit(msg) {
26+
if (msg) {
27+
console.error('\n' + msg);
28+
29+
process.exit(1);
30+
}
31+
32+
process.exit(0);
33+
}
34+
35+
if (require.main === module) { // Called directly
36+
const argv = require('minimist')(process.argv.slice(2), {boolean: ['merge']});
37+
const filename = argv._[0];
38+
const option = {merge: Boolean(argv.merge)};
39+
if (!filename) {
40+
exit('Usage: save-blog-json [--merge] JSONFILE');
41+
}
42+
43+
const filepath = resolve(process.cwd(), filename);
44+
45+
console.log('json file name: ' + filename);
46+
console.log('json full path: ' + filepath);
47+
main(filepath, option)
48+
.then(() => {
49+
console.log('Done!');
50+
})
51+
.catch(exit);
52+
} else { // Required as a module
53+
module.exports = main;
54+
}

mocha.opts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--colors
2+
--reporter spec
3+
--ui bdd
4+
--full-trace
5+
--exit
6+
--require chai/register-should

package.json

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"name": "dev-blog-directory-save-json-cli",
3+
"version": "0.3.0",
4+
"description": "A CLI for saves the JSON format blog list to documents/*.yml.",
5+
"main": "index.js",
6+
"bin": {
7+
"save-blog-json": "index.js"
8+
},
9+
"scripts": {
10+
"demo": "save-blog-json ./test.json && save-blog-json ./test-arr.json",
11+
"lint": "xo",
12+
"lint:fix": "xo --fix",
13+
"test": "mocha test.js",
14+
"test:cov": "nyc npm run test",
15+
"test:loop": "while npm test --silent; do :; done"
16+
},
17+
"dependencies": {
18+
"dev-blog-directory-save": "~0.4.0",
19+
"fs-extra": "^8.1.0",
20+
"minimist": "^1.2.0"
21+
},
22+
"devDependencies": {
23+
"chai": "^4.2.0",
24+
"chai-as-promised": "^7.1.1",
25+
"dev-blog-directory-save-json-cli": "file:.",
26+
"mocha": "^6.2.1",
27+
"nyc": "^14.1.1",
28+
"xo": "^0.25.3"
29+
},
30+
"keywords": [
31+
"save",
32+
"json",
33+
"yaml",
34+
"blog",
35+
"cli"
36+
],
37+
"files": [
38+
"index.js",
39+
"LICENSE",
40+
"README.md"
41+
],
42+
"engines": {
43+
"node": ">=8.6.0"
44+
},
45+
"author": "dailyrandomphoto <[email protected]> (https://www.dailyrandomphoto.com/)",
46+
"license": "MIT",
47+
"repository": {
48+
"type": "git",
49+
"url": "git+https://github.com/dailyrandomphoto/dev-blog-directory-save-json-cli.git"
50+
},
51+
"bugs": {
52+
"url": "https://github.com/dailyrandomphoto/dev-blog-directory-save-json-cli/issues"
53+
},
54+
"homepage": "https://github.com/dailyrandomphoto/dev-blog-directory-save-json-cli#readme",
55+
"xo": {
56+
"space": 2,
57+
"rules": {
58+
"promise/prefer-await-to-then": 0
59+
},
60+
"overrides": [
61+
{
62+
"files": "test.js",
63+
"envs": [
64+
"node",
65+
"mocha"
66+
]
67+
}
68+
]
69+
}
70+
}

test-arr.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"name": "Daily Random Photo",
4+
"url": "https://dailyrandomphoto.now.sh/",
5+
"author": "dailyrandomphoto"
6+
},
7+
{
8+
"name": "Daily Random Photo",
9+
"url": "https://dailyrandomphoto.web.app/",
10+
"author": "dailyrandomphoto"
11+
}
12+
]

test.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const fs = require('fs-extra');
4+
const chai = require('chai');
5+
const chaiAsPromised = require('chai-as-promised');
6+
chai.use(chaiAsPromised);
7+
const {expect} = chai;
8+
const main = require('dev-blog-directory-save-json-cli');
9+
10+
describe('dev-blog-directory-save-json-cli', () => {
11+
before(() => {
12+
return fs.remove('./documents')
13+
.then(() => {
14+
console.log('remove ./documents success!');
15+
})
16+
.catch(error => {
17+
console.error(error);
18+
});
19+
});
20+
21+
it('module should to be a function', () => {
22+
expect(main).to.be.a('function');
23+
});
24+
25+
it('should throw an error', () => {
26+
return expect(main()).to.be.rejected;
27+
});
28+
29+
it('should throw a SyntaxError', () => {
30+
return expect(main('test.js')).to.be.rejectedWith(SyntaxError);
31+
});
32+
33+
it('should not throw an error', () => {
34+
return expect(main('test.json')).to.be.fulfilled;
35+
});
36+
37+
it('duplicated url', () => {
38+
return expect(main('test.json')).to.be.rejectedWith('Duplicated url');
39+
});
40+
41+
it('with merge option', () => {
42+
return expect(main('test.json', {merge: true})).to.be.fulfilled;
43+
});
44+
});

test.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Daily Random Photo",
3+
"url": "https://www.dailyrandomphoto.com/",
4+
"author": "dailyrandomphoto"
5+
}

0 commit comments

Comments
 (0)