-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportCommand.js
More file actions
179 lines (149 loc) · 5.5 KB
/
importCommand.js
File metadata and controls
179 lines (149 loc) · 5.5 KB
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
const fs = require('fs')
const path = require('path')
const { Command } = require('commander')
const matter = require('gray-matter')
const uuid = require('@polyblog/polyblog-js-client/uuid.js')
const addOrUpdateArticle = require('@polyblog/polyblog-js-client/addOrUpdateArticle.js')
const rehydrateSession = require('./session/rehydrateSession.js')
const isLoggedInSession = require('./session/isLoggedInSession.js')
const getBlog = require('./getBlog.js')
const getFiles = require('./getFiles.js')
const login = require('./login.js')
function importCommand() {
const command = new Command('import')
command.option('--blog [blog]', 'blog id or name')
command.action(async options => {
try {
console.log('import')
rehydrateSession()
if (!isLoggedInSession()) {
await login()
}
let blog = await getBlog(options?.blog)
let defaultLocale = blog.defaultLocale || 'en'
console.log({ blog, defaultLocale })
let files = getFiles()
const { locales, paths } = blog
console.log({ locales, paths })
let articles = []
paths.forEach(path_ => {
if (path_.includes('%lang%') || path_.includes('%LANG%')) {
const suffix = path_
.replace('%LANG%', defaultLocale)
.replace('%lang%', defaultLocale)
.replace('%slug%', '(?<slug>.+)')
.replace('%SLUG%', '(?<slug>.+)')
.replace('%YYYY%', '(?<YYYY>[0-9]{4})')
.replace('%MM%', '(?<MM>[0-9]{2})')
.replace('%DD%', '(?<DD>[0-9]{2})')
let endsWithRegex = new RegExp(`${suffix}$`)
let matchingFiles = files.filter(file => endsWithRegex.test(file))
console.log(
`Found ${matchingFiles.length} matching files for path ${path_}`,
)
// matchingFiles = [
// '/_i18n/en/_posts/2010-09-03-welcome-dnsimple-blog.markdown',
// ]
// console.log({ matchingFiles })
if (matchingFiles.length === 0) {
console.log(`No matching files for path ${path_}`)
} else {
matchingFiles.forEach(file => {
// console.log(` ${file}`)
if (file.startsWith('/')) {
file = file.substring(1)
}
let filePath = path.resolve(file)
let fileString = fs.readFileSync(filePath, 'utf8')
// console.log({ fileString })
let json = matter(fileString)
json = {
...json.data,
content: json.content,
excerpt: json.excerpt || undefined,
}
console.log({ ...json, content: '...' })
let match = new RegExp(suffix).exec(file)
let { YYYY, MM, DD } = match?.groups || {}
const _id = uuid()
let description = json.description
if (!description) {
description = json.excerpt
delete json.excerpt
}
if (!json.date) {
json.date = `${YYYY}-${MM}-${DD} 00:00:00`
}
if (json.date?.length === 19) {
json.date += '.000Z'
}
const creationTime = json.date
? new Date(json.date).toISOString()
: new Date().toISOString()
let { published, slug } = json
delete json.published
slug = slug || match?.groups?.slug
slug = `${YYYY}/${MM}/${slug}`
if (!slug) {
throw new Error(`Could not find slug for file ${file}`)
}
if (typeof variable !== 'boolean') {
published = true
}
let article = {
_id,
creationTime,
lastEditTime: creationTime,
googleTranslate: false,
locale: defaultLocale,
title: json.title,
description,
slug,
content: json.content,
author: json.author,
published,
categories: [],
originalArticleId: _id,
format: blog.articlesFormat || 'markdown',
organizationId: blog.organizationId,
blogId: blog._id,
blog: blog.name,
metadata: {
...json,
title: undefined,
description: undefined,
content: undefined,
language: undefined,
locale: undefined,
author: undefined,
slug: undefined,
slugOriginal: undefined,
date: undefined,
},
}
article = JSON.parse(JSON.stringify(article))
console.log({ ...article, content: '...' })
articles.push(article)
})
}
} else {
console.log(`Path ${path_} does not include %lang%`)
}
})
for (let i = 0; i < articles.length; i += 1) {
let article = articles[i]
try {
await addOrUpdateArticle(article)
console.log(`${i + 1}: imported article ${article.slug}`)
} catch (error) {
console.log(`error for article ${article.slug}`, error)
}
}
console.log(`Imported ${articles.length} articles`)
} catch (error) {
console.log('error', error)
}
})
return command
}
module.exports = importCommand