iitcp
Version:
IITC Plugin creator and developer tools
150 lines (141 loc) • 4.3 kB
JavaScript
const program = require('commander'),
fs = require('fs'),
co = require('co'),
prompt = require('co-prompt'),
chalk = require('chalk'),
colMsg = chalk.bgHex('#0e3d4e').yellowBright,
server = require('./server'),
build = require('./build');
let action,
name,
newConfig = {};
program.arguments('<action>').action(function(a) {
// console.log(a, n);
action = a;
});
// .option('-n, --name <name>', 'The name of your project');
program.on('--help', helpExtend).parse(process.argv);
if (action === 'new') {
newConfig = {};
co(function*() {
console.log(colMsg('Please enter a name for this new iitcp project:'));
newConfig.name = yield prompt('Project Name: ');
newConfig.dir = newConfig.name.split(' ').join('_');
return;
}).then(_ => {
setupNewProject();
});
} else if (action === 'serve') {
console.log(colMsg('serving you up some intel...'));
server.start();
} else if (action === 'build') {
console.log(colMsg('building your plugin...'));
build()
.then(_ => {
console.log(colMsg('Success, plugin can be found in the build folder'));
})
.catch(error => {
console.log(colMsg('An error occured'));
console.error(error);
});
} else {
console.log(colMsg('Try "iitcp --help" for help'));
}
function setupNewProject() {
try {
if (!newConfig.name) {
console.log(chalk.red('A new project needs a name!'));
throw new Error('Project name is falsey');
}
newConfig.GM_sandbox = false;
newConfig.path = `${process.cwd()}`;
let inDirectory = true;
if (!newConfig.path.endsWith(newConfig.dir)) {
newConfig.path = newConfig.path + '/' + newConfig.dir;
inDirectory = false;
}
console.log(
colMsg(
`Creating a new project called "${newConfig.name}" in ${
inDirectory ? 'current' : 'new'
} directory ${newConfig.path}`
)
);
co(function*() {
let ok = yield prompt('are you sure? (Y/N)');
return ok.toLowerCase();
}).then(async ok => {
if (ok === 'y') {
console.log(colMsg('Creating project'));
if (!inDirectory) {
fs.mkdirSync(newConfig.path);
}
fs.mkdirSync(newConfig.path + '/build');
fs.mkdirSync(newConfig.path + '/code');
fs.mkdirSync(newConfig.path + '/header');
console.log(colMsg('Folders created'));
fs.copyFileSync(
__dirname + '/template/code/setup.js',
newConfig.path + '/code/setup.js'
);
fs.writeFileSync(
newConfig.path + '/iitcp_config.json',
JSON.stringify(newConfig, null, 2)
);
fs.writeFileSync(
newConfig.path + '/header/header.js',
generateHeader(newConfig.name) + '// ==/UserScript=='
);
fs.writeFileSync(
newConfig.path + '/localDevStub.user.js',
generateHeader(newConfig.name + ' Local') +
`// @require file://${newConfig.path}/build/${
newConfig.dir
}.user.js
// ==/UserScript==`
);
console.log(colMsg('Files created'));
await build(newConfig);
console.log(colMsg('Build created'));
if (!inDirectory) {
console.log(`Try "cd ${newConfig.dir}" to use your new project.`);
}
process.exit();
} else {
console.log(colMsg('OK, cancelling now.'));
process.exit();
}
});
} catch (e) {
console.log(chalk.red(e));
process.exit(1);
}
}
function helpExtend() {
console.log(`
Actions:
new Create a new project
serve Starts a server on localhost
build Builds your IITC Plugin
Examples:
$ iitcp --help
$ iitcp -h
$ iitcp new
$ iitcp serve
$ iitcp build
`);
}
function generateHeader(pluginName) {
return `// ==UserScript==
// @id ${pluginName.split(' ').join('_')}
// @name IITC Plugin: ${pluginName}
// @category Misc
// @version 0.0.1
// @namespace https://iitc.me/iitcp/${pluginName.split(' ').join('_')}
// @description Created with IITCP
// @match https://*.ingress.com/intel*
// @include /^https:\/\/.*ingress\.com\/intel.*/
// @grant none
`;
}