Skip to content

Commit 8495ed2

Browse files
feat: set params for each (#21)
1 parent 3e286e0 commit 8495ed2

File tree

6 files changed

+448
-34
lines changed

6 files changed

+448
-34
lines changed

__tests__/file.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ describe('findMarkdownFiles', () => {
4545
'name': 'readme-with-nested-markdown.md',
4646
'path': resolve(__dirname, 'fixtures/readme-with-nested-markdown.md'),
4747
},
48+
{
49+
'name': 'readme-with-params.md',
50+
'path': resolve(__dirname, 'fixtures/readme-with-params.md'),
51+
},
4852
{
4953
'name': 'readme-with-weird-headers.md',
5054
'path': resolve(__dirname, 'fixtures/readme-with-weird-headers.md'),
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
# dockops [![build status](https://secure.travis-ci.org/technote-space/dockops.png)](http://travis-ci.org/technote-space/dockops)
2+
3+
docker convenience functions on top of dockerode
4+
5+
<!-- START doctoc generated TOC please keep comment here to allow auto update param::entryPrefix::-:: -->
6+
<!-- param::mode::github.com:: -->
7+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
8+
<!-- param::isNotitle::false:: param::maxHeaderLevel::8:: -->
9+
**Table of Contents** *generated with [DocToc](https://github.com/technote-space/doctoc)*
10+
11+
- [Installation](#installation)
12+
- [API](#api)
13+
- [License](#license)
14+
15+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
16+
17+
```js
18+
var dockops = require('dockops')
19+
var docker = dockops.createDocker({ dockerhost: dockerhost });
20+
21+
var images = new dockops.Images(docker);
22+
dockops.logEvents(images, 'verbose');
23+
24+
var containers = new dockops.Containers(docker);
25+
dockops.logEvents(containers, 'verbose');
26+
27+
build('test:uno', testUnoTar, function () {
28+
build('toast:uno', toastUnoTar, function () {
29+
30+
containers.run(.. // run test:uno and toast:uno containers
31+
32+
containers.listRunning(function (err, res) {
33+
inspect(res);
34+
containers.stopRemoveGroup('test', function (err, res) {
35+
containers.listRunning(function (err, res) {
36+
inspect(res);
37+
http.request({ port: 49222 }, function (res) {
38+
console.log('--------------------------')
39+
inspect({ status: res.statusCode, headers: res.headers })
40+
res.pipe(process.stdout)
41+
}).end()
42+
})
43+
})
44+
})
45+
})
46+
})
47+
```
48+
49+
[full example](https://github.com/technote-space/dockops/blob/master/example/create-wipe.js)
50+
51+
![output](https://github.com/technote-space/dockops/raw/master/assets/output.gif)
52+
53+
## Installation
54+
55+
npm install dockops
56+
57+
## API
58+
59+
<!-- START docme generated API please keep comment here to allow auto update -->
60+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN docme TO UPDATE -->
61+
62+
<div>
63+
<div class="jsdoc-githubify">
64+
<section>
65+
<article>
66+
<div class="container-overview">
67+
<dl class="details">
68+
</dl>
69+
</div>
70+
<dl>
71+
<dt>
72+
<h4 class="name" id="dockops::Containers"><span class="type-signature"></span>dockops::Containers<span class="signature">(docker)</span><span class="type-signature"> &rarr; {Object}</span></h4>
73+
</dt>
74+
<dd>
75+
<div class="description">
76+
<p>Creates a new containers instance that will use the given docker instance to communicate with docker.</p>
77+
</div>
78+
<h5>Parameters:</h5>
79+
<table class="params">
80+
<thead>
81+
<tr>
82+
<th>Name</th>
83+
<th>Type</th>
84+
<th class="last">Description</th>
85+
</tr>
86+
</thead>
87+
<tbody>
88+
<tr>
89+
<td class="name"><code>docker</code></td>
90+
<td class="type">
91+
<span class="param-type">Object</span>
92+
</td>
93+
<td class="description last"><p>dockerode instance to communicate with docker</p></td>
94+
</tr>
95+
</tbody>
96+
</table>
97+
<dl class="details">
98+
<dt class="tag-source">Source:</dt>
99+
<dd class="tag-source"><ul class="dummy">
100+
<li>
101+
<a href="https://github.com/technote-space/dockops/blob/master/containers.js">containers.js</a>
102+
<span>, </span>
103+
<a href="https://github.com/technote-space/dockops/blob/master/containers.js#L24">lineno 24</a>
104+
</li>
105+
</ul></dd>
106+
</dl>
107+
<h5>Returns:</h5>
108+
<div class="param-desc">
109+
<p>initialized containers</p>
110+
</div>
111+
<dl>
112+
<dt>
113+
Type
114+
</dt>
115+
<dd>
116+
<span class="param-type">Object</span>
117+
</dd>
118+
</dl>
119+
</dd>
120+
<dt>
121+
<h4 class="name" id="dockops::Containers::activePorts"><span class="type-signature"></span>dockops::Containers::activePorts<span class="signature">(cb)</span><span class="type-signature"></span></h4>
122+
</dt>
123+
<dd>
124+
<div class="description">
125+
<p>Lists all running containers by the ports they expose.</p>
126+
</div>
127+
<h5>Parameters:</h5>
128+
<table class="params">
129+
<thead>
130+
<tr>
131+
<th>Name</th>
132+
<th>Type</th>
133+
<th class="last">Description</th>
134+
</tr>
135+
</thead>
136+
<tbody>
137+
<tr>
138+
<td class="name"><code>cb</code></td>
139+
<td class="type">
140+
<span class="param-type">function</span>
141+
</td>
142+
<td class="description last"><p>called back with list of containers hashed by their port number</p></td>
143+
</tr>
144+
</tbody>
145+
</table>
146+
<dl class="details">
147+
<dt class="tag-source">Source:</dt>
148+
<dd class="tag-source"><ul class="dummy">
149+
<li>
150+
<a href="https://github.com/technote-space/dockops/blob/master/containers.js">containers.js</a>
151+
<span>, </span>
152+
<a href="https://github.com/technote-space/dockops/blob/master/containers.js#L275">lineno 275</a>
153+
</li>
154+
</ul></dd>
155+
</dl>
156+
</dd>
157+
<dt>
158+
<h4 class="name" id="dockops::Containers::clean"><span class="type-signature"></span>dockops::Containers::clean<span class="signature">(id, cb)</span><span class="type-signature"></span></h4>
159+
</dt>
160+
<dd>
161+
<div class="description">
162+
<p>Stops and/or kills and then removes a container.</p>
163+
<p>Heavy machinery clean operation.
164+
It was useful when running on arch with docker not always working as promised.
165+
This may not be needed anymore as docker got more stable.</p>
166+
</div>
167+
<h5>Parameters:</h5>
168+
<table class="params">
169+
<thead>
170+
<tr>
171+
<th>Name</th>
172+
<th>Type</th>
173+
<th class="last">Description</th>
174+
</tr>
175+
</thead>
176+
<tbody>
177+
<tr>
178+
<td class="name"><code>id</code></td>
179+
<td class="type">
180+
<span class="param-type">string</span>
181+
</td>
182+
<td class="description last"><p>container id</p></td>
183+
</tr>
184+
<tr>
185+
<td class="name"><code>cb</code></td>
186+
<td class="type">
187+
<span class="param-type">function</span>
188+
</td>
189+
<td class="description last"><p>called back after container was cleaned or maximum attempts were exceeded</p></td>
190+
</tr>
191+
</tbody>
192+
</table>
193+
<dl class="details">
194+
<dt class="tag-source">Source:</dt>
195+
<dd class="tag-source"><ul class="dummy">
196+
<li>
197+
<a href="https://github.com/technote-space/dockops/blob/master/containers.js">containers.js</a>
198+
<span>, </span>
199+
<a href="https://github.com/technote-space/dockops/blob/master/containers.js#L97">lineno 97</a>
200+
</li>
201+
</ul></dd>
202+
</dl>
203+
</dd>
204+
</article>
205+
</section>
206+
</div>
207+
208+
*generated with [docme](https://github.com/technote-space/docme)*
209+
</div>
210+
<!-- END docme generated API please keep comment here to allow auto update -->
211+
212+
<h2>^License</h2>
213+
214+
MIT

__tests__/params.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* eslint-disable no-magic-numbers */
2+
import {extractParams, getStartSection, getParamsSection} from '../src/lib/params';
3+
import {CLOSING_COMMENT, OPENING_COMMENT} from '../src';
4+
5+
describe('extractParams', () => {
6+
const opening = OPENING_COMMENT.replace('\n', ' ');
7+
it('should return params', () => {
8+
expect(extractParams(opening)).toEqual({});
9+
expect(extractParams(opening + '<!-- param::maxHeaderLevel::2:: param::isNotitle::true:: param::isFolding::false:: param::processAll::1:: param::updateOnly::0:: param::isCustomMode:::: -->')).toEqual({
10+
maxHeaderLevel: 2,
11+
isNotitle: true,
12+
isFolding: false,
13+
processAll: true,
14+
updateOnly: false,
15+
isCustomMode: false,
16+
});
17+
expect(extractParams(opening + '<!-- param::mode::test mode:: param::moduleName::test name:: param::title:::: param::entryPrefix::-:: -->')).toEqual({
18+
mode: 'test mode',
19+
moduleName: 'test name',
20+
title: '',
21+
entryPrefix: '-',
22+
});
23+
expect(extractParams(opening + '<!-- param::customTemplate::<ul>${ITEMS}</ul>:: param::itemTemplate::<li><a href="${LINK}" target="_blank">${TEXT}</a></li>:: param::separator:::: -->')).toEqual({
24+
customTemplate: '<ul>${ITEMS}</ul>',
25+
itemTemplate: '<li><a href="${LINK}" target="_blank">${TEXT}</a></li>',
26+
separator: '',
27+
});
28+
expect(extractParams(opening + '<!-- param::test1:::: param::test2:::: -->')).toEqual({});
29+
});
30+
});
31+
32+
describe('getStartSection', () => {
33+
it('should return empty if hasStart is false', () => {
34+
expect(getStartSection(['<!-- param::test1::test1:: -->', '<!-- param::test2::test2:: -->'], {
35+
hasStart: false,
36+
hasEnd: false,
37+
startIdx: -1,
38+
endIdx: -1,
39+
})).toEqual([]);
40+
});
41+
42+
it('should return start line', () => {
43+
expect(getStartSection(['', ...OPENING_COMMENT.split('\n'), '<!-- param::test1::test1:: -->', '<!-- param::test2::test2:: -->'], {
44+
hasStart: true,
45+
hasEnd: true,
46+
startIdx: 1,
47+
endIdx: 5,
48+
})).toEqual([OPENING_COMMENT.split('\n')[0]]);
49+
});
50+
51+
it('should return start section', () => {
52+
expect(getStartSection(['', ...OPENING_COMMENT.split('\n'), '<!-- param::test1::test1:: -->', '<!-- param::test2::test2:: -->', 'title', '', CLOSING_COMMENT, '', ''], {
53+
hasStart: true,
54+
hasEnd: true,
55+
startIdx: 1,
56+
endIdx: 7,
57+
})).toEqual([...OPENING_COMMENT.split('\n'), '<!-- param::test1::test1:: -->', '<!-- param::test2::test2:: -->']);
58+
});
59+
});
60+
61+
describe('getParamsSection', () => {
62+
it('should return empty if no options', () => {
63+
expect(getParamsSection({})).toBe('');
64+
});
65+
66+
it('should return params section', () => {
67+
expect(getParamsSection({
68+
mode: 'test mode',
69+
isNotitle: true,
70+
isFolding: false,
71+
})).toBe(['', '<!-- param::mode::test mode:: -->', '<!-- param::isNotitle::true:: -->', '<!-- param::isFolding::false:: -->'].join('\n'));
72+
});
73+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* eslint-disable no-magic-numbers */
2+
import {resolve} from 'path';
3+
import {readFileSync} from 'fs';
4+
import {transform} from '../src';
5+
6+
describe('transform', () => {
7+
it('override params', () => {
8+
const content = readFileSync(resolve(__dirname, 'fixtures/readme-with-params.md'), 'utf8');
9+
const headers = transform(content, {isNotitle: true, isFolding: false, maxHeaderLevel: 1, entryPrefix: '*'});
10+
11+
expect(headers.wrappedToc.split('\n')).toEqual(
12+
[
13+
'<!-- START doctoc generated TOC please keep comment here to allow auto update -->',
14+
'<!-- DON\'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->',
15+
'<!-- param::entryPrefix::-:: -->',
16+
'<!-- param::mode::github.com:: -->',
17+
'<!-- param::isNotitle::false:: -->',
18+
'<!-- param::maxHeaderLevel::8:: -->',
19+
'**Table of Contents** *generated with [DocToc](https://github.com/technote-space/doctoc)*',
20+
'',
21+
'- [Installation](#installation)',
22+
'- [API](#api)',
23+
' - [dockops::Containers(docker) → {Object}](#dockopscontainersdocker-%E2%86%92-object)',
24+
' - [Parameters:](#parameters)',
25+
' - [Returns:](#returns)',
26+
' - [dockops::Containers::activePorts(cb)](#dockopscontainersactiveportscb)',
27+
' - [Parameters:](#parameters-1)',
28+
' - [dockops::Containers::clean(id, cb)](#dockopscontainerscleanid-cb)',
29+
' - [Parameters:](#parameters-2)',
30+
'- [^License](#license)',
31+
'',
32+
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->',
33+
],
34+
);
35+
});
36+
37+
it('not override params', () => {
38+
const content = readFileSync(resolve(__dirname, 'fixtures/readme-with-params.md'), 'utf8');
39+
const headers = transform(content, {isFolding: true, title: 'Test title'});
40+
41+
expect(headers.wrappedToc.split('\n')).toEqual(
42+
[
43+
'<!-- START doctoc generated TOC please keep comment here to allow auto update -->',
44+
'<!-- DON\'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->',
45+
'<!-- param::entryPrefix::-:: -->',
46+
'<!-- param::mode::github.com:: -->',
47+
'<!-- param::isNotitle::false:: -->',
48+
'<!-- param::maxHeaderLevel::8:: -->',
49+
'<details>',
50+
'<summary>Test title</summary>',
51+
'',
52+
'- [Installation](#installation)',
53+
'- [API](#api)',
54+
' - [dockops::Containers(docker) → {Object}](#dockopscontainersdocker-%E2%86%92-object)',
55+
' - [Parameters:](#parameters)',
56+
' - [Returns:](#returns)',
57+
' - [dockops::Containers::activePorts(cb)](#dockopscontainersactiveportscb)',
58+
' - [Parameters:](#parameters-1)',
59+
' - [dockops::Containers::clean(id, cb)](#dockopscontainerscleanid-cb)',
60+
' - [Parameters:](#parameters-2)',
61+
'- [^License](#license)',
62+
'',
63+
'</details>',
64+
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->',
65+
],
66+
);
67+
});
68+
});

0 commit comments

Comments
 (0)