-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeploy.js
111 lines (90 loc) · 3.26 KB
/
deploy.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
#!/usr/bin/env node
const EXPIRATION_DATE_IN_DAYS = 7;
const fs = require('fs');
const FtpClient = require('ftp');
const glob = require('glob');
var args = process.argv.slice(2);
const basePath = './build';
const destinationPath = args[0] === '--prod' ? process.env.SE_PROD_LOCATION : args[0] === '--dev' ? process.env.SE_STAGING_LOCATION : '';
const config = {
// We store the credentials for
// our FTP server as environemnt
// variables for security reasons.
host: process.env.SE_FTP_HOST,
password: process.env.SE_FTP_PASSWORD,
user: process.env.SE_FTP_USER,
isTest: (args[0] === '--test')
};
const ftpClient = new FtpClient();
function createDirectory(destination) {
return ftpClient.mkdir(destination, true, (error) => {
if (error) throw error;
ftpClient.end();
});
}
function uploadFile(file, destination) {
ftpClient.put(file, destination, (error) => {
if (error) throw error;
console.log(`${file} => ${destination}`);
ftpClient.end();
});
}
// Check if the path is a directory and
// either create the directory on the server
// if it is a directory, or upload the file
// if it is a file.
function handlePath(path) {
const relativeFile = path.replace(`${basePath}/`, '');
const destination = `${destinationPath}/${relativeFile}`;
if (fs.lstatSync(path).isDirectory()) {
return createDirectory(destination);
}
return uploadFile(path, destination);
}
// function isExpired(date) {
// return false; // files never expire
// // const oneDayInMilliseconds = 86400000;
// // const timestamp = new Date(date).getTime();
// // const expirationTimestamp = Date.now() - (oneDayInMilliseconds * EXPIRATION_DATE_IN_DAYS);
// // return timestamp < expirationTimestamp;
// }
// function cleanup(pathObject, directory) {
// if (pathObject.name === '.' || pathObject.name === '..') return;
// const path = `${directory}/${pathObject.name}`;
// // If the current path is a directory
// // we recursively check the files in it.
// if (pathObject.type === 'd') {
// return cleanupRemoteDirectory(path);
// }
// if (isExpired(pathObject.date)) {
// ftpClient.delete(path, (error) => {
// if (error) throw error;
// console.log(`Removed: ${path}`);
// ftpClient.end();
// });
// }
// }
// function cleanupRemoteDirectory(directory) {
// return ftpClient.list(directory, (error, pathObjects) => {
// if (error) throw error;
// pathObjects.forEach(pathObject => cleanup(pathObject, directory));
// ftpClient.end();
// });
// }
ftpClient.on('ready', () => {
// Get an array of all files and directories
// in the given base path and send them to the
// `handlePath()` function to decide if a
// directory is created on the server or the
// file is uploaded.
glob.sync(`${basePath}/**/*`).forEach(handlePath);
// Cleanup files older than the given amount of
// days. Keep in mind that this only makes sense
// if you've deployed at least once since the
// given amount of days.
// cleanupRemoteDirectory(destinationPath); //don't clean up old files
});
// In testing, we do nothing
if (!config.isTest) {
ftpClient.connect(config);
}