ryuu
Version:
Domo App Dev Studio CLI, The main tool used to create, edit, and publish app designs to Domo
269 lines • 11.5 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var inquirer = require("inquirer");
var _ = require("lodash");
var isValidJSName_1 = require("../util/isValidJSName");
var slug = require("slug");
var chalk = require("chalk");
var shelljs_1 = require("shelljs");
var async_1 = require("async");
var fs = require("fs-extra");
var path = require("path");
var log_1 = require("../util/log");
var multichoice_1 = require("../util/multichoice");
//import { init, initOptions } from '../models';
var commander_1 = require("commander");
module.exports = function (program) {
program
.command('init')
.option('-n, --design_name <value>', 'Name of the design')
.addOption(new commander_1.Option('-s, --starter <value>', 'Name of the starter kit').choices([
'hello world',
'manifest only',
'basic chart',
'map chart',
'sugarforce',
]))
.option('-d, --dataset-prompt', 'Include this flag if you wish to skip the prompting for datasets')
.option('-i, --dataset-id [value...]', 'Give dataset ids as multiple arguments after the flag instead of using the prompt')
.option('-a, --dataset-alias [value...]', 'Give dataset aliases as multiple arguments after the flag instead of using the prompt')
.description('initialize a new Custom App design')
.action(function (options) {
var prompts = [];
if (!options.design_name) {
prompts.push({
type: 'input',
message: 'design name',
name: 'name',
});
}
if (!options.starter) {
prompts.push((0, multichoice_1.default)({
type: 'list',
message: 'select a starter',
name: 'starter',
choices: [
'hello world',
'manifest only',
'basic chart',
'map chart',
'sugarforce',
],
}));
}
inquirer.prompt(prompts).then(function (answers) {
if (options.design_name) {
answers.name = options.design_name;
}
if (options.starter) {
answers.starter = options.starter;
}
answers.datasources =
options.datasetId && options.datasetAlias
? options.datasetId.map(function (item, i) {
return { id: item, alias: options.datasetAlias[i] };
})
: [];
askDatasource(answers, options);
});
});
};
// recursively ask user to enter all their datasources
var askDatasource = function (currentAnswers, options) {
if (!options['datasetPrompt']) {
inquirer
.prompt([
{
type: 'confirm',
message: currentAnswers.datasources.length > 0
? 'add another dataset?'
: 'would you like to connect to any datasets?',
name: 'askDatasource',
},
])
.then(function (answers) {
if (answers.askDatasource) {
addDatasource(_.merge(currentAnswers, answers))
.then(function (combinedAnswers) {
// ask for more datasources
askDatasource(combinedAnswers, options);
})
.catch(function (err) {
console.log('why' + err);
});
}
else {
// all done with datasources, continue on.
initiate(_.merge(currentAnswers, answers));
}
});
}
else {
var answers = {
askDatasource: false,
};
initiate(_.merge(currentAnswers, answers));
}
};
var addDatasource = function (currentAnswers) {
return (inquirer
.prompt([
{
type: 'input',
message: 'dataset id',
name: 'id',
},
{
type: 'input',
message: 'dataset alias',
name: 'alias',
validate: function (name) {
if (!(0, isValidJSName_1.default)(name))
return 'Alias must be a valid JavaScript property';
return true;
},
},
])
//issue right here
.then(function (datasource) {
currentAnswers.datasources.push(datasource);
//askDatasource(currentAnswers, {dataSource: true});
return Promise.resolve(currentAnswers);
}));
};
var writeFilesIfNonexistent = function (files, dirs, allAnswers, callback) {
(0, async_1.reduce)(files, [], function (filesThatExist, file, reduceCallback) {
fs.open(file, 'r', function (err, fd) {
var fileExists = !err;
if (fileExists) {
fs.closeSync(fd);
filesThatExist.push(file);
}
reduceCallback(null, filesThatExist);
});
}, function (err, filesThatExist) {
if (filesThatExist.length > 0) {
callback(filesThatExist, null);
}
else {
_.zip(files, dirs).forEach(writeTemplateFile.bind(_this, allAnswers));
callback(null, files);
}
});
};
var writeTemplateFile = function (allAnswers, filedir) {
var templateFileExt = ['.js', '.css', '.html', '.json'];
var file = filedir[0];
var dir = filedir[1] || __dirname + '/../templates/';
var filePath = path.resolve(dir + file);
var destPath = path.parse(path.resolve(process.cwd(), file));
var data;
if (templateFileExt.some(function (ext) { return file.endsWith(ext); })) {
data = fs.readFileSync(filePath, 'utf8');
data = _.template(data)(allAnswers);
}
else {
data = fs.readFileSync(filePath);
}
fs.writeFileSync(path.resolve(destPath.dir, destPath.base), data);
};
var initiate = function (allAnswers) {
var files;
var dirs;
var step = 0;
var stepNumber = function () {
step++;
return "".concat(step, ".");
};
var nextStepsMessage = 'Next steps: \n';
var dirname = slug(allAnswers.name, { lower: false });
if (allAnswers.starter === 'manifest only') {
files = ['manifest.json'];
dirs = [null];
(0, shelljs_1.mkdir)('-p', dirname);
process.chdir(path.resolve(dirname));
}
else if (allAnswers.starter === 'hello world') {
files = ['manifest.json', 'index.html', 'app.js', 'app.css'];
var templateDir_1 = path.join(__dirname, '/../templates/', 'hello world/');
dirs = __spreadArray([], new Array(files.length), true).map(function (_) { return templateDir_1; });
nextStepsMessage += "".concat(stepNumber(), " ").concat(chalk.cyan('cd'), " into the ").concat(chalk.green(dirname), " directory\n");
(0, shelljs_1.mkdir)('-p', dirname);
process.chdir(path.resolve(dirname));
}
else if (allAnswers.starter === 'basic chart') {
files = ['manifest.json', 'index.html', 'app.js', 'app.css'];
var customChartTemplateDir_1 = path.join(__dirname, '/../templates/', 'basic chart/');
dirs = __spreadArray([], new Array(files.length), true).map(function (_) { return customChartTemplateDir_1; });
nextStepsMessage += "".concat(stepNumber(), " ").concat(chalk.cyan('cd'), " into the ").concat(chalk.green(dirname), " directory\n");
(0, shelljs_1.mkdir)('-p', dirname);
process.chdir(path.resolve(dirname));
}
else if (allAnswers.starter === 'map chart') {
files = ['manifest.json', 'index.html', 'app.js', 'app.css'];
var customChartTemplateDir_2 = path.join(__dirname, '/../templates/', 'map chart/');
dirs = __spreadArray([], new Array(files.length), true).map(function (_) { return customChartTemplateDir_2; });
nextStepsMessage += "".concat(stepNumber(), " ").concat(chalk.cyan('cd'), " into the ").concat(chalk.green(dirname), " directory\n");
(0, shelljs_1.mkdir)('-p', dirname);
process.chdir(path.resolve(dirname));
}
else if (allAnswers.starter === 'sugarforce') {
files = files = [
'thumbnail.png',
'manifest.json',
'index.html',
path.join('styles', 'app.css'),
path.join('components', 'autocomplete.js'),
path.join('components', 'floatingactionbutton.js'),
path.join('components', 'modal.js'),
path.join('components', 'router.js'),
path.join('components', 'table.js'),
path.join('js', 'app.js'),
path.join('js', 'helpers.js'),
path.join('js', 'home.js'),
path.join('js', 'leads.js'),
path.join('js', 'opportunities.js'),
path.join('js', 'reports.js'),
path.join('views', 'home.html'),
path.join('views', 'leads.html'),
path.join('views', 'opportunities.html'),
path.join('views', 'reports.html'),
];
var customChartTemplateDir_3 = path.join(__dirname, '/../templates/', 'sugarforce/');
dirs = __spreadArray([], new Array(files.length), true).map(function (_) { return customChartTemplateDir_3; });
nextStepsMessage += "".concat(stepNumber(), " ").concat(chalk.cyan('cd'), " into the ").concat(chalk.green(dirname), " directory\n");
(0, shelljs_1.mkdir)('-p', dirname);
(0, shelljs_1.mkdir)('-p', path.join(dirname, 'styles'));
(0, shelljs_1.mkdir)('-p', path.join(dirname, 'components'));
(0, shelljs_1.mkdir)('-p', path.join(dirname, 'js'));
(0, shelljs_1.mkdir)('-p', path.join(dirname, 'views'));
process.chdir(path.resolve(dirname));
}
// populate and write out the template files
writeFilesIfNonexistent(files, dirs, allAnswers, function (err, res) {
var dirname = slug(allAnswers.name, { lower: false });
if (err) {
log_1.log.fail('Cannot initialize new design. Doing so would overwrite existing files:' +
err);
}
else {
nextStepsMessage += "".concat(stepNumber(), " Edit the files that were just generated. \n").concat(stepNumber(), " Run ").concat(chalk.cyan('domo login'), " if you haven't already. \n").concat(stepNumber(), " Run ").concat(chalk.cyan('domo publish'), " to finish initializiation and whenever you make changes. \n").concat(stepNumber(), " Run ").concat(chalk.cyan('domo dev'), " to see what your app will look like and locally develop\n").concat(stepNumber(), " Add a Custom App card from the design published to any page in Domo. ");
log_1.log.ok("New design initialized in the ".concat(chalk.green(dirname || 'current'), " directory"), nextStepsMessage);
}
if (allAnswers.starter === 'hello world') {
fs.remove('node_modules');
}
process.exit();
});
};
//# sourceMappingURL=init.js.map