forked from BeAPI/beapi-frontend-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
153 lines (145 loc) · 4.22 KB
/
app.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
const axios = require('axios')
const inquirer = require('inquirer')
const fuzzy = require('fuzzy')
const Promise = require('promise')
const download = require('download-file')
const reqUrl = {
scss: 'https://api.github.com/repos/BeAPI/bff-components/contents/scss',
js: 'https://api.github.com/repos/BeAPI/bff-components/contents/js',
html: 'https://api.github.com/repos/BeAPI/bff-components/contents/html',
}
let files = []
let fileOpts = {}
console.log('Hi, welcome to composerjs')
// Init autocomplete prompt type
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'))
inquirer
.prompt({
type: 'list',
name: 'req',
message: 'What kind of snippet do you need?',
choices: ['scss', 'js', 'html'],
})
.then(data => {
fileOpts.ext = data.req
axios
.get(reqUrl[data.req])
.then(res => {
files = res.data.map(file => file.name)
inquirer
.prompt({
type: 'autocomplete',
name: 'file',
message: 'Which file you need to download ?',
source: searchFiles,
})
.then(data => {
fileOpts.name = data.file
let dirPath = setDirPath(fileOpts.ext)
axios.get(reqUrl[fileOpts.ext]).then(res => {
let remoteFile = res.data.find(f => f.name === fileOpts.name)
fileOpts.downloadUrl = remoteFile.download_url
inquirer
.prompt({
type: 'list',
name: 'path',
message: 'Where should I put the downloaded file ?',
choices: dirPath,
})
.then(data => {
fileOpts.path = data.path
let downloadOpts = {
directory: fileOpts.path,
filename: fileOpts.name,
}
download(fileOpts.downloadUrl, downloadOpts, err => {
if (err) {
throw err
}
console.log('\n\nYour snippet was download successfully 😃\n')
printImportToConsole()
console.log('\n')
})
})
})
})
})
.catch(error => {
console.log(error)
})
})
/**
* FuzzySort to search file in prompt
* @param {array} answers
* @param {*} input
*/
function searchFiles(answers, input) {
input = input || ''
return new Promise(resolve => {
setTimeout(() => {
let fuzzyResult = fuzzy.filter(input, files)
resolve(
fuzzyResult.map(el => {
return el.original
})
)
}, 100)
})
}
/**
* Prompt default path dir from BFF theme
* @param {string} ext
* @return {array}
*/
function setDirPath(ext) {
if (ext === 'scss') {
return [
'../src/css/patterns/',
'../src/css/components/',
'../src/css/pages/',
'../src/css/plugins/',
'../src/css/root/',
'../src/css/vendor/',
]
} else if (ext === 'js') {
return ['../src/js/src/', '../src/js/vendor', '../src/js/vendor_async']
} else if (ext === 'html') {
return ['../src/templates', '../src/templates/blocks', '../src/templates/plugins', '../src/templates/widgets']
}
}
/**
* Print import details to the console
*/
function printImportToConsole() {
if (fileOpts.ext === 'scss') {
let sassPath = formatSassPath()
let sassFileName = formatSassFileName()
console.log('You can now add this line in your style.scss')
console.log(`@import "${sassPath}${sassFileName}"`)
} else if (fileOpts.ext === 'js') {
console.log('You can require your script where you need it')
} else if (fileOpts === 'html') {
console.log("You can include your php files with <?php include '' ?>")
}
}
/**
* Format sass path for default style.css file
* @return {string}
*/
function formatSassPath() {
let path = fileOpts.path
path = path.split('/')
return `${path[path.length - 2]}/`
}
/**
* Remove _ and extension form sass file name
* @return {string}
*/
function formatSassFileName() {
let fileName = fileOpts.name
if (fileName[0] === '_') {
fileName = fileName.substr(1)
}
fileName = fileName.replace(/\.[^/.]+$/, '')
return fileName
}