forked from ProtoSchool/protoschool.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
141 lines (116 loc) · 4.08 KB
/
Copy pathutils.js
File metadata and controls
141 lines (116 loc) · 4.08 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
const promisify = require('util').promisify
const fs = require('fs')
const inquirer = require('inquirer')
const log = require('npmlog')
const api = require('../../../src/api')
// *** DATA FETCHING & MANIPULATION ***
async function saveStaticJsonFile (file, data) {
return promisify(fs.writeFile)(`src/static/${file.replace('.json', '')}.json`, JSON.stringify(data, null, 2))
}
// *** SHARED INPUT VALIDATION ***
function validateStringPresent (string) {
if (string.trim()) {
return true
} else {
return `Oops! You can't leave this blank, but you'll have a chance to edit it later.`
}
}
// *** TRANSITIONAL DIALOGS & PROMPTS (INQUIRER) ***
async function selectTutorial (newItemType, { createTutorial, createResource, createLesson }) {
let tutorial
let tutorials = await api.tutorials.list.get()
let latestTutorial = await api.tutorials.list.getLatest()
const tutorialResponses1 = await inquirer.prompt([
{
type: 'confirm',
name: 'latestTutorial',
message: `Should we add your ${newItemType} to the "${latestTutorial.title}" tutorial?`
}
])
// set data to latest tutorial
if (tutorialResponses1.latestTutorial) {
tutorial = latestTutorial
// offer selection of existing tutorials or creating a new one
} else {
let tutorialChoices = [
new inquirer.Separator(),
{ name: '+ Create new tutorial', value: 'new' },
new inquirer.Separator()
]
Object.keys(tutorials).sort().forEach(tutorialId => {
tutorialChoices.push({ name: tutorials[tutorialId].title, value: tutorialId })
})
const tutorialResponses2 = await inquirer
.prompt([
{
type: 'list',
name: 'tutorialId',
message: `Which of these existing tutorials should we add your ${newItemType} to?`,
choices: tutorialChoices.reverse()
}
])
// set data based on other selected tutorial
if (tutorialResponses2.tutorialId !== 'new') {
tutorial = tutorials[tutorialResponses2.tutorialId]
// create new tutorial and set data accordingly
} else {
tutorial = await createTutorial({ createLesson, createResource }, { skipPromptLesson: true })
}
}
return tutorial
}
async function promptRepeat (type) {
const { confirm } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `Would you like to add another ${type}?`
}
])
return confirm
}
async function promptCreateFirst (itemType, tutorialId) {
let tutorial = await api.tutorials.get(tutorialId)
const { confirm } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `Are you ready to add your first ${itemType} to the "${tutorial.title}" tutorial?`
}
])
return confirm
}
// *** LOGGING ***
function logEverythingDone (tutorial) {
log.info(`Awesome work! "${tutorial.title}" has both lesson files and resources!`)
logPreview('your tutorial', tutorial.url)
log.info(`To create the content of your lessons, edit the files in the \`src/tutorials/${tutorial.formattedId}-${tutorial.url}/\` directory. (Learn more at https://bit.ly/protoschool-content.)`)
log.info(`To update your tutorial's title, description, or resources, edit its entry in the \`src/static/tutorials.json\` file. (Learn more at https://bit.ly/protoschool-metadata.)`)
logGuide()
}
function logList (message, items) {
log.info(`${message}:
‣ ${items.join('\n ‣ ')}`)
}
function logPreview (item, tutorialUrl, pageUrl = '') {
log.info(`To preview ${item}, first run \`npm start\` in a separate terminal window or tab, then visit this page in your web browser: http://localhost:3000/#/${tutorialUrl}/${pageUrl}`)
}
function logCreateLater (items) {
log.info(`Okay, no problem. You can run the ProtoWizard later to add ${items}.`)
logGuide()
}
function logGuide () {
log.info(`View our detailed guide to developing tutorials at: https://bit.ly/protoschool-developing`)
}
module.exports = {
saveStaticJsonFile,
logEverythingDone,
logList,
promptCreateFirst,
promptRepeat,
selectTutorial,
validateStringPresent,
logPreview,
logGuide,
logCreateLater
}