1 | import chalk from 'chalk'
|
2 | import morphers from './morphers.js'
|
3 |
|
4 |
|
5 | export default function maybeMorph({
|
6 | as,
|
7 | getFontImport,
|
8 | getSystemImport,
|
9 | tools,
|
10 | view,
|
11 | viewsById,
|
12 | viewsToFiles,
|
13 | verbose,
|
14 | }) {
|
15 | let result = null
|
16 | try {
|
17 | result = morphers[as]({
|
18 | getFontImport,
|
19 | getSystemImport,
|
20 | tools,
|
21 | view,
|
22 | viewsById,
|
23 | viewsToFiles,
|
24 | })
|
25 |
|
26 | view.version++
|
27 |
|
28 | verbose &&
|
29 | console.log(
|
30 | `${chalk.green('M')} ${view.id}@${view.version}:${chalk.dim(view.file)}`
|
31 | )
|
32 |
|
33 | return result.code
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | } catch (error) {
|
41 | console.error(chalk.red('M'), view, error.codeFrame || error)
|
42 | return `import { useEffect } from 'react'
|
43 |
|
44 | export default function ${view.id}() {
|
45 | useEffect(() => {
|
46 | console.error({
|
47 | type: 'morph',
|
48 | view: '${view.id}',
|
49 | file: '${view.file}',
|
50 | todo: "Report to https://github.com/viewstools/morph/issues/new with .view and .view.js files and what changed when it failed. This will help us improve the morpher. Thanks!",
|
51 | })
|
52 | }, [])
|
53 |
|
54 | return "😳 Can't morph '${view.id}'. See console for more details."
|
55 | }
|
56 |
|
57 | /*
|
58 | >>> CODE
|
59 | ${result && result.code.replace(/(\/\*|\*\/)/g, '')}
|
60 |
|
61 |
|
62 | >>> ERROR
|
63 | ${error.message}
|
64 | ${error.stack}
|
65 | */`
|
66 | }
|
67 | }
|