@symbo.ls/cli 2.11.288 → 2.11.291

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/bin/fetch.js +36 -29
  2. package/bin/fs.js +97 -0
  3. package/package.json +2 -2
package/bin/fetch.js CHANGED
@@ -7,33 +7,39 @@ import { program } from './program.js'
7
7
  import * as fetch from '@symbo.ls/fetch'
8
8
  import * as utils from '@domql/utils'
9
9
  import { convertFromCli } from './convert.js'
10
+ import { createDirs } from './fs.js'
10
11
 
11
12
  const { isObjectLike } = utils.default
12
13
  const { fetchRemote } = fetch.default
13
14
 
14
15
  const RC_PATH = process.cwd() + '/symbols.json'
15
- const LOCAL_CONFIG_PATH = process.cwd() + '/node_modules/@symbo.ls/init/dynamic.json'
16
+ const LOCAL_CONFIG_PATH =
17
+ process.cwd() + '/node_modules/@symbo.ls/init/dynamic.json'
16
18
  const DEFAULT_REMOTE_REPOSITORY = 'https://github.com/symbo-ls/default-config/'
17
- const DEFAULT_REMOTE_CONFIG_PATH = 'https://api.symbols.app/' // eslint-disable-line
19
+ const DEFAULT_REMOTE_CONFIG_PATH = "https://api.symbols.app/"; // eslint-disable-line
18
20
 
19
- const API_URL_LOCAL = 'http://localhost:13335/'
21
+ const API_URL_LOCAL = 'http://localhost:13335/get/'
20
22
  const API_URL = 'https://api.symbols.app/get/'
21
23
 
22
- const rcFile = loadModule(RC_PATH) // eslint-disable-line
23
- const localConfig = loadModule(LOCAL_CONFIG_PATH) // eslint-disable-line
24
+ const rcFile = loadModule(RC_PATH); // eslint-disable-line
25
+ const localConfig = loadModule(LOCAL_CONFIG_PATH); // eslint-disable-line
24
26
 
25
- const debugMsg = chalk.dim('Use --verbose to debug the error or open the issue at https://github.com/symbo-ls/smbls')
27
+ const debugMsg = chalk.dim(
28
+ 'Use --verbose to debug the error or open the issue at https://github.com/symbo-ls/smbls'
29
+ )
26
30
 
27
31
  let rc = {}
28
32
  try {
29
- rc = loadModule(RC_PATH) // eslint-disable-line
30
- } catch (e) { console.error('Please include symbols.json to your root of respository') }
33
+ rc = loadModule(RC_PATH); // eslint-disable-line
34
+ } catch (e) {
35
+ console.error('Please include symbols.json to your root of respository')
36
+ }
31
37
 
32
38
  export const fetchFromCli = async (opts) => {
33
39
  const { dev, verbose, prettify, convert: convertOpt } = opts
34
40
 
35
- await rc.then(async data => {
36
- const { key, framework } = data
41
+ await rc.then(async (data) => {
42
+ const { key, framework, distDir } = data
37
43
 
38
44
  const endpoint = dev ? API_URL_LOCAL : API_URL
39
45
 
@@ -54,7 +60,11 @@ export const fetchFromCli = async (opts) => {
54
60
 
55
61
  if (verbose) {
56
62
  if (key) {
57
- console.log(chalk.bold('Symbols'), 'data fetched for', chalk.green(body.name))
63
+ console.log(
64
+ chalk.bold('Symbols'),
65
+ 'data fetched for',
66
+ chalk.green(body.name)
67
+ )
58
68
  } else {
59
69
  console.log(
60
70
  chalk.bold('Symbols'),
@@ -85,33 +95,30 @@ export const fetchFromCli = async (opts) => {
85
95
  delete body.designsystem
86
96
  }
87
97
 
88
- const monetized = false
89
- if (!monetized) {
90
- const bodyString = JSON.stringify(body, null, prettify ?? 2)
91
-
92
- try {
93
- await fs.writeFileSync(LOCAL_CONFIG_PATH, bodyString)
98
+ const bodyString = JSON.stringify(body, null, prettify ?? 2)
94
99
 
95
- if (verbose) {
96
- console.log(chalk.dim('\ndynamic.json has been updated:'))
97
- console.log(chalk.dim.underline(LOCAL_CONFIG_PATH))
98
- }
100
+ try {
101
+ await fs.writeFileSync(LOCAL_CONFIG_PATH, bodyString)
99
102
 
100
- console.log(chalk.bold.green('\nSuccessfully wrote file'))
101
- } catch (e) {
102
- console.log(chalk.bold.red('\nError writing file'))
103
- if (verbose) console.error(e)
104
- else console.log(debugMsg)
103
+ if (verbose) {
104
+ console.log(chalk.dim('\ndynamic.json has been updated:'))
105
+ console.log(chalk.dim.underline(LOCAL_CONFIG_PATH))
105
106
  }
106
- } else {
107
- // tokos magic here
108
- console.log(body)
107
+
108
+ console.log(chalk.bold.green('\nSuccessfully wrote file'))
109
+ } catch (e) {
110
+ console.log(chalk.bold.red('\nError writing file'))
111
+ if (verbose) console.error(e)
112
+ else console.log(debugMsg)
109
113
  }
110
114
 
111
115
  console.log(convertOpt)
112
116
  if (body.components && convertOpt && framework) {
113
117
  convertFromCli(body.components, { ...opts, framework })
114
118
  }
119
+
120
+ console.log(Object.keys(body))
121
+ createDirs(body, distDir)
115
122
  })
116
123
  }
117
124
 
package/bin/fs.js ADDED
@@ -0,0 +1,97 @@
1
+ 'use strict'
2
+
3
+ import fs from 'fs'
4
+ import path from 'path'
5
+ import utils from '@domql/utils'
6
+ const { deepDestringify, objectToString, joinArrays } = utils
7
+
8
+ const keys = ['components', 'snippets', 'pages']
9
+ const singleFileKeys = ['designSystem', 'state']
10
+
11
+ export function createDirs (body, distDir) {
12
+ if (!body) {
13
+ console.error('No JSON object provided. Exiting.')
14
+ return
15
+ }
16
+
17
+ distDir = distDir || path.join(process.cwd(), 'dist')
18
+
19
+ keys.forEach((key) => {
20
+ createKeyDirectoryAndFiles(key, body, distDir)
21
+ })
22
+
23
+ singleFileKeys.forEach((key) => {
24
+ if (body[key] && typeof body[key] === 'object') {
25
+ createSingleFileFolderAndFile(key, body[key], distDir)
26
+ }
27
+ })
28
+
29
+ generateRootIndexjsFile(joinArrays(keys, singleFileKeys), distDir)
30
+ }
31
+
32
+ async function generateIndexjsFile (dirs, dirPath) {
33
+ const indexContent = dirs.map(d => `export * from './${d}'`).join('\n') + '\n'
34
+ const indexFilePath = path.join(dirPath, 'index.js')
35
+ const fh = await fs.promises.open(indexFilePath, 'w')
36
+ await fh.writeFile(indexContent, 'utf8')
37
+ await fh.close()
38
+ }
39
+
40
+ async function generateRootIndexjsFile (dirs, dirPath) {
41
+ const indexContent = dirs.map(d => {
42
+ const isSingleFile = singleFileKeys.includes(d)
43
+ if (isSingleFile) return `export { default as ${d} } from './${d}'`
44
+ return `export * as ${d} from './${d}'`
45
+ }).join('\n') + '\n'
46
+ const indexFilePath = path.join(dirPath, 'index.js')
47
+ const fh = await fs.promises.open(indexFilePath, 'w')
48
+ await fh.writeFile(indexContent, 'utf8')
49
+ await fh.close()
50
+ }
51
+
52
+ async function createKeyDirectoryAndFiles (key, body, distDir) {
53
+ const dirPath = path.join(distDir, key)
54
+ fs.existsSync(dirPath) || fs.mkdirSync(dirPath, { recursive: true })
55
+ console.log(
56
+ `${fs.existsSync(dirPath) ? 'Found' : 'Created'} directory: ${dirPath}`
57
+ )
58
+
59
+ const dirs = []
60
+ if (body[key] && typeof body[key] === 'object') {
61
+ Object.entries(body[key]).forEach(([entryKey, value]) => {
62
+ let childKey = entryKey
63
+ if (childKey.startsWith('/')) {
64
+ childKey = childKey.slice(1)
65
+ if (!childKey.length) childKey = 'main'
66
+ }
67
+ createOrUpdateFile(dirPath, childKey, value)
68
+ dirs.push(childKey)
69
+ })
70
+ }
71
+
72
+ generateIndexjsFile(dirs, dirPath, key)
73
+ }
74
+
75
+ function createOrUpdateFile (dirPath, childKey, value) {
76
+ const filePath = path.join(dirPath, `${childKey}.js`)
77
+
78
+ const content = deepDestringify(value)
79
+ const stringifiedContent = `export const ${childKey} = ${objectToString(content)}`
80
+
81
+ const fileExists = fs.existsSync(filePath)
82
+
83
+ console.log(`${fileExists ? 'Updating' : 'Creating new'} file: ${filePath}`);
84
+ (fileExists && fs.readFileSync(filePath, 'utf8') === content) ||
85
+ fs.writeFileSync(filePath, stringifiedContent)
86
+ }
87
+
88
+ function createSingleFileFolderAndFile (key, data, distDir) {
89
+ const filePath = path.join(distDir, `${key}.js`)
90
+ const content = deepDestringify(data)
91
+ const stringifiedContent = `export default ${objectToString(content)}`
92
+ const fileExists = fs.existsSync(filePath)
93
+
94
+ console.log(`${fileExists ? 'Updating' : 'Creating new'} file: ${filePath}`);
95
+ (fileExists && fs.readFileSync(filePath, 'utf8') === content) ||
96
+ fs.writeFileSync(filePath, stringifiedContent)
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/cli",
3
- "version": "2.11.288",
3
+ "version": "2.11.291",
4
4
  "description": "Fetch your Symbols configuration",
5
5
  "main": "bin/fetch.js",
6
6
  "author": "Symbols",
@@ -27,5 +27,5 @@
27
27
  "peerDependencies": {
28
28
  "@symbo.ls/convert": "latest"
29
29
  },
30
- "gitHead": "2793d8d6b6903041e9d988b292ee6fdf7ed67176"
30
+ "gitHead": "54f3dd0f443356a9cc26b3005ad352d5333f0c11"
31
31
  }