-
Notifications
You must be signed in to change notification settings - Fork 0
/
budo-to-netlify.js
executable file
·71 lines (63 loc) · 2.01 KB
/
budo-to-netlify.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
#!/usr/bin/env node
const fs = require('fs-extra')
const path = require('path')
const browserify = require('browserify')
const html = require('simple-html-index')
const { promisify } = require('util')
const child_process = require('child_process')
const publishDir = process.argv[3] || './publish-to-netlify'
async function awaitablePipe(src, dst) {
src.pipe(dst)
return new Promise(function(resolve, reject) {
src.on('end', resolve)
src.on('error', reject)
})
}
async function publish() {
// Clean up any old artifacts which shouldn't exist
try {
fs.removeSync(publishDir)
} catch (e) {}
// Create a temporary output directory to publish from
fs.mkdirSync(publishDir)
const b = browserify();
b.add(process.argv[2] || './index.js')
console.log(process.argv[2])
var output = fs.createWriteStream(path.join(publishDir, 'bundle.js'));
const bundler = b.bundle()
console.log("Building bundle.js")
await awaitablePipe(bundler, output)
// Copy or create the index.html page
const indexPath = path.join(publishDir, 'index.html')
if (fs.pathExistsSync('./index.html')) {
console.log("Copying existing index.html")
fs.copySync('./index.html', indexPath)
} else {
console.log("Generating index.html")
const htmlStream = html({
title: 'budo',
entry: 'bundle.js'
})
const htmlOut = fs.createWriteStream(indexPath)
console.log(indexPath, 'index')
await awaitablePipe(htmlStream, htmlOut)
}
const assetDirectories = ['public', 'assets', 'images', 'static']
assetDirectories.forEach(dir => {
if (fs.pathExistsSync(dir)) {
console.log("Copying asset path:", dir)
fs.copySync(dir, path.join(publishDir, dir))
}
})
process.chdir(publishDir)
child_process.spawn(`now`, ['--public']).stdout.on('data', function (data) {
console.log('Publish: ' + data.toString());
}).on('close', function() {
// Cleanup artifacts
fs.removeSync(publishDir)
})
}
publish().catch(e => {
console.error("Error", e)
fs.removeSync(publishDir)
})