Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: support project runner #277

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const runner = require('../modules/runner');
const utils = require('../utils');
const config = require('../modules/config');
const frontendlib = require('../modules/frontendlib');
const projectRunner = require('../modules/projectRunner');

module.exports.register = (program) => {
program
Expand All @@ -14,6 +15,17 @@ module.exports.register = (program) => {
.action(async (command) => {
utils.checkCurrentProject();
let configObj = config.get();

if(configObj.cli && configObj.cli.projectRunner) {
try {
await projectRunner.runApp();
process.exit(0);
} catch (error) {
utils.log(error);
process.exit(1);
}
}

let containsFrontendLibApp = frontendlib.containsFrontendLibApp();
let argsOpt = "";

Expand Down
30 changes: 29 additions & 1 deletion src/modules/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const asar = require('@electron/asar');
const config = require('./config');
const constants = require('../constants');
const frontendlib = require('./frontendlib');
const projectRunner = require('./projectRunner');
const utils = require('../utils');
const path = require('path');

async function createAsarFile() {
utils.log(`Generating ${constants.files.resourceFile}...`);
Expand Down Expand Up @@ -57,19 +59,26 @@ module.exports.bundleApp = async (isRelease, copyStorage) => {
let configObj = config.get();
let binaryName = configObj.cli.binaryName;
const buildDir = configObj.cli.distributionPath ? utils.trimPath(configObj.cli.distributionPath) : 'dist';
const projectRunnerConfig = configObj.cli ? configObj.cli.projectRunner : undefined;

try {
if (frontendlib.containsFrontendLibApp()) {
await frontendlib.runCommand('buildCommand');
}

if(projectRunner.containsRunnerApp()) {
if(projectRunnerConfig.buildCommand){
await projectRunner.runCommand('buildCommand', true);
}
}

await createAsarFile();
utils.log('Copying binaries...');

for (let platform in constants.files.binaries) {
for (let arch in constants.files.binaries[platform]) {
let originalBinaryFile = constants.files.binaries[platform][arch];
let destinationBinaryFile = originalBinaryFile.replace('neutralino', binaryName);
let destinationBinaryFile = projectRunner.containsRunnerApp() ? originalBinaryFile : originalBinaryFile.replace('neutralino', binaryName);
if (fse.existsSync(`bin/${originalBinaryFile}`)) {
fse.copySync(`bin/${originalBinaryFile}`, `${buildDir}/${binaryName}/${destinationBinaryFile}`);
}
Expand All @@ -91,6 +100,25 @@ module.exports.bundleApp = async (isRelease, copyStorage) => {
}
}

if(projectRunner.containsRunnerApp() && projectRunnerConfig && projectRunnerConfig.buildPath){
utils.log('Copying Project Runner build...');

if(fse.existsSync(projectRunnerConfig.buildPath)){
// copy contents from builddir/appname -> buildDir/appname/bin
fse.mkdirSync(`${buildDir}/${binaryName}/bin`, { recursive: true });
fse.readdirSync(`${buildDir}/${binaryName}`, { withFileTypes: true, recursive: true }).forEach(file => {
if(file.isDirectory() && file.name == "bin") return;
const sourcePath = path.join(`${buildDir}/${binaryName}`, file.name);
fse.moveSync(sourcePath, `${buildDir}/${binaryName}/bin/${file.name}`, { overwrite: true });
});

// copy projectRunner buildPath -> buildDir/appname
fse.copySync(projectRunnerConfig.buildPath, `${buildDir}/${binaryName}/`);
} else {
utils.error('Unable to copy projectRunner data from the build directory. Please check if the directory exists');
}
}

if (isRelease) {
utils.log('Making app bundle ZIP file...');
let output = fs.createWriteStream(`${buildDir}/${binaryName}-release.zip`);
Expand Down
5 changes: 5 additions & 0 deletions src/modules/creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fse = require('fs-extra');
const config = require('../modules/config');
const downloader = require('./downloader');
const frontendlib = require('../modules/frontendlib');
const projectRunner = require('../modules/projectRunner');
const utils = require('../utils');

module.exports.createApp = async (binaryName, template) => {
Expand Down Expand Up @@ -47,6 +48,10 @@ module.exports.createApp = async (binaryName, template) => {
await frontendlib.runCommand('initCommand');
}

if(projectRunner.containsRunnerApp()) {
await projectRunner.runCommand('initCommand');
}

console.log('-------');
utils.log(`Enter 'cd ${binaryName} && neu run' to run your application.`);
}
2 changes: 1 addition & 1 deletion src/modules/frontendlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module.exports.runCommand = (commandKey) => {
utils.log(`Running ${commandKey}: ${cmd}...`);
const proc = spawnCommand(cmd, { stdio: 'inherit', cwd: projectPath });
proc.on('exit', (code) => {
utils.log(`${commandKey} completed with exit code: ${code}`);
utils.log(`FrontendLib: ${commandKey} completed with exit code: ${code}`);
resolve();
});
});
Expand Down
52 changes: 52 additions & 0 deletions src/modules/projectRunner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const process = require('process');
const spawnCommand = require('spawn-command');
const config = require('./config');
const utils = require('../utils');

module.exports.containsRunnerApp = () => {
let configObj = config.get();
return !!(configObj.cli && configObj.cli.projectRunner);
}

module.exports.runCommand = async (commandKey) => {
let configObj = config.get();
let projectRunner = configObj.cli ? configObj.cli.projectRunner : undefined;

if (projectRunner && projectRunner.projectPath && projectRunner[commandKey]) {

return new Promise((resolve, _reject) => {
let projectPath = utils.trimPath(projectRunner.projectPath);
let cmd = projectRunner[commandKey];

utils.log(`Running ${commandKey}: ${cmd}...`);

const proc = spawnCommand(cmd, { stdio: 'inherit', cwd: projectPath});
proc.on('exit', (code) => {
utils.log(`Project Runner: ${commandKey} completed with exit code: ${code}`);
resolve();
});
});
}
}

module.exports.runApp = async () => {
let configObj = config.get();
let projectRunner = configObj.cli ? configObj.cli.projectRunner : undefined;

if (projectRunner && projectRunner.projectPath && projectRunner.devCommand) {
return new Promise((resolve, _reject) => {
let projectPath = utils.trimPath(projectRunner.projectPath);
let cmd = projectRunner.devCommand;

utils.log(`Running Project Runner: ${cmd} ...`);
const proc = spawnCommand(cmd, { stdio: 'inherit', cwd: projectPath});
proc.on('exit', (code) => {
utils.log(`Project stopped with exit code: ${code}`);
resolve();
});
});
} else {
utils.error("Config file does not contain projectRunner.devCommand to run the project!!!");
process.exit(1);
}
}