-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.ts
109 lines (92 loc) · 2.83 KB
/
engine.ts
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
import { logging } from '@angular-devkit/core';
import { Schema } from '../deploy/schema';
const Heroku = require('heroku-client');
import * as tar from 'tar';
const fetch = require("node-fetch");
import {
ensureDir, copy, remove, move,
copyFileSync, readFileSync, createWriteStream
} from 'fs-extra';
// TODO: add your deployment code here!
export async function run(dir: string,
options: Schema,
outDir: string,
logger: logging.LoggerApi) {
try {
const herokuToken = process.env.HEROKU_TOKEN;
if(!herokuToken) throw new Error('HEROKU_TOKEN not found in your environment!!');
const heroku = new Heroku({ token: herokuToken });
const result = await heroku.get('/apps');
const site = result.find((app => app.name === 'ngx-deploy-demo'))
const slugResult = await heroku.post(`/apps/${site.name}/slugs`, {
body: {
buildpack_provided_description: "heroku/nodejs",
process_types: { "web": `node-v12.12.0-linux-x64/bin/node index.js` }
}
});
logger.info('Copying Build Files');
await remove(`${dir}/app`);
await remove(`${dir}/tmp`);
await remove(`${dir}/slug.tgz`);
await ensureDir(`${dir}/app`);
await ensureDir(`${dir}/tmp`);
await download();
await copy(`${outDir}`, `${dir}/app`);
await moveNodeJS('node-v12.12.0-linux-x64', `${dir}/app/node-v12.12.0-linux-x64`)
copyFileSync('index.js', `${dir}/app/index.js`);
const tarResponse = await tar.c(
{
gzip: true,
file: 'slug.tgz'
},
['./app']
);
const buf = readFileSync(`slug.tgz`);
const response = await fetch(slugResult.blob.url, {
method: 'PUT', // or 'PUT'
// body: JSON.stringify(data), // data can be `string` or {object}!
body: buf,
headers: {
"Content-Type": ""
}
})
logger.info('Starting deployment');
const release = await heroku.post(`/apps/${site.name}/releases`, {
body: {
slug: `${slugResult.id}`
}
});
logger.info('Deployment Success!');
// await remove(`${dir}/app`);
// await remove(`${dir}/tmp`);
// await remove(`${dir}/slug.tgz`);
}
catch (error) {
logger.error('❌ An error occurred!');
throw error;
}
};
async function download() {
const res = await fetch('http://nodejs.org/dist/latest-v12.x/node-v12.12.0-linux-x64.tar.gz');
await new Promise((resolve, reject) => {
const fileStream = createWriteStream('./tmp/node-v12.12.0-linux-x64.tar.gz');
res.body.pipe(fileStream);
res.body.on("error", (err) => {
reject(err);
});
fileStream.on("finish", function () {
tar.x({
file: './tmp/node-v12.12.0-linux-x64.tar.gz'
})
resolve();
});
});
}
async function moveNodeJS(src, dest) {
try {
await move(src, dest)
console.log('success!')
} catch (err) {
console.error(err)
}
}