-
Notifications
You must be signed in to change notification settings - Fork 261
/
init-acs-env.ts
executable file
·186 lines (167 loc) · 5.5 KB
/
init-acs-env.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
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
185
186
const alfrescoApi = require('@alfresco/js-api');
const program = require('commander');
const fs = require ('fs');
const path = require('path');
import { logger } from './logger';
// eslint-disable-next-line @typescript-eslint/naming-convention
const { SharedlinksApi, FavoritesApi, NodesApi } = require('@alfresco/js-api');
const MAX_RETRY = 10;
let counter = 0;
const TIMEOUT = 6000;
const ACS_DEFAULT = require('./resources').ACS_DEFAULT;
let alfrescoJsApi;
export default async function() {
await main();
}
async function main() {
program
.version('0.1.0')
.option('--host [type]', 'Remote environment host')
.option('-p, --password [type]', 'password ')
.option('-u, --username [type]', 'username ')
.parse(process.argv);
await checkEnv();
logger.info(`***** Step initialize ACS *****`);
await initializeDefaultFiles();
}
async function initializeDefaultFiles() {
const e2eFolder = ACS_DEFAULT.e2eFolder;
const parentFolder = await createFolder(e2eFolder.name, '-my-');
const parentFolderId = parentFolder.entry.id;
for (let j = 0; j < ACS_DEFAULT.files.length; j++) {
const fileInfo = ACS_DEFAULT.files[j];
switch (fileInfo.action) {
case 'UPLOAD':
await uploadFile(fileInfo.name, parentFolderId);
break;
case 'LOCK':
const fileToLock = await uploadFile(fileInfo.name, parentFolderId);
await lockFile(fileToLock.entry.id);
break;
case 'SHARE':
const fileToShare = await uploadFile(fileInfo.name, parentFolderId);
await shareFile(fileToShare.entry.id);
break;
case 'FAVORITE':
const fileToFav = await uploadFile(fileInfo.name, parentFolderId);
await favoriteFile(fileToFav.entry.id);
break;
default:
logger.error('No action found for file ', fileInfo.name, parentFolderId);
break;
}
}
}
async function createFolder(folderName: string, parentId: string) {
let createdFolder: any;
const body = {
name: folderName,
nodeType: 'cm:folder'
};
try {
createdFolder = await new NodesApi(alfrescoJsApi).createNode(parentId, body, {overwrite: true});
logger.info(`Folder ${folderName} was created`);
} catch (err) {
if (err.status === 409) {
const relativePath = `/${folderName}`;
createdFolder = await new NodesApi(alfrescoJsApi).getNode('-my-', { relativePath });
}
}
return createdFolder;
}
async function uploadFile(fileName: string, fileDestination: string) {
const filePath = `../resources/content/${fileName}`;
const file = fs.createReadStream(path.join(__dirname, filePath));
let uploadedFile: any;
try {
uploadedFile = await alfrescoJsApi.upload.uploadFile(
file,
'',
fileDestination,
null,
{
name: fileName,
nodeType: 'cm:content',
renditions: 'doclib',
overwrite: true
}
);
logger.info(`File ${fileName} was uploaded`);
} catch (err) {
logger.error(`Failed to upload file with error: `, err.stack);
}
return uploadedFile;
}
async function lockFile(nodeId) {
const data = {
type: 'ALLOW_OWNER_CHANGES'
};
try {
await alfrescoJsApi.nodes.lockNode(nodeId, data);
logger.info('File was locked');
} catch (error) {
logger.error('Failed to lock file with error: ', error.stack);
}
}
async function shareFile(nodeId) {
const data = {
nodeId
};
try {
await new SharedlinksApi(alfrescoJsApi).createSharedLink(data);
logger.info('File was shared');
} catch (error) {
logger.error('Failed to share file with error: ', error.stack);
}
}
async function favoriteFile(nodeId) {
const data = {
target: {
['file']: {
guid: nodeId
}
}
};
try {
await new FavoritesApi(alfrescoJsApi).createFavorite('-me-', data);
logger.info('File was add to favorites');
} catch (error) {
logger.error('Failed to add the file to favorites with error: ', error.stack);
}
}
async function checkEnv() {
try {
alfrescoJsApi = new alfrescoApi.AlfrescoApiCompatibility({
provider: 'ALL',
hostBpm: program.host,
hostEcm: program.host,
authType: 'OAUTH',
oauth2: {
host: `${program.host}/auth/realms/alfresco`,
clientId: 'alfresco',
scope: 'openid'
}
});
await alfrescoJsApi.login(program.username, program.password);
} catch (e) {
if (e.error.code === 'ETIMEDOUT') {
logger.error('The env is not reachable. Terminating');
process.exit(1);
}
logger.error('Login error environment down or inaccessible');
counter++;
if (MAX_RETRY === counter) {
logger.error('Give up');
process.exit(1);
} else {
logger.error(`Retry in 1 minute attempt N ${counter}`);
sleep(TIMEOUT);
checkEnv();
}
}
}
/* eslint-enable */
function sleep(delay) {
const start = new Date().getTime();
while (new Date().getTime() < start + delay) { }
}