@oneblink/release-cli 1.2.0-beta.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_modules/changelog-parser/.DS_Store +0 -0
- package/node_modules/changelog-parser/.travis.yml +7 -0
- package/node_modules/changelog-parser/CHANGELOG.md +128 -0
- package/node_modules/changelog-parser/CONTRIBUTING.md +55 -0
- package/node_modules/changelog-parser/LICENSE.md +15 -0
- package/node_modules/changelog-parser/README.md +247 -0
- package/node_modules/changelog-parser/bin/cli.js +19 -0
- package/node_modules/changelog-parser/index.js +201 -0
- package/node_modules/changelog-parser/package.json +54 -0
- package/node_modules/changelog-parser/test/fixtures/CHANGELOG.md +44 -0
- package/node_modules/changelog-parser/test/fixtures/expected.js +113 -0
- package/node_modules/changelog-parser/test/fixtures/remove-markdown-expected.js +113 -0
- package/node_modules/changelog-parser/test/index.js +105 -0
- package/node_modules/line-reader/LICENSE +20 -0
- package/node_modules/line-reader/README.md +75 -0
- package/node_modules/line-reader/lib/line_reader.js +174 -0
- package/node_modules/line-reader/package.json +18 -0
- package/node_modules/line-reader/test/data/empty_file.txt +0 -0
- package/node_modules/line-reader/test/data/multi_separator_file.txt +2 -0
- package/node_modules/line-reader/test/data/multibyte_file.txt +2 -0
- package/node_modules/line-reader/test/data/normal_file.txt +6 -0
- package/node_modules/line-reader/test/data/one_line_file.txt +1 -0
- package/node_modules/line-reader/test/data/separator_file.txt +2 -0
- package/node_modules/line-reader/test/data/three_line_file.txt +3 -0
- package/node_modules/line-reader/test/line_reader.js +222 -0
- package/node_modules/remove-markdown/.npmignore +1 -0
- package/node_modules/remove-markdown/LICENSE +21 -0
- package/node_modules/remove-markdown/README.md +44 -0
- package/node_modules/remove-markdown/index.js +60 -0
- package/node_modules/remove-markdown/package.json +31 -0
- package/node_modules/remove-markdown/test/remove-markdown.js +139 -0
- package/package.json +4 -1
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "changelog-parser",
|
|
3
|
+
"description": "Change log parser for node.",
|
|
4
|
+
"version": "2.8.0",
|
|
5
|
+
"author": "Nate Goldman <ungoldman@gmail.com>",
|
|
6
|
+
"bin": {
|
|
7
|
+
"changelog-parser": "./bin/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/hypermodules/changelog-parser/issues"
|
|
11
|
+
},
|
|
12
|
+
"contributors": [
|
|
13
|
+
"Nate Goldman <ungoldman@gmail.com>",
|
|
14
|
+
"Rachel Nehmer <kneemer@gmail.com>",
|
|
15
|
+
"Viktor Havrylin <fer0x.feo@gmail.com>",
|
|
16
|
+
"J R Mykolyn <jrmykolyn@gmail.com>",
|
|
17
|
+
"Bogdan Plieshka <bogdanplieshka@gmail.com>",
|
|
18
|
+
"Ciro Nunes <ciroanunes@gmail.com>",
|
|
19
|
+
"Andy Edwards <jedwards@fastmail.com>"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"line-reader": "^0.2.4",
|
|
23
|
+
"remove-markdown": "^0.2.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"gh-release": "^3.1.1",
|
|
27
|
+
"standard": "^10.0.2",
|
|
28
|
+
"tap-spec": "^4.1.1",
|
|
29
|
+
"tape": "^4.6.0"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/hypermodules/changelog-parser",
|
|
32
|
+
"keywords": [
|
|
33
|
+
"CHANGELOG.md",
|
|
34
|
+
"changelog",
|
|
35
|
+
"parser",
|
|
36
|
+
"semantic",
|
|
37
|
+
"semver",
|
|
38
|
+
"versioning"
|
|
39
|
+
],
|
|
40
|
+
"license": "ISC",
|
|
41
|
+
"main": "index.js",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "https://github.com/hypermodules/changelog-parser.git"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"release": "gh-release && npm publish",
|
|
48
|
+
"test": "standard && node test | tap-spec"
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
,"_resolved": "https://registry.npmjs.org/changelog-parser/-/changelog-parser-2.8.0.tgz"
|
|
52
|
+
,"_integrity": "sha512-ZtSwN0hY7t+WpvaXqqXz98RHCNhWX9HsvCRAv1aBLlqJ7BpKtqdM6Nu6JOiUhRAWR7Gov0aN0fUnmflTz0WgZg=="
|
|
53
|
+
,"_from": "changelog-parser@2.8.0"
|
|
54
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# changelog title
|
|
2
|
+
|
|
3
|
+
A cool description (optional).
|
|
4
|
+
|
|
5
|
+
## unreleased
|
|
6
|
+
* foo
|
|
7
|
+
|
|
8
|
+
## x.y.z - YYYY-MM-DD
|
|
9
|
+
* bar
|
|
10
|
+
|
|
11
|
+
## [a.b.c]
|
|
12
|
+
|
|
13
|
+
### Changes
|
|
14
|
+
|
|
15
|
+
* Update API
|
|
16
|
+
* Fix bug #1
|
|
17
|
+
|
|
18
|
+
# 2.3.0 - 2018-12-18
|
|
19
|
+
|
|
20
|
+
### Added
|
|
21
|
+
|
|
22
|
+
- Some changelog generators such as [standard-version](https://github.com/conventional-changelog/standard-version) would produce H1s for major versions and H2s for minor versions. We want the parser to be able to parse both.
|
|
23
|
+
|
|
24
|
+
## 2.2.3-pre.1 - 2013-02-14
|
|
25
|
+
|
|
26
|
+
### Added
|
|
27
|
+
- Added an item.
|
|
28
|
+
* Added another item.
|
|
29
|
+
|
|
30
|
+
* Update API
|
|
31
|
+
|
|
32
|
+
## 2.0.0-x.7.z.92 - 2013-02-14
|
|
33
|
+
* bark bark
|
|
34
|
+
* woof
|
|
35
|
+
* arf
|
|
36
|
+
|
|
37
|
+
## v1.3.0
|
|
38
|
+
|
|
39
|
+
* make it so
|
|
40
|
+
|
|
41
|
+
## [1.2.3](link)
|
|
42
|
+
* init
|
|
43
|
+
|
|
44
|
+
[a.b.c]: http://altavista.com
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
var EOL = require('os').EOL
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
title: 'changelog title',
|
|
5
|
+
description: 'A cool description (optional).',
|
|
6
|
+
versions: [
|
|
7
|
+
{
|
|
8
|
+
version: null,
|
|
9
|
+
title: 'unreleased',
|
|
10
|
+
'date': null,
|
|
11
|
+
body: '* foo',
|
|
12
|
+
parsed: {
|
|
13
|
+
_: [
|
|
14
|
+
'foo'
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
version: 'x.y.z',
|
|
20
|
+
title: 'x.y.z - YYYY-MM-DD',
|
|
21
|
+
'date': null,
|
|
22
|
+
body: '* bar',
|
|
23
|
+
parsed: {
|
|
24
|
+
_: [
|
|
25
|
+
'bar'
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
version: 'a.b.c',
|
|
31
|
+
title: '[a.b.c]',
|
|
32
|
+
'date': null,
|
|
33
|
+
body: '### Changes' + EOL + EOL + '* Update API' + EOL + '* Fix bug #1',
|
|
34
|
+
parsed: {
|
|
35
|
+
_: [
|
|
36
|
+
'Update API',
|
|
37
|
+
'Fix bug #1'
|
|
38
|
+
],
|
|
39
|
+
Changes: [
|
|
40
|
+
'Update API',
|
|
41
|
+
'Fix bug #1'
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
version: '2.3.0',
|
|
47
|
+
title: '2.3.0 - 2018-12-18',
|
|
48
|
+
'date': '2018-12-18',
|
|
49
|
+
body: '### Added' + EOL + EOL + '- Some changelog generators such as [standard-version](https://github.com/conventional-changelog/standard-version) would produce H1s for major versions and H2s for minor versions. We want the parser to be able to parse both.',
|
|
50
|
+
parsed: {
|
|
51
|
+
_: [
|
|
52
|
+
'Some changelog generators such as standard-version would produce H1s for major versions and H2s for minor versions. We want the parser to be able to parse both.'
|
|
53
|
+
],
|
|
54
|
+
Added: [
|
|
55
|
+
'Some changelog generators such as standard-version would produce H1s for major versions and H2s for minor versions. We want the parser to be able to parse both.'
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
version: '2.2.3-pre.1',
|
|
61
|
+
title: '2.2.3-pre.1 - 2013-02-14',
|
|
62
|
+
'date': '2013-02-14',
|
|
63
|
+
body: '### Added' + EOL + '- Added an item.' + EOL + '* Added another item.' + EOL + EOL + '* Update API',
|
|
64
|
+
parsed: {
|
|
65
|
+
_: [
|
|
66
|
+
'Added an item.',
|
|
67
|
+
'Added another item.',
|
|
68
|
+
'Update API'
|
|
69
|
+
],
|
|
70
|
+
Added: [
|
|
71
|
+
'Added an item.',
|
|
72
|
+
'Added another item.',
|
|
73
|
+
'Update API'
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
version: '2.0.0-x.7.z.92',
|
|
79
|
+
title: '2.0.0-x.7.z.92 - 2013-02-14',
|
|
80
|
+
'date': '2013-02-14',
|
|
81
|
+
body: '* bark bark' + EOL + '* woof' + EOL + '* arf',
|
|
82
|
+
parsed: {
|
|
83
|
+
_: [
|
|
84
|
+
'bark bark',
|
|
85
|
+
'woof',
|
|
86
|
+
'arf'
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
version: '1.3.0',
|
|
92
|
+
title: 'v1.3.0',
|
|
93
|
+
'date': null,
|
|
94
|
+
body: '* make it so',
|
|
95
|
+
parsed: {
|
|
96
|
+
_: [
|
|
97
|
+
'make it so'
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
version: '1.2.3',
|
|
103
|
+
title: '[1.2.3](link)',
|
|
104
|
+
'date': null,
|
|
105
|
+
body: '* init',
|
|
106
|
+
parsed: {
|
|
107
|
+
_: [
|
|
108
|
+
'init'
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
]
|
|
113
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
var EOL = require('os').EOL
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
title: 'changelog title',
|
|
5
|
+
description: 'A cool description (optional).',
|
|
6
|
+
versions: [
|
|
7
|
+
{
|
|
8
|
+
version: null,
|
|
9
|
+
title: 'unreleased',
|
|
10
|
+
'date': null,
|
|
11
|
+
body: '* foo',
|
|
12
|
+
parsed: {
|
|
13
|
+
_: [
|
|
14
|
+
'* foo'
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
version: 'x.y.z',
|
|
20
|
+
title: 'x.y.z - YYYY-MM-DD',
|
|
21
|
+
'date': null,
|
|
22
|
+
body: '* bar',
|
|
23
|
+
parsed: {
|
|
24
|
+
_: [
|
|
25
|
+
'* bar'
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
version: 'a.b.c',
|
|
31
|
+
title: '[a.b.c]',
|
|
32
|
+
'date': null,
|
|
33
|
+
body: '### Changes' + EOL + EOL + '* Update API' + EOL + '* Fix bug #1',
|
|
34
|
+
parsed: {
|
|
35
|
+
_: [
|
|
36
|
+
'* Update API',
|
|
37
|
+
'* Fix bug #1'
|
|
38
|
+
],
|
|
39
|
+
Changes: [
|
|
40
|
+
'* Update API',
|
|
41
|
+
'* Fix bug #1'
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
version: '2.3.0',
|
|
47
|
+
title: '2.3.0 - 2018-12-18',
|
|
48
|
+
'date': '2018-12-18',
|
|
49
|
+
body: '### Added' + EOL + EOL + '- Some changelog generators such as [standard-version](https://github.com/conventional-changelog/standard-version) would produce H1s for major versions and H2s for minor versions. We want the parser to be able to parse both.',
|
|
50
|
+
parsed: {
|
|
51
|
+
_: [
|
|
52
|
+
'- Some changelog generators such as [standard-version](https://github.com/conventional-changelog/standard-version) would produce H1s for major versions and H2s for minor versions. We want the parser to be able to parse both.'
|
|
53
|
+
],
|
|
54
|
+
Added: [
|
|
55
|
+
'- Some changelog generators such as [standard-version](https://github.com/conventional-changelog/standard-version) would produce H1s for major versions and H2s for minor versions. We want the parser to be able to parse both.'
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
version: '2.2.3-pre.1',
|
|
61
|
+
title: '2.2.3-pre.1 - 2013-02-14',
|
|
62
|
+
'date': '2013-02-14',
|
|
63
|
+
body: '### Added' + EOL + '- Added an item.' + EOL + '* Added another item.' + EOL + EOL + '* Update API',
|
|
64
|
+
parsed: {
|
|
65
|
+
_: [
|
|
66
|
+
'- Added an item.',
|
|
67
|
+
'* Added another item.',
|
|
68
|
+
'* Update API'
|
|
69
|
+
],
|
|
70
|
+
Added: [
|
|
71
|
+
'- Added an item.',
|
|
72
|
+
'* Added another item.',
|
|
73
|
+
'* Update API'
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
version: '2.0.0-x.7.z.92',
|
|
79
|
+
title: '2.0.0-x.7.z.92 - 2013-02-14',
|
|
80
|
+
'date': '2013-02-14',
|
|
81
|
+
body: '* bark bark' + EOL + '* woof' + EOL + '* arf',
|
|
82
|
+
parsed: {
|
|
83
|
+
_: [
|
|
84
|
+
'* bark bark',
|
|
85
|
+
'* woof',
|
|
86
|
+
'* arf'
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
version: '1.3.0',
|
|
92
|
+
title: 'v1.3.0',
|
|
93
|
+
'date': null,
|
|
94
|
+
body: '* make it so',
|
|
95
|
+
parsed: {
|
|
96
|
+
_: [
|
|
97
|
+
'* make it so'
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
version: '1.2.3',
|
|
103
|
+
title: '[1.2.3](link)',
|
|
104
|
+
'date': null,
|
|
105
|
+
body: '* init',
|
|
106
|
+
parsed: {
|
|
107
|
+
_: [
|
|
108
|
+
'* init'
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
]
|
|
113
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
var parseChangelog = require('..')
|
|
2
|
+
var test = require('tape')
|
|
3
|
+
var path = require('path')
|
|
4
|
+
var fs = require('fs')
|
|
5
|
+
var expected = require('./fixtures/expected')
|
|
6
|
+
var removeMarkdownExpected = require('./fixtures/remove-markdown-expected')
|
|
7
|
+
var filePath = path.join(__dirname, 'fixtures', 'CHANGELOG.md')
|
|
8
|
+
|
|
9
|
+
test('throws on bad params', function (t) {
|
|
10
|
+
t.plan(3)
|
|
11
|
+
|
|
12
|
+
var missing = 'must provide filePath or text'
|
|
13
|
+
var invalidPath = 'invalid filePath, expected string'
|
|
14
|
+
var invalidText = 'invalid text, expected string'
|
|
15
|
+
|
|
16
|
+
t.throws(parseChangelog, missing, missing)
|
|
17
|
+
t.throws(function () {
|
|
18
|
+
parseChangelog({ filePath: 0 })
|
|
19
|
+
}, invalidPath, invalidPath)
|
|
20
|
+
t.throws(function () {
|
|
21
|
+
parseChangelog({ text: 0 })
|
|
22
|
+
}, invalidText, invalidText)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
test('parses example changelog', function (t) {
|
|
26
|
+
t.plan(1)
|
|
27
|
+
|
|
28
|
+
parseChangelog(filePath, function (err, result) {
|
|
29
|
+
if (err) throw err
|
|
30
|
+
|
|
31
|
+
t.deepEqual(result, expected)
|
|
32
|
+
t.end()
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('parses example changelog as text', function (t) {
|
|
37
|
+
t.plan(1)
|
|
38
|
+
|
|
39
|
+
parseChangelog({text: fs.readFileSync(filePath, 'utf8')}, function (err, result) {
|
|
40
|
+
if (err) throw err
|
|
41
|
+
|
|
42
|
+
t.deepEqual(result, expected)
|
|
43
|
+
t.end()
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('returns a Promise when invoked without a valid `callback`', function (t) {
|
|
48
|
+
t.plan(2)
|
|
49
|
+
|
|
50
|
+
var result = parseChangelog(filePath)
|
|
51
|
+
|
|
52
|
+
t.true(typeof result === 'object')
|
|
53
|
+
t.true(result instanceof Promise)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('resolved Promise contains a "CHANGELOG" object', function (t) {
|
|
57
|
+
t.plan(1)
|
|
58
|
+
|
|
59
|
+
parseChangelog(filePath).then(function (result) {
|
|
60
|
+
t.deepEqual(result, expected)
|
|
61
|
+
t.end()
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('callback and Promise methods should yield identical values', function (t) {
|
|
66
|
+
t.plan(1)
|
|
67
|
+
|
|
68
|
+
parseChangelog(filePath, function (err, resultA) {
|
|
69
|
+
if (err) t.fail()
|
|
70
|
+
|
|
71
|
+
parseChangelog(filePath)
|
|
72
|
+
.then(function (resultB) {
|
|
73
|
+
t.deepEqual(resultA, resultB)
|
|
74
|
+
t.end()
|
|
75
|
+
})
|
|
76
|
+
.catch(t.fail)
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('accepts object as first argument', function (t) {
|
|
81
|
+
t.plan(1)
|
|
82
|
+
|
|
83
|
+
parseChangelog({ filePath: filePath }, function (err, result) {
|
|
84
|
+
if (err) throw err
|
|
85
|
+
|
|
86
|
+
t.deepEqual(result, expected)
|
|
87
|
+
t.end()
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('accepts { removeMardown: false } option', function (t) {
|
|
92
|
+
t.plan(1)
|
|
93
|
+
|
|
94
|
+
var options = {
|
|
95
|
+
filePath: filePath,
|
|
96
|
+
removeMarkdown: false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
parseChangelog(options, function (err, result) {
|
|
100
|
+
if (err) throw err
|
|
101
|
+
|
|
102
|
+
t.deepEqual(result, removeMarkdownExpected)
|
|
103
|
+
t.end()
|
|
104
|
+
})
|
|
105
|
+
})
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (C) 2012 by Nick Ewing
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
|
20
|
+
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
Line Reader
|
|
2
|
+
===========
|
|
3
|
+
|
|
4
|
+
Asynchronous line-by-line file reader.
|
|
5
|
+
|
|
6
|
+
Install
|
|
7
|
+
-------
|
|
8
|
+
|
|
9
|
+
`npm install line-reader`
|
|
10
|
+
|
|
11
|
+
Usage
|
|
12
|
+
-----
|
|
13
|
+
|
|
14
|
+
The `eachLine` function reads each line of the given file. Upon each new line,
|
|
15
|
+
the given callback function is called with two parameters: the line read and a
|
|
16
|
+
boolean value specifying whether the line read was the last line of the file.
|
|
17
|
+
If the callback returns `false`, reading will stop and the file will be closed.
|
|
18
|
+
|
|
19
|
+
var lineReader = require('line-reader');
|
|
20
|
+
|
|
21
|
+
lineReader.eachLine('file.txt', function(line, last) {
|
|
22
|
+
console.log(line);
|
|
23
|
+
|
|
24
|
+
if (/* done */) {
|
|
25
|
+
return false; // stop reading
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
`eachLine` can also be used in an asynchronous manner by providing a third
|
|
30
|
+
callback parameter like so:
|
|
31
|
+
|
|
32
|
+
var lineReader = require('line-reader');
|
|
33
|
+
|
|
34
|
+
lineReader.eachLine('file.txt', function(line, last, cb) {
|
|
35
|
+
console.log(line);
|
|
36
|
+
|
|
37
|
+
if (/* done */) {
|
|
38
|
+
cb(false); // stop reading
|
|
39
|
+
} else {
|
|
40
|
+
cb();
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
The `eachLine` function also returns an object with one property, `then`. If a
|
|
45
|
+
callback is provided to `then`, it will be called once all lines have been read.
|
|
46
|
+
|
|
47
|
+
var lineReader = require('line-reader');
|
|
48
|
+
|
|
49
|
+
// read all lines:
|
|
50
|
+
lineReader.eachLine('file.txt', function(line) {
|
|
51
|
+
console.log(line);
|
|
52
|
+
}).then(function () {
|
|
53
|
+
console.log("I'm done!!");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
For more granular control, `open`, `hasNextLine`, and `nextLine` maybe be used
|
|
58
|
+
to iterate a file:
|
|
59
|
+
|
|
60
|
+
// or read line by line:
|
|
61
|
+
lineReader.open('file.txt', function(reader) {
|
|
62
|
+
if (reader.hasNextLine()) {
|
|
63
|
+
reader.nextLine(function(line) {
|
|
64
|
+
console.log(line);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
Contributors
|
|
70
|
+
------------
|
|
71
|
+
|
|
72
|
+
* Nick Ewing
|
|
73
|
+
* Jameson Little (beatgammit)
|
|
74
|
+
|
|
75
|
+
Copyright 2011 Nick Ewing.
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
(function() {
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
var fs = require('fs'),
|
|
5
|
+
StringDecoder = require('string_decoder').StringDecoder;
|
|
6
|
+
|
|
7
|
+
function LineReader(fd, cb, separator, encoding, bufferSize) {
|
|
8
|
+
var filePosition = 0,
|
|
9
|
+
encoding = encoding || 'utf8',
|
|
10
|
+
separator = separator || '\n',
|
|
11
|
+
bufferSize = bufferSize || 1024,
|
|
12
|
+
buffer = new Buffer(bufferSize),
|
|
13
|
+
bufferStr = '',
|
|
14
|
+
decoder = new StringDecoder(encoding),
|
|
15
|
+
closed = false,
|
|
16
|
+
eof = false,
|
|
17
|
+
separatorIndex = -1;
|
|
18
|
+
|
|
19
|
+
function close() {
|
|
20
|
+
if (!closed) {
|
|
21
|
+
fs.close(fd, function(err) {
|
|
22
|
+
if (err) {
|
|
23
|
+
throw err;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
closed = true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function readToSeparator(cb) {
|
|
31
|
+
function readChunk() {
|
|
32
|
+
fs.read(fd, buffer, 0, bufferSize, filePosition, function(err, bytesRead) {
|
|
33
|
+
var separatorAtEnd;
|
|
34
|
+
|
|
35
|
+
if (err) {
|
|
36
|
+
throw err;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (bytesRead < bufferSize) {
|
|
40
|
+
eof = true;
|
|
41
|
+
close();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
filePosition += bytesRead;
|
|
45
|
+
|
|
46
|
+
bufferStr += decoder.write(buffer.slice(0, bytesRead));
|
|
47
|
+
|
|
48
|
+
if (separatorIndex < 0) {
|
|
49
|
+
separatorIndex = bufferStr.indexOf(separator);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
separatorAtEnd = separatorIndex === bufferStr.length - 1;
|
|
53
|
+
if (bytesRead && (separatorIndex === -1 || separatorAtEnd) && !eof) {
|
|
54
|
+
readChunk();
|
|
55
|
+
} else {
|
|
56
|
+
cb();
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
readChunk();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function hasNextLine() {
|
|
65
|
+
return bufferStr.length > 0 || !eof;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function nextLine(cb) {
|
|
69
|
+
function getLine() {
|
|
70
|
+
var ret = bufferStr.substring(0, separatorIndex);
|
|
71
|
+
|
|
72
|
+
bufferStr = bufferStr.substring(separatorIndex + separator.length);
|
|
73
|
+
separatorIndex = -1;
|
|
74
|
+
cb(ret);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (separatorIndex < 0) {
|
|
78
|
+
separatorIndex = bufferStr.indexOf(separator);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (separatorIndex < 0) {
|
|
82
|
+
if (eof) {
|
|
83
|
+
if (hasNextLine()) {
|
|
84
|
+
separatorIndex = bufferStr.length;
|
|
85
|
+
getLine();
|
|
86
|
+
} else {
|
|
87
|
+
throw new Error('No more lines to read.');
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
readToSeparator(getLine);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
getLine();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
this.hasNextLine = hasNextLine;
|
|
98
|
+
this.nextLine = nextLine;
|
|
99
|
+
this.close = close;
|
|
100
|
+
|
|
101
|
+
readToSeparator(cb);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function open(filename, cb, separator, encoding, bufferSize) {
|
|
105
|
+
fs.open(filename, 'r', parseInt('666', 8), function(err, fd) {
|
|
106
|
+
var reader;
|
|
107
|
+
if (err) {
|
|
108
|
+
throw err;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
reader = new LineReader(fd, function() {
|
|
112
|
+
cb(reader);
|
|
113
|
+
}, separator, encoding, bufferSize);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function eachLine(filename, cb, separator, encoding, bufferSize) {
|
|
118
|
+
var finalFn,
|
|
119
|
+
asyncCb = cb.length == 3;
|
|
120
|
+
|
|
121
|
+
function finish() {
|
|
122
|
+
if (finalFn && typeof finalFn === 'function') {
|
|
123
|
+
finalFn();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
open(filename, function(reader) {
|
|
128
|
+
function newRead() {
|
|
129
|
+
if (reader.hasNextLine()) {
|
|
130
|
+
setImmediate(readNext);
|
|
131
|
+
} else {
|
|
132
|
+
finish();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function continueCb(continueReading) {
|
|
137
|
+
if (continueReading !== false) {
|
|
138
|
+
newRead();
|
|
139
|
+
} else {
|
|
140
|
+
finish();
|
|
141
|
+
reader.close();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function readNext() {
|
|
146
|
+
reader.nextLine(function(line) {
|
|
147
|
+
var last = !reader.hasNextLine();
|
|
148
|
+
|
|
149
|
+
if (asyncCb) {
|
|
150
|
+
cb(line, last, continueCb);
|
|
151
|
+
} else {
|
|
152
|
+
if (cb(line, last) !== false) {
|
|
153
|
+
newRead();
|
|
154
|
+
} else {
|
|
155
|
+
finish();
|
|
156
|
+
reader.close();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
newRead();
|
|
163
|
+
}, separator, encoding, bufferSize);
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
then: function(cb) {
|
|
167
|
+
finalFn = cb;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
module.exports.open = open;
|
|
173
|
+
module.exports.eachLine = eachLine;
|
|
174
|
+
}());
|