Skip to content

Commit

Permalink
Fix packaging app and compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiyozz committed Feb 19, 2019
1 parent 933b452 commit 26f2c26
Show file tree
Hide file tree
Showing 10 changed files with 905 additions and 983 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-destructuring"]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
out
packaged
7 changes: 7 additions & 0 deletions electron-sources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": {
"src/index.js": "out/index.js",
"src/main-process/app-compiler.js": "out/main-process/app-compiler.js",
"src/main-process/gamepath-util.js": "out/main-process/gamepath-util.js"
}
}
97 changes: 97 additions & 0 deletions package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const { exec } = require('child_process')
const packager = require('electron-packager')
const path = require('path')
const fs = require('fs-extra')
const rimraf = require('rimraf')
const escape = require('escape-string-regexp')
const colors = require('colors/safe')
const babel = require('@babel/core')

const outDir = path.join(__dirname, 'out')
const srcDir = path.join(__dirname, 'src')
const appDir = path.join(srcDir, 'app')
const packagedDir = path.join(__dirname, 'packaged')
const angularAppDir = path.join(appDir, 'papyrus-compiler-app')
const packageJsonFile = path.join(__dirname, 'package.json')
const packageLockJsonFile = path.join(__dirname, 'yarn.lock')
const packageJsonFileOutDir = path.join(outDir, 'package.json')
const packageLockJsonFileOutDir = path.join(outDir, 'yarn.lock')
const electronSourcesFile = path.join(__dirname, 'electron-sources.json')

function execAsync(...args) {
return new Promise((resolve, reject) => {
exec(...args, (err, stdout) => {
if (err) {
reject(err)

return
}

resolve(stdout)
})
})
}

function filterFilesNotToCopy(src, dest) {
const regex = new RegExp('^.*node_modules.*$')
const regexApp = new RegExp('^' + escape(appDir) + '$')

if (!regex.test(src) && !regexApp.test(src)) {
return true;
}

return false;
}

async function compileElectronSource() {
//const { code: codeIndex } = await babel.transformFileAsync(path.join(srcDir, 'index.js'))
//await fs.writeFile(path.join(outDir, 'index.js'), codeIndex)
const { files } = await fs.readJson(electronSourcesFile)

const iterable = Object.entries(files).map(objArray => {
return objArray.map(obj => path.join(__dirname, obj))
})

for (const [src, dest] of iterable) {
const { code } = await babel.transformFileAsync(src)
await fs.writeFile(dest, code)
}
}

const bootstrap = async () => {
try {
console.log(colors.blue('Build Angular application'))
const stdout = await execAsync('yarn build-electron', { cwd: angularAppDir })
console.log(stdout)

console.log(colors.blue('Copying source files...'))
await fs.copy(srcDir, outDir, { filter: filterFilesNotToCopy })
await fs.copy(packageJsonFile, packageJsonFileOutDir)
await fs.copy(packageLockJsonFile, packageLockJsonFileOutDir)
const json = await fs.readJson(packageJsonFileOutDir)
json.main = 'index.js'
await fs.writeFile(packageJsonFileOutDir, JSON.stringify(json))
await compileElectronSource()
const stdoutYarn = await execAsync('yarn install --production', { cwd: outDir })
console.log(stdoutYarn)
console.log("\n\n" + colors.green('Done copying source files'))
await fs.ensureDir(packagedDir)
const r = await packager({
name: 'papyrus-compiler-app',
icon: path.join(srcDir, 'papyrus-compiler-app.ico'),
dir: outDir,
out: packagedDir,
platform: 'win32',
arch: 'ia32',
asar: true,
overwrite: true,
})
console.log(colors.green('Done packaging app'))
} catch (e) {
console.error(colors.red('Error during packaging.'))
console.error(e)
process.exit(1)
}
}

bootstrap()
24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"name": "papyrus-compiler-electron",
"productName": "papyrus-compiler-electron",
"version": "1.0.0",
"version": "1.0.1",
"description": "My Electron application description",
"main": "src/index.js",
"main": "src/index-dev.js",
"scripts": {
"start": "electron-forge start --inspect-electron",
"package": "electron-forge package --platform=win32",
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint": "eslint src --color"
"start": "concurrently -n Electron,Angular -c blue,green \"yarn run start:electron\" \"yarn run start:angular\"",
"start:electron": "electron-forge start",
"start:angular": "cd src/app/papyrus-compiler-app && yarn start",
"package": "cross-env NODE_ENV=production cross-env DEBUG=electron-packager node ./package.js"
},
"keywords": [],
"author": "lyrod",
Expand Down Expand Up @@ -45,15 +44,24 @@
"electron-squirrel-startup": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.3.3",
"@babel/plugin-transform-destructuring": "^7.3.2",
"@babel/preset-env": "^7.3.1",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"colors": "^1.3.3",
"concurrently": "^4.1.0",
"cross-env": "^5.2.0",
"electron-forge": "^5.2.4",
"electron-prebuilt-compile": "4.0.0",
"escape-string-regexp": "^1.0.5",
"eslint": "^3",
"eslint-config-airbnb": "^15",
"eslint-plugin-import": "^2",
"eslint-plugin-jsx-a11y": "^5",
"eslint-plugin-react": "^7"
"eslint-plugin-react": "^7",
"fs-extra": "^7.0.1",
"rimraf": "^2.6.3"
}
}
2 changes: 1 addition & 1 deletion src/app/papyrus-compiler-app
85 changes: 85 additions & 0 deletions src/index-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { app, BrowserWindow, ipcMain } from 'electron';
import { AppCompiler } from './main-process/app-compiler';
import GamePathUtil from './main-process/gamepath-util';

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

const createWindow = () => {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
titleBarStyle: 'hidden',
});

// and load the index.html of the app.
mainWindow.loadURL('http://localhost:4200');

// Open the DevTools.
mainWindow.webContents.openDevTools();

ipcMain.on('compile-script', (event, { script, output, imports, flag, gamePath }) => {
const gamePathUtil = new GamePathUtil({
imports,
output,
gamePath,
flag,
});
const appCompiler = new AppCompiler(gamePathUtil);

console.log('on compile');

appCompiler.compile(script)
.then(result => {
console.log('good ' + script);
event.sender.send('compile-success', result)
})
.catch(e => {
console.log('' + e.message);
event.sender.send('compile-failed', e.message);
});
});

// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
10 changes: 1 addition & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ const createWindow = () => {
});

// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/app/papyrus-compiler-app/dist/index.html`);
// mainWindow.loadURL('http://localhost:4200');

// Open the DevTools.
mainWindow.webContents.openDevTools();
mainWindow.loadURL(`file://${__dirname}/index.html`);

ipcMain.on('compile-script', (event, { script, output, imports, flag, gamePath }) => {
const gamePathUtil = new GamePathUtil({
Expand All @@ -37,16 +33,12 @@ const createWindow = () => {
flag,
});
const appCompiler = new AppCompiler(gamePathUtil);

console.log('on compile');

appCompiler.compile(script)
.then(result => {
console.log('good ' + script);
event.sender.send('compile-success', result)
})
.catch(e => {
console.log('' + e.message);
event.sender.send('compile-failed', e.message);
});
});
Expand Down
Binary file added src/papyrus-compiler-app.ico
Binary file not shown.
Loading

0 comments on commit 26f2c26

Please sign in to comment.