Skip to content

Commit

Permalink
[ADF-5324] - Expose a new adf-cli command to initialise acs-env (#6551)
Browse files Browse the repository at this point in the history
* Expose a new adf-cli command to initialize acs-env

* Use latest js api

* consume the latest js-api

* Use the previous commander version
  • Loading branch information
mauriziovitale committed Jan 21, 2021
1 parent 4e28d53 commit 5712b50
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 23 deletions.
18 changes: 9 additions & 9 deletions lib/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
"dist": "rm -rf ./dist/ && npm run build && cp -R ./bin ./dist/ && cp -R ./resources ./dist && cp -R ./templates ./dist && cp ./package.json ./dist/"
},
"dependencies": {
"@alfresco/js-api": "4.2.0",
"@alfresco/js-api": "4.3.0-61350fee919fbeb2b6c949621161b6b076d44133",
"commander": "^4.0.0",
"ejs": "^2.6.1",
"license-checker": "^25.0.1",
"npm-registry-fetch": "^4.0.5",
"request": "2.88.2",
"rxjs": "^6.5.5",
"shelljs": "^0.8.3",
"spdx-license-list": "^5.0.0",
"ejs": "^2.6.1"
"spdx-license-list": "^5.0.0"
},
"keywords": [
"alfresco-component"
Expand Down
160 changes: 160 additions & 0 deletions lib/cli/scripts/init-acs-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/* tslint:disable */
let alfrescoApi = require('@alfresco/js-api');
let program = require('commander');
let fs = require ('fs');
const path = require('path');
import { logger } from './logger';
const { SharedlinksApi, FavoritesApi } = require('@alfresco/js-api');
let MAX_RETRY = 10;
let counter = 0;
let TIMEOUT = 6000;
const ACS_DEFAULT = require('./resources').ACS_DEFAULT;
/* tslint:enable */

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() {
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, fileInfo.destination);
break;
case 'LOCK':
const fileToLock = await uploadFile(fileInfo.name, fileInfo.destination);
await lockFile(fileToLock.entry.id);
break;
case 'SHARE':
const fileToShare = await uploadFile(fileInfo.name, fileInfo.destination);
await shareFile(fileToShare.entry.id);
break;
case 'FAVORITE':
const fileToFav = await uploadFile(fileInfo.name, fileInfo.destination);
await favoriteFile(fileToFav.entry.id);
break;
default:
logger.error('No action found for file ', fileInfo.name);
break;
}
}
}

async function uploadFile(fileName, fileDestination) {
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: 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) {
logger.info('Login error environment down or inaccessible');
counter++;
if (MAX_RETRY === counter) {
logger.info('Give up');
process.exit(1);
} else {
logger.info(`Retry in 1 minute attempt N ${counter}`);
sleep(TIMEOUT);
checkEnv();
}
}
}

/* tslint:enable */

function sleep(delay) {
const start = new Date().getTime();
while (new Date().getTime() < start + delay) { }
}
34 changes: 34 additions & 0 deletions lib/cli/scripts/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,37 @@ export const ACTIVITI_APPS: any = {
}
]
};
export const ACS_DEFAULT: any = {
files : [
{
name: 'share_profile_pic.png',
destination: '-my-',
action: 'UPLOAD'
},
{
name: 'share_profile_pic.jpg',
destination: '-my-',
action: 'UPLOAD'
},
{
name: 'lock.png',
destination: '-my-',
action: 'LOCK'
},
{
name: 'second_lock.png',
destination: '-my-',
action: 'LOCK'
},
{
name: 'share_file.jpg',
destination: '-my-',
action: 'SHARE'
},
{
name: 'favorite_file.jpg',
destination: '-my-',
action: 'FAVORITE'
}
]
};
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"process services-cloud"
],
"dependencies": {
"@alfresco/js-api": "4.3.0-2dcc5f6dd1e2bc2e0e9ba28c1aa5bc8f6a8ddf61",
"@alfresco/js-api": "4.3.0-61350fee919fbeb2b6c949621161b6b076d44133",
"@angular/animations": "^10.0.4",
"@angular/cdk": "10.1.3",
"@angular/common": "^10.0.4",
Expand Down Expand Up @@ -121,7 +121,7 @@
"@types/selenium-webdriver": "^4.0.11",
"ajv-cli": "^3.0.0",
"codelyzer": "^6.0.1",
"commander": "7.0.0",
"commander": "6.2.1",
"concurrently": "^5.3.0",
"cspell": "^5.1.3",
"css-loader": "^4.3.0",
Expand Down

0 comments on commit 5712b50

Please sign in to comment.