@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
83 lines • 3.29 kB
JavaScript
import commands from '../../commands.js';
import SpoCommand from '../../../base/SpoCommand.js';
import { globalOptionsZod } from '../../../../Command.js';
import { z } from 'zod';
import { validation } from '../../../../utils/validation.js';
import { cli } from '../../../../cli/cli.js';
import { urlUtil } from '../../../../utils/urlUtil.js';
import request from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
export const options = z.strictObject({
...globalOptionsZod.shape,
webUrl: z.string()
.refine(url => validation.isValidSharePointUrl(url) === true, {
error: e => `'${e.input}' is not a valid SharePoint Online site URL.`
})
.alias('u'),
url: z.string().optional(),
id: z.uuid().optional().alias('i'),
force: z.boolean().optional().alias('f')
});
class SpoFileArchiveCommand extends SpoCommand {
get name() {
return commands.FILE_ARCHIVE;
}
get description() {
return 'Archives a file';
}
get schema() {
return options;
}
getRefinedSchema(schema) {
return schema
.refine(options => [options.url, options.id].filter(o => o !== undefined).length === 1, {
error: `Specify 'url' or 'id', but not both.`
});
}
getExcludedOptionsWithUrls() {
return ['url'];
}
async commandAction(logger, args) {
const { webUrl, url, id, force, verbose } = args.options;
if (!force) {
const result = await cli.promptForConfirmation({ message: `Are you sure you would like to archive this item? You will be able to reactivate it instantly for the first 7 days. After that, it will take up to 24 hours to reactivate.` });
if (!result) {
return;
}
}
try {
if (verbose) {
await logger.logToStderr(`Archiving file ${url || id} at site ${webUrl}...`);
}
let requestUrl = `${webUrl}/_api/web`;
if (id) {
requestUrl += `/GetFileById('${formatting.encodeQueryParameter(id)}')`;
}
else if (url) {
const serverRelativePath = urlUtil.getServerRelativePath(webUrl, url);
requestUrl += `/GetFileByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter(serverRelativePath)}')`;
}
requestUrl += '?$select=ListId,ListItemAllFields/Id&$expand=ListItemAllFields';
const fileInfo = await request.get({
url: requestUrl,
headers: {
accept: 'application/json;odata=nometadata'
},
responseType: 'json'
});
const requestOptions = {
url: `${webUrl}/_api/Lists(guid'${fileInfo.ListId}')/items(${fileInfo.ListItemAllFields.Id})/Archive`,
headers: {
accept: 'application/json;odata=nometadata'
},
responseType: 'json'
};
await request.post(requestOptions);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
}
export default new SpoFileArchiveCommand();
//# sourceMappingURL=file-archive.js.map