@graphql-cli/init
Version:
Creates a GraphQL project using a template or GraphQL Config file for your existing project.
168 lines • 6.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const common_1 = require("@graphql-cli/common");
const inquirer_1 = require("inquirer");
const path_1 = require("path");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const fs_extra_1 = require("fs-extra");
const js_yaml_1 = require("js-yaml");
const common_2 = require("./common");
const inspector_1 = require("./features/inspector");
const codegen_1 = require("./features/codegen");
const from_existing_1 = require("./sources/from-existing");
const from_scratch_1 = require("./sources/from-scratch");
const from_open_api_1 = require("./sources/from-open-api");
exports.default = common_1.defineCommand(() => {
return {
command: 'init',
builder(yargs) {
return yargs.options({
projectName: {
describe: 'Project name',
type: 'string',
},
templateName: {
describe: 'Name of the predefined template',
type: 'string',
},
templateUrl: {
describe: 'GitHub URL of the template. For example (http://github.com/example/graphql-cli-example-template)',
type: 'string',
},
});
},
async handler(args) {
let { projectName, templateName, templateUrl } = args;
const initializationType = await askForInitializationType();
const context = {
name: projectName,
path: process.cwd(),
graphqlConfig: {
extensions: {},
},
};
const project = common_2.managePackageManifest();
project.addDependency('graphql-cli');
if (initializationType === common_2.InitializationType.ExistingGraphQL) {
await from_existing_1.fromExisting({
context,
project,
});
}
else if (initializationType === common_2.InitializationType.FromScratch) {
await from_scratch_1.fromScratch({
context,
templateName,
templateUrl,
});
}
if (initializationType !== common_2.InitializationType.FromScratch) {
context.type = await askForProject();
}
loadGraphQLConfig(context);
if (initializationType === common_2.InitializationType.ExistingOpenAPI) {
await from_open_api_1.fromExistingOpenAPI(context);
}
await askForSchema(context);
await askForDocuments(context);
await codegen_1.askForCodegen({ context, project });
await inspector_1.askForInspector({ context, project });
await Promise.all([
writeGraphQLConfig(context),
project.writePackage({
path: context.path,
name: projectName,
initializationType,
})
]);
const successMessages = [
`🚀 GraphQL CLI project successfully initialized:`,
context.name,
'Next Steps:',
`- Change directory to the project folder - ${chalk_1.default.cyan(`cd ${context.path}`)}`,
`- Run ${chalk_1.default.cyan(`yarn install`)} to install dependencies`,
];
if (initializationType !== common_2.InitializationType.ExistingGraphQL) {
successMessages.push(`- ${chalk_1.default.cyan(`(Optional)`)} Initialize your git repo. ${chalk_1.default.cyan(`git init`)}.`, `- Follow the instructions in README.md to continue...`);
}
console.info(successMessages.join('\n'));
process.exit(0);
},
};
});
function askForProject() {
return common_2.askForEnum({
enum: common_2.ProjectType,
message: 'What is the type of the project?',
defaultValue: common_2.ProjectType.FullStack,
});
}
function askForInitializationType() {
return common_2.askForEnum({
enum: common_2.InitializationType,
message: 'Select the best option for you',
defaultValue: common_2.InitializationType.FromScratch,
ignoreList: [],
});
}
function loadGraphQLConfig(context) {
const graphqlConfigPath = path_1.join(context.path, '.graphqlrc.yml');
try {
if (fs_extra_1.existsSync(graphqlConfigPath)) {
context.graphqlConfig = js_yaml_1.safeLoad(fs_extra_1.readFileSync(graphqlConfigPath, 'utf8'));
}
}
catch (e) {
console.warn(`Existing GraphQL Config file looks broken! Skipping...`);
}
}
async function askForSchema(context) {
if (!context.graphqlConfig.schema) {
const { schema } = await inquirer_1.prompt([
{
type: 'input',
name: 'schema',
message: 'Where is your schema?',
default: './schema.graphql',
},
]);
context.graphqlConfig.schema = schema.endsWith('.ts')
? {
[schema]: {
require: 'ts-node/register',
},
}
: schema;
}
}
async function askForDocuments(context) {
if (!context.graphqlConfig.documents &&
(context.type === common_2.ProjectType.FullStack || context.type === common_2.ProjectType.FrontendOnly)) {
const { documents } = await inquirer_1.prompt([
{
type: 'input',
name: 'documents',
message: 'Where are your operation documents?',
},
]);
context.graphqlConfig.documents = documents;
}
}
async function writeGraphQLConfig(context) {
const configPath = path_1.join(context.path, '.graphqlrc.yml');
await fs_extra_1.ensureFile(configPath);
const keys = ['schema', 'documents', 'extensions'];
function sortKeys(a, b) {
const ai = keys.indexOf(a);
const bi = keys.indexOf(b);
if (ai === -1 && bi === -1) {
return a.localeCompare(b);
}
return ai <= bi ? -1 : 1;
}
fs_extra_1.writeFileSync(configPath, js_yaml_1.safeDump(context.graphqlConfig, {
sortKeys,
}));
}
//# sourceMappingURL=index.js.map