-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd
executable file
·128 lines (113 loc) · 2.59 KB
/
add
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
#!/usr/bin/env node
require('dotenv').config()
const path = require('path')
const inquirer = require('inquirer')
const links = require('./links')
const { parseTags, scrapePageTitle, isTweet } = require('./src/helpers')
const addLink = require('./src/addLink')
const writ = require('./src/writ')
const git = require('./src/git')
const publish = require('./src/publish')
let linkIsTweet
const questions = [
{
type: 'input',
name: 'url',
message: 'URL',
async validate(value) {
if (!value) {
return 'Url is required'
}
const url = value
linkIsTweet = isTweet(url)
const alreadyAdded = links.find(link =>
link.url === url || (
link.url.match(/\/$/) && link.url.slice(0, -1) === url
)
)
if (alreadyAdded) {
return 'This link already exists.'
}
return true
},
filter: String
},
{
type: 'input',
name: 'title',
message: 'Title',
async default({ url }) {
try {
return await scrapePageTitle(url)
} catch (e) {
console.log("Couldn't scrape title, enter manually", e)
return undefined
}
},
validate(value) {
return !!value || 'Title is required'
},
filter: String
},
{
type: 'input',
name: 'RTQuote',
message: 'Retweet quote',
filter: String,
when: () => linkIsTweet
},
{
type: 'input',
name: 'tags',
message: 'Tags (comma separated)',
filter: String
},
{
type: 'confirm',
name: 'isConfirmed',
message: 'Sure?',
default: true
},
{
type: 'confirm',
name: 'shouldPublish',
message: 'Publish now?',
default: true
}
]
console.log('Add a new link')
inquirer.prompt(questions).then(async answers => {
const tags = parseTags(answers.tags)
const twitterOptions = linkIsTweet ? {
isTweet: true,
retweetQuote: answers.RTQuote
} : {}
const link = {
url: answers.url,
title: answers.title,
tags,
datePublished: Date.now(),
...twitterOptions
}
if (!answers.isConfirmed) {
return console.log('OK, link is not added.')
}
await addLink({
outputPath: './links.json',
link
})
await writ.build({
links: links.concat(link),
rootDirectory: '.'
})
console.log('Added link:', JSON.stringify(link, null, 2))
// Commit msg with this mark will trigger gh-action publish workflow
const messagePrefix = answers.shouldPublish ? '[publish] ' : ''
await git.commit({
message: `${messagePrefix}Add: ${link.url}`,
paths: ['links.json']
})
if (answers.shouldPublish) {
await git.push()
}
})