Cleanup code

This commit is contained in:
Jia Hao 2016-01-18 22:07:22 +08:00
parent 5379740dda
commit 0eb2e83dc2
4 changed files with 158 additions and 148 deletions

View File

@ -2,12 +2,15 @@
"name": "nativefier",
"version": "6.0.0",
"description": "Wrap web apps natively",
"main": "index.js",
"main": "lib/buildApp.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel src -d lib",
"watch": "babel --watch src -d lib"
},
"bin": {
"nativefier": "lib/cli.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jiahaog/nativefier.git"

88
src/buildApp.js Normal file
View File

@ -0,0 +1,88 @@
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
var tmpobj = tmp.dirSync({unsafeCleanup: true});
const tmpPath = tmpobj.name;
async.waterfall([
function (callback) {
console.log("Dir: ", tmpobj.name);
copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.badge, options.width, options.height, callback);
},
function (tempDir, callback) {
console.log('copied to ', tempDir);
options.dir = tempDir;
packager(options, callback);
},
function (appPath, callback) {
tmpobj.removeCallback();
callback(null, appPath);
}
], callback);
}
/**
* @callback tempDirCallback
* @param error
* @param [tempDirPath]
*/
/**
* 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
* @param {number} [width]
* @param {number} [height]
* @param {tempDirCallback} callback
*/
function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, width, height, callback) {
copy(srcAppDir, tempDir, function (error) {
if (error) {
console.error(error);
callback(`Error Copying temporary directory: ${error}`);
return;
}
const appArgs = {
name: name,
targetUrl: targetURL,
badge: badge,
width: width,
height: height
};
fs.writeFileSync(path.join(tempDir, '/targetUrl.txt'), JSON.stringify(appArgs));
callback(null, tempDir);
});
};
export default buildApp;

View File

@ -1,107 +1,6 @@
import os from 'os';
import fs from 'fs';
import path from 'path';
import packager from 'electron-packager';
import commander from 'commander';
import tmp from 'tmp';
import ncp from 'ncp';
import async from 'async';
const copy = ncp.ncp;
const TEMPLATE_APP_DIR = path.join(__dirname, '../', 'app');
const ELECTRON_VERSION = '0.36.4';
function optionsFactory(name = 'MyApp',
targetUrl = 'http://google.com',
platform = detectPlatform(),
architecture = detectArch(),
version = ELECTRON_VERSION,
outDir = os.homedir(),
overwrite = true,
conceal = true,
iconDir,
badge = false,
width = 1280,
height = 800) {
return {
dir: TEMPLATE_APP_DIR,
name: name,
targetUrl: targetUrl,
platform: platform,
arch: architecture,
version: version,
out: outDir,
// optionals
overwrite: overwrite,
asar: conceal,
icon: iconDir,
// app configuration
badge: badge,
width: width,
height: height
}
}
/**
* @callback buildAppCallback
* @param error
* @param appPath
*/
/**
*
* @param options
* @param {buildAppCallback} callback
*/
function buildApp(options, callback) {
// pre process app
var tmpobj = tmp.dirSync({unsafeCleanup: true});
const tmpPath = tmpobj.name;
async.waterfall([
function (callback) {
console.log("Dir: ", tmpobj.name);
copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.badge, options.width, options.height, callback);
},
function (tempDir, callback) {
console.log('copied to ', tempDir);
options.dir = tempDir;
packager(options, callback);
},
function (appPath, callback) {
tmpobj.removeCallback();
callback(null, appPath);
}
], callback);
}
function detectPlatform() {
const platform = os.platform();
if (platform === 'darwin' || platform === 'win32' || platform === 'linux') {
return platform;
}
console.warn(`Warning: Untested platform ${platform} detected, assuming linux`);
return 'linux';
}
function detectArch() {
const arch = os.arch();
if (arch !== 'ia32' && arch !== 'x64') {
throw `Incompatible architecture ${arch} detected`;
}
return os.arch();
}
import optionsFactory from './options';
import buildApp from './buildApp';
function main() {
const options = optionsFactory();
@ -113,49 +12,6 @@ function main() {
console.log(`App built to ${appPath}`);
});
}
/**
* @callback tempDirCallback
* @param error
* @param [tempDirPath]
*/
/**
* 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
* @param {number} [width]
* @param {number} [height]
* @param {tempDirCallback} callback
*/
function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, width, height, callback) {
copy(srcAppDir, tempDir, function (error) {
if (error) {
console.error(error);
callback(`Error Copying temporary directory: ${error}`);
return;
}
const appArgs = {
name: name,
targetUrl: targetURL,
badge: badge,
width: width,
height: height
};
fs.writeFileSync(path.join(tempDir, '/targetUrl.txt'), JSON.stringify(appArgs));
callback(null, tempDir);
});
};
main();
main();

63
src/options.js Normal file
View File

@ -0,0 +1,63 @@
import os from 'os';
import path from 'path';
const TEMPLATE_APP_DIR = path.join(__dirname, '../', 'app');
const ELECTRON_VERSION = '0.36.4';
function optionsFactory(name = 'MyApp',
targetUrl = 'http://google.com',
platform = detectPlatform(),
architecture = detectArch(),
version = ELECTRON_VERSION,
outDir = os.homedir(),
overwrite = true,
conceal = true,
iconDir,
badge = false,
width = 1280,
height = 800) {
return {
dir: TEMPLATE_APP_DIR,
name: name,
targetUrl: targetUrl,
platform: platform,
arch: architecture,
version: version,
out: outDir,
// optionals
overwrite: overwrite,
asar: conceal,
icon: iconDir,
// app configuration
badge: badge,
width: width,
height: height
}
}
function detectPlatform() {
const platform = os.platform();
if (platform === 'darwin' || platform === 'win32' || platform === 'linux') {
return platform;
}
console.warn(`Warning: Untested platform ${platform} detected, assuming linux`);
return 'linux';
}
function detectArch() {
const arch = os.arch();
if (arch !== 'ia32' && arch !== 'x64') {
throw `Incompatible architecture ${arch} detected`;
}
return os.arch();
}
export default optionsFactory;