blob: 92a9d61c85ff2a60e3e32c693e518a8bbe666131 [file] [log] [blame]
#!/usr/bin/env node
/*
*
* Copyright 2013 Canonical Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
var path = require('path');
var args = process.argv;
var ROOT = path.join(__dirname, '..');
var fs = require('fs');
function help() {
console.log('Usage: ' + path.relative(process.cwd(), path.join(ROOT, 'bin', 'create')) + ' <path_to_new_project> <package_name> <project_name>');
console.log(' <path_to_new_project>: Path to your new Cordova Ubuntu project');
console.log(' <package_name>: Package name');
console.log(' <project_name>: Project name');
process.exit(0);
}
function create(project_path, package_name, project_name, project_template_dir) {
function create_project() {
var shell = require("shelljs");
// Check if project already exists
if(fs.existsSync(project_path)) {
console.error('Project already exists! Delete and recreate');
process.exit(2);
}
shell.mkdir(project_path);
shell.cp('-r', path.join(ROOT, '*'), path.join(project_path, 'build'));
shell.mkdir(path.join(project_path, 'native'));
shell.cp('-r', path.join(ROOT, 'bin/build/*'), path.join(project_path, 'cordova'));
shell.cp('-r', path.join(ROOT, 'bin/check_reqs'), path.join(project_path, 'cordova'));
shell.cp('-r', path.join(ROOT, 'bin/node_modules'), path.join(project_path, 'cordova'));
shell.cp('-r', path.join(ROOT, 'xml/config.xml'), project_path);
shell.mkdir(path.join(project_path, 'www'));
shell.cp(path.join(ROOT, 'www/cordova.js'), path.join(project_path, 'www'));
shell.cd(project_path);
}
try {
require.resolve("shelljs");
require.resolve("elementtree");
require.resolve("colors");
require.resolve("optimist");
require.resolve("q");
create_project();
} catch(e) {
console.log('Shelljs module was not found, running \'npm install\'.....');
var exec = require('child_process').exec;
var cwd = process.cwd();
var block = true;
exec('npm install', {cwd: __dirname}, function (error, stdout, stderr) {
block = false;
if (error !== null) {
console.error('ERROR : running \'npm install\' is npm installed? ' + error);
console.error(stderr);
process.exit(error.code);
}
});
function wait() {
if (block)
setTimeout(wait, 1500);
else
create_project();
};
setTimeout(wait, 1500);
}
}
if(args.length < 3 || (args[2] == '--help' || args[2] == '/?' || args[2] == '-h' ||
args[2] == 'help' || args[2] == '-help' || args[2] == '/help')) {
help();
} else {
create(args[2], args[3], args[4], args[5]);
}