forked from mathieudutour/medium-to-own-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·235 lines (200 loc) · 6.43 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env node
const { spawn } = require('child_process')
const inquirer = require('inquirer')
const ora = require('ora')
const JSZip = require('jszip')
const fs = require('fs-extra')
const { getProfile } = require('./get-profile')
const { getMarkdownFromPost } = require('./generate-md')
const { addGatsbyFiles } = require('./add-gatsby-files')
const { exec, withOutputPath } = require('./utils')
// eslint-disable-next-line no-console
console.log(` -------------------------
Hello there!
Let's move your Medium content to your very own website.
First of all, you need to download your medium content.
1. Head over https://medium.com/me/export
2. Click on the "Download your information" button
In a bit, you will receive an email from Medium with a link to "Download your archive".
Download it and drag-and-drop the resulting zip file here.
`)
let spinner
let profile = {}
inquirer
.prompt([
{
name: 'archivePath',
message: 'Path to the Medium archive',
},
])
.then(({ archivePath }) => {
spinner = ora('Parsing Medium content').start()
return fs.readFile(archivePath.trim())
})
.then(body => {
return JSZip.loadAsync(body)
})
.then(zip => {
if (!zip.file('profile/profile.html')) {
throw new Error(
'It seems that we cannot find your profile in the Medium archive. Are you sure you gave the right path?\nIf so, please open an issue here: https://github.com/mathieudutour/medium-to-own-blog/issues/new.'
)
}
spinner.succeed('Parsed Medium content')
spinner.start('Parsing the Medium profile')
return getProfile(zip)
.then(_profile => {
profile = _profile
})
.then(() => zip)
})
.then(zip => {
spinner.succeed('Parsed the Medium profile')
spinner.start('Parsing the Medium posts')
return zip.file(/^posts/).reduce((p, zippedPost, index, array) => {
return p
.then(() => {
spinner.start(
`Parsing the Medium posts [${index + 1}/${array.length}]`
)
})
.then(() => zippedPost.async('text'))
.then(content => getMarkdownFromPost(profile, content, zippedPost.name))
}, Promise.resolve())
})
.then(() => {
spinner.succeed('Parsed the Medium posts')
spinner.start('Preparing the Gatsby project')
return addGatsbyFiles(profile)
})
.then(() => {
spinner.succeed('Prepared the Gatsby project')
spinner.stop()
// eslint-disable-next-line no-console
console.log(`
-------------------------
Yay, your blog is ready!
Now let's work on putting it online.
GitHub is a free platform to host code online.
1. Head over https://github.com/new and create a new repository
2. Copy paste the URL of your new repository here
`)
return inquirer.prompt([
{
name: 'repoURL',
message: 'URL of the repository',
},
])
})
.then(({ repoURL }) => {
repoURL = repoURL.trim()
if (!repoURL) {
throw new Error(`
Looks like the repository's URL is empty.
You can still use the generated project as a normal Gatsby project.
You will find some information about the generated project in its README.
Happy writing!`)
}
spinner.start('Updating the project to use the repository URL')
const remoteURL = repoURL
if (repoURL.match(/^git@github.com:.*/i)) {
repoURL = repoURL
.replace(/^git@github.com:.*/i, 'https://github.com/')
.replace(/\.git$/i, '')
}
return Promise.all([
fs
.readFile(withOutputPath(profile, './package.json'), 'utf8')
.then(content =>
fs.writeFile(
withOutputPath(profile, './package.json'),
content.replace(/{{ githubURL }}/g, repoURL || ''),
'utf8'
)
),
fs
.readFile(withOutputPath(profile, './gatsby-config.js'), 'utf8')
.then(content =>
fs.writeFile(
withOutputPath(profile, './gatsby-config.js'),
content.replace(/{{ githubURL }}/g, repoURL || ''),
'utf8'
)
),
]).then(() =>
exec(
`git init && git remote add origin ${remoteURL} && git add . && git commit -m "first commit :tada:"`,
{
cwd: withOutputPath(profile),
}
)
)
})
.then(() => {
spinner.succeed('Updated the project to use the repository URL')
spinner.stop()
// eslint-disable-next-line no-console
console.log(
'Pushing the code for your blog to GitHub (you might be prompted for your GitHub identifiers)...\n'
)
return new Promise((resolve, reject) => {
const child = spawn('git', ['push', 'origin', 'master'], {
cwd: withOutputPath(profile),
stdio: 'inherit',
})
child.on('error', () => reject())
child.on('close', () => resolve())
child.on('exit', () => resolve())
})
})
.then(() => {
// eslint-disable-next-line no-console
console.log(`
-------------------------
Now that the code for your blog is online,
we need to configure how it will be deployed.
Netlify is hosting your personal project for free.
1. Head over https://app.netlify.com/start
2. Connect with GitHub
3. Pick the repository you just created
4. Click on the big green "Deploy Site" button
Netlify probably gave a funny name to your project,
like friendly-keller-0b06be or something,
but you can choose one by go to the "Site Settings"
and clicking on "Change site name".
`)
return inquirer.prompt([
{
name: 'dontCare',
message: 'Press enter when you are ready',
},
])
})
.then(() => {
// eslint-disable-next-line no-console
console.log(`
-------------------------
Your blog is ready to go! 🙌
We created a folder called "${profile.mediumUsername}-blog" with
everything needed inside.
Your blog posts are in the "content" sub-folder but the first thing
you will want to edit is the "gatsby-config.js" file. It contains
a few values like your bio or your social media links that you should edit.
Every time you will push to the master branch on GitHub, your blog will be
deployed. If that sentence doesn't make sense to you, you can check the guides on
https://github.com/mathieudutour/medium-to-own-blog/master/tree/master/docs.
Happy blogging!
...
`)
})
.then(() => process.exit(0))
.catch(err => {
if (spinner) {
spinner.fail()
}
// eslint-disable-next-line no-console
console.log()
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})