Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
feat(proxy): Add proxy options --concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Apr 16, 2018
1 parent f9e995e commit 308516c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
],
"check-coverage": true,
"lines": 96.35,
"statements": 96.35,
"statements": 96.84,
"functions": 97.06,
"branches": 81.82,
"branches": 80.77,
"all": true
}
41 changes: 22 additions & 19 deletions bin/nsc-proxy-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const config = require('../src/config');

commander
.alias('nsc proxy-server')
.option('--local <locale>', 'Load a snippet for a given localization')
.option('-p, --package <package>', 'Load a snippet for a given localization')
.option('-c, --concurrently <npm task>', 'Load a snippet for a given localization')
.parse(process.argv);

runInteractive(commander);
Expand All @@ -18,30 +19,31 @@ function runInteractive(options) {
let questions;

if (config.proxyUrls.length) {
questions = [ {
type: 'list',
name: 'proxyUrl',
message: 'Which url do you want to proxify ? ',
choices: config.proxyUrls.concat([ new inquirer.Separator(), 'Enter new url' ]),
required: true
},
{
type: 'input',
name: 'proxyUrl',
when: (answers) => answers.proxyUrl === 'Enter new url',
message: 'Which url do you want to proxify ? ',
default: config.siteUrl,
required: true
}
questions = [
{
type: 'list',
name: 'proxyUrl',
message: 'Which url do you want to proxify ? ',
choices: config.proxyUrls.concat([new inquirer.Separator(), 'Enter new url']),
required: true
},
{
type: 'input',
name: 'proxyUrl',
when: (answers) => answers.proxyUrl === 'Enter new url',
message: 'Which url do you want to proxify ? ',
default: config.siteUrl,
required: true
}
];
} else {
questions = [ {
questions = [{
type: 'input',
name: 'proxyUrl',
message: 'Which url do you want to proxify ? ',
default: config.siteUrl,
required: true
} ];
}];
}


Expand All @@ -52,7 +54,8 @@ function runInteractive(options) {

proxyServer({
url: answers.proxyUrl,
local: options.local
package: options.package,
concurrently: options.concurrently
});
})
.catch(er => log.error(er));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@
"release": {
"branch": "production"
}
}
}
25 changes: 16 additions & 9 deletions src/proxy-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const log = require('fancy-log');
const browserSync = require('browser-sync');
const config = require('./config');
const formatPath = require('./format-path');
const execa = require('execa');

module.exports = (options) => {
options = Object.assign({
Expand All @@ -14,14 +15,20 @@ module.exports = (options) => {
}, options);

const {
url, port, logLevel, local
url, port, logLevel
} = options;

const staticPath = formatPath(path.join(config.get('instanceRoot'), config.get('websiteRoot')));

if (options.concurrently) {
execa.shell(options.concurrently, {
stdio: ['inherit', 'inherit', 'inherit']
});
}

return browserSync.create().init({
open: false,
files: [ `${staticPath}/**/*.{js,css}` ],
files: [`${staticPath}/**/*.{js,css}`],
proxy: {
target: url
},
Expand All @@ -31,15 +38,15 @@ module.exports = (options) => {
staticPath
],
serveStaticOptions: {
extensions: [ 'html' ] // pretty urls
extensions: ['html'] // pretty urls
},
snippetOptions: {
// Provide a custom Regex for inserting the snippet.
rule: {
match: /<\/body>/i,
fn: (snippet, match) => {
if (local) {
snippet += snippetLocalization(options);
if (options.package) {
snippet += snippetPackage(options);
}

return snippet + match;
Expand All @@ -49,10 +56,10 @@ module.exports = (options) => {
});
};

function snippetLocalization(options) {
log.info(chalk.green('[info] Load snippet localization =>'), options.local);
function snippetPackage(options) {
log.info(chalk.green('[info] Load snippet localization =>'), options.package);
return `
<link rel="stylesheet" type="text/css" href="/themes/${options.local}/bundle.css">
<script type="text/javascript" src="/themes/${options.local}/bundle.js"></script>
<link rel="stylesheet" type="text/css" href="/themes/${options.package}/bundle.css">
<script type="text/javascript" src="/themes/${options.package}/bundle.js"></script>
`;
}
17 changes: 12 additions & 5 deletions test/unit/proxy-server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
} = require('../tools');
const config = require('../../src/config');
const path = require('path');
const execa = require('execa');
const formatPath = require('../../src/format-path');
const proxyServer = require('../../src/proxy-server');

Expand All @@ -16,25 +17,31 @@ describe('proxyServer', () => {
};

this.browserSyncCreateStub = Sinon.stub(browserSync, 'create').returns(this.browserSyncStub);
this.shellStub = Sinon.stub(execa, 'shell');

proxyServer({
url: 'http://host',
port: 8080,
local: 'FR'
package: 'FR',
concurrently: 'npm run test'
});

this.result = this.browserSyncStub.init.getCall(0).args[ 0 ].snippetOptions.rule.fn('</body>', '</html>');
this.result = this.browserSyncStub.init.getCall(0).args[0].snippetOptions.rule.fn('</body>', '</html>');
});
after(() => {
this.browserSyncCreateStub.restore();
this.shellStub.restore();
});
it('should have been called', () => {
it('should call browserSync', () => {
this.browserSyncCreateStub.should.have.been.calledWithExactly();
});
it('should call execa.shell', () => {
this.shellStub.should.have.been.calledWithExactly('npm run test', { stdio: ['inherit', 'inherit', 'inherit'] });
});
it('should have the correct params', () => {
this.browserSyncStub.init.should.have.been.calledWithExactly(Sinon.match({
open: false,
files: [ `${staticPath}/**/*.{js,css}` ],
files: [`${staticPath}/**/*.{js,css}`],
proxy: {
target: 'http://host'
},
Expand All @@ -44,7 +51,7 @@ describe('proxyServer', () => {
staticPath
],
serveStaticOptions: {
extensions: [ 'html' ] // pretty urls
extensions: ['html'] // pretty urls
},
snippetOptions: Sinon.match.any
}));
Expand Down

0 comments on commit 308516c

Please sign in to comment.