Skip to content

Commit 3c943e1

Browse files
committed
Add an update command to remotely trigger an update check
1 parent 7d9de7e commit 3c943e1

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/httptoolkit-server.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as _ from 'lodash';
2+
import * as events from 'events';
23
import { GraphQLServer } from 'graphql-yoga'
34
import { GraphQLScalarType } from 'graphql';
45

@@ -24,6 +25,7 @@ const typeDefs = `
2425
id: ID!,
2526
proxyPort: Int!
2627
): Boolean!
28+
triggerUpdate: Void
2729
}
2830
2931
type InterceptionConfig {
@@ -40,11 +42,13 @@ const typeDefs = `
4042
4143
scalar Json
4244
scalar Error
45+
scalar Void
4346
`
4447

4548
const buildResolvers = (
4649
config: HtkConfig,
47-
interceptors: _.Dictionary<Interceptor>
50+
interceptors: _.Dictionary<Interceptor>,
51+
eventEmitter: events.EventEmitter
4852
) => {
4953
return {
5054
Query: {
@@ -73,6 +77,9 @@ const buildResolvers = (
7377

7478
await interceptor.deactivate(proxyPort, options);
7579
return !interceptor.isActive(proxyPort);
80+
},
81+
triggerUpdate: () => {
82+
eventEmitter.emit('update-requested');
7683
}
7784
},
7885

@@ -93,6 +100,14 @@ const buildResolvers = (
93100
parseLiteral: (): any => { throw new Error('JSON literals are not supported') }
94101
}),
95102

103+
Void: new GraphQLScalarType({
104+
name: 'Void',
105+
description: 'Nothing at all',
106+
serialize: (value: any) => null,
107+
parseValue: (input: string): any => null,
108+
parseLiteral: (): any => { throw new Error('Void literals are not supported') }
109+
}),
110+
96111
Error: new GraphQLScalarType({
97112
name: 'Error',
98113
description: 'An error',
@@ -114,16 +129,18 @@ const buildResolvers = (
114129
}
115130
};
116131

117-
export class HttpToolkitServer {
132+
export class HttpToolkitServer extends events.EventEmitter {
118133

119134
private graphql: GraphQLServer;
120135

121136
constructor(config: HtkConfig) {
137+
super();
138+
122139
let interceptors = buildInterceptors(config);
123140

124141
this.graphql = new GraphQLServer({
125142
typeDefs,
126-
resolvers: buildResolvers(config, interceptors)
143+
resolvers: buildResolvers(config, interceptors, this)
127144
});
128145
}
129146

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as fs from 'fs';
44
import * as envPaths from 'env-paths';
55
import { getStandalone, generateCACertificate } from 'mockttp';
66

7+
import updateCommand from '@oclif/plugin-update/lib/commands/update';
8+
79
import { HttpToolkitServer } from './httptoolkit-server';
810

911
const canAccess = util.promisify(fs.access);
@@ -64,6 +66,10 @@ export async function runHTK(options: {
6466
configPath,
6567
https: httpsConfig
6668
});
69+
htkServer.on('update-requested', () => {
70+
updateCommand.run(['stable']);
71+
});
72+
6773
await htkServer.start();
6874

6975
console.log('Server started');

0 commit comments

Comments
 (0)