@pnp/cli-microsoft365 6.0.0-beta.d4d33ed → 6.0.0-beta.f305313

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const cli_1 = require("../../../../cli");
4
+ const request_1 = require("../../../../request");
5
+ const utils_1 = require("../../../../utils");
6
+ const SpoCommand_1 = require("../../../base/SpoCommand");
7
+ const commands_1 = require("../../commands");
8
+ const removeCommand = require('./file-remove');
9
+ class SpoFileRenameCommand extends SpoCommand_1.default {
10
+ get name() {
11
+ return commands_1.default.FILE_RENAME;
12
+ }
13
+ get description() {
14
+ return 'Renames a file';
15
+ }
16
+ getTelemetryProperties(args) {
17
+ const telemetryProps = super.getTelemetryProperties(args);
18
+ telemetryProps.force = !!args.options.force;
19
+ return telemetryProps;
20
+ }
21
+ commandAction(logger, args, cb) {
22
+ const webUrl = args.options.webUrl;
23
+ const originalFileServerRelativeUrl = utils_1.urlUtil.getServerRelativePath(args.options.webUrl, args.options.sourceUrl);
24
+ this
25
+ .getFile(originalFileServerRelativeUrl, webUrl)
26
+ .then((_) => {
27
+ if (args.options.force) {
28
+ return this.deleteFile(webUrl, args.options.sourceUrl, args.options.targetFileName);
29
+ }
30
+ return Promise.resolve();
31
+ })
32
+ .then(_ => {
33
+ const requestBody = {
34
+ formValues: [{
35
+ FieldName: 'FileLeafRef',
36
+ FieldValue: args.options.targetFileName
37
+ }]
38
+ };
39
+ const requestOptions = {
40
+ url: `${webUrl}/_api/web/GetFileByServerRelativeUrl('${encodeURIComponent(originalFileServerRelativeUrl)}')/ListItemAllFields/ValidateUpdateListItem()`,
41
+ headers: {
42
+ 'accept': 'application/json;odata=nometadata'
43
+ },
44
+ data: requestBody,
45
+ responseType: 'json'
46
+ };
47
+ return request_1.default.post(requestOptions);
48
+ })
49
+ .then((resp) => {
50
+ logger.log(resp.value);
51
+ cb();
52
+ }, (err) => this.handleRejectedODataJsonPromise(err, logger, cb));
53
+ }
54
+ getFile(originalFileServerRelativeUrl, webUrl) {
55
+ const requestUrl = `${webUrl}/_api/web/GetFileByServerRelativeUrl('${encodeURIComponent(originalFileServerRelativeUrl)}')?$select=UniqueId`;
56
+ const requestOptions = {
57
+ url: requestUrl,
58
+ headers: {
59
+ 'accept': 'application/json;odata=nometadata'
60
+ },
61
+ responseType: 'json'
62
+ };
63
+ return request_1.default.get(requestOptions);
64
+ }
65
+ deleteFile(webUrl, sourceUrl, targetFileName) {
66
+ const targetFileServerRelativeUrl = `${utils_1.urlUtil.getServerRelativePath(webUrl, sourceUrl.substring(0, sourceUrl.lastIndexOf('/')))}/${targetFileName}`;
67
+ const options = {
68
+ webUrl: webUrl,
69
+ url: targetFileServerRelativeUrl,
70
+ recycle: true,
71
+ confirm: true,
72
+ debug: this.debug,
73
+ verbose: this.verbose
74
+ };
75
+ return cli_1.Cli.executeCommandWithOutput(removeCommand, { options: Object.assign(Object.assign({}, options), { _: [] }) })
76
+ .then(_ => {
77
+ return Promise.resolve();
78
+ }, (err) => {
79
+ if (err.error !== null && err.error.message !== null && err.error.message.includes('does not exist')) {
80
+ return Promise.resolve();
81
+ }
82
+ return Promise.reject(err);
83
+ });
84
+ }
85
+ options() {
86
+ const options = [
87
+ {
88
+ option: '-u, --webUrl <webUrl>'
89
+ },
90
+ {
91
+ option: '-s, --sourceUrl <sourceUrl>'
92
+ },
93
+ {
94
+ option: '-t, --targetFileName <targetFileName>'
95
+ },
96
+ {
97
+ option: '--force'
98
+ }
99
+ ];
100
+ const parentOptions = super.options();
101
+ return options.concat(parentOptions);
102
+ }
103
+ validate(args) {
104
+ return utils_1.validation.isValidSharePointUrl(args.options.webUrl);
105
+ }
106
+ }
107
+ module.exports = new SpoFileRenameCommand();
108
+ //# sourceMappingURL=file-rename.js.map
@@ -54,6 +54,7 @@ exports.default = {
54
54
  FILE_LIST: `${prefix} file list`,
55
55
  FILE_MOVE: `${prefix} file move`,
56
56
  FILE_REMOVE: `${prefix} file remove`,
57
+ FILE_RENAME: `${prefix} file rename`,
57
58
  FILE_SHARINGINFO_GET: `${prefix} file sharinginfo get`,
58
59
  FOLDER_ADD: `${prefix} folder add`,
59
60
  FOLDER_COPY: `${prefix} folder copy`,
@@ -0,0 +1,43 @@
1
+ # spo file rename
2
+
3
+ Renames a file
4
+
5
+ ## Usage
6
+
7
+ ```sh
8
+ m365 spo file rename [options]
9
+ ```
10
+
11
+ ## Options
12
+
13
+ `-u, --webUrl <webUrl>`
14
+ : The URL of the site where the file is located
15
+
16
+ `-s, --sourceUrl <sourceUrl>`
17
+ : Site-relative URL of the file to rename
18
+
19
+ `-t, --targetFileName <targetFileName>`
20
+ : New file name of the file
21
+
22
+ `--force`
23
+ : If a file already exists with target file name, it will be moved to the recycle bin. If omitted, the rename operation will be canceled if a file already exists with the specified file name
24
+
25
+ --8<-- "docs/cmd/_global.md"
26
+
27
+ ## Remarks
28
+
29
+ If you try to rename a file without the `--force` flag and a file with this name already exists, the operation will be cancelled.
30
+
31
+ ## Examples
32
+
33
+ Renames a file with server-relative URL _/Shared Documents/Test1.docx_ located in site _<https://contoso.sharepoint.com/sites/project-x>_ to _Test2.docx_
34
+
35
+ ```sh
36
+ m365 spo file rename --webUrl https://contoso.sharepoint.com/sites/project-x --sourceUrl '/Shared Documents/Test1.docx' --targetFileName 'Test2.docx'
37
+ ```
38
+
39
+ Renames a file with server-relative URL _/Shared Documents/Test1.docx_ located in site _<https://contoso.sharepoint.com/sites/project-x>_ to _Test2.docx_. If the file with the target file name already exists, this file will be moved to the recycle bin
40
+
41
+ ```sh
42
+ m365 spo file rename --webUrl https://contoso.sharepoint.com/sites/project-x --sourceUrl '/Shared Documents/Test1.docx' --targetFileName 'Test2.docx' --force
43
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnp/cli-microsoft365",
3
- "version": "6.0.0-beta.d4d33ed",
3
+ "version": "6.0.0-beta.f305313",
4
4
  "description": "Manage Microsoft 365 and SharePoint Framework projects on any platform",
5
5
  "license": "MIT",
6
6
  "main": "./dist/api.js",