From 4495b26c92271204cea1ff4a34a4e419c4f7d74b Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Sun, 5 Jul 2015 16:43:32 +0800 Subject: [PATCH] Fixed enormous bug in not passing the app configuration properly --- .gitignore | 2 -- app/main.js | 5 ++--- cli.js | 34 +++++++++++++++++++--------------- package.json | 1 + tempDir.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 tempDir.js diff --git a/.gitignore b/.gitignore index 0809b03..e6bba47 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,6 @@ node_modules .DS_Store build/ -# the file used to pass variables from the cli to the app -app/targetUrl.txt diff --git a/app/main.js b/app/main.js index fa26256..34ea2b7 100644 --- a/app/main.js +++ b/app/main.js @@ -31,13 +31,12 @@ app.on('ready', function() { ); mainWindow.loadUrl('file://' + __dirname + '/index.html'); - //mainWindow.openDevTools(); mainWindow.webContents.on('did-finish-load', function() { - - fs.readFile(__dirname + '/targetUrl.txt', 'utf8', function (error, data) { + fs.readFile(APP_ARGS_FILE_PATH, 'utf8', function (error, data) { if (error) { console.error('Error reading file: ' + error); } else { + console.log(data); mainWindow.webContents.send('params', data); } diff --git a/cli.js b/cli.js index 7342b04..053b01e 100755 --- a/cli.js +++ b/cli.js @@ -4,8 +4,10 @@ var args = require('minimist')(process.argv.slice(2), {boolean: ['prune', 'asar' var packager = require('./'); var usage = fs.readFileSync(__dirname + '/usage.txt').toString(); var validator = require('validator'); +var tempDir = require('./tempDir'); -args.dir = './app'; + +args.dir = 'blah'; // set to true first args.name = args._[0]; var protocolSchemes = [].concat(args.protocol || []); @@ -29,22 +31,24 @@ if (!validator.isURL(args.target)) { process.exit(1); } -// writes parameters for the app into a text file -// I'm not exactly sure how to pass these as an argument through code -var appArgs = { - name: args.name, - targetUrl: args.target -}; -fs.writeFileSync(__dirname + '/app/targetUrl.txt', JSON.stringify(appArgs)); +tempDir(args.name, args.target, function (error, appDir) { - -packager(args, function done(err, appPaths) { - if (err) { - if (err.message) console.error(err.message); - else console.error(err, err.stack); + if (error) { + console.error(error); process.exit(1); + } else { + + args.dir = appDir; + packager(args, function done(err, appPaths) { + if (err) { + if (err.message) console.error(err.message); + else console.error(err, err.stack); + process.exit(1); + } + + if (appPaths.length > 1) console.error('Wrote new apps to:\n' + appPaths.join('\n')); + else if (appPaths.length === 1) console.error('Wrote new app to', appPaths[0]); + }); } - if (appPaths.length > 1) console.error('Wrote new apps to:\n' + appPaths.join('\n')); - else if (appPaths.length === 1) console.error('Wrote new app to', appPaths[0]); }); diff --git a/package.json b/package.json index f09da7b..b8cb119 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "rcedit": "^0.3.0", "rimraf": "^2.3.2", "run-series": "^1.1.1", + "temp": "^0.8.3", "validator": "^3.40.1" }, "devDependencies": { diff --git a/tempDir.js b/tempDir.js new file mode 100644 index 0000000..62fdfdf --- /dev/null +++ b/tempDir.js @@ -0,0 +1,46 @@ +/** + * Created by JiaHao on 5/7/15. + */ + +var fs = require('fs'); +var temp = require('temp').track(); +var ncp = require('ncp').ncp; + + +/** + * @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} name + * @param {string} targetURL + * @param {tempDirCallback} callback + */ +module.exports = function (name, targetURL, callback) { + + var tempDir = temp.path(); + + ncp('./app', tempDir, function (error) { + if (error) { + callback('Error creating temporary directory', null); + + } else { + + var appArgs = { + name: name, + targetUrl: targetURL + }; + + fs.writeFileSync(tempDir + '/targetUrl.txt', JSON.stringify(appArgs)); + + callback(error, tempDir); + } + }); +}; + +