Skip to content

Commit c45373a

Browse files
committed
refactor: use left-phalange-api
1 parent 6b559db commit c45373a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+202
-386
lines changed

package.json

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,18 @@
2020
],
2121
"license": "MIT",
2222
"files": [
23-
"bin",
24-
"lib"
23+
"bin"
2524
],
2625
"bin": {
2726
"left-phalange": "bin/cli",
2827
"lp": "bin/cli"
2928
},
3029
"dependencies": {
31-
"@iarna/toml": "^2.2.3",
3230
"ansi-colors": "^3.2.4",
3331
"clipboardy": "^2.0.0",
34-
"esm": "^3.2.25",
3532
"get-stdin": "^7.0.0",
36-
"import-fresh": "^3.0.0",
37-
"ini": "^1.3.5",
38-
"js-yaml": "^3.13.1",
39-
"json5": "^2.1.0",
33+
"left-phalange-api": "^1.1.1",
4034
"meow": "^5.0.0",
41-
"parse-json": "^4.0.0",
4235
"update-notifier": "^3.0.0"
4336
},
4437
"config": {
@@ -74,12 +67,14 @@
7467
"ava": "^1.4.1",
7568
"cz-conventional-changelog-emoji": "^0.1.0",
7669
"eslint": "^5.16.0",
70+
"esm": "^3.2.25",
71+
"execa": "^1.0.0",
7772
"husky": "^2.3.0",
7873
"lint-staged": "^8.1.7",
7974
"markdownlint-cli": "^0.16.0",
8075
"npm-run-all": "^4.1.5",
8176
"prettier": "^1.17.1",
82-
"rollup": "^1.12.3",
77+
"rollup": "^1.12.4",
8378
"rollup-plugin-commonjs": "^10.0.0",
8479
"rollup-plugin-filesize": "^6.1.0",
8580
"rollup-plugin-json": "^4.0.0",

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ lp --help
5959
- toml
6060
- yaml
6161

62+
## Related
63+
64+
- [left-phalange-api](https://github.com/fisker/left-phalange-api) - API for this module
65+
6266
## License
6367

6468
MIT © [fisker Cheung](https://github.com/fisker)

rollup.config.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,13 @@ const plugins = [cjs(), json(), filesize()]
77

88
const external = ['path', 'fs', ...Object.keys(dependencies)]
99

10-
export default [
11-
{
12-
input: 'src/index.js',
13-
output: {
14-
file: 'lib/index.js',
15-
format: 'cjs',
16-
},
17-
external,
18-
plugins,
10+
export default {
11+
input: 'src/index.js',
12+
output: {
13+
file: 'bin/cli',
14+
format: 'cjs',
15+
banner: '#!/usr/bin/env node',
1916
},
20-
{
21-
input: 'src/cli.js',
22-
output: {
23-
file: 'bin/cli',
24-
format: 'cjs',
25-
banner: '#!/usr/bin/env node',
26-
},
27-
external,
28-
plugins,
29-
},
30-
]
17+
external,
18+
plugins,
19+
}

src/cli.js

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import meow from 'meow'
2-
import updateNotifier from 'update-notifier'
32
import colors from 'ansi-colors'
4-
import {join, isAbsolute} from 'path'
5-
import getStdin from 'get-stdin'
6-
import {writeSync as copyToClipboard} from 'clipboardy'
7-
import transform from '.'
8-
9-
updateNotifier({pkg: require('../package.json')}).notify()
103

114
const cli = meow(
125
`
@@ -25,7 +18,7 @@ const cli = meow(
2518
Examples
2619
$ lp data.toml > data.json
2720
$ cat data.yaml | lp -p
28-
`,
21+
`,
2922
{
3023
flags: {
3124
pretty: {
@@ -54,48 +47,4 @@ const cli = meow(
5447
}
5548
)
5649

57-
function processor(
58-
file,
59-
{
60-
pretty = false,
61-
input: inputType,
62-
output: outputType = 'json',
63-
copy = false,
64-
} = {}
65-
) {
66-
const input = {
67-
type: inputType,
68-
}
69-
70-
const output = {
71-
type: outputType,
72-
pretty,
73-
}
74-
75-
if (file) {
76-
file = isAbsolute(file) ? file : join(process.cwd(), file)
77-
input.file = file
78-
showResult(input, output, copy)
79-
} else {
80-
getStdin().then(content => {
81-
if (content) {
82-
input.content = content
83-
showResult(input, output, copy)
84-
} else {
85-
cli.showHelp()
86-
}
87-
})
88-
}
89-
}
90-
91-
function showResult(input, output, copy) {
92-
const result = transform(input, output)
93-
94-
if (copy) {
95-
copyToClipboard(result)
96-
} else {
97-
console.log(result)
98-
}
99-
}
100-
101-
processor(cli.input[0], cli.flags)
50+
export default cli

src/index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1+
import getStdin from 'get-stdin'
2+
import cli from './cli'
13
import load from './load'
2-
import parse from './parse'
34
import stringify from './stringify'
5+
import parse from './parse'
6+
import showResult from './show-result'
7+
import update from './update'
48

5-
function transform(input, output = {}) {
6-
const {file, content, type} = input
9+
update()
710

8-
const data = file ? load(file, type) : parse(content, type)
11+
const file = cli.input[0]
12+
const {flags} = cli
913

10-
return stringify(data, output)
14+
if (file) {
15+
const data = load(file, flags)
16+
const result = stringify(data, flags)
17+
showResult(result, flags)
18+
} else {
19+
getStdin().then(content => {
20+
if (content) {
21+
const data = parse(content, flags)
22+
const result = stringify(data, flags)
23+
showResult(result, flags)
24+
} else {
25+
cli.showHelp()
26+
}
27+
})
1128
}
12-
13-
export default transform

src/load.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import * as loaders from './loader'
2-
import {getFileType} from './utils'
1+
import {join, isAbsolute} from 'path'
2+
import {load} from 'left-phalange-api'
33

4-
function loadFile(file, type) {
5-
type = type || getFileType(file)
6-
return loaders[type](file)
4+
function loadFileData(file, {input} = {}) {
5+
file = isAbsolute(file) ? file : join(process.cwd(), file)
6+
const data = load(file, {
7+
type: input,
8+
})
9+
return data
710
}
811

9-
export default loadFile
12+
export default loadFileData

src/loader/cjs.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/loader/esm-require.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/loader/index.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/parse.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import * as parsers from './parser'
1+
import {parse} from 'left-phalange-api'
22

3-
function parse(content, type = 'yaml') {
4-
return parsers[type](content)
3+
function parseData(content, {input} = {}) {
4+
return parse(content, {
5+
type: input,
6+
})
57
}
68

7-
export default parse
9+
export default parseData

src/parser/index.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/parser/ini.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/parser/json.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/parser/json5.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/parser/toml.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/parser/yaml.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/printer/cjs.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/printer/esm.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/printer/index.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/printer/ini.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/printer/json.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/printer/json5.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/printer/toml.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/printer/yaml.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/show-result.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {writeSync as copyToClipboard} from 'clipboardy'
2+
3+
function showResult(result, {copy = false} = {}) {
4+
if (copy) {
5+
copyToClipboard(result)
6+
} else {
7+
console.log(result)
8+
}
9+
}
10+
11+
export default showResult

src/stringify.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import * as printers from './printer'
1+
import {stringify} from 'left-phalange-api'
22

3-
function stringify(data, {type = 'json', ...options}) {
4-
return printers[type](data, options)
3+
function stringifyData(data, {output, pretty} = {}) {
4+
return stringify(data, {
5+
type: output,
6+
pretty,
7+
})
58
}
69

7-
export default stringify
10+
export default stringifyData

src/update.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import updateNotifier from 'update-notifier'
2+
3+
function update() {
4+
updateNotifier({pkg: require('../package.json')}).notify()
5+
}
6+
7+
export default update

0 commit comments

Comments
 (0)