Skip to content

Commit

Permalink
feat: finish implementation. Missing ast generation
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocerquone committed Mar 9, 2021
1 parent c73dcb3 commit d2d1fa8
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 53 deletions.
11 changes: 0 additions & 11 deletions example/asd.md

This file was deleted.

11 changes: 11 additions & 0 deletions example/nested/lol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Aborting requests in React Native
date: 2019-04-14 11:11:30
description: How can we close an active connection initiated with Fetch? And most important, what do we mean by "closing a connection"?
image: "./closed-connection.jpg"
imageAlt: market closed sign
slug: blog/aborting-fetch-react-native
draft: false
---

test2
25 changes: 12 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const chalk = require("chalk");
const { buildGqlClient } = require("./src/client");
const { createModel } = require("./src/createModel");
const createModel = require("./src/createModel");
const prompts = require("prompts");
const fetchMds = require("./src/fetchMds");
const extractModel = require("./src/extractModel");
const uploadMds = require("./src/uploadMds");

const argv = hideBin(process.argv);

Expand Down Expand Up @@ -36,27 +37,25 @@ yargs(argv)
if (!argv.path) {
return console.error(chalk.red("You must specify a path"));
}
// if (!argv.url) {
// return console.error(chalk.red("You must specify your graphcms url"));
// }
// if (!argv.token) {
// return console.error(chalk.red("You must specify your graphcms token"));
// }
if (!argv.url) {
return console.error(chalk.red("You must specify your graphcms url"));
}
if (!argv.token) {
return console.error(chalk.red("You must specify your graphcms token"));
}

const response = await prompts({
type: "text",
name: "modelName",
message: 'How do you want to call the model? (Defaults to "Post")',
});

const files = await fetchMds(argv.path);
const model = extractModel(files?.[0]);
const mds = await fetchMds(argv.path);
const model = extractModel(mds?.[0]);

await createModel(argv.url, argv.token, model, response.modelName);

console.log(model);

// await buildGqlClient(argv.url, argv.token);
await buildGqlClient(argv.url, argv.token);
await uploadMds(mds, response.modelName, argv.token, argv.url);
}
)
.option("verbose", {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A CLI tool to automate the migration from markdown files to graphcms (useful for gatsby blogs for example and everything else)",
"main": "index.js",
"scripts": {
"start": "./index.js migrate --path ./example",
"start": "./index.js migrate --path ./example -u https://api-eu-central-1.graphcms.com/v2/cklz96ohfx2ou01xp2iff9hxc/master",
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
Expand Down
3 changes: 1 addition & 2 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const { GraphQLClient } = require("graphql-request");
const queries = require("./queries");

let client;

const getGqlClient = () => client;

const buildGqlClient = (endpoint, token) => {
client = new GraphQLClient(endpoint, {
headers: { Authorization: `Bearer ${token}` },
headers: { authorization: `Bearer ${token}` },
});
};

Expand Down
33 changes: 17 additions & 16 deletions src/createModel.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
const { newMigration, FieldType } = require("@graphcms/management");
const { newMigration } = require("@graphcms/management");

const createModel = async (url, token, model, modelName = "Post") => {
try {
const migration = newMigration({
authToken: token,
endpoint: url,
name: "Creating post model",
});

migration.createModel({
apiId: modelName,
apiIdPlural: `${modelName}s`,
displayName: modelName,
});

model.forEach((modelField) => {
migration.addSimpleField({
apiId: modelField.name,
displayName: modelField.name,
type: FieldType.String,
});
});
model.reduce(
(acc, modelField) => {
acc.addSimpleField({
apiId: modelField.name,
displayName: modelField.name,
type: modelField.type,
});
return acc;
},
migration.createModel({
apiId: modelName,
apiIdPlural: `${modelName}s`,
displayName: modelName,
})
);

const { errors, name } = await migration.run(true);

Expand All @@ -33,7 +34,7 @@ const createModel = async (url, token, model, modelName = "Post") => {
console.log(name);
}
} catch (e) {
console.log("errors creating model");
console.log("errors creating model", e);
}
};

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

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

if (!data) {
throw new Error("The first MD doesn't seem to have a yaml frontmatter");
}

return Object.keys(data).map((frontKey) => {
return Object.keys({ ...data, content: "" }).map((frontKey) => {
const value = data[frontKey];

if (frontKey === "content") {
return {
name: "content",
type: FieldType.Richtext,
};
}
if (typeof value === "boolean") {
return {
name: string.sanitize(frontKey),
Expand Down
7 changes: 0 additions & 7 deletions src/queries.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/uploadMds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { gql } = require("graphql-request");
const { getGqlClient } = require("./client");
const matter = require("gray-matter");

const produceMutation = (modelName = "Post") => gql`
mutation uploadMds($data: ${modelName}CreateInput!) {
create${modelName}(data: $data) {
id
}
}
`;

const uploadMds = (mds, modelName) => {
const client = getGqlClient();
return Promise.all(
mds.map((md) => {
const { content, data } = matter(md);
// need to create AST from markdown
return client.request(produceMutation(modelName), {
data: { content: content, ...data },
});
})
);
};

module.exports = uploadMds;

0 comments on commit d2d1fa8

Please sign in to comment.