-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd-handler.js
158 lines (140 loc) · 5.11 KB
/
cmd-handler.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
/*
* Copyright 2025 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import fs from 'fs';
import chalk from 'chalk';
import path from 'path';
import { cleanup, downloadAssets } from './download-assets.js';
import { uploadAssets } from './upload-assets.js';
import { installPackage } from './package-helper.js';
import fetch from 'node-fetch';
import { getDamRootFolder } from './aem-util.js';
/**
* Validate the existence of the asset-mapping.json and content package files.
* @param {string} assetMappingFile - The path to the asset-mapping.json file
* @param {string} contentPackagePath - The path to the content package ZIP file
* @return {boolean} True if the files exist, false otherwise
*/
function validateFiles(assetMappingFile, contentPackagePath) {
const files = [
{ path: contentPackagePath, message: `Content package not found: ${contentPackagePath}` },
{ path: assetMappingFile, message: `asset-mapping.json file not found: ${assetMappingFile}` },
];
for (const file of files) {
if (!fs.existsSync(file.path)) {
console.error(chalk.red(file.message));
return false;
}
}
return true;
}
/**
* Validate the AEM login token by making a HEAD request to the target environment.
* @param url - The AEM target environment
* @param token - The AEM login token
* @return {Promise<boolean>} True if the token is valid, false otherwise
*/
async function validateLogin(url, token) {
try {
const headers = {
Authorization: `Bearer ${token}`,
};
const response = await fetch(url, { method: 'HEAD', headers });
if (!response.ok) {
console.error(chalk.red(`Login failed with status: ${response.status} - ${response.statusText}`));
if (response.status === 401 || response.status === 403) {
console.error(chalk.red('Unauthorized: Invalid token'));
}
return false;
}
const text = await response.text();
if (text.includes('Invalid token') || text.includes('Unauthorized')) {
console.error(chalk.red(`Invalid token detected in response body: ${text}`));
return false;
}
return response.status === 200;
} catch (error) {
console.error(chalk.red(`Network error: ${error.message}`));
return false;
}
}
export const aemBuilder = (yargs) => {
return yargs
.option('zip', {
type: 'string',
describe: 'Absolute path to the content package ZIP file',
demandOption: true,
})
.option('asset-mapping', {
type: 'string',
describe: 'Absolute path to the image-mapping.json file',
demandOption: true,
})
.option('token', {
describe: 'AEM login token or path to a file containing the token',
type: 'string',
demandOption: true,
})
.option('target', {
describe: 'AEM target environment',
type: 'string',
demandOption: true,
})
.option('output', {
describe: 'Output directory for downloaded assets',
type: 'string',
default: 'aem-assets',
})
.option('keep', {
describe: 'If keep is true, local assets are not deleted after upload',
type: 'boolean',
default: false,
});
}
export const aemHandler = async (args) => {
if (!validateFiles(args['asset-mapping'], args['zip'])) {
process.exit(1);
}
if (!fs.existsSync(args.output)) {
fs.mkdirSync(args.output);
}
// check to see if the token is a string value or a file
let token = args.token;
if (fs.existsSync(token)) {
token = fs.readFileSync(token, 'utf-8').trim();
}
console.log(chalk.yellow('Validating token...'));
if (!await validateLogin(args.target, token)) {
process.exit(1);
}
try {
const assetMappingJson = JSON.parse(fs.readFileSync(args['asset-mapping'], 'utf-8'));
const assetMapping = new Map(Object.entries(assetMappingJson));
const downloadFolder = args.output === 'aem-assets'
? path.join(process.cwd(), args.output)
: args.output;
console.log(chalk.yellow('Downloading origin assets...'));
await downloadAssets(assetMapping, downloadFolder);
const assetFolder = path.join(downloadFolder, getDamRootFolder(assetMapping));
console.log(chalk.yellow(`Uploading downloaded assets to ${args.target}...`));
await uploadAssets(args.target, token, assetFolder);
console.log(chalk.yellow(`Uploading content package ${args.target}...`));
await installPackage(args.target, token, args['zip']);
if (!args.keep) {
await cleanup(downloadFolder);
}
} catch (err) {
console.error(chalk.red('Error during upload:', err));
process.exit(1);
}
console.log(chalk.green('Content uploaded successfully.'));
process.exit(0)
}