Created
May 1, 2019 22:50
-
-
Save kcmr/e10c9dc0129bf14ae4ec755be0de597e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const browserSync = require('browser-sync').create(); | |
const portfinder = require('portfinder'); | |
const {spawn} = require('child_process'); | |
const COMPONENT_PATH = process.env.npm_package_name; | |
const browserSyncConfig = { | |
open: false, | |
notify: false, | |
ghostMode: false, | |
reloadOnRestart: true, | |
ui: false, | |
proxy: `localhost:{{port}}/components/${COMPONENT_PATH}/`, | |
files: ['**/*.{js,html}'] | |
}; | |
const startServer = (port) => { | |
const polymerCliParams = ['serve', '--npm', '-p', port]; | |
const polymerServe = spawn('polymer', polymerCliParams); | |
polymerServe.stdout.on('data', (data) => { | |
console.log(`${data}`); | |
browserSync.init(getServerConfig(port)); | |
}); | |
polymerServe.stderr.on('data', (data) => { | |
console.log(`Error: ${data}`); | |
}); | |
polymerServe.on('close', (code) => { | |
console.log(`child process exited with code ${code}`); | |
}); | |
}; | |
const getServerConfig = (port) => { | |
return JSON.parse( | |
JSON.stringify(browserSyncConfig) | |
.replace(/{{port}}/g, port) | |
); | |
}; | |
const serve = () => { | |
portfinder.getPortPromise() | |
.then(startServer) | |
.catch((error) => console.error(error)); | |
}; | |
serve(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment