2016-01-18 14:07:22 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import packager from 'electron-packager';
|
|
|
|
import tmp from 'tmp';
|
|
|
|
import ncp from 'ncp';
|
|
|
|
import async from 'async';
|
|
|
|
|
|
|
|
const copy = ncp.ncp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback buildAppCallback
|
|
|
|
* @param error
|
|
|
|
* @param appPath
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* @param {buildAppCallback} callback
|
|
|
|
*/
|
|
|
|
function buildApp(options, callback) {
|
|
|
|
// pre process app
|
|
|
|
|
2016-01-19 03:58:26 +00:00
|
|
|
var tmpObj = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
const tmpPath = tmpObj.name;
|
2016-01-18 14:07:22 +00:00
|
|
|
|
|
|
|
async.waterfall([
|
2016-01-18 15:56:17 +00:00
|
|
|
callback => {
|
2016-01-19 12:28:58 +00:00
|
|
|
copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.badge, options.width, options.height, options.userAgent, callback);
|
2016-01-18 14:07:22 +00:00
|
|
|
},
|
2016-01-18 15:56:17 +00:00
|
|
|
|
|
|
|
(tempDir, callback) => {
|
2016-01-18 14:07:22 +00:00
|
|
|
options.dir = tempDir;
|
|
|
|
packager(options, callback);
|
|
|
|
},
|
2016-01-18 15:56:17 +00:00
|
|
|
|
|
|
|
(appPath, callback) => {
|
2016-01-18 14:07:22 +00:00
|
|
|
callback(null, appPath);
|
|
|
|
}
|
|
|
|
], callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback tempDirCallback
|
|
|
|
* @param error
|
2016-01-18 15:56:17 +00:00
|
|
|
* @param {string} [tempDirPath]
|
2016-01-18 14:07:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration
|
|
|
|
* for the single page app.
|
|
|
|
*
|
|
|
|
* @param {string} srcAppDir
|
|
|
|
* @param {string} tempDir
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string} targetURL
|
|
|
|
* @param {boolean} badge
|
2016-01-19 03:30:42 +00:00
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
|
|
|
* @param {string} userAgent
|
2016-01-18 14:07:22 +00:00
|
|
|
* @param {tempDirCallback} callback
|
|
|
|
*/
|
2016-01-19 12:28:58 +00:00
|
|
|
function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, width, height, userAgent, callback) {
|
2016-01-18 15:56:17 +00:00
|
|
|
copy(srcAppDir, tempDir, error => {
|
2016-01-18 14:07:22 +00:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
|
|
|
callback(`Error Copying temporary directory: ${error}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const appArgs = {
|
|
|
|
name: name,
|
|
|
|
targetUrl: targetURL,
|
|
|
|
badge: badge,
|
|
|
|
width: width,
|
2016-01-19 03:30:42 +00:00
|
|
|
height: height,
|
2016-01-19 12:28:58 +00:00
|
|
|
userAgent: userAgent
|
2016-01-18 14:07:22 +00:00
|
|
|
};
|
|
|
|
|
2016-01-19 03:45:17 +00:00
|
|
|
fs.writeFileSync(path.join(tempDir, '/nativefier.json'), JSON.stringify(appArgs));
|
2016-01-19 13:06:42 +00:00
|
|
|
|
|
|
|
// change name of packageJson so that temporary files will not be shared across different app instances
|
|
|
|
const packageJsonPath = path.join(tempDir, '/package.json');
|
|
|
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
|
|
|
|
packageJson.name = appArgs.name + '-nativefier';
|
|
|
|
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
|
|
|
|
|
2016-01-18 14:07:22 +00:00
|
|
|
callback(null, tempDir);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default buildApp;
|