@symbo.ls/cli 2.11.132 → 2.11.159

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 +163 -58
  2. package/package.json +4 -6
package/bin/convert.js CHANGED
@@ -6,10 +6,7 @@ import { parse } from 'globusa'
6
6
  import fs from 'fs'
7
7
  import path from 'path'
8
8
 
9
- // Set up webpack
10
- import syncWebpack from 'webpack'
11
- import { promisify } from 'util'
12
- const webpack = promisify(syncWebpack)
9
+ import * as esbuild from 'esbuild'
13
10
 
14
11
  // Set up jsdom
15
12
  import { JSDOM } from 'jsdom'
@@ -17,18 +14,41 @@ const jsdom = new JSDOM('<html><head></head><body></body></html>')
17
14
  global.window = jsdom.window
18
15
  global.document = window.document
19
16
 
20
- const IGNORED_FILES = ['index.js', 'package.json', 'node_modules', 'dist']
21
- const EXCLUDED_FROM_INTERNAL_UIKIT = [
22
- 'Svg',
23
- 'keySetters',
24
- 'getSystemTheme',
25
- 'splitTransition',
26
- 'transformDuration',
27
- 'transformShadow',
28
- 'transformTransition',
29
- 'DatePickerDay',
30
- 'DatePickerGrid',
31
- ]
17
+ const INTERNAL_UIKIT_CONF = {
18
+ excludedComponents: [
19
+ // We have our own React Svg implementation
20
+ 'Svg',
21
+
22
+ // We have our own React Box implementation
23
+ 'Box',
24
+
25
+ // These are not domql objects
26
+ 'keySetters',
27
+ 'getSystemTheme',
28
+ 'splitTransition',
29
+ 'transformDuration',
30
+ 'transformShadow',
31
+ 'transformTransition',
32
+
33
+ // FIXME: Temporary list of components we want to skip
34
+ 'DatePicker',
35
+ 'DatePickerDay',
36
+ 'DatePickerTwoColumns',
37
+ 'DatePickerGrid',
38
+ 'DatePickerGridContainer',
39
+
40
+ // Not a domql object (headless-datepicker)
41
+ 'calendar',
42
+ ],
43
+
44
+ // Can be strings or regex patterns
45
+ excludedDirectories: [
46
+ // TODO: Review these ignores with @nikoloza
47
+ /Threejs$/,
48
+ /Editorjs$/,
49
+ /User$/,
50
+ ],
51
+ }
32
52
  const TMP_DIR_NAME = '.smbls_convert_tmp'
33
53
  const TMP_DIR_PACKAGE_JSON_STR = JSON.stringify({
34
54
  name: 'smbls_convert_tmp',
@@ -37,10 +57,57 @@ const TMP_DIR_PACKAGE_JSON_STR = JSON.stringify({
37
57
  license: 'ISC'
38
58
  })
39
59
 
40
- function isDirectory (dir) { // eslint-disable-line no-unused-vars
41
- if (!fs.existsSync(dir)) return false
60
+ function generatePackageJsonFile(
61
+ sourcePackageJsonPath,
62
+ destPath,
63
+ globusaStruct,
64
+ desiredFormat,
65
+ options
66
+ ) {
67
+ // Extract package name from source package.json
68
+ const str = fs.readFileSync(sourcePackageJsonPath, { encoding: 'utf8' })
69
+ let packageStruct
70
+ try {
71
+ packageStruct = JSON.parse(str)
72
+ } catch (error) {
73
+ console.error(`Error when parsing ${sourcePackageJsonPath}`)
74
+ return;
75
+ }
76
+ const split = packageStruct.name.split('/')
77
+ const packageName = split[split.length - 1]
78
+
79
+ // Generate list of dependencies
80
+ const deps = {
81
+ 'css-in-props': 'latest',
82
+ [`@emotion/${desiredFormat}`]: '^11.11.0',
83
+ '@emotion/css': '^11.11.0',
84
+ '@symbo.ls/create': 'latest',
85
+ '@symbo.ls/react': 'latest',
86
+ }
87
+ globusaStruct.imports
88
+ .filter(imp => imp.path.match(/^@symbo\.ls\//))
89
+ .filter(imp => imp.path !== packageName)
90
+ .forEach(imp => deps[imp.path] = 'latest')
91
+
92
+ // Generate final package.json string
93
+ const genStr = JSON.stringify({
94
+ name: `@symbo.ls/${desiredFormat}-${packageName}`,
95
+ version: packageStruct.version ?? '1.0.0',
96
+ license: packageStruct.license ?? 'UNLICENSED',
97
+ dependencies: deps,
98
+ peerDependencies: {
99
+ 'react': '^18.2.0',
100
+ 'react-dom': '^18.2.0'
101
+ },
102
+ main: 'index.js',
103
+ source: 'index.js'
104
+ }, undefined, 2)
42
105
 
43
- const stat = fs.statSync(dir)
106
+ fs.writeFileSync(destPath, genStr)
107
+ }
108
+
109
+ function isDirectory (dir) {
110
+ const stat = fs.statSync(dir, { throwIfNoEntry: false })
44
111
  if (!stat) return false
45
112
 
46
113
  return stat.isDirectory()
@@ -75,7 +142,7 @@ function convertDomqlModule(domqlModule, globusaStruct, desiredFormat, options)
75
142
  })
76
143
  .filter(exportName => {
77
144
  if (!options.internalUikit) return true
78
- if (EXCLUDED_FROM_INTERNAL_UIKIT.includes(exportName)) {
145
+ if (INTERNAL_UIKIT_CONF.excludedComponents.includes(exportName)) {
79
146
  console.log(`Skipping ${exportName} component due to internal uikit exclusion`)
80
147
  return false
81
148
  }
@@ -84,6 +151,7 @@ function convertDomqlModule(domqlModule, globusaStruct, desiredFormat, options)
84
151
 
85
152
  const isSingleComponent = (exports.length === 1)
86
153
  const uniqueImports = []
154
+ let globalSymbolTable = {}
87
155
  for (const idx in exports) {
88
156
  const exportName = exports[idx]
89
157
 
@@ -103,6 +171,7 @@ function convertDomqlModule(domqlModule, globusaStruct, desiredFormat, options)
103
171
  const kaldunaOpts = {
104
172
  verbose: false,
105
173
  returnMitosisIR: true,
174
+ globalSymbolTable,
106
175
  exportDefault: isSingleComponent,
107
176
  importsToRemove: uniqueImports,
108
177
 
@@ -123,8 +192,10 @@ function convertDomqlModule(domqlModule, globusaStruct, desiredFormat, options)
123
192
  out = convert(dobj, desiredFormat, {
124
193
  ...kaldunaOpts,
125
194
  removeReactImport: false,
126
- importsToInclude: globusaStruct.imports,
127
- declarationsToInclude: globusaStruct.declarations,
195
+ // NOTE(nikaoto): Commented these out because we're using deps now, so
196
+ // all the imports and decls are going to be redundant
197
+ // importsToInclude: globusaStruct.imports,
198
+ // declarationsToInclude: globusaStruct.declarations,
128
199
  })
129
200
  } else {
130
201
  out = convert(dobj, desiredFormat, {
@@ -138,6 +209,7 @@ function convertDomqlModule(domqlModule, globusaStruct, desiredFormat, options)
138
209
  convertedStr += '\n'
139
210
  }
140
211
  uniqueImports.push(...out.mitosisIR.imports)
212
+ globalSymbolTable = out.mitosisIR._globalSymbolTable
141
213
  console.groupEnd()
142
214
  }
143
215
  console.groupEnd()
@@ -148,6 +220,7 @@ function convertDomqlModule(domqlModule, globusaStruct, desiredFormat, options)
148
220
  // Takes a source file, then bundles, parses and converts it and writes the
149
221
  // result to the destination. The tmpDirPath is used as a working directory for
150
222
  // temporary files.
223
+ // Returns globusaStruct for later usage.
151
224
  async function convertFile(srcPath, tmpDirPath, destPath,
152
225
  desiredFormat, options) {
153
226
  // Parse with globusa
@@ -155,28 +228,23 @@ async function convertFile(srcPath, tmpDirPath, destPath,
155
228
  const fileContent = await fs.promises.readFile(srcPath, 'utf8')
156
229
  const globusaStruct = parse(fileContent)
157
230
 
158
- // Bundle with webpack
159
- const libraryName = 'banunu' // This can literally be anything
160
231
  const fileName = path.basename(srcPath)
161
232
  const bundledFilePath = path.resolve(tmpDirPath, fileName)
162
- console.log(`Webpack ${srcPath} -> ${bundledFilePath}`)
163
- await webpack({
164
- entry: srcPath,
165
- output: {
166
- path: tmpDirPath,
167
- filename: fileName,
168
- chunkFormat: 'commonjs',
169
- library: { name: libraryName,
170
- type: 'commonjs-static' },
171
- },
172
- // experiments: { outputModule: true },
173
- target: 'node',
174
- mode: 'development'
233
+
234
+ // Bundle with esbuild
235
+ await esbuild.build({
236
+ entryPoints: [srcPath],
237
+ bundle: true,
238
+ sourcemap: true,
239
+ target: 'node12',
240
+ format: 'cjs',
241
+ outdir: tmpDirPath
175
242
  })
176
243
 
177
244
  // Import the bundled module to obtain exported domql objects
178
245
  console.log(`Importing ${bundledFilePath}`)
179
- const domqlModule = (await import(bundledFilePath))[libraryName]
246
+ const mod = (await import(bundledFilePath))
247
+ const domqlModule = mod.default
180
248
 
181
249
  // Convert it/them with kalduna
182
250
  console.log(`Converting components in ${bundledFilePath}:`)
@@ -196,12 +264,13 @@ async function convertFile(srcPath, tmpDirPath, destPath,
196
264
  await fh.writeFile(convertedModuleStr, 'utf8')
197
265
  await fh.close()
198
266
  }
267
+
268
+ return globusaStruct
199
269
  }
200
270
 
201
271
  program
202
272
  .command('convert')
203
- .description('(DEPRECATED) Convert and copy all DomQL components ' +
204
- 'under a directory')
273
+ .description('Convert and copy all DomQL components under a directory')
205
274
  .argument('[src]', 'Source directory/file. By default, it is "src/"')
206
275
  .argument('[dest]',
207
276
  'Destination directory/file. Will be overwritten. By ' +
@@ -214,16 +283,12 @@ program
214
283
  'Use this directory for storing intermediate & build files instead of ' +
215
284
  `the default (dest/${TMP_DIR_NAME})`)
216
285
  .option('-o, --only <components>',
217
- 'Only convert these components; comma separated ' +
286
+ 'Only convert these components; comma separated ' +
218
287
  '(for example: --only=Flex,Img)')
219
288
  .option('--internal-uikit',
220
- '(For internal use only). ' +
289
+ '(For internal use only). ' +
221
290
  'Excludes particular components from the conversion')
222
291
  .action(async (src, dest, options) => {
223
- console.log('smbls convert is deprecated. ' +
224
- 'Please use the Kalduna build script instead.')
225
- return 1
226
-
227
292
  if (!convert) {
228
293
  throw new Error(
229
294
  'convert() from `kalduna` is not defined. Try to install ' +
@@ -252,7 +317,6 @@ program
252
317
  console.erorr(`Source directory/file ('${srcPath}') does not exist`)
253
318
  return 1
254
319
  }
255
- const srcIsDir = fs.statSync(srcPath).isDirectory()
256
320
 
257
321
  // Resolve & create tmp dir
258
322
  const tmpDirPath = options.tmpDir ??
@@ -268,7 +332,7 @@ program
268
332
  await pj.close()
269
333
 
270
334
  // Convert single file. Output will also be a single file.
271
- if (!srcIsDir) {
335
+ if (!isDirectory(srcPath)) {
272
336
  // Determine destFilePath and create it if needed
273
337
  let destFilePath
274
338
  if (dest) {
@@ -276,7 +340,7 @@ program
276
340
  if (!fs.existsSync(dest)) {
277
341
  // dest doesn't exist. That's the output file we'll create.
278
342
  destFilePath = path.resolve(dest)
279
- } else if (fs.statSync(dest).isDirectory()) {
343
+ } else if (isDirectory(dest)) {
280
344
  // dest exists and is a directory. Create our output file inside it.
281
345
  destFilePath = path.join(path.resolve(dest), path.basename(srcPath))
282
346
  } else {
@@ -309,7 +373,7 @@ program
309
373
  // dest doesn't exist. Create it.
310
374
  destDirPath = path.resolve(dest)
311
375
  await mkdirp(destDirPath)
312
- } else if (fs.statSync(dest).isDirectory()) {
376
+ } else if (isDirectory(dest)) {
313
377
  // dest exists and is a directory.
314
378
  destDirPath = path.resolve(dest)
315
379
  } else {
@@ -320,18 +384,59 @@ program
320
384
  return 1
321
385
  }
322
386
 
323
- const sourceFileNames = (await fs.promises.readdir(srcPath))
324
- .filter(file => !IGNORED_FILES.includes(file))
387
+ const ignoredFiles = ['index.js', 'package.json', 'node_modules', 'dist']
388
+ const sourceDirNames = (await fs.promises.readdir(srcPath))
389
+ .filter(dir => !ignoredFiles.includes(dir))
325
390
 
326
- for (const file of sourceFileNames) {
327
- const indexFilePath = path.join(srcPath, file, 'index.js')
391
+ const dirs = []
328
392
 
329
- await convertFile(
330
- indexFilePath,
331
- path.join(tmpDirPath, file),
332
- path.join(destDirPath, file, 'index.js'),
393
+ for (const dir of sourceDirNames) {
394
+ // Ignored directories
395
+ if (options.internalUikit) {
396
+ let skip = false
397
+ for (const pat of INTERNAL_UIKIT_CONF.excludedDirectories)
398
+ if (dir.match(pat)) { skip = true; break; }
399
+ if (skip) continue
400
+ }
401
+
402
+ const dirPath = path.join(srcPath, dir)
403
+ if (!isDirectory(dirPath)) {
404
+ console.log(`Skipping ${dirPath} because it is not a directory`)
405
+ continue
406
+ }
407
+ const indexFilePath = path.join(dirPath, 'index.js')
408
+ const pjFilePath = path.join(dirPath, 'package.json')
409
+
410
+ const globusaStruct = await convertFile(
411
+ indexFilePath, // src
412
+ path.join(tmpDirPath, dir), // tmp
413
+ path.join(destDirPath, dir, 'index.js'), // dst
333
414
  desiredFormat,
334
415
  options
335
416
  )
417
+
418
+ if (fs.existsSync(pjFilePath)) {
419
+ generatePackageJsonFile(
420
+ pjFilePath, // src
421
+ path.join(destDirPath, dir, 'package.json'), // dst
422
+ globusaStruct,
423
+ desiredFormat,
424
+ options
425
+ )
426
+ }
427
+
428
+ dirs.push(dir)
429
+ }
430
+
431
+ // Generate top index.js file
432
+ if (dirs.length > 0) {
433
+ //const importLines = dirs.map(d => `import ${d} from './${d}'`).join('\n') + '\n'
434
+ //const exportLines = 'export {\n' + dirs.map(d => ` ${d}`).join(',\n') + '\n}\n'
435
+ //const fileContent = importLines + '\n' + exportLines
436
+ const fileContent = dirs.map(d => `export * from './${d}'`).join('\n')
437
+
438
+ const fh = await fs.promises.open(path.join(destDirPath, 'index.js'), 'w')
439
+ await fh.writeFile(fileContent, 'utf8')
440
+ await fh.close()
336
441
  }
337
442
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/cli",
3
- "version": "2.11.132",
3
+ "version": "2.11.159",
4
4
  "description": "Fetch your Symbols configuration",
5
5
  "main": "bin/fetch.js",
6
6
  "author": "Symbols",
@@ -19,13 +19,11 @@
19
19
  "@symbo.ls/socket": "latest",
20
20
  "chalk": "^5.0.0",
21
21
  "commander": "latest",
22
+ "esbuild": "^0.19.2",
22
23
  "globusa": "latest",
23
- "jsdom": "^21.1.0",
24
+ "jsdom": "^22.1.0",
24
25
  "node-fetch": "^3.1.0",
25
26
  "v8-compile-cache": "^2.3.0"
26
27
  },
27
- "gitHead": "13027443598f113dd2b79ddf7aaf1adbd46d97d7",
28
- "devDependencies": {
29
- "webpack": "^5.88.2"
30
- }
28
+ "gitHead": "4dbce17a65f09e9c7e9451f5d9042ddbe351280b"
31
29
  }