@google/dscc-gen
Version:
Create component & connector projects with sane defaults.
134 lines (133 loc) • 4.66 kB
JavaScript
;
/**
* @license
* Copyright 2018 Google LLC
*
* 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
*
* https://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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const index_1 = require("./index");
const util = require("./util");
const files = require("./files");
const argparse = require("argparse");
const vizQuestions = require("./viz/questions");
const connectorQuestions = require("./connector/questions");
const prompt_1 = require("./prompt");
var ProjectChoice;
(function (ProjectChoice) {
ProjectChoice["VIZ"] = "community-viz";
ProjectChoice["CONNECTOR"] = "community-connector";
})(ProjectChoice = exports.ProjectChoice || (exports.ProjectChoice = {}));
const templateOptions = [
ProjectChoice.VIZ,
ProjectChoice.CONNECTOR,
];
const PROJECT_CHOICE = {
cmdName: '--project_choice',
inquirerName: 'projectChoice',
};
const PROJECT_NAME = {
cmdName: '--project_name',
inquirerName: 'projectName',
};
const projectNameRegEx = /^([-_A-Za-z\d])+$/;
const projectNameValidator = async (input) => {
if (!projectNameRegEx.test(input)) {
return 'Name may only include letters, numbers, dashes, and underscores.';
}
const projectPath = path.join(index_1.PWD, input);
if (await util.fileExists(projectPath)) {
return `The directory ${input} already exists.`;
}
return true;
};
exports.COMMON_QUESTIONS = [
{
name: PROJECT_CHOICE.inquirerName,
type: 'list',
message: 'What project template would you like to use?',
choices: templateOptions,
},
{
name: PROJECT_NAME.inquirerName,
type: 'input',
message: 'Project name',
validate: projectNameValidator,
},
];
const getArgs = async (baseDir) => {
const parser = new argparse.ArgumentParser({
version: (await files.getPackageJson(baseDir)).version,
addHelp: true,
description: 'Template-based project generator for Data Studio developer features',
});
parser.addArgument(['--yarn'], {
dest: 'yarn',
action: 'storeTrue',
help: 'Use yarn as the build tool.',
});
parser.addArgument(['--npm'], {
dest: 'npm',
action: 'storeTrue',
help: 'Use npm as the build tool.',
});
parser.addArgument([PROJECT_CHOICE.cmdName], {
dest: PROJECT_CHOICE.inquirerName,
choices: templateOptions,
help: 'Which template to use.',
});
parser.addArgument([PROJECT_NAME.cmdName], {
dest: PROJECT_NAME.inquirerName,
help: 'The name of your project',
});
parser.addArgument(['--script_id'], {
dest: 'scriptId',
help: 'Community Connector: Use this scriptId instead of creating a new script.',
});
const args = parser.parseArgs();
Object.keys(args).forEach((key) => {
if (args[key] === null) {
delete args[key];
}
});
return args;
};
const questionsWithArgs = async (args, questions) => {
await Promise.all(questions.map(async (question) => {
const argValue = args[question.name];
if (argValue && question.validate) {
const isValid = await question.validate(argValue);
if (isValid !== true) {
throw new Error(`Invalid response for ${question.name}: "${argValue}". ${isValid}`);
}
}
}));
return questions.filter((question) => {
return args[question.name] === undefined;
});
};
exports.getAnswers = async (baseDir) => {
const args = await getArgs(baseDir);
const questions = await questionsWithArgs(args, exports.COMMON_QUESTIONS);
const promptAnswers = await prompt_1.prompt(questions);
const commonAnswers = Object.assign({}, promptAnswers, args);
switch (commonAnswers.projectChoice) {
case ProjectChoice.VIZ:
return vizQuestions.getAnswers(args, commonAnswers);
case ProjectChoice.CONNECTOR:
return connectorQuestions.getAnswers(args, commonAnswers);
default:
throw new Error(`${commonAnswers.projectChoice} is not supported.`);
}
};