-
Notifications
You must be signed in to change notification settings - Fork 25
/
bin.js
executable file
·152 lines (133 loc) · 3.83 KB
/
bin.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
#!/usr/bin/env node
const fs = require('fs')
const { join } = require('path')
;(async () => {
console.log(
yellow(
'🧪 This feature is experimental, let us know what you think ' +
'=> https://github.com/thgh/vercel-sapper/pull/47\n'
)
)
// Fallback to current working directory
const dir = process.argv[2] || '.'
// TODO: Create a new project if the directory is empty
// Create a new project if the directory does not exist
if (!fs.existsSync(dir)) {
await degit(dir)
}
// Configure the project for Vercel
patch(dir)
console.log(green('\nDeploy your Sapper project:\n'))
console.log(' cd', dir)
console.log(' vercel\n')
})()
function degit(dir) {
return new Promise((resolve) => {
console.log(green('degit'), `sveltejs/sapper-template#rollup`)
require('child_process').execFile(
'npx',
['degit', 'sveltejs/sapper-template#rollup', dir],
(err, stdout, stderr) => {
if (err) {
console.error(red('something went wrong with degit'), err)
return
}
stdout && console.log(stdout.trim())
stderr && console.log(stderr.trim())
console.log(green('Created'), `Sapper project in ${dir}`)
resolve()
}
)
})
}
async function patch(dir) {
try {
const serverFile = fs.existsSync(join(dir, 'src/server.js'))
? 'src/server.js'
: fs.existsSync(join(dir, 'src/server.ts'))
? 'src/server.ts'
: ''
if (!serverFile) {
throw new Error(
`Expected src/server.{js|ts} to exist, is this really a Sapper project?`
)
}
const server = fs.readFileSync(join(dir, serverFile), 'utf8')
fs.writeFileSync(join(dir, serverFile), patchServer(server, serverFile))
console.log(green('Patched'), serverFile)
} catch (e) {
console.error(red(e.message))
}
try {
if (fs.existsSync(join(dir, 'vercel.json'))) {
throw new Error('vercel.json already exists')
}
fs.writeFileSync(
join(dir, 'vercel.json'),
`{
"version": 2,
"builds": [
{
"src": "package.json",
"use": "vercel-sapper"
}
]
}`
)
console.log(green('Created'), 'vercel.json')
} catch (e) {
console.error(red(e.message))
}
try {
if (fs.existsSync(join(dir, '.vercelignore'))) {
throw new Error('.vercelignore already exists')
}
fs.writeFileSync(
join(dir, '.vercelignore'),
`/__sapper__
/cypress
!node_modules
/node_modules/*`
)
console.log(green('Created'), '.vercelignore')
} catch (e) {
console.error(red(e.message))
}
}
function patchServer(server, serverFile) {
if (server.includes('export default')) {
throw new Error(`${serverFile} was already patched`)
}
if (server.includes('module.exports')) {
throw new Error(`${serverFile} was already patched`)
}
if (!server.includes('polka()') && !server.includes('express()')) {
throw new Error(`${serverFile} should contain polka() or express()`)
}
// Simplest case
if (server.includes('\npolka()')) {
return server.replace('\npolka()', '\nexport default polka()')
}
if (server.includes('\nexpress()')) {
return server.replace('\nexpress()', '\nexport default express()')
}
// Server is assigned to variable
if (server.includes('= polka()')) {
return server.replace('= polka()', '= (module.exports = polka())')
}
if (server.includes('= express()')) {
return server.replace('= express()', '= (module.exports = express())')
}
throw new Error(
`${serverFile} needs manual patch: https://www.npmjs.com/package/vercel-sapper#manual-configuration`
)
}
function red(text) {
return '\u001b[1m\u001b[31m' + text + '\u001b[39m\u001b[22m'
}
function green(text) {
return '\u001b[1m\u001b[32m' + text + '\u001b[39m\u001b[22m'
}
function yellow(text) {
return '\u001b[1m\u001b[33m' + text + '\u001b[39m\u001b[22m'
}