-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
184 lines (167 loc) · 5 KB
/
gulpfile.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const gulp = require('gulp');
const zip = require('gulp-zip');
const path = require('path').posix;
const merge = require('merge-stream');
const config = require('./config.js');
const PersoniumAuthClient = require('./tools/auth');
const PersoniumWebdavClient = require('./tools/webdav');
const CONSTANT = {
RESOURCETYPE: {
SERVICE: 'service',
COLLECTION: 'collection',
STATICFILE: 'staticfile',
},
};
gulp.task('build_bar', () => {
return gulp
.src(['src/bar/**/*', '!src/bar/**/*.example.*'])
.pipe(zip(`${config.personium.CELL_NAME}.bar`))
.pipe(gulp.dest('dist'));
});
gulp.task('copy_statics', () => {
const tasks = config.personium.DIRECTORY_MAPPING.map(mapping => {
const getDstDir = mapping => {
const strPaths = ['build', mapping.dstDir];
if (mapping.resourceType === CONSTANT.RESOURCETYPE.SERVICE) {
strPaths.push('__src');
}
return path.join(...strPaths);
};
const dstDir = getDstDir(mapping);
return gulp
.src(mapping.filePattern, { base: mapping.srcDir })
.pipe(gulp.dest(dstDir));
});
return merge(tasks);
});
/*
* upload Ordinal folder. (collection)
*/
async function deployCollection(
client,
srcFolder,
dstName,
deploySubdirectory = true
) {
const { getContents } = require('./tools/directories');
const collectionPath = path.join('/__/', dstName);
try {
await client.createCollection(collectionPath);
} catch (e) {
if (e.response.status === 405) {
console.log(
`the collection ${collectionPath} is already exists (perhaps)`
);
} else {
console.log(e);
throw e;
}
}
const contents = await getContents(srcFolder);
return Promise.all(
contents.map(content => {
const { stat } = content;
if (deploySubdirectory && stat.isDirectory()) {
const dstPath = path.join(dstName, content.filename);
return deployCollection(client, content.filepath, dstPath);
} else {
const dstPath = path.join(collectionPath, content.filename);
console.log(`file upload: ${content.filepath} -> ${dstPath}`);
return client.putFile(dstPath, content.filepath);
}
})
);
}
/*
* upload Engine folder.
* It referers `__src` subdirectory as engine script.
*/
async function deployEngine(client, engineFolderPath, engineName, engineMeta) {
const { getFiles } = require('./tools/directories');
const enginePath = path.join('/__/', engineName);
const engineSrcFolderPath = path.join(engineFolderPath, '__src');
try {
await client.createEngine(enginePath);
} catch (e) {
if (e.response.status === 405) {
console.log(`the engine ${enginePath} is already exists (perhaps)`);
} else {
console.log(e);
throw e;
}
}
// upload engine scripts
const files = await getFiles(engineSrcFolderPath);
const results = await Promise.all(
files.map(fileInfo => {
console.log(`engine upload: ${fileInfo.filepath}`);
return client.putFileToEngine(
enginePath,
fileInfo.filepath,
fileInfo.filename
);
})
);
// setting props
try {
await client.updateServiceEndpoint(enginePath, engineMeta);
} catch (e) {
console.log(e);
throw e;
}
return results;
}
gulp.task('deploy', () => {
const cellURL = `https://${config.personium.CELL_FQDN}/`;
const username = config.personium.CELL_ADMIN;
const password = config.personium.CELL_ADMIN_PASS;
return (async () => {
const tokens = await PersoniumAuthClient.getTokenROPC(
cellURL,
username,
password
);
const client = new PersoniumWebdavClient(cellURL, tokens.access_token);
return Promise.all(
config.personium.DIRECTORY_MAPPING.map(mapping => {
if (mapping.resourceType === CONSTANT.RESOURCETYPE.SERVICE) {
return deployEngine(
client,
path.join('build', mapping.dstDir),
mapping.dstDir,
mapping.meta
);
} else if (mapping.resourceType === CONSTANT.RESOURCETYPE.STATICFILE) {
const { getFiles } = require('./tools/directories');
return getFiles(path.join('build', mapping.dstDir)).then(contents =>
Promise.all(
contents.map(content => {
const collectionPath = path.join('/__/', mapping.dstDir);
const dstPath = path.join(collectionPath, content.filename);
console.log(`file upload: ${content.filepath} -> ${dstPath}`);
return client.putFile(dstPath, content.filepath);
})
)
);
} else {
return deployCollection(
client,
path.join('build', mapping.dstDir),
mapping.dstDir
);
}
})
);
})();
});
const webpack = require('webpack-stream');
gulp.task('webpack', () => {
return gulp
.src('src/app/frontend/index.js')
.pipe(
webpack(
Object.assign(require('./webpack.config'), { mode: 'production' })
)
)
.pipe(gulp.dest('build/public'));
});