graphcool-cli-core
Version:
313 lines • 17.2 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var graphcool_cli_engine_1 = require("graphcool-cli-engine");
var chalk_1 = require("chalk");
var examples_1 = require("../../examples");
var fs = require("fs-extra");
var path = require("path");
var inquirer = require("inquirer");
var lodash_1 = require("lodash");
var Init = /** @class */ (function (_super) {
__extends(Init, _super);
function Init() {
return _super !== null && _super.apply(this, arguments) || this;
}
Init.prototype.run = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, copy, template, force, dirName, newDefinitionPath, pjson, files, projectDefinition, info, newDefinition, relativeDir, createdFiles, cdInstruction;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this.flags, copy = _a.copy, template = _a.template, force = _a.force;
dirName = this.args.dirName;
if (dirName) {
newDefinitionPath = path.join(process.cwd(), dirName + '/');
fs.mkdirpSync(newDefinitionPath);
this.config.definitionDir = newDefinitionPath;
this.config.localRCPath = path.join(newDefinitionPath, '.graphcoolrc');
}
pjson = __assign({}, examples_1.defaultPjson, { name: path.basename(this.config.definitionDir) });
files = fs.readdirSync(this.config.definitionDir);
if (!(files.length > 0 && !(files.length === 1 && files[0] === '.graphcoolrc') && files.includes('graphcool.yml'))) return [3 /*break*/, 3];
this.out.log("\nThe directory " + chalk_1.default.green(this.config.definitionDir) + " contains files that could conflict:\n\n" + files.map(function (f) { return " " + f; }).join('\n') + "\n\nEither try using a new directory name, or remove the files listed above.\n\n" + chalk_1.default.bold('NOTE:') + " The behavior of the init command changed, to deploy a project, please use " + chalk_1.default.green('graphcool deploy') + "\n\nTo force the init process in this folder, use " + chalk_1.default.green('graphcool init --force'));
if (!force) return [3 /*break*/, 2];
return [4 /*yield*/, this.askForConfirmation(this.config.definitionDir)];
case 1:
_b.sent();
return [3 /*break*/, 3];
case 2:
this.out.exit(1);
_b.label = 3;
case 3:
if (template) {
projectDefinition = examples_1.examples[template];
if (!projectDefinition) {
this.out.error(template + " is not a valid template");
}
this.definition.set(projectDefinition);
}
if (!copy) return [3 /*break*/, 5];
return [4 /*yield*/, this.client.fetchProjectInfo(copy)];
case 4:
info = _b.sent();
this.definition.set(info.projectDefinition);
_b.label = 5;
case 5:
if (!this.definition.definition) {
newDefinition = examples_1.defaultDefinition;
this.definition.set(newDefinition);
}
relativeDir = path.relative(process.cwd(), this.config.definitionDir);
relativeDir = relativeDir.length === 0 ? '.' : relativeDir;
this.out.action.start("Creating a new Graphcool service in " + chalk_1.default.green(relativeDir));
this.definition.save(undefined, false);
this.out.action.stop();
this.out.log("" + chalk_1.default.dim.bold('\nWritten files' + ':'));
fs.writeFileSync(path.join(this.config.definitionDir, 'package.json'), JSON.stringify(pjson, null, 2));
createdFiles = lodash_1.flatten(this.definition.definition.modules.map(function (module) { return Object.keys(module.files); })).concat(['graphcool.yml', 'package.json']);
this.out.filesTree(createdFiles);
cdInstruction = relativeDir === '.' ? '' : "To get started, cd into the new directory:\n " + chalk_1.default.green("cd " + relativeDir) + "\n";
this.out.log(cdInstruction + "\nTo deploy your Graphcool service:\n " + chalk_1.default.green('graphcool deploy') + "\n\nTo start your local Graphcool cluster:\n " + chalk_1.default.green('graphcool local up') + "\n \nTo add facebook authentication to your service:\n " + chalk_1.default.green('graphcool add-template facebook-auth') + "\n\nYou can find further instructions in the " + chalk_1.default.green('graphcool.yml') + " file,\nwhich is the central project configuration.\n");
return [2 /*return*/];
}
});
});
};
Init.prototype.interactiveInit = function () {
return __awaiter(this, void 0, void 0, function () {
var initQuestion, init, _a, projectId, info;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
initQuestion = {
name: 'init',
type: 'list',
message: 'How do you want to start?',
choices: [
{
value: 'blank',
name: [
"" + chalk_1.default.bold('New blank project'),
" Creates a new Graphcool project from scratch.",
'',
].join('\n'),
},
{
value: 'copy',
name: [
"" + chalk_1.default.bold('Copying an existing project'),
" Copies a project from your account",
'',
].join('\n'),
},
],
pageSize: 13,
};
return [4 /*yield*/, this.out.prompt([initQuestion])];
case 1:
init = (_b.sent()).init;
_a = init;
switch (_a) {
case 'blank': return [3 /*break*/, 2];
case 'copy': return [3 /*break*/, 3];
case 'example': return [3 /*break*/, 7];
}
return [3 /*break*/, 9];
case 2:
this.out.up(7);
return [2 /*return*/, examples_1.defaultDefinition];
case 3: return [4 /*yield*/, this.auth.ensureAuth()];
case 4:
_b.sent();
return [4 /*yield*/, this.projectSelection()];
case 5:
projectId = _b.sent();
this.out.up(4);
return [4 /*yield*/, this.client.fetchProjectInfo(projectId)];
case 6:
info = _b.sent();
return [2 /*return*/, info.projectDefinition];
case 7: return [4 /*yield*/, this.exampleSelection()];
case 8: return [2 /*return*/, _b.sent()];
case 9: return [2 /*return*/, null];
}
});
});
};
Init.prototype.projectSelection = function () {
return __awaiter(this, void 0, void 0, function () {
var projects, choices, question, project;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.client.fetchProjects()];
case 1:
projects = _a.sent();
choices = projects.map(function (p) { return ({
name: p.name + " (" + p.id + ")",
value: p.id,
}); }).concat(new inquirer.Separator(chalk_1.default.bold.green(lodash_1.repeat('-', 50))));
question = {
name: 'project',
type: 'list',
message: 'Please choose a project',
choices: choices,
pageSize: Math.min(process.stdout.rows, projects.length) - 2,
};
return [4 /*yield*/, this.out.prompt([question])];
case 2:
project = (_a.sent()).project;
return [2 /*return*/, project];
}
});
});
};
Init.prototype.exampleSelection = function () {
return __awaiter(this, void 0, void 0, function () {
var question, example;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
question = {
name: 'example',
type: 'list',
message: 'Please choose an example',
choices: [
{
value: 'instagram',
name: [
"" + chalk_1.default.bold('Instagram'),
"Contains an instagram clone with permission logic",
'',
].join('\n'),
},
{
value: 'stripe',
name: [
"" + chalk_1.default.bold('Stripe Checkout'),
"An example integrating the stripe checkout with schema extensions",
'',
].join('\n'),
},
{
value: 'sendgrid',
name: [
"" + chalk_1.default.bold('Sendgrid Mails'),
"An example that shows how to connect Graphcool to the Sendgrid API",
'',
].join('\n'),
},
],
pageSize: 12,
};
return [4 /*yield*/, this.out.prompt(question)];
case 1:
example = (_a.sent()).example;
return [2 /*return*/, examples_1.examples[example]];
}
});
});
};
Init.prototype.askForConfirmation = function (folder) {
return __awaiter(this, void 0, void 0, function () {
var confirmationQuestion, confirmation;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
confirmationQuestion = {
name: 'confirmation',
type: 'input',
message: "Are you sure that you want to init a new service in " + chalk_1.default.green(folder) + "? y/N",
default: 'n'
};
return [4 /*yield*/, this.out.prompt(confirmationQuestion)];
case 1:
confirmation = (_a.sent()).confirmation;
if (confirmation.toLowerCase().startsWith('n')) {
this.out.exit(0);
}
return [2 /*return*/];
}
});
});
};
Init.topic = 'init';
Init.description = 'Create files for new services';
Init.group = 'general';
Init.help = "\n \n " + chalk_1.default.green.bold('Examples:') + "\n \n " + chalk_1.default.gray('-') + " Initialize a new Graphcool project\n " + chalk_1.default.green('$ graphcool init') + "\n ";
Init.flags = {
force: graphcool_cli_engine_1.flags.boolean({
char: 'f',
description: 'Initialize even if the folder already contains graphcool files',
}),
copy: graphcool_cli_engine_1.flags.string({
char: 'c',
description: 'ID or alias of the project, that the schema should be copied from',
}),
template: graphcool_cli_engine_1.flags.string({
char: 't',
description: 'The template to base the init on. (options: blank, instagram)',
}),
};
Init.args = [
{
name: 'dirName',
description: 'Folder to initialize in (optional)',
},
];
return Init;
}(graphcool_cli_engine_1.Command));
exports.default = Init;
//# sourceMappingURL=index.js.map