#! /usr/bin/env node

var execSync = require('child_process').execSync;
var path = require('path');
var fs = require('fs');
var mkdirp = require('mkdirp');
var program = require('./../src/customcommander');
var jsonfile = require('jsonfile');
var helper = require('./../src/clihelper');

var registryPath = path.join(__dirname, '..', 'registry.json');
var widgetName, widgetPath, registry;

program
    .alias('appsngen widget create')
    .usage('<name> [path]')
    .arguments('<name> [path]')
    .action(function (name, path) {
        widgetName = name;
        widgetPath = path;
    })
    .parse(process.argv);

if (typeof widgetName === 'undefined') {
    console.error('No widget name provided');
    process.exit(1);
}
if (typeof widgetPath !== 'undefined') {
    widgetPath = path.join(widgetPath, widgetName);
} else {
    widgetPath = path.join('.', widgetName);
}
helper.validateWidgetName(widgetName)
    .then(function () {
        try {
            if (fs.readdirSync(widgetPath).length !== 0) {
                throw 'Path already exists and is not empty: ' + path.resolve(widgetPath);
            }
        } catch (e) {
            if (e.code !== 'ENOENT') {
                throw e;
            }
        }
        try {
            registry = jsonfile.readFileSync(registryPath);
        } catch (e) {
            if (e.code !== 'ENOENT') {
                throw e;
            }
        }
        registry = registry || {};
        if (registry[widgetName]) {
            throw 'Widget with this name already exist.';
        }
        mkdirp.sync(widgetPath);
        execSync('npm run yo appsngen-web-widget ' + path.resolve(widgetPath), {
            cwd: path.join(__dirname, '..'),
            stdio: 'inherit'
        });
        registry[widgetName] = {
            path: path.resolve(widgetPath)
        };
        jsonfile.writeFileSync(registryPath, registry, {
            spaces: 4
        });
    })
    .catch(function (error) {
        console.error(error.toString());
        process.exit(1);
    });
@@ -1627,16 +1627,17 @@
 -widget 
+%22
 ' + path
@@ -1656,16 +1656,22 @@
 getPath)
+ + '%22'
 , %7B%0A    
@@ -2036,12 +2036,13 @@
 (1);%0A    %7D);
+%0A

Fix work for names with whitespaces