-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
executable file
·146 lines (122 loc) · 4.41 KB
/
fetch.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
#!/usr/bin/env node
import fs from 'fs'
import chalk from 'chalk'
import { loadModule } from './require.js'
import { program } from './program.js'
import * as fetch from '@symbo.ls/fetch'
import * as utils from '@domql/utils'
import { convertFromCli } from './convert.js'
import { createFs } from './fs.js'
const { isObjectLike } = (utils.default || utils)
const { fetchRemote } = (fetch.default || fetch)
const RC_PATH = process.cwd() + '/symbols.json'
const LOCAL_CONFIG_PATH =
process.cwd() + '/node_modules/@symbo.ls/init/dynamic.json'
const DEFAULT_REMOTE_REPOSITORY = 'https://github.com/symbo-ls/default-config/'
const DEFAULT_REMOTE_CONFIG_PATH = "https://api.symbols.app/"; // eslint-disable-line
const API_URL_LOCAL = 'http://localhost:13335/get'
const API_URL = 'https://api.symbols.app/get'
const rcFile = loadModule(RC_PATH); // eslint-disable-line
const localConfig = loadModule(LOCAL_CONFIG_PATH); // eslint-disable-line
const debugMsg = chalk.dim(
'Use --verbose to debug the error or open the issue at https://github.com/symbo-ls/smbls'
)
let rc = {}
try {
rc = loadModule(RC_PATH); // eslint-disable-line
} catch (e) {
console.error('Please include symbols.json to your root of respository')
}
export const fetchFromCli = async (opts) => {
const { dev, verbose, prettify, convert: convertOpt, metadata: metadataOpt, update, force } = opts
await rc.then(async (data) => {
const { key, framework, distDir, metadata } = data
const endpoint = dev ? API_URL_LOCAL : API_URL
console.log('\nFetching from:', chalk.bold(endpoint), '\n')
const body = await fetchRemote(key, {
endpoint,
metadata: metadata || metadataOpt,
onError: (e) => {
console.log(chalk.red('Failed to fetch:'), key)
if (verbose) console.error(e)
else console.log(debugMsg)
}
})
// console.log('ON FETCH:')
// console.log(body.components.Configuration)
if (!body) return
const { version, ...config } = body
if (body.designsystem) {
body.designSystem = body.designsystem
delete body.designsystem
}
if (verbose) {
if (key) {
console.log(
chalk.bold('Symbols'),
'data fetched for',
chalk.green(body.name)
)
} else {
console.log(
chalk.bold('Symbols'),
'config fetched from',
chalk.bold('default-config from:'),
chalk.dim.underline(DEFAULT_REMOTE_REPOSITORY)
)
}
console.log()
}
for (const t in config) {
const type = config[t]
const arr = []
if (isObjectLike(type)) {
for (const v in type) arr.push(v)
if (arr.length) {
console.log(chalk.dim(t + ':'))
console.log(chalk.bold(arr.join(', ')))
} else {
console.log(chalk.dim(t + ':'), chalk.dim('- empty -'))
}
} else console.log(chalk.dim(t + ':'), chalk.bold(type))
}
if (!distDir) {
const bodyString = JSON.stringify(body, null, prettify ?? 2)
try {
await fs.writeFileSync(LOCAL_CONFIG_PATH, bodyString)
if (verbose) {
console.log(chalk.dim('\ndynamic.json has been updated:'))
console.log(chalk.dim.underline(LOCAL_CONFIG_PATH))
}
console.log(chalk.bold.green('\nSuccessfully wrote file'))
} catch (e) {
console.log(chalk.bold.red('\nError writing file'))
if (verbose) console.error(e)
else console.log(debugMsg)
}
console.log()
console.warn('No --dist-dir option or "distDir" in symbols.json provided. Saving in ./node_modules/@symbo.ls/init/dynamic.json.')
return {}
}
if (body.components && convertOpt && framework) {
convertFromCli(body.components, { ...opts, framework })
}
if (update || force) {
createFs(body, distDir, { update: true, metadata })
} else {
createFs(body, distDir, { metadata })
}
})
}
program
.command('fetch')
.description('Fetch Symbols')
.option('-d, --dev', 'Running from local server')
.option('-v, --verbose', 'Verbose errors and warnings')
.option('--convert', 'Verbose errors and warnings', true)
.option('--metadata', 'Include metadata', false)
.option('--force', 'Force overriding changes from platform')
.option('--update', 'Overriding changes from platform')
.option('--verbose-code', 'Verbose errors and warnings')
.option('--dist-dir', 'Directory to import files to.')
.action(fetchFromCli)