-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.js
72 lines (64 loc) · 1.83 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
/* eslint-disable no-console, prefer-promise-reject-errors */
const azure = require('azure-storage');
const fs = require('fs');
const filePath = require('path');
const blobService = azure.createBlobService(process.env.storage_key);
const storageCollection = '$web';
const files = [];
const rootPath = './';
const buildPath = 'build/';
const contentTypes = {
'.js': 'text/javascript',
'.map': 'text/javascript',
'.json': 'application/json',
'.html': 'text/html',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.woff2': 'font/woff2',
'.xml': 'text/xml',
'.txt': 'text/plain',
};
/**
* Checks if a path is a folder
* @param {String} path
* @return {Boolean} is folder
*/
function isFolder(path) {
return Object.keys(contentTypes).filter((type) => path.indexOf(type) > -1).length === 0;
}
/**
* Function to read in a folder and build up a list of files
* @param {String} route
*/
function readFolder(route) {
const sub = fs.readdirSync(route);
sub.forEach((elm) => {
const localPath = (`${route}/${elm}`).replace(rootPath, '');
if (isFolder(elm)) {
readFolder(rootPath + localPath);
}
files.push(localPath);
});
}
readFolder(rootPath + buildPath);
Promise.all(files.map((file) => new Promise((resolve, reject) => {
const contentType = contentTypes[filePath.extname(file)];
if (contentType) {
blobService.createBlockBlobFromLocalFile(storageCollection, file.replace(`${buildPath}/`, ''), file, { contentSettings: { contentType } }, (err) => {
if (err) {
return reject(err);
}
return resolve();
});
}
return reject(`No matching file type ${filePath.extname(file)} for file ${file}`);
})))
.then(() => {
console.log('Done :)');
})
.catch((err) => {
console.error('Error', err);
});