-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.js
197 lines (166 loc) · 5.66 KB
/
link.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
const path = require('path')
const ora = require('ora')
const inquirer = require('inquirer')
const inquirerFileTreeSelection = require('inquirer-file-tree-selection-prompt')
const { readdirSync, existsSync } = require('fs')
const Command = require('../base.js')
const { flags } = require('@oclif/command')
const { app } = require('../config/firebase')
const credentials = require('../config/credentials')
const api = require('../config/axios')
const fuzzy = require('fuzzy')
const ManifestService = require('../services/manifest.js')
const PackageService = require('../services/package.js')
const packageService = new PackageService()
inquirer.registerPrompt('file-tree-selection', inquirerFileTreeSelection)
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'))
class LinkExtensionCommand extends Command {
init () {
super.init({ injectProjectRoot: true })
}
async run () {
if (this.args.entryPointPath && !existsSync(this.args.entryPointPath)) {
throw new Error(
`O arquivo de ponto de entrada de extensão fornecido não foi encontrado em '${this.args.entryPointPath}', certifique-se de que o arquivo existe`
)
}
if (
this.args.entryPointPath &&
!this.args.entryPointPath.endsWith('.vue')
) {
throw new Error(
'O arquivo de ponto de entrada de extensão deve ser um arquivo .vue'
)
}
const { selectedEntryPoint } = await inquirer.prompt([
{
name: 'selectedEntryPoint',
message: 'Qual é o entry point (arquivo principal) da sua extensão?',
type: 'file-tree-selection',
validate: file => file.endsWith('.vue'),
hideRoot: true,
when: !this.args.entryPointPath
}
])
const spinner = ora({
text: 'Buscando extensões',
spinner: 'dots3'
}).start()
const extensions = await this.listExtensions(
credentials.institution,
this.flags.build
).catch(err => {
spinner.fail('Falha ao carregar extensões')
throw err
})
if (extensions.length === 0) {
spinner.fail('Não encontramos nenhuma extensão.')
return
}
spinner.succeed('Lista de extensões obtida')
const extensionsChoices = extensions.map(ext => ({
name: ext.title,
value: ext
}))
const { selectedExtension } = await inquirer.prompt([
{
name: 'selectedExtension',
message: 'Escolha sua extensão:',
type: 'autocomplete',
choices: extensionsChoices,
searchText: 'Carregando...',
emptyText: 'Nem resultado encontrado para a pesquisa realizada',
source: function (answersSoFar, input) {
if (input) {
const fuzzyResult = fuzzy.filter(
input,
extensionsChoices.map(e => e.name)
)
return fuzzyResult.map(fr => extensionsChoices[fr.index])
} else {
return extensionsChoices
}
}
}
])
const absoluteExtensionPath = path.resolve(
this.args.entryPointPath || selectedEntryPoint
)
await packageService.addExtensionToPackageJson(
absoluteExtensionPath,
this.projectRoot
)
this.upsertManifest({
manifestPath: path.resolve(
path.dirname(absoluteExtensionPath),
'manifest.json'
),
extensionData: selectedExtension,
institution: credentials.institution
})
this.logger.success('Extensão selecionada! \\o/')
this.logger.success(
'Para desenvolver, execute qt serve para fazer deploy execute qt deploy'
)
}
upsertManifest ({ manifestPath, extensionData, institution }) {
const manifest = new ManifestService(manifestPath)
manifest.extensionId = extensionData.id
manifest.extensionStorageId = extensionData.extensionStorageId
manifest.type = extensionData.type === 'Com build' ? 'build' : 'noBuild'
manifest.name = extensionData.title
manifest.extensionUUID = extensionData.extensionUUID
manifest.meta = extensionData.meta
manifest.institution = institution
manifest.save()
return manifest
}
async listExtensions (institution, withBuild) {
const token = await app.auth().currentUser.getIdToken()
const result = await api.axios.get(`/${institution}/dynamic-components/`, {
headers: {
Authorization: `Bearer ${token}`
}
})
let extensions
if (typeof withBuild !== 'undefined') {
extensions = result.data.filter(
extension =>
(withBuild && extension.type === 'Com build') ||
(!withBuild && extension.type === 'Sem build')
)
} else {
extensions = result.data
}
return extensions
}
getFilesAsChoices (fileExtensionsAllowed = ['.vue']) {
const dirents = readdirSync(process.cwd(), { withFileTypes: true })
const filesNames = dirents
.filter(dirent => dirent.isFile())
.filter(dirent =>
fileExtensionsAllowed.some(fileExt => dirent.name.endsWith(fileExt))
)
.map(dirent => dirent.name)
return filesNames.map(fileName => ({ name: fileName, value: fileName }))
}
static aliases = ['l', 'link-extension', 'link']
static description = 'Faça um link de uma extensão no Quoti com o seu código'
static args = [
{
name: 'entryPointPath',
required: false,
description: 'Endereço do entry point (arquivo principal) da extensão'
}
]
static flags = {
build: flags.boolean({
allowNo: true,
char: 'b',
description:
'Use (--build|-b) se você está selecionando uma extensão com build ou use --no-build se você está selecionando uma extensão sem build',
exclusive: []
})
}
}
module.exports = LinkExtensionCommand