Skip to content

Commit

Permalink
feat: add asset reference in model creation if thumb_field command op…
Browse files Browse the repository at this point in the history
…tion is specified
  • Loading branch information
giacomocerquone committed Mar 21, 2021
1 parent 0dc434d commit 69e72a1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ You can provide the following optional parameters to the CLI:

- Add tests
- Add content media upload
- Add possibility to exclude certain fields from the extracted model
- Add possibility to exclude specific fields from the extracted model
Binary file added example/nested/closed-connection.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ yargs(argv).command(
type: "string",
description: "Your graphCMS token",
})
.option("post-thumbnail", {
alias: "pt",
.option("thumb_field", {
type: "string",
description:
'The name of the yaml field to recognize as a post thumbnail (it will be uploaded automatically to the "Asset" model)',
Expand All @@ -59,6 +58,12 @@ yargs(argv).command(
return logger.error("You must specify your graphcms token");
}

if (!argv.thumb_field) {
logger.warn(
"You didn't specify any yaml field to be used as post thumbnail"
);
}

const response = await prompts(
{
type: "text",
Expand All @@ -76,7 +81,7 @@ yargs(argv).command(
restartSpinner("Fetching and parsing mds file");
const mds = await fetchMds(argv.path);
restartSpinner("Extracting model from first md file");
const model = extractModel(mds?.[0]);
const model = extractModel(mds?.[0], argv.thumb_field);
restartSpinner("Creating the model inside your graphCMS instance");
await createModel(
argv.url,
Expand All @@ -94,7 +99,9 @@ yargs(argv).command(
);
spinner.succeed();
} catch (e) {
spinner.fail();
logger.error("Something went wrong", e);
process.exit();
}
}
).argv;
30 changes: 21 additions & 9 deletions src/createModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { newMigration } = require("@graphcms/management");
const { logger } = require("./utils");
const { logger, spinner } = require("./utils");

const createModel = async (url, token, model, modelName) => {
try {
Expand All @@ -10,14 +10,24 @@ const createModel = async (url, token, model, modelName) => {

model.reduce(
(acc, modelField) => {
acc.addSimpleField({
apiId: modelField.name,
displayName: modelField.name,
type: modelField.type,
...(modelField.formRenderer && {
formRenderer: modelField.formRenderer,
}),
});
if (modelField.model) {
acc.addRelationalField({
apiId: modelField.name,
displayName: modelField.name,
relationType: modelField.relationType,
model: modelField.model,
});
} else {
acc.addSimpleField({
apiId: modelField.name,
displayName: modelField.name,
type: modelField.type,
...(modelField.formRenderer && {
formRenderer: modelField.formRenderer,
}),
});
}

return acc;
},
migration.createModel({
Expand All @@ -38,7 +48,9 @@ const createModel = async (url, token, model, modelName) => {
logger.info(name);
}
} catch (e) {
spinner.fail();
logger.error("errors creating model", e);
process.exit();
}
};

Expand Down
11 changes: 9 additions & 2 deletions src/extractModel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { FieldType, Renderer } = require("@graphcms/management");
const { FieldType, Renderer, RelationType } = require("@graphcms/management");
const string = require("string-sanitizer");
const matter = require("gray-matter");

const extractModel = (md) => {
const extractModel = (md, thumbField) => {
const { data } = matter(md);

if (!data) {
Expand All @@ -12,6 +12,13 @@ const extractModel = (md) => {
return Object.keys({ ...data, content: "" }).map((frontKey) => {
const value = data[frontKey];

if (frontKey === thumbField) {
return {
name: frontKey,
model: "Asset",
relationType: RelationType.OneToOne,
};
}
if (frontKey === "content") {
return {
name: "content",
Expand Down

0 comments on commit 69e72a1

Please sign in to comment.