Skip to content

Commit

Permalink
Merge pull request #6 from rule110-io/development
Browse files Browse the repository at this point in the history
v.1.1.0
  • Loading branch information
WizardOfCodez authored Apr 10, 2021
2 parents 8692bfd + 41e447c commit 6aa7987
Show file tree
Hide file tree
Showing 39 changed files with 12,851 additions and 18,913 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Editor config helps developers define and maintain consistent
# coding styles between different editors and IDEs
# EditorConfig is awesome: http://editorconfig.org
Expand Down
3 changes: 3 additions & 0 deletions .electron-nuxt/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("./check-engines");
process.env.NODE_ENV = "production";
require("./index");
93 changes: 93 additions & 0 deletions .electron-nuxt/check-engines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
This module cannot contain any external libraries!
*/

const { engines } = require("../package");

const RESET = "\x1b[0m";
const FG_RED = "\x1b[31m";

function checkNodeVersion() {
if (engines.node === undefined) return;
const requiredMinVersion = engines.node.replace(/[=<>]/g, "");
const installedVersion = process.versions.node;
if (compare(requiredMinVersion, installedVersion) === 1) {
console.log(FG_RED);
console.log(
`\tYou are running version v${installedVersion} of Node.js, which is not supported by Electron-nuxt.`
);
console.log(
`\tThe official Node.js version that is supported is ${requiredMinVersion} or greater.`
);
console.log(RESET);
console.log(
"\n\tPlease visit https://nodejs.org/en/ to find instructions on how to update Node.js.\n"
);

throw new Error("Invalid node version");
}
}

//https://github.com/yarnpkg/yarn/issues/5063
function disallowNpm() {
const execPath = process.env.npm_execpath;
if (!execPath.includes("yarn")) {
console.log(FG_RED);
console.log(`\tElectron-nuxt supports only Yarn package manager.`);
console.log(RESET);
console.log(
"\n\tPlease visit https://legacy.yarnpkg.com/en/docs/install to find instructions on how to install Yarn.\n"
);

throw new Error("Invalid package manager");
}
}

//https://stackoverflow.com/questions/6832596/how-to-compare-software-version-number-using-js-only-number
// Return 1 if a > b
// Return -1 if a < b
// Return 0 if a == b
function compare(a, b) {
if (a === b) {
return 0;
}

const a_components = a.split(".");
const b_components = b.split(".");

const len = Math.min(a_components.length, b_components.length);

// loop while the components are equal
for (let i = 0; i < len; i++) {
// A bigger than B
if (parseInt(a_components[i]) > parseInt(b_components[i])) {
return 1;
}

// B bigger than A
if (parseInt(a_components[i]) < parseInt(b_components[i])) {
return -1;
}
}

// If one's a prefix of the other, the longer one is greater.
if (a_components.length > b_components.length) {
return 1;
}

if (a_components.length < b_components.length) {
return -1;
}

// Otherwise they are the same.
return 0;
}

try {
checkNodeVersion();
disallowNpm();
// https://stackoverflow.com/questions/6398196/detect-if-called-through-require-or-directly-by-command-line
if (require.main === module) process.exit(0);
} catch (e) {
process.exit(1);
}
24 changes: 13 additions & 11 deletions .electron-nuxt/config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
const path = require('path')
const path = require("path");

const PROJECT_ROOT = path.join(__dirname, '..')
const SRC_DIR = path.join(PROJECT_ROOT, 'src')
const PROJECT_ROOT = path.join(__dirname, "..");
const SRC_DIR = path.join(PROJECT_ROOT, "src");

const config = {
ELECTRON_RELAUNCH_CODE: 250, // valid range in unix system: <1,255>
ELECTRON_INSPECTION_PORT: 5858,
SERVER_PORT: 9080,
SERVER_HOST: 'http://localhost',
SERVER_HOST: "http://localhost",

PROJECT_ROOT,
SRC_DIR,
MAIN_PROCESS_DIR: path.join(SRC_DIR, 'main'),
RENDERER_PROCESS_DIR: path.join(SRC_DIR, 'renderer'),
RESOURCES_DIR: path.join(SRC_DIR, 'resources'),
DIST_DIR: path.join(PROJECT_ROOT, 'dist'),
BUILD_DIR: path.join(PROJECT_ROOT, 'build')
}
MAIN_PROCESS_DIR: path.join(SRC_DIR, "main"),
RENDERER_PROCESS_DIR: path.join(SRC_DIR, "renderer"),
RESOURCES_DIR: path.join(SRC_DIR, "extraResources"),
DIST_DIR: path.join(PROJECT_ROOT, "dist"),
BUILD_DIR: path.join(PROJECT_ROOT, "build"),

module.exports = Object.freeze(config)
DISABLE_BABEL_LOADER: false, // experimental
};

module.exports = Object.freeze(config);
3 changes: 3 additions & 0 deletions .electron-nuxt/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("./check-engines");
process.env.NODE_ENV = "development";
require("./index");
84 changes: 50 additions & 34 deletions .electron-nuxt/index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,75 @@
const path = require("path");
const webpack = require("webpack");
const electron = require("electron");

const path = require('path')
const webpack = require('webpack')
const electron = require('electron')
const { Pipeline, Logger } = require("@xpda-dev/core");
const { ElectronLauncher } = require("@xpda-dev/electron-launcher");
const { ElectronBuilder } = require("@xpda-dev/electron-builder");
const { Webpack } = require("@xpda-dev/webpack-step");
const resourcesPath = require("./resources-path-provider");
const {
DIST_DIR,
MAIN_PROCESS_DIR,
SERVER_HOST,
SERVER_PORT,
} = require("./config");
const NuxtApp = require("./renderer/NuxtApp");

const { Pipeline, Logger } = require('@xpda-dev/core')
const { ElectronLauncher } = require('@xpda-dev/electron-launcher')
const { ElectronBuilder } = require('@xpda-dev/electron-builder')
const { Webpack } = require('@xpda-dev/webpack-step')
const resourcesPath = require('./resources-path-provider')
const { DIST_DIR, MAIN_PROCESS_DIR, SERVER_HOST, SERVER_PORT } = require('./config')
const NuxtApp = require('./renderer/NuxtApp')
const isDev = process.env.NODE_ENV === "development";

const isDev = process.env.NODE_ENV === 'development'
const electronLogger = new Logger("Electron", "teal");
electronLogger.ignore((text) =>
text.includes("nhdogjmejiglipccpnnnanhbledajbpd")
); // Clear vue devtools errors

const launcher = new ElectronLauncher({
logger: electronLogger,
electronPath: electron,
entryFile: path.join(DIST_DIR, 'main/index.js')
})
entryFile: path.join(DIST_DIR, "main/index.js"),
});

function hasConfigArgument(array) {
for (const el of array) if (el === "--config" || el === "-c") return true;
return false;
}
const argumentsArray = process.argv.slice(2);
if (!hasConfigArgument(argumentsArray))
argumentsArray.push("--config", "builder.config.js");

const builder = new ElectronBuilder({
cliOptions: {
config: path.join(__dirname, '../builder.config.js')
}
})
processArgv: argumentsArray,
});

const webpackConfig = Webpack.getBaseConfig({
entry: isDev
? path.join(MAIN_PROCESS_DIR, 'index.dev.js')
: path.join(MAIN_PROCESS_DIR, 'index.js'),
? path.join(MAIN_PROCESS_DIR, "boot/index.dev.js")
: path.join(MAIN_PROCESS_DIR, "boot/index.prod.js"),
output: {
filename: 'index.js',
path: path.join(DIST_DIR, 'main')
filename: "index.js",
path: path.join(DIST_DIR, "main"),
},
plugins: [
new webpack.DefinePlugin({
INCLUDE_RESOURCES_PATH: resourcesPath.mainProcess(),
'process.env.DEV_SERVER_URL': `'${SERVER_HOST}:${SERVER_PORT}'`
})
]
})
"process.resourcesPath": resourcesPath.mainProcess(),
"process.env.DEV_SERVER_URL": `'${SERVER_HOST}:${SERVER_PORT}'`,
}),
],
});

const webpackMain = new Webpack({
logger: new Logger('Main', 'olive'),
logger: new Logger("Main", "olive"),
webpackConfig,
launcher // need to restart launcher after compilation
})
launcher, // need to restart launcher after compilation
});

const nuxt = new NuxtApp(new Logger('Nuxt', 'green'))
const nuxt = new NuxtApp(new Logger("Nuxt", "green"));

const pipe = new Pipeline({
title: 'Electron-nuxt',
title: "Electron-nuxt",
isDevelopment: isDev,
steps: [webpackMain, nuxt],
launcher,
builder
})
builder,
});

pipe.run()
pipe.run();
52 changes: 28 additions & 24 deletions .electron-nuxt/renderer/NuxtApp.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
const path = require('path')
const { fork } = require('child_process')
const { utils } = require('@xpda-dev/core')
const { killWithAllSubProcess } = utils
const path = require("path");
const { fork } = require("child_process");
const { utils } = require("@xpda-dev/core");
const { killWithAllSubProcess } = utils;

const NUXT_PROCESS_PATH = path.join(__dirname, 'nuxt-process.js')
const NUXT_PROCESS_PATH = path.join(__dirname, "nuxt-process.js");

/**
* @implements {IStep}
*/
class NuxtApp {
constructor (logger) {
this.logger = logger
constructor(logger) {
this.logger = logger;
}

async build (isDev) {
this.nuxtProcess = fork(NUXT_PROCESS_PATH, { silent: true })
this.redirectStdout()
async build(isDev) {
this.nuxtProcess = fork(NUXT_PROCESS_PATH, { silent: true });
this.redirectStdout();
return new Promise((resolve, reject) => {
this.nuxtProcess.send({ action: 'build', target: isDev ? 'development' : 'production' })
this.nuxtProcess.once('message', ({ status, err }) => {
if (status === 'ok') resolve()
else reject(err)
})
})
this.nuxtProcess.send({
action: "build",
target: isDev ? "development" : "production",
});
this.nuxtProcess.once("message", ({ status, err }) => {
if (status === "ok") resolve();
else reject(err);
});
});
}

redirectStdout () {
this.nuxtProcess.stdout.pipe(this.logger.stdout)
this.nuxtProcess.stderr.pipe(this.logger.stderr)
redirectStdout() {
this.nuxtProcess.stdout.pipe(this.logger.stdout);
this.nuxtProcess.stderr.pipe(this.logger.stderr);
}

async terminate () {
this.nuxtProcess.kill()
if (this.nuxtProcess && !this.nuxtProcess.killed) killWithAllSubProcess(this.nuxtProcess.pid)
this.nuxtProcess = null
async terminate() {
this.nuxtProcess.kill();
if (this.nuxtProcess && !this.nuxtProcess.killed)
killWithAllSubProcess(this.nuxtProcess.pid);
this.nuxtProcess = null;
}
}

module.exports = NuxtApp
module.exports = NuxtApp;
Loading

0 comments on commit 6aa7987

Please sign in to comment.