@symbo.ls/cli 2.11.5 → 2.11.21

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 (2) hide show
  1. package/bin/convert.js +187 -41
  2. package/package.json +2 -2
package/bin/convert.js CHANGED
@@ -7,12 +7,40 @@ import fs from 'fs'
7
7
  import path from 'path'
8
8
  import { JSDOM } from 'jsdom'
9
9
 
10
+ const l = (n) => console.log(`mark${n}`) // eslint-disable-line no-unused-vars
11
+
10
12
  const jsdom = new JSDOM('<html><head></head><body></body></html>')
11
13
  global.window = jsdom.window
12
14
  global.document = window.document
13
15
 
16
+ const IGNORED_FILES = ['index.js', 'package.json', 'node_modules', 'dist']
17
+ const EXCLUDED_FROM_INTERNAL_UIKIT = [
18
+ 'Svg',
19
+ 'keySetters',
20
+ 'getSystemTheme',
21
+ 'splitTransition',
22
+ 'transformDuration',
23
+ 'transformShadow',
24
+ 'transformTransition'
25
+ ]
14
26
  const TMP_DIR_NAME = '.smbls_convert_tmp'
27
+ const TMP_DIR_PACKAGE_JSON_STR = JSON.stringify({
28
+ name: 'smbls_convert_tmp',
29
+ version: '1.0.0',
30
+ // "main": "index.js",
31
+ license: 'ISC'
32
+ })
33
+
34
+ function isDirectory (dir) { // eslint-disable-line no-unused-vars
35
+ if (!fs.existsSync(dir)) return false
36
+
37
+ const stat = fs.statSync(dir)
38
+ if (!stat) return false
15
39
 
40
+ return stat.isDirectory()
41
+ }
42
+
43
+ // Essentially does 'mkdir -P'
16
44
  async function mkdirp (dir) {
17
45
  try {
18
46
  return await fs.promises.mkdir(dir)
@@ -24,17 +52,72 @@ async function mkdirp (dir) {
24
52
  return null
25
53
  }
26
54
 
27
- const IGNORED_FILES = ['index.js', 'package.json', 'node_modules', 'dist']
55
+ async function importDomqlModule (modulePath) {
56
+ console.log(`importing ${modulePath}`)
57
+ return (await import(modulePath)).default
58
+ }
59
+
60
+ function convertDomqlModule (domqlModule, desiredFormat, options) {
61
+ let convertedStr = ''
62
+
63
+ console.group()
64
+ const uniqueImports = []
65
+ let first = true
66
+ let removeUseContextImport = false
67
+ const exportCount = Object.keys(domqlModule).length
68
+ for (const key in domqlModule) {
69
+ if (options.internalUikit &&
70
+ EXCLUDED_FROM_INTERNAL_UIKIT.includes(key)) {
71
+ console.log(`Skipping ${key} component due to exclusion`)
72
+ continue
73
+ }
74
+ console.log(key)
75
+ // try {
76
+ // import('domql-to-mitosis').then(({ convert }) => {
77
+ // console.log('convert', convert)
78
+ // if (convert) {
79
+ console.group()
80
+ const component = domqlModule[key]
81
+ component.__name = key
82
+
83
+ const out = convert(component, desiredFormat, {
84
+ verbose: false,
85
+ exportDefault: exportCount === 1,
86
+ returnMitosisIR: true,
87
+ importsToRemove: uniqueImports,
88
+ removeReactImport: !first,
89
+ removeUseContextImport
90
+ })
91
+
92
+ convertedStr = convertedStr + out.str + '\n'
93
+ uniqueImports.push(...out.mitosisIR.imports)
94
+ first = false
95
+ if (out.mitosisIR._useContext) { removeUseContextImport = true }
96
+ console.groupEnd()
97
+ // } else {
98
+ // throw new Error('Convert from `domql-to-mitosis` is not defined. Try to install `domql-to-mitosis` and run this command again.')
99
+ // }
100
+ // })
101
+ // } catch (err) {
102
+ // throw new Error('`domql-to-mitosis` is not found.')
103
+ // }
104
+ }
105
+ console.groupEnd()
106
+
107
+ return convertedStr
108
+ }
28
109
 
29
110
  program
30
111
  .command('convert')
31
112
  .description('Recursively convert and copy all DomQL components under a directory')
32
- .argument('[src]', 'Source directory. By default, it is "src/"')
33
- .argument('[dest]', 'Destination directory. By default, it becomes the name of the desired format')
113
+ .argument('[src]', 'Source directory/file. By default, it is "src/"')
114
+ .argument('[dest]', 'Destination directory/file. Will be overwritten. By default, it becomes the name of the desired format')
34
115
  .option('--react', 'Convert all DomQL components to React')
35
116
  .option('--angular', 'Convert all DomQL components to Angular')
36
117
  .option('--vue2', 'Convert all DomQL components to Vue2')
37
118
  .option('--vue3', 'Convert all DomQL components to Vue3')
119
+ .option('--internal-uikit', '(For internal use only). Excludes particular components from the conversion')
120
+ .option('--tmp-dir', `Use this directory for storing intermediate & build files instead of the default (dest/${TMP_DIR_NAME})`)
38
121
  .action(async (src, dest, options) => {
39
122
  // Desired format
40
123
  let desiredFormat = 'react'
@@ -46,16 +129,102 @@ program
46
129
  desiredFormat = 'vue3'
47
130
  }
48
131
 
49
- // Resolve source & destination directories
132
+ // Resolve source file/dir
50
133
  const srcPath = path.resolve(src || './src')
51
- const destPath = path.resolve(dest || desiredFormat)
52
- const tmpDirPath = path.resolve(path.dirname(destPath), TMP_DIR_NAME)
134
+ if (!fs.existsSync(srcPath)) {
135
+ console.erorr(`Source directory/file ('${srcPath}') does not exist`)
136
+ return 1
137
+ }
138
+ const srcIsDir = fs.statSync(srcPath).isDirectory()
53
139
 
54
- // Make tmp and dist directories
140
+ // Resolve & create tmp dir
141
+ const tmpDirPath = options.tmpDir ??
142
+ path.resolve(path.dirname(srcPath), TMP_DIR_NAME)
55
143
  await mkdirp(tmpDirPath)
56
- await mkdirp(destPath)
57
144
 
58
- const origFiles = await (await fs.promises.readdir(srcPath)).filter(file => !IGNORED_FILES.includes(file))
145
+ // Put a package.json file so that when we import() the modules from the
146
+ // directory, node doesn't recognize them as ES modules (in case the parent
147
+ // directory of the tmp dir has "type": "module" in its package.json
148
+ const pj = await fs.promises.open(path.resolve(tmpDirPath, 'package.json'), 'w')
149
+ await pj.writeFile(TMP_DIR_PACKAGE_JSON_STR, 'utf8')
150
+ await pj.close()
151
+
152
+ // Convert single file. Output will also be a single file.
153
+ if (!srcIsDir) {
154
+ // Determine destFilePath and create it if needed
155
+ let destFilePath
156
+ if (dest) {
157
+ // dest is given.
158
+ if (!fs.existsSync(dest)) {
159
+ // dest doesn't exist. That's the output file we'll create.
160
+ destFilePath = path.resolve(dest)
161
+ } else if (fs.statSync(dest).isDirectory()) {
162
+ // dest exists and is a directory. Create our output file inside it.
163
+ destFilePath = path.join(path.resolve(dest), path.basename(srcPath))
164
+ } else {
165
+ // dest exists and is not a directory. Overwrite the file.
166
+ destFilePath = path.resolve(dest)
167
+ }
168
+ } else {
169
+ // dest not given. Use default (desiredFormat as directory).
170
+ const destDir = path.resolve(desiredFormat)
171
+ await mkdirp(destDir)
172
+ destFilePath = path.join(destDir, path.basename(srcPath))
173
+ }
174
+
175
+ const bundledFilePath = path.join(tmpDirPath, path.basename(srcPath))
176
+ console.log(`ESbuild ${srcPath} -> ${bundledFilePath}`)
177
+
178
+ // Bundle the component
179
+ await esbuild.build({
180
+ entryPoints: [srcPath],
181
+ bundle: true,
182
+ sourcemap: true,
183
+ target: 'node12',
184
+ format: 'cjs',
185
+ outfile: bundledFilePath
186
+ })
187
+
188
+ // Import the module
189
+ const domqlModule = await importDomqlModule(bundledFilePath, options)
190
+
191
+ // Convert & append each exported domql object
192
+ console.log(`Converting modules in ${bundledFilePath}:`)
193
+ const convertedModuleStr = convertDomqlModule(
194
+ domqlModule,
195
+ desiredFormat,
196
+ options
197
+ )
198
+
199
+ // Write file
200
+ if (convertedModuleStr.length > 0) {
201
+ const fh = await fs.promises.open(destFilePath, 'w')
202
+ await fh.writeFile(convertedModuleStr, 'utf8')
203
+ await fh.close()
204
+ }
205
+
206
+ return 0
207
+ }
208
+
209
+ // We're converting multiple files (in a directory)
210
+ // Determine destDirPath & create it if needed
211
+ if (!dest) dest = path.resolve(desiredFormat)
212
+ let destDirPath
213
+ if (!fs.existsSync(dest)) {
214
+ // dest doesn't exist. Create it.
215
+ destDirPath = path.resolve(dest)
216
+ await mkdirp(destDirPath)
217
+ } else if (fs.statSync(dest).isDirectory()) {
218
+ // dest exists and is a directory.
219
+ destDirPath = path.resolve(dest)
220
+ } else {
221
+ // dest exists and is not a directory.
222
+ console.error(`The destination ('${path.resolve(dest)}') must be a directory when the source ('${srcPath}') is a directory`)
223
+ return 1
224
+ }
225
+
226
+ const origFiles = (await fs.promises.readdir(srcPath))
227
+ .filter(file => !IGNORED_FILES.includes(file))
59
228
 
60
229
  // Bundle components
61
230
  await esbuild.build({
@@ -70,51 +239,28 @@ program
70
239
  // Convert components
71
240
  const componentDirs = await fs.promises.readdir(tmpDirPath)
72
241
  for (const componentDir of componentDirs) {
73
- console.log(componentDirs)
74
242
  const importDir = path.join(tmpDirPath, componentDir)
75
243
  if ((await fs.promises.stat(importDir)).isDirectory()) {
76
244
  // Import the module
77
245
  const importPath = `${importDir}/index.js`
78
- console.log(`importing ${componentDir}/`)
79
- const domqlModule = (await import(importPath)).default
246
+ const domqlModule = await importDomqlModule(importPath)
80
247
 
81
248
  // Create directory for component in dest dir
82
- const destComponentDirPath = `${destPath}/${componentDir}`
249
+ const destComponentDirPath = `${destDirPath}/${componentDir}`
83
250
  await mkdirp(destComponentDirPath)
84
251
 
85
252
  // Convert & append each exported domql object
86
- console.log(`Converting modules in ${componentDir}:`)
87
- console.group()
88
- const uniqueImports = []
89
- let fileContents = ''
90
- let first = true
91
- const exportCount = Object.keys(domqlModule).length
92
- for (const key in domqlModule) {
93
- console.log(key)
94
- console.group()
95
- const component = domqlModule[key]
96
- component.__name = key
97
- const out = convert(component, desiredFormat, {
98
- verbose: false,
99
- exportDefault: exportCount === 1,
100
- returnMitosisIR: true,
101
- importsToRemove: uniqueImports,
102
- removeReactImport: !first
103
- })
104
-
105
- fileContents = fileContents + out.str + '\n'
106
- uniqueImports.push(...out.mitosisIR.imports)
107
- first = false
108
- console.groupEnd()
109
- }
110
-
111
- console.groupEnd()
253
+ const convertedStr = convertDomqlModule(
254
+ domqlModule,
255
+ desiredFormat,
256
+ options
257
+ )
112
258
 
113
259
  // Write file
114
- if (fileContents.length > 0) {
260
+ if (convertedStr.length > 0) {
115
261
  const fh = await fs.promises
116
262
  .open(`${destComponentDirPath}/index.js`, 'w')
117
- await fh.writeFile(fileContents, 'utf8')
263
+ await fh.writeFile(convertedStr, 'utf8')
118
264
  await fh.close()
119
265
  }
120
266
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/cli",
3
- "version": "2.11.5",
3
+ "version": "2.11.21",
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
  "domql-to-mitosis": "latest"
29
29
  },
30
- "gitHead": "d5256e4c77123737a42641919ddbddb5acb6615c"
30
+ "gitHead": "56cbd0c8ecdf0ec6427bcbfa7f853dbd04020b9d"
31
31
  }