Created
March 10, 2020 01:53
-
-
Save lacymorrow/d843fa4087308799627dd8ee41dd0efb to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env node | |
'use strict'; | |
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | |
var fs = _interopDefault(require('fs')); | |
var arg = _interopDefault(require('arg')); | |
var chalk = _interopDefault(require('chalk')); | |
var spawn = _interopDefault(require('cross-spawn')); | |
var delay = _interopDefault(require('delay')); | |
var webpack = _interopDefault(require('webpack')); | |
var path = _interopDefault(require('path')); | |
var webpackMerge = require('webpack-merge'); | |
function _defineProperty(obj, key, value) { | |
if (key in obj) { | |
Object.defineProperty(obj, key, { | |
value: value, | |
enumerable: true, | |
configurable: true, | |
writable: true | |
}); | |
} else { | |
obj[key] = value; | |
} | |
return obj; | |
} | |
function ownKeys(object, enumerableOnly) { | |
var keys = Object.keys(object); | |
if (Object.getOwnPropertySymbols) { | |
var symbols = Object.getOwnPropertySymbols(object); | |
if (enumerableOnly) symbols = symbols.filter(function (sym) { | |
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | |
}); | |
keys.push.apply(keys, symbols); | |
} | |
return keys; | |
} | |
function _objectSpread2(target) { | |
for (var i = 1; i < arguments.length; i++) { | |
var source = arguments[i] != null ? arguments[i] : {}; | |
if (i % 2) { | |
ownKeys(Object(source), true).forEach(function (key) { | |
_defineProperty(target, key, source[key]); | |
}); | |
} else if (Object.getOwnPropertyDescriptors) { | |
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); | |
} else { | |
ownKeys(Object(source)).forEach(function (key) { | |
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | |
}); | |
} | |
} | |
return target; | |
} | |
const cwd = process.cwd(); | |
const externals = require(path.join(cwd, 'package.json')).dependencies; | |
var configure = (env => ({ | |
mode: env, | |
target: 'electron-main', | |
node: { | |
__dirname: false, | |
__filename: false | |
}, | |
externals: [...Object.keys(externals || {})], | |
devtool: 'source-map', | |
resolve: { | |
extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'], | |
modules: [path.join(cwd, 'app'), 'node_modules'] | |
}, | |
output: { | |
libraryTarget: 'commonjs2' | |
}, | |
module: { | |
rules: [{ | |
test: /\.(js|ts)x?$/, | |
use: { | |
loader: 'babel-loader', | |
options: { | |
cacheDirectory: true, | |
presets: ['@babel/preset-typescript'] | |
} | |
}, | |
exclude: [/node_modules/, path.join(cwd, 'renderer')] | |
}] | |
}, | |
plugins: [new webpack.EnvironmentPlugin({ | |
NODE_ENV: env | |
})] | |
})); | |
const cwd$1 = process.cwd(); | |
const ext = fs.existsSync(path.join(cwd$1, 'tsconfig.json')) ? '.ts' : '.js'; | |
const getNextronConfig = () => { | |
const nextronConfigPath = path.join(cwd$1, 'nextron.config.js'); | |
if (fs.existsSync(nextronConfigPath)) { | |
return require(nextronConfigPath); | |
} else { | |
return {}; | |
} | |
}; | |
const getWebpackConfig = env => { | |
const { | |
mainSrcDir, | |
webpack | |
} = getNextronConfig(); | |
const userConfig = webpackMerge.smart(configure(env), { | |
entry: { | |
background: path.join(cwd$1, mainSrcDir || 'main', `background${ext}`) | |
}, | |
output: { | |
filename: '[name].js', | |
path: path.join(cwd$1, 'app') | |
} | |
}); | |
const userWebpack = webpack || {}; | |
if (typeof userWebpack === 'function') { | |
return userWebpack(userConfig, env); | |
} else { | |
return webpackMerge.smart(userConfig, userWebpack); | |
} | |
}; | |
const args = arg({ | |
'--help': Boolean, | |
'--version': Boolean, | |
'--port': Number, | |
'--run-only': Boolean, | |
'--custom-server': String, | |
'-h': '--help', | |
'-v': '--version', | |
'-p': '--port', | |
'-r': '--run', | |
'-c': '--custom-server' | |
}); | |
if (args['--help']) { | |
console.log(chalk` | |
{bold.cyan nextron dev} - Starts the nextron application in development mode | |
{bold USAGE} | |
{bold $} {cyan nextron dev} --help | |
{bold $} {cyan nextron dev} | |
{bold OPTIONS} | |
--help, -h shows this help message | |
--version, -v displays the current version of nextron | |
`); | |
process.exit(0); | |
} | |
const rendererPort = args['--port'] || 8888; | |
const spawnOptions = { | |
cwd: process.cwd(), | |
stdio: 'inherit' | |
}; | |
async function dev() { | |
const { | |
rendererSrcDir | |
} = getNextronConfig(); | |
let firstCompile = true; | |
let watching; | |
let mainProcess; | |
let rendererProcess; | |
const startMainProcess = () => { | |
mainProcess = spawn('electron', ['.', `${rendererPort}`], _objectSpread2({ | |
detached: true | |
}, spawnOptions)); | |
mainProcess.unref(); | |
}; | |
const startRendererProcess = () => { | |
let child; | |
if (args['--custom-server']) { | |
if (fs.existsSync('nodemon.json')) { | |
child = spawn('nodemon', [args['--custom-server']], spawnOptions); | |
} else { | |
child = spawn('node', [args['--custom-server']], spawnOptions); | |
} | |
} else { | |
child = spawn('next', ['-p', rendererPort, rendererSrcDir || 'renderer'], spawnOptions); | |
} | |
child.on('close', () => { | |
process.exit(0); | |
}); | |
return child; | |
}; | |
const killWholeProcess = () => { | |
if (watching) { | |
watching.close(() => {}); | |
} | |
if (mainProcess) { | |
mainProcess.kill(); | |
} | |
if (rendererProcess) { | |
rendererProcess.kill(); | |
} | |
}; | |
const webpackCallback = async (err, stats) => { | |
if (err) { | |
console.error(err.stack || err); | |
if (err.details) { | |
console.error(err.details); | |
} | |
} | |
const info = stats.toJson('errors-warnings'); | |
if (stats.hasErrors()) { | |
console.error(info.errors); | |
} | |
if (stats.hasWarnings()) { | |
console.warn(info.warnings); | |
} | |
if (firstCompile) { | |
firstCompile = false; | |
} | |
if (!err && !stats.hasErrors()) { | |
if (!firstCompile) { | |
if (mainProcess) { | |
mainProcess.kill(); | |
} | |
} | |
startMainProcess(); | |
} | |
}; | |
process.on('SIGINT', killWholeProcess); | |
process.on('SIGTERM', killWholeProcess); | |
process.on('exit', killWholeProcess); | |
rendererProcess = startRendererProcess(); // wait until renderer process is ready | |
await delay(8000); | |
const compiler = webpack(getWebpackConfig('development')); | |
if (args['--run-only']) { | |
watching = compiler.watch({}, webpackCallback); | |
} else { | |
compiler.run(webpackCallback); | |
} | |
} | |
dev(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment