forked from DAB0mB/Appfairy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
182 lines (147 loc) · 4.72 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import cheerio from 'cheerio'
import path from 'path'
import git from './git'
import { fs, ncp, reread } from './libs'
import { encapsulateCSS } from './utils'
import { ViewWriter, ScriptWriter, StyleWriter } from './writers'
export const transpile = async (config) => {
let inputFiles
let outputFiles = []
await Promise.all([
reread(config.input).then((files) => {
inputFiles = files.map((file) => path.relative(config.input, file))
}),
git.removeAppfairyFiles().then((files) => {
outputFiles.push(...files)
}),
])
const isHTML = (file) => path.extname(file) == '.html'
const htmlFiles = inputFiles.filter(isHTML)
const publicFiles = inputFiles.filter(file => !isHTML(file))
const scriptWriter = new ScriptWriter({
baseUrl: config.input,
prefetch: config.prefetch,
})
const styleWriter = new StyleWriter({
baseUrl: config.input,
prefetch: config.prefetch,
source: config.srouce,
})
const transpilingHTMLFiles = htmlFiles.map((htmlFile) => {
return transpileHTMLFile(
config,
htmlFile,
scriptWriter,
styleWriter,
)
})
const writingFiles = Promise.all(transpilingHTMLFiles).then((viewWriters) => {
return Promise.all([
ViewWriter.writeAll(
viewWriters, config.output.src.views, config.output.src.controllers
).then((paths) => outputFiles.push(...paths)),
scriptWriter.write(
config.output.src.scripts
).then((paths) => outputFiles.push(...paths)),
styleWriter.write(
config.output.src.styles
).then((paths) => outputFiles.push(...paths)),
])
})
const makingPublicDir = makePublicDir(
config,
publicFiles,
).then((paths) => outputFiles.push(...paths))
await Promise.all([
writingFiles,
makingPublicDir,
])
return git.add(outputFiles).then((files) => {
return git.commit(files, 'Migrate')
})
}
const transpileHTMLFile = async (
config,
htmlFile,
scriptWriter,
styleWriter,
) => {
const html = (await fs.readFile(`${config.input}/${htmlFile}`)).toString()
const $ = cheerio.load(html)
const $head = $('head')
const $body = $('body')
const dataAttrs = $(':root').data()
const viewWriter = new ViewWriter({
folder: path.dirname(htmlFile),
name: path.basename(htmlFile).split('.').slice(0, -1).join('.'),
baseUrl: config.baseUrl,
source: config.source,
})
setScripts(scriptWriter, $head, $)
setStyles(viewWriter, styleWriter, $head, $)
setHTML(viewWriter, $body, $)
setWfData(viewWriter, dataAttrs)
return viewWriter
}
const makePublicDir = async (config, publicFiles) => {
const publicDir = config.output.public
await Promise.all(publicFiles.map((publicFile) => {
return ncp(
`${config.input}/${publicFile}`,
`${publicDir}/${publicFile}`,
)
}))
// Resolving relative paths
const filePaths = await reread(config.input)
const relativePaths = filePaths.map((filePath) => {
const relativePath = path.relative(config.input, filePath)
return `${publicDir}/${relativePath}`
})
// Encapsulate CSS files
await Promise.all(relativePaths.map(async (relativePath) => {
if (path.extname(relativePath) != '.css') return
// Don't encapsulate normalize.css because it increases the
// specificity of its selectors which might cause them to
// override other styles.
if (path.basename(relativePath) == 'normalize.css') return
let css = (await fs.readFile(relativePath)).toString()
css = encapsulateCSS(css, config.source)
await fs.writeFile(relativePath, css)
}))
return relativePaths
}
const setScripts = (scriptWriter, $head) => {
const $scripts = $head.find('script[type="text/javascript"]')
$scripts.each((i, script) => {
const $script = $head.find(script)
scriptWriter.setScript($script.attr('src'), $script.html(), {
isAsync: !!$script.attr('async'),
})
})
}
const setStyles = (viewWriter, styleWriter, $head) => {
let $styles
$styles = $head.find('link[rel="stylesheet"][type="text/css"]')
$styles.each((i, style) => {
const $style = $head.find(style)
viewWriter.setStyle($style.attr('href'), $style.html())
styleWriter.setStyle($style.attr('href'), $style.html())
})
$styles = $head.find('style')
$styles.each((i, style) => {
const $style = $head.find(style)
viewWriter.setStyle($style.attr('href'), $style.html())
styleWriter.setStyle($style.attr('href'), $style.html())
})
}
const setHTML = (viewWriter, $body, $) => {
// Create a wrap around $body so we can inherit its style without actually
// using a <body> tag
const $div = $('<div>')
$div.html($body.html())
$div.attr($body.attr())
viewWriter.html = $.html($div)
}
const setWfData = (viewWriter, dataAttrs) => {
viewWriter.wfData = dataAttrs
}