From d2d1fa8583dcf008f2cfcf4556f6fecd75293849 Mon Sep 17 00:00:00 2001 From: Giacomo Cerquone Date: Tue, 9 Mar 2021 02:36:42 +0100 Subject: [PATCH] feat: finish implementation. Missing ast generation --- example/asd.md | 11 ----------- example/nested/lol.md | 11 +++++++++++ index.js | 25 ++++++++++++------------- package.json | 2 +- src/client.js | 3 +-- src/createModel.js | 33 +++++++++++++++++---------------- src/extractModel.js | 12 +++++++++--- src/queries.js | 7 ------- src/uploadMds.js | 26 ++++++++++++++++++++++++++ 9 files changed, 77 insertions(+), 53 deletions(-) delete mode 100644 example/asd.md delete mode 100644 src/queries.js create mode 100644 src/uploadMds.js diff --git a/example/asd.md b/example/asd.md deleted file mode 100644 index 0af88ee..0000000 --- a/example/asd.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -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 diff --git a/example/nested/lol.md b/example/nested/lol.md index e69de29..0af88ee 100644 --- a/example/nested/lol.md +++ b/example/nested/lol.md @@ -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 diff --git a/index.js b/index.js index 67bc6f9..7930953 100755 --- a/index.js +++ b/index.js @@ -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); @@ -36,12 +37,12 @@ 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", @@ -49,14 +50,12 @@ yargs(argv) 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", { diff --git a/package.json b/package.json index fd826a9..c0c647b 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/client.js b/src/client.js index b032cbf..9bccc4f 100644 --- a/src/client.js +++ b/src/client.js @@ -1,5 +1,4 @@ const { GraphQLClient } = require("graphql-request"); -const queries = require("./queries"); let client; @@ -7,7 +6,7 @@ const getGqlClient = () => client; const buildGqlClient = (endpoint, token) => { client = new GraphQLClient(endpoint, { - headers: { Authorization: `Bearer ${token}` }, + headers: { authorization: `Bearer ${token}` }, }); }; diff --git a/src/createModel.js b/src/createModel.js index 3b6ce1b..9363bd0 100644 --- a/src/createModel.js +++ b/src/createModel.js @@ -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); @@ -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); } }; diff --git a/src/extractModel.js b/src/extractModel.js index a8a3e46..41da6f4 100644 --- a/src/extractModel.js +++ b/src/extractModel.js @@ -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), diff --git a/src/queries.js b/src/queries.js deleted file mode 100644 index f998313..0000000 --- a/src/queries.js +++ /dev/null @@ -1,7 +0,0 @@ -const { gql } = require("graphql-request"); - -const postModelCreation = gql``; - -module.exports = { - postModelCreation, -}; diff --git a/src/uploadMds.js b/src/uploadMds.js new file mode 100644 index 0000000..e2eb1bd --- /dev/null +++ b/src/uploadMds.js @@ -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;