Skip to content

Commit 4b711da

Browse files
refactor: use singleton config for execution
It will be easy to not put config as parameter each time
1 parent 806defa commit 4b711da

File tree

5 files changed

+172
-148
lines changed

5 files changed

+172
-148
lines changed

__tests__/bin-readme.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const path = require('path');
2-
const os = require('os');
32
const exec = require('child_process').exec;
43
const tmp = require('tmp');
54
const fs = require('fs-extra');

__tests__/lib/merge_config.js

Lines changed: 92 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,108 @@
11
const path = require('path');
2-
const _ = require('lodash');
2+
const config = require('../../src/config');
33
const mergeConfig = require('../../src/merge_config');
44

5-
test('bad config', async function() {
6-
try {
7-
await mergeConfig({ config: 'DOES-NOT-EXIST' });
8-
} catch (err) {
9-
expect(err).toBeTruthy();
10-
}
11-
});
5+
describe('single config tests', function () {
6+
beforeEach(function () {
7+
config.reset();
8+
});
129

13-
test('right merging package configuration', async function() {
14-
// Omit configuration from output, for simplicity
15-
const nc = _.curryRight(_.omit, 2)([
16-
'config',
17-
'no-package',
18-
'parseExtension',
19-
'project-homepage',
20-
'project-version',
21-
'project-description'
22-
]);
23-
return mergeConfig({
24-
config: path.join(__dirname, '../config_fixture/config.json'),
25-
'no-package': true,
26-
'project-name': 'cool Documentation'
27-
})
28-
.then(nc)
29-
.then(res => {
30-
expect(res).toEqual({
31-
'project-name': 'cool Documentation',
32-
foo: 'bar'
33-
});
10+
test('Should be failed on bad config', async function () {
11+
try {
12+
await mergeConfig({ config: 'DOES-NOT-EXIST' });
13+
} catch (err) {
14+
expect(err).toBeTruthy();
15+
return;
16+
}
17+
return Promise.reject(new Error('should be failed on bad config'));
18+
});
19+
20+
test('right merging package configuration', async function () {
21+
const list = [
22+
'config',
23+
'no-package',
24+
'parseExtension',
25+
'project-homepage',
26+
'project-version',
27+
'project-description'
28+
];
29+
await mergeConfig({
30+
config: path.join(__dirname, '../config_fixture/config.json'),
31+
'no-package': true,
32+
'project-name': 'cool Documentation'
3433
});
35-
});
3634

37-
test('nc(mergeConfig)', async function() {
38-
// Omit configuration from output, for simplicity
39-
const nc = _.curryRight(_.omit, 2)([
35+
const res = config.globalConfig;
36+
list.forEach(key => delete res[key]);
37+
expect(res).toEqual({
38+
'project-name': 'cool Documentation',
39+
foo: 'bar'
40+
});
41+
});
42+
43+
const list = [
4044
'config',
4145
'no-package',
4246
'parseExtension',
4347
'project-homepage',
4448
'project-name',
4549
'project-version',
4650
'project-description'
47-
]);
51+
];
4852

49-
return Promise.all(
53+
[
54+
[
55+
{ config: path.join(__dirname, '../config_fixture/config.json') },
56+
{ foo: 'bar' }
57+
],
58+
[
59+
{
60+
passThrough: true,
61+
config: path.join(__dirname, '../config_fixture/config.json')
62+
},
63+
{ foo: 'bar', passThrough: true }
64+
],
65+
[
66+
{
67+
config: path.join(__dirname, '../config_fixture/config_comments.json')
68+
},
69+
{ foo: 'bar' }
70+
],
71+
[
72+
{ config: path.join(__dirname, '../config_fixture/config.yaml') },
73+
{ foo: 'bar' }
74+
],
5075
[
51-
[
52-
{ config: path.join(__dirname, '../config_fixture/config.json') },
53-
{ foo: 'bar' }
54-
],
55-
[
56-
{
57-
passThrough: true,
58-
config: path.join(__dirname, '../config_fixture/config.json')
59-
},
60-
{ foo: 'bar', passThrough: true }
61-
],
62-
[
63-
{
64-
config: path.join(__dirname, '../config_fixture/config_comments.json')
65-
},
66-
{ foo: 'bar' }
67-
],
68-
[
69-
{ config: path.join(__dirname, '../config_fixture/config.yaml') },
70-
{ foo: 'bar' }
71-
],
72-
[
73-
{ config: path.join(__dirname, '../config_fixture/config.yml') },
74-
{ foo: 'bar' }
75-
],
76-
[
77-
{ config: path.join(__dirname, '../config_fixture/config') },
78-
{ foo: 'bar' }
79-
],
80-
[
81-
{ config: path.join(__dirname, '../config_fixture/config_links.yml') },
82-
{ foo: 'hello [link](https://github.com/my/link) world' }
83-
],
84-
[
85-
{ config: path.join(__dirname, '../config_fixture/config_file.yml') },
86-
{
87-
toc: [
88-
{
89-
name: 'snowflake',
90-
file: path.join(__dirname, '../fixture/snowflake.md')
91-
}
92-
]
93-
}
94-
]
95-
].map(pair =>
96-
mergeConfig(Object.assign(pair[0], { 'no-package': true }))
97-
.then(nc)
98-
.then(res => {
99-
expect(res).toEqual(pair[1]);
100-
})
101-
)
102-
);
76+
{ config: path.join(__dirname, '../config_fixture/config.yml') },
77+
{ foo: 'bar' }
78+
],
79+
[
80+
{ config: path.join(__dirname, '../config_fixture/config') },
81+
{ foo: 'bar' }
82+
],
83+
[
84+
{
85+
config: path.join(__dirname, '../config_fixture/config_links.yml')
86+
},
87+
{ foo: 'hello [link](https://github.com/my/link) world' }
88+
],
89+
[
90+
{ config: path.join(__dirname, '../config_fixture/config_file.yml') },
91+
{
92+
toc: [
93+
{
94+
name: 'snowflake',
95+
file: path.join(__dirname, '../fixture/snowflake.md')
96+
}
97+
]
98+
}
99+
]
100+
].forEach((pair, index) => {
101+
test(`nc(mergeConfig) ${index}`, async function () {
102+
await mergeConfig(Object.assign(pair[0], { 'no-package': true }));
103+
const res = config.globalConfig;
104+
list.forEach(key => delete res[key]);
105+
expect(res).toEqual(pair[1]);
106+
});
107+
});
103108
});

__tests__/test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const path = require('path');
1111
const fs = require('fs');
1212
const _ = require('lodash');
1313
const chdir = require('chdir');
14+
const config = require('../src/config');
1415

1516
const UPDATE = !!process.env.UPDATE;
1617

@@ -30,6 +31,10 @@ function readOptionsFromFile(file) {
3031
return {};
3132
}
3233

34+
beforeEach(function () {
35+
config.reset();
36+
});
37+
3338
if (fs.existsSync(path.join(__dirname, '../.git'))) {
3439
test('git option', async function () {
3540
jest.setTimeout(10000); // 10 second timeout. After update flow.js on 0.56 version the test is executed more time.
@@ -95,10 +100,8 @@ describe('html', function () {
95100
.sync(path.join(__dirname, 'fixture/html', '*.input.js'))
96101
.forEach(function (file) {
97102
test(path.basename(file), async function () {
98-
const result = await documentation.build(
99-
[file],
100-
readOptionsFromFile(file)
101-
);
103+
const options = readOptionsFromFile(file);
104+
const result = await documentation.build([file], options);
102105
const html = await outputHtml(result, {});
103106
const clean = html
104107
.sort((a, b) => a.path > b.path)

src/config.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const defaultConfig = {
2+
// package.json ignored and don't get project infromation
3+
'no-package': false,
4+
// Extenstions which by dafault are parse
5+
parseExtension: ['mjs', 'js', 'jsx', 'es5', 'es6', 'vue']
6+
};
7+
8+
function normalaze(config, global) {
9+
if (config.parseExtension) {
10+
config.parseExtension = Array.from(
11+
new Set([...config.parseExtension, ...global.parseExtension])
12+
);
13+
}
14+
15+
return config;
16+
}
17+
18+
module.exports = {
19+
globalConfig: {
20+
...defaultConfig
21+
},
22+
reset() {
23+
this.globalConfig = { ...defaultConfig };
24+
this.globalConfig.parseExtension = [...defaultConfig.parseExtension];
25+
},
26+
add(parameters) {
27+
Object.assign(this.globalConfig, normalaze(parameters, this.globalConfig));
28+
}
29+
};

0 commit comments

Comments
 (0)