-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-morpher.js
128 lines (115 loc) · 2.99 KB
/
make-morpher.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
import {
makeGetFontImport,
morphAllFonts,
processCustomFonts,
} from './fonts.js'
import { promises as fs } from 'fs'
import ensureViewsFiles from './ensure-views-files.js'
import makeGetSystemImport from './make-get-system-import.js'
import morphAllViews from './morph-all-views.js'
import morphAllViewGraphlFiles from './morph-all-view-graphql-files.js'
import parseViews from './parse-views.js'
import processViewCustomFiles from './process-view-custom-files.js'
import processViewFiles from './process-view-files.js'
export default function makeMorpher({
appName = 'app',
as = 'react-dom',
profile,
src,
tools = false,
verbose = true,
designSystemImportRoot,
}) {
let state = {
appName,
as,
src,
pass: 0,
profile,
tools,
verbose,
designSystemImportRoot,
customFonts: new Map(),
viewsById: new Map(),
viewsToFiles: new Map(),
}
state.processFiles = async function processFiles({
filesFontCustom = new Set(),
filesView = new Set(),
filesViewCustom = new Set(),
filesViewLogic = new Set(),
filesViewGraphql = new Set(),
}) {
if (filesFontCustom.size > 0) {
processCustomFonts({
customFonts: state.customFonts,
filesFontCustom,
})
}
// detect .view files
await processViewFiles({
filesView,
filesViewLogic,
filesViewGraphql,
viewsById: state.viewsById,
viewsToFiles: state.viewsToFiles,
})
// detect DesignSystem react.js files
processViewCustomFiles({
filesViewCustom,
viewsById: state.viewsById,
viewsToFiles: state.viewsToFiles,
})
// TODO optimise
// parse views
parseViews({
customFonts: state.customFonts,
filesView,
src: state.src,
verbose: state.verbose,
viewsById: state.viewsById,
viewsToFiles: state.viewsToFiles,
})
let morphedFonts = morphAllFonts({
as: state.as,
customFonts: state.customFonts,
filesView,
src: state.src,
viewsToFiles: state.viewsToFiles,
})
let morphedViewGraphqlFiles = morphAllViewGraphlFiles({
appName,
filesViewGraphql,
src: state.src,
})
// TODO optimise
// morph views
let morphedViews = morphAllViews({
as: state.as,
filesView,
getFontImport: makeGetFontImport(state),
getSystemImport: makeGetSystemImport(state),
profile: state.profile,
src,
tools: state.tools,
viewsById: state.viewsById,
viewsToFiles: state.viewsToFiles,
designSystemImportRoot: state.designSystemImportRoot,
})
// TODO optimise, only if they changed, cache, etc
let viewsFiles = await ensureViewsFiles(state)
let filesToWrite = [
...morphedFonts,
...morphedViewGraphqlFiles,
...morphedViews,
...viewsFiles,
].filter(Boolean)
await Promise.all(
filesToWrite.map(async ({ file, content }) =>
fs.writeFile(file, await content, 'utf-8')
)
)
state.pass++
}
return state
}