diff --git a/.nycrc b/.nycrc index ab0a34c..1f2c2b6 100644 --- a/.nycrc +++ b/.nycrc @@ -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 } \ No newline at end of file diff --git a/bin/nsc-proxy-server.js b/bin/nsc-proxy-server.js index 05c3018..f6e7201 100644 --- a/bin/nsc-proxy-server.js +++ b/bin/nsc-proxy-server.js @@ -9,7 +9,8 @@ const config = require('../src/config'); commander .alias('nsc proxy-server') - .option('--local ', 'Load a snippet for a given localization') + .option('-p, --package ', 'Load a snippet for a given localization') + .option('-c, --concurrently ', 'Load a snippet for a given localization') .parse(process.argv); runInteractive(commander); @@ -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 - } ]; + }]; } @@ -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)); diff --git a/package.json b/package.json index b165ed7..c7270b1 100644 --- a/package.json +++ b/package.json @@ -86,4 +86,4 @@ "release": { "branch": "production" } -} \ No newline at end of file +} diff --git a/src/proxy-server.js b/src/proxy-server.js index d76dcf8..083a7ff 100644 --- a/src/proxy-server.js +++ b/src/proxy-server.js @@ -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({ @@ -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 }, @@ -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; @@ -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 ` - - + + `; } diff --git a/test/unit/proxy-server.spec.js b/test/unit/proxy-server.spec.js index 2a10676..ae8bcd3 100644 --- a/test/unit/proxy-server.spec.js +++ b/test/unit/proxy-server.spec.js @@ -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'); @@ -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('', ''); + this.result = this.browserSyncStub.init.getCall(0).args[0].snippetOptions.rule.fn('', ''); }); 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' }, @@ -44,7 +51,7 @@ describe('proxyServer', () => { staticPath ], serveStaticOptions: { - extensions: [ 'html' ] // pretty urls + extensions: ['html'] // pretty urls }, snippetOptions: Sinon.match.any }));