1 | #!/usr/bin/env node
|
2 |
|
3 | var fs = require('fs');
|
4 | var path = require('path');
|
5 | var spawn = require('child_process').spawn;
|
6 | var bin = require('../lib/findpath.js')();
|
7 |
|
8 | function run() {
|
9 | // Rename nw.js's own package.json as workaround for https://github.com/nwjs/nw.js/issues/1503
|
10 | var packagejson = path.resolve(__dirname, '..', 'package.json');
|
11 | var packagejsonBackup = path.resolve(__dirname, '..', 'package_backup.json');
|
12 | if (!fs.existsSync(packagejsonBackup)) {
|
13 | try {
|
14 | fs.renameSync(packagejson, packagejsonBackup);
|
15 | } catch (err) {}
|
16 | }
|
17 |
|
18 | // copy over any asset files (icons, etc) specified via CLI args:
|
19 | require('../lib/app_assets').copyAssets(process.platform, bin);
|
20 |
|
21 | // Normalize cli args
|
22 | var args = process.argv.slice(2);
|
23 | var cwd = (args.length < 1) ? '.' : args[0];
|
24 | if (!fs.existsSync(path.resolve(cwd))) {
|
25 | args = ['.'].concat(args);
|
26 | } else {
|
27 | args = [cwd].concat(args.slice(1));
|
28 | }
|
29 |
|
30 | // Spawn node-webkit
|
31 | var nw = spawn(bin, args, { stdio: 'inherit' });
|
32 | nw.on('close', function() {
|
33 | process.nextTick(function() {
|
34 | process.exit(0);
|
35 | });
|
36 | });
|
37 |
|
38 | // Restore package.json shortly after nw is spawned
|
39 | setTimeout(function() {
|
40 | try {
|
41 | if (fs.existsSync(packagejsonBackup)) {
|
42 | fs.renameSync(packagejsonBackup, packagejson);
|
43 | }
|
44 | } catch (err) {}
|
45 | }, 1000);
|
46 | }
|
47 |
|
48 | if (!fs.existsSync(bin)) {
|
49 | console.log('nw.js appears to have failed to download and extract. Attempting to download and extract again...');
|
50 | var child = spawn(process.execPath, [path.resolve(__dirname, '..', 'scripts', 'install.js')], { stdio: 'inherit' });
|
51 | child.on('close', run);
|
52 | } else {
|
53 | run();
|
54 | }
|