forked from giacomocerquone/graphcms-markdown-migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·157 lines (150 loc) · 4.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const { buildGqlClient } = require("./src/client");
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 { capitalize, logger, spinner, restartSpinner } = require("./src/utils");
const { fetchFilenames } = require("./src/fetchFilenames");
const path = require("path");
const argv = hideBin(process.argv);
yargs(argv).command(
"$0 [path] [url] [token]",
"start the migration of the md files from the specified path to the specified GraphCMS instance.",
(yargs) => {
return (
yargs
.option("path", {
alias: "p",
type: "string",
description: "Path of the folder of the md files to migrate.",
})
.option("url", {
alias: "u",
type: "string",
description: "Your graphCMS URL",
})
// .option("publish", {
// alias: "p",
// type: "boolean",
// description:
// "Set this if you want your post to be published (defaults to draft posts)",
// })
.option("token", {
alias: "t",
type: "string",
description: "Your graphCMS token",
})
.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)',
})
.option("exclude", {
type: "string",
description:
'Comma separated list of fields to exclude from your mds frontmatter: "field1, field2, field3"',
})
.option("model-from", {
type: "string",
description:
'The filename of the specific MD file to use to extract the model (you must add the ".md" extension when specifying it)',
})
.option("model-name", {
type: "string",
description:
'The name of the model that will be created in GraphCMS (Defaults to "Post")',
})
);
},
async (argv) => {
if (!argv.path) {
return logger.error("You must specify a path");
}
if (!argv.url) {
return logger.error("You must specify your graphcms url");
}
if (!argv.token) {
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"
);
}
if (!argv["exclude"]) {
logger.warn(
"You didn't specify any yaml field to exclude, therefore all the fields will be taken"
);
}
if (!argv["model-from"]) {
logger.warn(
"You didn't specify any MD file to extract the model from, therefore the first one will be used"
);
}
if (!argv["model-name"]) {
logger.warn(
'You didn\'t specify a name for the model that will be created, therefore "Post" will be used'
);
}
let promptsRes = {};
if (!argv["model-name"]) {
promptsRes = await prompts(
{
type: "text",
name: "modelName",
message: 'How do you want to call the model? (Defaults to "Post")',
},
{ onCancel: () => process.exit() }
);
}
promptsRes.modelName = promptsRes.modelName || argv["model-name"] || "Post";
try {
restartSpinner("Fetching and parsing mds file");
const fileNames = await fetchFilenames(argv.path);
const mds = await fetchMds(fileNames);
if (!mds.length) {
spinner.succeed();
logger.error("No md file found!");
process.exit();
}
const extractModelFrom = argv["model-from"]
? mds[
fileNames.findIndex(
(fN) => path.basename(fN) === argv["model-from"]
)
]
: mds[0];
restartSpinner("Extracting model from first md file");
const model = extractModel(
extractModelFrom,
argv["thumb-field"],
argv.exclude ? argv.exclude.split(", ") : []
);
restartSpinner("Creating the model inside your graphCMS instance");
await createModel(
argv.url,
argv.token,
model,
capitalize(promptsRes.modelName)
);
restartSpinner("Creating the mds inside your graphCMS instance");
await buildGqlClient(argv.url, argv.token);
await uploadMds(
mds,
capitalize(promptsRes.modelName),
argv["thumb-field"],
argv.token,
argv.url
);
spinner.succeed();
} catch (e) {
spinner.fail();
logger.error("Something went wrong", e);
process.exit();
}
}
).argv;