Skip to content

Commit

Permalink
Feat: support project runner (#277)
Browse files Browse the repository at this point in the history
* feat: Add support for projectRunner

* fix: passed test cases

* refactor: Improve bundler.js logic for handling projectRunner apps

* feat: support projectPath Config in projectRunner

* Update .gitignore

---------

Co-authored-by: Shalitha Suranga <shalithasuranga@gmail.com>
  • Loading branch information
viralgupta and shalithasuranga authored Aug 19, 2024
1 parent 271e535 commit 5827fca
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
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
29 changes: 28 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 @@ -90,6 +99,24 @@ module.exports.bundleApp = async (isRelease, copyStorage) => {
process.exit(1);
}
}

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

if(fse.existsSync(projectRunnerConfig.buildPath)){
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 });
});

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...');
await zl.archiveFolder(`${buildDir}/${binaryName}`, `${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);
}
}

0 comments on commit 5827fca

Please sign in to comment.