Skip to content

Commit 4118f7f

Browse files
authored
Changed filename for user config. (#1)
1 parent 1efc37c commit 4118f7f

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ this config is copied to the Cacher directory as `~/.cacher/run-server.config.js
119119
you would like to add a file handler to this config (and let everyone use it), please submit a pull request instead. See
120120
the [Contributing](#contributing) section below.
121121

122-
- **`~/.cacher/run-server.user.config.js`** - Edit this file to add or overwrite file extension handlers on your local
122+
- **`~/.cacher/run-server.user-config.js`** - Edit this file to add or overwrite file extension handlers on your local
123123
machine. Here are some examples of functions you might add to handle new file types:
124124

125125
**Add command for `.awesome` file extension.**
126126

127127
```javascript
128-
// ~/.cacher/run-server.user.config.js
128+
// ~/.cacher/run-server.user-config.js
129129

130130
return {
131131
rules: [
@@ -141,7 +141,7 @@ return {
141141
**Compile `.md` (Markdown) file into HTML, then display in default browser.**
142142

143143
```javascript
144-
// ~/.cacher/run-server.user.config.js
144+
// ~/.cacher/run-server.user-config.js
145145

146146
// Requires `npm -g markdown` to be run first.
147147
return {
@@ -165,7 +165,7 @@ return {
165165
**Match Javascript files by `.js` extension and file content. Calls `nvm use [version]` before execution.**
166166

167167
```javascript
168-
// ~/.cacher/run-server.user.config.js
168+
// ~/.cacher/run-server.user-config.js
169169

170170
return {
171171
rules: [

src/config/config.default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* DO NOT EDIT THIS FILE! It is here only for reference.
3-
* Override/append to these rules by editing ~/.cacher/run-server.user.config.js
3+
* Override/append to these rules by editing ~/.cacher/run-server.user-config.js
44
*
55
* See https://github.com/cacherapp/cacher-run-server/README.md for complete documentation.
66
*/

src/lib/exec-command.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ChildProcess, spawn } from 'child_process';
44

55
const fs = require('fs');
66
const path = require('path');
7-
const vm = require('vm');
87

98
import * as _ from 'lodash';
109
import * as os from 'os';
@@ -14,7 +13,6 @@ import chalk from 'chalk'
1413
const pretty = require('js-object-pretty-print').pretty;
1514

1615
const WORK_DIR = path.resolve(`${os.homedir()}/.cacher/run`);
17-
const USER_CONFIG = path.resolve(`${os.homedir()}/.cacher/run-server.user.config.js`);
1816

1917
export class ExecCommand {
2018
/**
@@ -53,15 +51,9 @@ export class ExecCommand {
5351

5452
const defaultConfig = require('../config/config.default.js');
5553

56-
// Read userConfig from external file
57-
const configUserScript = fs.readFileSync(USER_CONFIG).toString();
58-
const userConfig = vm.runInThisContext(
59-
configUserScript
60-
)(require);
61-
6254
// Prepend user config rules to default config
6355
const config = Object.assign({}, defaultConfig);
64-
_.each(userConfig.rules, (rule: any) => {
56+
_.each(args.userConfig.rules, (rule: any) => {
6557
config.rules.unshift(rule);
6658
});
6759

@@ -97,7 +89,7 @@ export class ExecCommand {
9789

9890
if (!cmd) {
9991
const userConfigFile =
100-
path.resolve(`${os.homedir()}/.cacher/run-server.user.config.js`);
92+
path.resolve(`${os.homedir()}/.cacher/run-server.user-config.js`);
10193
const noCmdMessage =
10294
`Error: Could not find a rule that matches filename '${this.clientCommand.file.filename}'. Please add one to: ${userConfigFile}.`;
10395
return spawn(`echo "${noCmdMessage}" && exit 127`, [], { shell: true });

src/lib/server.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as express from 'express';
33

44
const os = require('os');
55
const path = require('path');
6+
const vm = require('vm');
67

78
const shell = require('shelljs');
89
const socketio = require('socket.io');
@@ -22,9 +23,11 @@ import { ExecCommand } from './exec-command';
2223

2324
const WORK_DIR = path.resolve(`${os.homedir()}/.cacher/run`);
2425
const LOGS_DIR = path.resolve(`${os.homedir()}/.cacher/logs`);
25-
const USER_CONFIG = path.resolve(`${os.homedir()}/.cacher/run-server.user.config.js`);
26+
const USER_CONFIG_FILE = path.resolve(`${os.homedir()}/.cacher/run-server.user-config.js`);
2627
const LOG_FILE = path.resolve(`${LOGS_DIR}/run-server.log`);
2728

29+
let USER_CONFIG = { rules: [] };
30+
2831
export class RunServer {
2932
/**
3033
* Uses the Socket.io Server to accept WebSocket command requests and execute
@@ -51,9 +54,9 @@ export class RunServer {
5154
RunServer.setup();
5255

5356
if (editor) {
54-
shell.exec(`${editor} ${USER_CONFIG}`);
57+
shell.exec(`${editor} ${USER_CONFIG_FILE}`);
5558
} else {
56-
opn(USER_CONFIG);
59+
opn(USER_CONFIG_FILE);
5760
}
5861
}
5962

@@ -115,15 +118,20 @@ export class RunServer {
115118
}
116119
}
117120

118-
if (!fs.existsSync(USER_CONFIG)) {
121+
if (!fs.existsSync(USER_CONFIG_FILE)) {
119122
const configExample =
120123
path.resolve(`${__dirname}/../config/user-config.example.js`);
121-
shell.cp(configExample, USER_CONFIG);
124+
shell.cp(configExample, USER_CONFIG_FILE);
122125

123126
if (logger) {
124-
logger.info(`Copied user configuration to:\n${chalk.bold(USER_CONFIG)}\n`);
127+
logger.info(`Copied user configuration to:\n${chalk.bold(USER_CONFIG_FILE)}\n`);
125128
}
126129
}
130+
131+
USER_CONFIG =
132+
vm.runInThisContext(
133+
fs.readFileSync(USER_CONFIG_FILE).toString()
134+
)(require);
127135
}
128136

129137
/**
@@ -153,7 +161,7 @@ export class RunServer {
153161
* Start the Run Server on the provided port.
154162
*/
155163
public start() {
156-
(async() => {
164+
(async () => {
157165
const app = express();
158166
const server = http.createServer(app);
159167

@@ -199,7 +207,8 @@ export class RunServer {
199207
socket,
200208
port: this.port,
201209
token: this.token,
202-
logger: this.logger
210+
logger: this.logger,
211+
userConfig: USER_CONFIG
203212
});
204213

205214
exec.call();

src/models/exec-command.model.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ export interface ExecCommandModel {
2626
/**
2727
* The Winston Logger. (https://github.com/winstonjs/winston)
2828
*/
29-
logger: Logger
29+
logger: Logger,
30+
31+
/**
32+
* User configuration based on `~/.cacher/run-server.user-config.js`.
33+
*/
34+
userConfig: any
3035
}

0 commit comments

Comments
 (0)