strapi-generate-types
Version:
CLI to generate types based on your Strapi API content types
146 lines (137 loc) • 4.5 kB
JavaScript
;
const path = require('path');
const fsExtra = require('fs-extra');
const chalk = require('chalk');
const core = require('@graphql-codegen/core');
const typescriptPlugin = require('@graphql-codegen/typescript');
const graphql = require('graphql');
const load = require('@graphql-tools/load');
const urlLoader = require('@graphql-tools/url-loader');
const commander = require('commander');
const enquirer = require('enquirer');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
const n = Object.create(null);
if (e) {
for (const k in e) {
if (k !== 'default') {
const d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
}
}
n["default"] = e;
return Object.freeze(n);
}
const typescriptPlugin__namespace = /*#__PURE__*/_interopNamespace(typescriptPlugin);
const quickGen = async (url, dir, name) => {
try {
const scope = {
host: url,
outputDir: dir,
outputName: name,
filePath: path.join(`${dir}/${name}`)
};
scope.host = scope.host.replace(/\/$/, "");
console.log(`${chalk.blue("Info")}: Generating types from GraphQL at ${chalk.green(scope.host + "/graphql")}.`);
const schema = await load.loadSchema(`${scope.host}/graphql`, {
loaders: [new urlLoader.UrlLoader()]
});
const config = {
schema: graphql.parse(graphql.printSchema(schema)),
filename: scope.outputName,
documents: [],
config: {},
plugins: [
{
typescript: {}
}
],
pluginMap: {
typescript: typescriptPlugin__namespace
}
};
const output = await core.codegen(config);
await fsExtra.ensureDir(scope.outputDir);
await fsExtra.outputFile(scope.filePath, output);
console.log(`${chalk.blue("Info")}: Generated your types at ${chalk.green(scope.filePath)}.`);
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
};
const promptOptions = [
{
type: "input",
name: "host",
message: "What is your Strapi host?",
initial: "http://localhost:1337"
},
{
type: "input",
name: "dir",
message: "Where do you want to generate your types?",
initial: "./models/"
},
{
type: "input",
name: "name",
message: "How do you want to name the generated file?",
initial: "types.ts"
}
];
const generateTypes = async () => {
try {
const options = await enquirer.prompt(promptOptions);
const scope = {
host: options.host,
outputDir: options.dir,
outputName: options.name,
filePath: path.join(`${options.dir}/${options.name}`)
};
const schema = await load.loadSchema(`${scope.host}/graphql`, {
loaders: [new urlLoader.UrlLoader()]
});
const config = {
schema: graphql.parse(graphql.printSchema(schema)),
filename: scope.outputName,
documents: [],
config: {},
plugins: [
{
typescript: {}
}
],
pluginMap: {
typescript: typescriptPlugin__namespace
}
};
const output = await core.codegen(config);
await fsExtra.ensureDir(scope.outputDir);
await fsExtra.outputFile(scope.filePath, output);
console.log(`${chalk.blue("Info")}: Generated your types at ${scope.filePath}.`);
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
};
var version = "1.0.0";
const program = new commander.Command();
program.version(version, "-v, --version", "Output the version number");
program.command("version").description("Output your version of Strapi SDK").action(() => {
process.stdout.write(version + "\n");
process.exit(0);
});
program.command("generate").description("generate Typescript's types based on your GraphQL Schema").action(async () => {
await generateTypes();
});
program.command("quickgen").description("quickly generate Typescript types based on your GraphQL Schema using Arguments instead of manual prompts. Useful for use in npm scripts.").argument("<url>", "Your Strapi URL").option("-p, --path <location>", "File path where you want to save the generated types", "./models/").option("-n, --file-name <filename>", "File name of the generated types", "types.ts").action(async (url, options) => {
await quickGen(url, options.path, options.fileName);
});
program.parse(process.argv);