This repository was archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathdeployProject.js
70 lines (58 loc) · 1.83 KB
/
deployProject.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
const { removeSync } = require("fs-extra");
const waitOn = require("wait-on");
const execa = require("execa");
const { join } = require("path");
const getBaseUrl = require("./getBaseUrl");
// Deploy the project locally, using netlify dev
const deployLocally = ({ project }, config) => {
process.stdout.write(`Deploying project: ${project}...`);
// Start server. Must start in detached mode, so that we can kill it later.
// Otherwise, we seem unable to kill it.
// See: https://medium.com/@almenon214/killing-processes-with-node-772ffdd19aad
const server = execa("npm", ["run", "preview"], {
cwd: join(config.buildsFolder, project),
detached: true,
localDir: true,
});
// Set deployment
config.activeDeployment = {
serverPID: server.pid,
};
// wait for server to start
return new Promise((resolve) => {
const url = getBaseUrl({ project }, config);
waitOn({ resources: [url] }).then(() => {
console.log(" Done! ✅");
resolve(true);
});
});
};
// Deploy the project on Netlify
const deployOnNetlify = ({ project }, config) => {
process.stdout.write(`Deploying project: ${project}...`);
// Trigger deploy
const deploy = execa.sync("npm", ["run", "deploy"], {
cwd: join(config.buildsFolder, project),
localDir: true,
});
// Verify success
const url = getBaseUrl({ project }, config);
if (!url) throw "Deployment failed";
config.activeDeployment = {
serverPID: null,
};
console.log(" Done! ✅");
console.log(`URL: ${url}`);
return true;
};
const deployProject = ({ project }, config) => {
// Local deployment
if (config.env.DEPLOY === "local") {
return deployLocally({ project }, config);
}
// Deployment on Netlify
else if (config.env.DEPLOY == "netlify") {
return deployOnNetlify({ project }, config);
}
};
module.exports = deployProject;