Skip to content

Commit e6cb99d

Browse files
author
Ovidiu Barabula
committed
feat(core): implement task manager methods, helpers and task template
1 parent e6f1c33 commit e6cb99d

File tree

10 files changed

+388
-116
lines changed

10 files changed

+388
-116
lines changed

src/config-manager/index.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { assert, expect } from 'chai';
22
import 'mocha';
33
import * as path from 'path';
4-
import ConfigManagerFactory, { IConfigManager } from './index';
5-
import { Config, ConfigReaderFactory, IConfigReader } from './package-json-config-reader';
4+
import ConfigManager, { IConfigManager } from './index';
5+
import { Config, ConfigReader, IConfigReader } from './package-json-config-reader';
66

77
describe('ConfigManager', () => {
88
const testDir = '/tmp/tests/';
99
const configFile = 'package.json';
1010
let configManager: IConfigManager;
1111

1212

13-
beforeEach(async () => configManager = await ConfigManagerFactory('frontvue'));
13+
beforeEach(async () => configManager = await ConfigManager('frontvue'));
1414

1515

1616
it('instantiates', async () => {
@@ -19,7 +19,7 @@ describe('ConfigManager', () => {
1919

2020

2121
it('instantiates with custom config reader', async () => {
22-
const customReader: ConfigReaderFactory = (namespace: string) => {
22+
const customReader: ConfigReader = (namespace: string) => {
2323
let config: Config = {};
2424
return {
2525
destroy(): Promise<Config> {
@@ -37,7 +37,7 @@ describe('ConfigManager', () => {
3737
};
3838
};
3939

40-
const customConfigManager = await ConfigManagerFactory('frontvue', customReader);
40+
const customConfigManager = await ConfigManager('frontvue', customReader);
4141
expect(customConfigManager).to.be.an('object').to.have.all.keys('get', 'has', 'remove', 'set');
4242
});
4343

src/config-manager/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import PackageJsonConfigReader, {
99
Config,
10-
ConfigReaderFactory,
10+
ConfigReader,
1111
IConfigReader,
1212
} from './package-json-config-reader';
1313

@@ -24,9 +24,9 @@ export interface IConfigManager {
2424
* @param namespace Configuration namespace, usually app name (used in package.json { config: { <namespace>: {} } })
2525
* @param customReader Custom ConfigReader
2626
*/
27-
async function ConfigManagerFactory(
27+
async function ConfigManager(
2828
namespace: string,
29-
customReader?: ConfigReaderFactory,
29+
customReader?: ConfigReader,
3030
): Promise<IConfigManager> {
3131
let configReader: IConfigReader;
3232

@@ -109,4 +109,4 @@ async function ConfigManagerFactory(
109109
});
110110
}
111111

112-
export default ConfigManagerFactory;
112+
export default ConfigManager;

src/config-manager/package-json-config-reader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface IConfigReader {
1818
update(config: Config): Promise<boolean>;
1919
}
2020

21-
export type ConfigReaderFactory = (namespace: string, filepath?: string) => IConfigReader;
21+
export type ConfigReader = (namespace: string, filepath?: string) => IConfigReader;
2222

2323
export const ERRORS = {
2424
NO_NAMESPACE: 'PackageJsonConfigReader requires parameter 1 to be string',

src/task-manager/index.spec.ts

Lines changed: 120 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { assert, expect } from 'chai';
2+
import * as gulp from 'gulp';
23
import 'mocha';
3-
import TaskManagerFactory, { ERRORS } from './index';
4+
import TaskManager, { ERRORS } from './index';
45

56

67
// Helper: create custom hook task
7-
function makeTask(hook: string) {
8+
function makeTask(hook: string, register: boolean = true) {
89
return {
910
install(subscriptions) {
10-
return subscriptions[hook](`${hook}-task`);
11+
// Register Gulp test task
12+
register && gulp.task(`${hook}-task`, done => done());
13+
14+
// Subscribe task to hook
15+
return subscriptions[hook] && subscriptions[hook](`${hook}-task`);
1116
},
1217
};
1318
}
@@ -19,98 +24,120 @@ describe('TaskManager', () => {
1924
};
2025

2126

22-
describe('Factory', () => {
23-
it('creates TaskManager instance', () => {
24-
const taskManager = TaskManagerFactory();
25-
expect(taskManager)
26-
.to.be.an('object')
27-
.to.have.all.keys('add', 'getHooks', 'getTasks');
28-
});
27+
it('creates TaskManager instance', () => {
28+
const taskManager = TaskManager();
29+
expect(taskManager)
30+
.to.be.an('object')
31+
.to.have.all.keys('add', 'run', 'hasTasks', 'getHooks', 'getTasks');
32+
});
33+
34+
35+
it('adds custom hooks through the options object argument', () => {
36+
const taskManager = TaskManager(options);
37+
expect(taskManager.getHooks())
38+
.to.be.an('array')
39+
.to.have.members(options.hooks);
40+
});
41+
42+
43+
it('returns task arrays for all hooks', () => {
44+
const taskManager = TaskManager(options);
45+
const task = {
46+
install(subscribers) {
47+
expect(subscribers)
48+
.to.be.an('object')
49+
.to.have.all.keys(options.hooks)
50+
.that.satisfies(subject =>
51+
Object.keys(subject).every(key => typeof subject[key] === 'function'),
52+
);
53+
},
54+
};
55+
56+
taskManager.add(task);
57+
});
58+
59+
60+
it('subscribes task to hook', () => {
61+
const taskManager = TaskManager(options);
62+
const task = makeTask('before');
63+
64+
taskManager.add(task);
65+
66+
expect(taskManager.getTasks().before)
67+
.to.be.an('array')
68+
.to.have.members(['before-task']);
69+
});
70+
71+
72+
it('returns true on task subscription', () => {
73+
const taskManager = TaskManager(options);
74+
const task = {
75+
install(subscriptions) {
76+
expect(subscriptions.before('before-task')).to.be.true;
77+
},
78+
};
79+
taskManager.add(task);
80+
});
81+
82+
83+
it('returns true when task is run', async () => {
84+
const taskManager = TaskManager(options);
85+
taskManager.add(makeTask('before'));
86+
expect(await taskManager.run('before')).to.be.true;
87+
});
88+
89+
90+
it('returns false when task is not run', async () => {
91+
const taskManager = TaskManager(options);
92+
expect(await taskManager.run('before')).to.be.false;
93+
});
94+
95+
96+
it('returns true if specific hook has tasks', () => {
97+
const taskManager = TaskManager(options);
98+
taskManager.add(makeTask('before'));
99+
expect(taskManager.hasTasks('before')).to.be.true;
100+
});
101+
102+
103+
it('returns false if no tasks are found for specific hook', () => {
104+
const taskManager = TaskManager(options);
105+
expect(taskManager.hasTasks('before')).to.be.false;
106+
});
107+
108+
109+
it('throws when .add() doesn\'t receive appropriate task', () => {
110+
const taskManager = TaskManager(options);
111+
assert.throws(() => taskManager.add(undefined), ERRORS.BAD_TASK);
112+
});
113+
114+
115+
it('returns false when trying to subscribe an already subscribed task name', () => {
116+
const task1 = {
117+
install(subscriptions) {
118+
expect(subscriptions.before('same-task-name')).to.be.true;
119+
},
120+
};
121+
const task2 = {
122+
install(subscriptions) {
123+
expect(subscriptions.before('same-task-name')).to.be.false;
124+
},
125+
};
126+
127+
const taskManager = TaskManager(options);
128+
taskManager.add(task1);
129+
taskManager.add(task2);
29130
});
30131

31132

32-
describe('Instance', () => {
33-
it('adds custom hooks through the options object argument', () => {
34-
const taskManager = TaskManagerFactory(options);
35-
expect(taskManager.getHooks())
36-
.to.be.an('array')
37-
.to.have.members(options.hooks);
38-
});
39-
40-
41-
it('returns task arrays for all hooks', () => {
42-
const taskManager = TaskManagerFactory(options);
43-
const task = {
44-
install(subscribers) {
45-
expect(subscribers)
46-
.to.be.an('object')
47-
.to.have.all.keys(options.hooks)
48-
.that.satisfies(subject =>
49-
Object.keys(subject).every(key => typeof subject[key] === 'function'),
50-
);
51-
},
52-
};
53-
54-
taskManager.add(task);
55-
});
56-
57-
58-
it('subscribes task to hook', () => {
59-
const taskManager = TaskManagerFactory(options);
60-
const task = makeTask('before');
61-
62-
taskManager.add(task);
63-
64-
expect(taskManager.getTasks().before)
65-
.to.be.an('array')
66-
.to.have.members(['before-task']);
67-
});
68-
69-
70-
it('returns true on task subscription', () => {
71-
const taskManager = TaskManagerFactory(options);
72-
const task = {
73-
install(subscriptions) {
74-
expect(subscriptions.before('before-task')).to.be.true;
75-
},
76-
};
77-
taskManager.add(task);
78-
});
79-
80-
81-
it('throws when .add() doesn\'t receive appropriate task', () => {
82-
const taskManager = TaskManagerFactory(options);
83-
assert.throws(() => taskManager.add(undefined), ERRORS.BAD_TASK);
84-
});
85-
86-
87-
it('returns false when trying to subscribe an already subscribed task name', () => {
88-
const task1 = {
89-
install(subscriptions) {
90-
expect(subscriptions.before('same-task-name')).to.be.true;
91-
},
92-
};
93-
const task2 = {
94-
install(subscriptions) {
95-
expect(subscriptions.before('same-task-name')).to.be.false;
96-
},
97-
};
98-
99-
const taskManager = TaskManagerFactory(options);
100-
taskManager.add(task1);
101-
taskManager.add(task2);
102-
});
103-
104-
105-
it('should not allow more than 1 subscription per hook', () => {
106-
const abusingTask = {
107-
install(subscriptions) {
108-
expect(subscriptions.before('task-name')).to.be.true;
109-
expect(subscriptions.before('devious-new-name')).to.be.undefined;
110-
},
111-
};
112-
const taskManager = TaskManagerFactory(options);
113-
taskManager.add(abusingTask);
114-
});
133+
it('should not allow more than 1 subscription per hook', () => {
134+
const abusingTask = {
135+
install(subscriptions) {
136+
expect(subscriptions.before('task-name')).to.be.true;
137+
expect(subscriptions.before('devious-new-name')).to.be.undefined;
138+
},
139+
};
140+
const taskManager = TaskManager(options);
141+
taskManager.add(abusingTask);
115142
});
116143
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy