Skip to content

Commit 1291991

Browse files
committed
Run external user config file in context.
1 parent 5ee5edc commit 1291991

File tree

3 files changed

+57
-51
lines changed

3 files changed

+57
-51
lines changed

src/config/user-config.example.js

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,61 @@
77
* for complete documentation.
88
*/
99

10-
// Use any standard Node library, along with opn (https://www.npmjs.com/package/opn) and
11-
// any globally-installed Node packages.
12-
const path = require('path');
13-
const childProcess = require('child_process');
14-
const opn = require('opn');
15-
const os = require('os');
10+
((require) => {
11+
// Use any standard Node library, along with opn (https://www.npmjs.com/package/opn) and
12+
// any globally-installed Node packages.
13+
const path = require('path');
14+
const childProcess = require('child_process');
15+
const opn = require('opn');
16+
const os = require('os');
1617

17-
module.exports = {
18-
rules: [
19-
/* EXAMPLES:
18+
return {
19+
rules: [
20+
/*
2021
21-
// Run script for new file extension
22-
{
23-
pattern: '\.awesome$',
24-
run: (command, filepath) => `awesome-compiler "${filepath}"`
25-
},
22+
// Run script for new file extension
23+
{
24+
pattern: '\.awesome$',
25+
run: (command, filepath) => `awesome-compiler "${filepath}"`
26+
},
2627
27-
// ------------------------------------------------------
28+
// ------------------------------------------------------
2829
29-
// Rule to compile .md (Markdown) file into HTML, then display in default browser.
30-
// Requires first running `npm -g markdown` (https://github.com/evilstreak/markdown-js)
31-
{
32-
pattern: '\.md$',
33-
run: (command, filepath, args) => {
34-
const outputHtmlFile = `${args.runDir}/${args.baseFilename}.html`;
35-
childProcess.execSync(`md2html ${filepath} > ${outputHtmlFile}`);
36-
opn(outputHtmlFile);
30+
// Rule to compile .md (Markdown) file into HTML, then display in default browser.
31+
// Requires first running `npm -g markdown` (https://github.com/evilstreak/markdown-js)
32+
{
33+
pattern: '\.md$',
34+
run: (command, filepath, args) => {
35+
const outputHtmlFile = `${args.runDir}/${args.baseFilename}.html`;
36+
childProcess.execSync(`md2html ${filepath} > ${outputHtmlFile}`);
37+
opn(outputHtmlFile);
3738
38-
// Must return a shell command
39-
return `echo "Generated '${outputHtmlFile}'" and opened in default browser`;
40-
}
41-
},
39+
// Must return a shell command
40+
return `echo "Generated '${outputHtmlFile}'" and opened in default browser`;
41+
}
42+
},
4243
43-
// ------------------------------------------------------
44+
// ------------------------------------------------------
4445
45-
// Filter file by both filename and content. Run pre-execution command.
46-
// Put "//nvm: v6.10.1" at top of snippet file to use a specific Node version for execution
47-
{
48-
pattern: (command) => {
49-
return /\.js$/.test(command.file.filename)
50-
&& command.file.content.indexOf('//nvm:') >= 0;
51-
},
52-
run: (command, filepath) => {
53-
let nvmVersion = command.file.content.match(/(\/\/nvm: v(.+))/)[0]
54-
.replace('//nvm: ', '');
46+
// Filter file by both filename and content. Run pre-execution command.
47+
// Put "//nvm: v6.10.1" at top of snippet file to use a specific Node version for execution
48+
{
49+
pattern: (command) => {
50+
return /\.js$/.test(command.file.filename)
51+
&& command.file.content.indexOf('//nvm:') >= 0;
52+
},
53+
run: (command, filepath) => {
54+
let nvmVersion = command.file.content.match(/(\/\/nvm: v(.+))/)[0]
55+
.replace('//nvm: ', '');
5556
56-
return `export NVM_DIR=~/.nvm ` +
57-
`&& source ~/.nvm/nvm.sh` +
58-
`&& nvm use ${nvmVersion} ` +
59-
`&& node "${filepath}"`;
57+
return `export NVM_DIR=~/.nvm ` +
58+
`&& source ~/.nvm/nvm.sh` +
59+
`&& nvm use ${nvmVersion} ` +
60+
`&& node "${filepath}"`;
61+
}
6062
}
61-
}
6263
63-
*/
64-
]
65-
};
64+
*/
65+
]
66+
};
67+
});

src/lib/exec-command.ts

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

55
const fs = require('fs');
66
const path = require('path');
7+
const vm = require('vm');
78

89
import * as _ from 'lodash';
910
import * as os from 'os';
@@ -13,6 +14,7 @@ import chalk from 'chalk'
1314
const pretty = require('js-object-pretty-print').pretty;
1415

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

1719
export class ExecCommand {
1820
/**
@@ -50,7 +52,12 @@ export class ExecCommand {
5052
this.logger = args.logger;
5153

5254
const defaultConfig = require('../config/config.default.js');
53-
const userConfig = require('../config/config.user.js');
55+
56+
// Read userConfig from external file
57+
const configUserScript = fs.readFileSync(USER_CONFIG).toString();
58+
const userConfig = vm.runInThisContext(
59+
configUserScript
60+
)(require);
5461

5562
// Prepend user config rules to default config
5663
const config = Object.assign({}, defaultConfig);

src/lib/server.ts

Lines changed: 1 addition & 4 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+
67
const shell = require('shelljs');
78
const socketio = require('socket.io');
89
const opn = require('opn');
@@ -123,10 +124,6 @@ export class RunServer {
123124
logger.info(`Copied user configuration to:\n${chalk.bold(USER_CONFIG)}\n`);
124125
}
125126
}
126-
127-
// Copy user config into this dir
128-
const configUser = path.resolve(`${__dirname}/../config/config.user.js`);
129-
shell.cp(USER_CONFIG, configUser);
130127
}
131128

132129
/**

0 commit comments

Comments
 (0)