powerbi-visuals-tools 7.0.0 → 7.0.2-beta.1

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.
Files changed (48) hide show
  1. package/Changelog.md +9 -0
  2. package/certs/blank +0 -0
  3. package/lib/CertificateTools.js +262 -0
  4. package/lib/CommandManager.js +75 -0
  5. package/lib/ConsoleWriter.js +188 -0
  6. package/lib/FeatureManager.js +43 -0
  7. package/lib/LintValidator.js +61 -0
  8. package/lib/Package.js +46 -0
  9. package/lib/TemplateFetcher.js +111 -0
  10. package/lib/Visual.js +29 -0
  11. package/lib/VisualGenerator.js +221 -0
  12. package/lib/VisualManager.js +316 -0
  13. package/lib/WebPackWrap.js +265 -0
  14. package/lib/features/APIVersion.js +16 -0
  15. package/lib/features/AdvancedEditMode.js +12 -0
  16. package/lib/features/AllowInteractions.js +12 -0
  17. package/lib/features/AnalyticsPane.js +17 -0
  18. package/lib/features/BaseFeature.js +9 -0
  19. package/lib/features/Bookmarks.js +12 -0
  20. package/lib/features/ColorPalette.js +12 -0
  21. package/lib/features/ConditionalFormatting.js +12 -0
  22. package/lib/features/ContextMenu.js +12 -0
  23. package/lib/features/DrillDown.js +16 -0
  24. package/lib/features/FeatureTypes.js +23 -0
  25. package/lib/features/FetchMoreData.js +22 -0
  26. package/lib/features/FileDownload.js +12 -0
  27. package/lib/features/FormatPane.js +12 -0
  28. package/lib/features/HighContrast.js +12 -0
  29. package/lib/features/HighlightData.js +12 -0
  30. package/lib/features/KeyboardNavigation.js +12 -0
  31. package/lib/features/LandingPage.js +12 -0
  32. package/lib/features/LaunchURL.js +12 -0
  33. package/lib/features/LocalStorage.js +12 -0
  34. package/lib/features/Localizations.js +12 -0
  35. package/lib/features/ModalDialog.js +12 -0
  36. package/lib/features/RenderingEvents.js +13 -0
  37. package/lib/features/SelectionAcrossVisuals.js +12 -0
  38. package/lib/features/SyncSlicer.js +12 -0
  39. package/lib/features/Tooltips.js +12 -0
  40. package/lib/features/TotalSubTotal.js +12 -0
  41. package/lib/features/VisualVersion.js +12 -0
  42. package/lib/features/WarningIcon.js +12 -0
  43. package/lib/features/index.js +28 -0
  44. package/lib/utils.js +43 -0
  45. package/lib/webpack.config.js +169 -0
  46. package/package.json +11 -8
  47. package/.eslintrc.json +0 -21
  48. package/CVClientLA.md +0 -85
package/lib/Package.js ADDED
@@ -0,0 +1,46 @@
1
+ /*
2
+ * Power BI Visual CLI
3
+ *
4
+ * Copyright (c) Microsoft Corporation
5
+ * All rights reserved.
6
+ * MIT License
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the ""Software""), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in
16
+ * all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ * THE SOFTWARE.
25
+ */
26
+ "use strict";
27
+ import isMatch from "lodash.ismatch";
28
+ /**
29
+ * Represents an instance of a visual package based on file path
30
+ */
31
+ export default class Package {
32
+ sourceCode;
33
+ capabilities;
34
+ visualFeatureType;
35
+ constructor(sourceCode, capabilities, visualFeatureType) {
36
+ this.sourceCode = sourceCode;
37
+ this.capabilities = capabilities;
38
+ this.visualFeatureType = visualFeatureType;
39
+ }
40
+ contain(keyword) {
41
+ return this.sourceCode.includes(keyword);
42
+ }
43
+ isCapabilityEnabled(expectedObject) {
44
+ return isMatch(this.capabilities, expectedObject);
45
+ }
46
+ }
@@ -0,0 +1,111 @@
1
+ import { createFolder, download, readJsonFromRoot } from './utils.js';
2
+ import ConsoleWriter from './ConsoleWriter.js';
3
+ import JSZip from 'jszip';
4
+ import VisualGenerator from "./VisualGenerator.js";
5
+ import { exec } from 'child_process';
6
+ import fs from 'fs-extra';
7
+ import path from 'path';
8
+ const config = await readJsonFromRoot('config.json');
9
+ export default class TemplateFetcher {
10
+ templateName;
11
+ visualName;
12
+ folderName;
13
+ apiVersion;
14
+ constructor(templateName, visualName, apiVersion) {
15
+ this.templateName = templateName;
16
+ this.visualName = visualName;
17
+ this.folderName = `${this.visualName}`;
18
+ this.apiVersion = apiVersion;
19
+ }
20
+ fetch() {
21
+ const folder = createFolder.call(this, this.folderName);
22
+ download.call(this, config.visualTemplates[this.templateName], path.join(folder, "template.zip"))
23
+ .then(this.extractFiles.bind(this))
24
+ .then(this.removeZipFile.bind(this))
25
+ .then(this.setVisualGUID.bind(this))
26
+ .then(this.setApiVersion.bind(this))
27
+ .then(this.runNpmInstall.bind(this))
28
+ .then(this.showNextSteps.bind(this));
29
+ }
30
+ async removeZipFile() {
31
+ const folder = path.join("./", this.folderName);
32
+ const fileName = path.join(folder, "template.zip");
33
+ await fs.unlink(`.${path.sep}${fileName}`, (err) => {
34
+ if (err) {
35
+ ConsoleWriter.warning(`.${path.sep}${fileName} was not deleted`);
36
+ }
37
+ });
38
+ }
39
+ async extractFiles(file) {
40
+ const filePath = path.join(process.cwd(), file.path);
41
+ const buffer = await fs.readFile(filePath);
42
+ const zip = await JSZip.loadAsync(buffer);
43
+ const filesList = Object.keys(zip.files);
44
+ for (const filename of filesList) {
45
+ if (filename[filename.length - 1] === "/") {
46
+ // generate folders for exclude parent folder
47
+ const dest = path.join(path.dirname(filePath), path.join(filename, ".."));
48
+ await fs.ensureDir(dest);
49
+ }
50
+ else {
51
+ // write files into dirs for exclude parent folder
52
+ const dest = path.join(path.dirname(filePath), path.join(path.dirname(filename), "..", filename.split("/").pop() ?? ""));
53
+ const content = await zip.file(filename)?.async('nodebuffer');
54
+ await fs.writeFile(dest, content);
55
+ }
56
+ }
57
+ }
58
+ async setApiVersion() {
59
+ if (!this.apiVersion) {
60
+ return;
61
+ }
62
+ ConsoleWriter.info(`Set Visual API to ${this.apiVersion}`);
63
+ const packageJsonFile = path.join(process.cwd(), this.folderName, "package.json");
64
+ const packageJson = await fs.readJson(packageJsonFile);
65
+ if (packageJson.devDependencies && packageJson.devDependencies["powerbi-visuals-api"]) {
66
+ packageJson.devDependencies["powerbi-visuals-api"] = `~${this.apiVersion}`;
67
+ }
68
+ if (packageJson.dependencies && packageJson.dependencies["powerbi-visuals-api"]) {
69
+ packageJson.dependencies["powerbi-visuals-api"] = `~${this.apiVersion}`;
70
+ }
71
+ await fs.writeJSON(packageJsonFile, packageJson);
72
+ }
73
+ async setVisualGUID() {
74
+ const pbivizJsonFile = path.join(process.cwd(), this.folderName, "pbiviz.json");
75
+ const pbivizJson = await fs.readJson(pbivizJsonFile);
76
+ pbivizJson.visual.guid = this.visualName + VisualGenerator.generateVisualGuid();
77
+ await fs.writeJSON(pbivizJsonFile, pbivizJson);
78
+ }
79
+ runNpmInstall() {
80
+ return new Promise((resolve, reject) => {
81
+ ConsoleWriter.info("Installing packages...");
82
+ process.chdir(this.folderName);
83
+ // const { stdout, stderr } = await exec('ls');
84
+ const child = exec('npm install', (error, stdout, stderr) => {
85
+ if (error) {
86
+ ConsoleWriter.error(error.stack);
87
+ ConsoleWriter.error(`Error code: ${error.code}`);
88
+ ConsoleWriter.error(`Signal received: ${error.signal}`);
89
+ }
90
+ ConsoleWriter.warning(stderr);
91
+ ConsoleWriter.info(stdout);
92
+ resolve(true);
93
+ });
94
+ child.on("error", (er) => {
95
+ ConsoleWriter.error(er);
96
+ reject();
97
+ });
98
+ child.on("exit", (code) => {
99
+ if (code !== 0) {
100
+ ConsoleWriter.error(`npm install stopped with code ${code}`);
101
+ reject();
102
+ }
103
+ });
104
+ });
105
+ }
106
+ showNextSteps() {
107
+ ConsoleWriter.blank();
108
+ ConsoleWriter.info("Run `npm run start` to start visual development");
109
+ ConsoleWriter.info("Run `npm run package` to create visual package");
110
+ }
111
+ }
package/lib/Visual.js ADDED
@@ -0,0 +1,29 @@
1
+ import { compareVersions } from "compare-versions";
2
+ import { VisualFeatureType } from "./features/FeatureTypes.js";
3
+ export class Visual {
4
+ visualFeatureType;
5
+ capabilities;
6
+ config;
7
+ visualVersion;
8
+ constructor(capabilities, config) {
9
+ this.capabilities = capabilities;
10
+ this.config = config;
11
+ this.visualFeatureType = this.getVisualFeatureType();
12
+ this.visualVersion = config.visual.version;
13
+ }
14
+ doesAPIVersionMatch(minAPIversion) {
15
+ return compareVersions(this.config.apiVersion ?? minAPIversion, minAPIversion) !== -1;
16
+ }
17
+ isVisualVersionValid(length) {
18
+ return this.visualVersion.split(".").length === length;
19
+ }
20
+ getVisualFeatureType() {
21
+ const isMatrixSupported = this.capabilities?.dataViewMappings?.some(dataView => dataView.matrix);
22
+ const isSlicer = Boolean(this.capabilities?.objects?.general?.properties?.filter?.type?.filter);
23
+ let type = isSlicer ? VisualFeatureType.Slicer : VisualFeatureType.NonSlicer;
24
+ if (isMatrixSupported) {
25
+ type = type | VisualFeatureType.Matrix;
26
+ }
27
+ return type;
28
+ }
29
+ }
@@ -0,0 +1,221 @@
1
+ /*
2
+ * Power BI Visual CLI
3
+ *
4
+ * Copyright (c) Microsoft Corporation
5
+ * All rights reserved.
6
+ * MIT License
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the ""Software""), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in
16
+ * all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ * THE SOFTWARE.
25
+ */
26
+ "use strict";
27
+ import crypto from 'crypto';
28
+ import { getRootPath, readJsonFromRoot } from './utils.js';
29
+ import { compareVersions } from "compare-versions";
30
+ import fs from 'fs-extra';
31
+ import lodashDefaults from 'lodash.defaults';
32
+ import path from 'path';
33
+ import template from '../templates/pbiviz-json-template.js';
34
+ const config = await readJsonFromRoot('config.json');
35
+ const VISUAL_TEMPLATES_PATH = path.join(getRootPath(), config.templates.visuals);
36
+ const API_VERSION = config.generate.apiVersion;
37
+ const minAPIversion = config.constants.minAPIversion;
38
+ /**
39
+ * Generates the data for the visual
40
+ */
41
+ function generateVisualOptions(visualName, apiVersion) {
42
+ const name = generateVisualName(visualName);
43
+ return {
44
+ name: name,
45
+ displayName: visualName,
46
+ guid: name + VisualGenerator.generateVisualGuid(),
47
+ visualClassName: 'Visual',
48
+ apiVersion: apiVersion
49
+ };
50
+ }
51
+ /**
52
+ *
53
+ */
54
+ function generateVisualName(displayName) {
55
+ return displayName.replace(/(?:^\w|[A-Z]|\b\w|_|\s+)/g, (match, index) => {
56
+ if (/\s|_+/.test(match)) {
57
+ return "";
58
+ }
59
+ return index === 0 ? match.toLowerCase() : match.toUpperCase();
60
+ });
61
+ }
62
+ /**
63
+ * Creates a default pbiviz.json config file
64
+ *
65
+ * @param {string} visualPath - path to the visual
66
+ * @param {Object} options - visual information for populating the pbiviz.json template
67
+ * @param {string} templateName - external js files
68
+ */
69
+ function createPbiVizJson(visualPath, options, templateName) {
70
+ // read the global template data
71
+ // and generate the actual file content
72
+ let data = template(options);
73
+ // write out the target file content
74
+ const targetPath = path.join(visualPath, 'pbiviz.json');
75
+ fs.writeFileSync(targetPath, data);
76
+ let templatePath = path.join(VISUAL_TEMPLATES_PATH, templateName);
77
+ templatePath = path.join(templatePath, 'pbiviz.json');
78
+ if (templateName && fileExists(templatePath)) {
79
+ //read the target file content
80
+ data = fs.readJsonSync(targetPath);
81
+ //override externalJS settings with those of the local template file
82
+ const templateData = fs.readJsonSync(templatePath);
83
+ for (const objKey of Object.keys(templateData)) {
84
+ data[objKey] = templateData[objKey];
85
+ }
86
+ // write out the target file content
87
+ fs.writeJsonSync(targetPath, data);
88
+ }
89
+ }
90
+ /**
91
+ * Checks if the specified file exists
92
+ *
93
+ * @param {string} file - path to the file
94
+ */
95
+ function fileExists(file) {
96
+ try {
97
+ fs.accessSync(file);
98
+ }
99
+ catch (e) {
100
+ return false;
101
+ }
102
+ return true;
103
+ }
104
+ /**
105
+ * Copies the visual template directory
106
+ *
107
+ * @param {string} targetPath - file path to root of visual package
108
+ * @param {string} templateName - template to use for generating the visual
109
+ */
110
+ function copyVisualTemplate(targetPath, templateName) {
111
+ fs.copySync(path.join(VISUAL_TEMPLATES_PATH, '_global'), targetPath);
112
+ fs.copySync(path.join(VISUAL_TEMPLATES_PATH, templateName), targetPath);
113
+ }
114
+ /**
115
+ * Checks if the specified template is valid
116
+ *
117
+ * @param {string} templateName - template to use for generating the visual
118
+ */
119
+ function validTemplate(templateName) {
120
+ try {
121
+ fs.accessSync(path.join(VISUAL_TEMPLATES_PATH, templateName));
122
+ }
123
+ catch (e) {
124
+ return false;
125
+ }
126
+ return true;
127
+ }
128
+ const defaultOptions = {
129
+ force: false,
130
+ template: 'default',
131
+ apiVersion: API_VERSION,
132
+ externalJS: []
133
+ };
134
+ export default class VisualGenerator {
135
+ /**
136
+ * Generates a new visual
137
+ *
138
+ * @param {string} targetPath - file path for creation of the new visual package
139
+ * @param {string} visualName - name of the new visual package
140
+ * @param {object} options - specify options for the visual generator
141
+ * @returns {Promise<string>} - promise resolves with the path to the newly created package
142
+ */
143
+ static generateVisual(targetPath, visualName, options) {
144
+ return new Promise((resolve, reject) => {
145
+ const buildOptions = lodashDefaults(options, defaultOptions);
146
+ if (!buildOptions.apiVersion || compareVersions(buildOptions.apiVersion, minAPIversion) === -1) {
147
+ return reject(new Error(`Can not generate a visual with an API below than ${minAPIversion}, current API is '${buildOptions.apiVersion}'.`));
148
+ }
149
+ const visualOptions = generateVisualOptions(visualName, buildOptions.apiVersion);
150
+ const validationResult = VisualGenerator.checkVisualName(visualOptions.name);
151
+ if (!visualOptions || !visualOptions.name || validationResult) {
152
+ return reject(new Error(validationResult || "Invalid visual name"));
153
+ }
154
+ if (!validTemplate(buildOptions.template)) {
155
+ return reject(new Error(`Invalid template "${buildOptions.template}"`));
156
+ }
157
+ const visualPath = path.join(targetPath, visualOptions.name);
158
+ fs.access(visualPath, err => {
159
+ if (!err && !buildOptions.force) {
160
+ return reject(new Error('This visual already exists. Use force to overwrite.'));
161
+ }
162
+ try {
163
+ if (!err && buildOptions.force) {
164
+ fs.removeSync(visualPath);
165
+ }
166
+ copyVisualTemplate(visualPath, buildOptions.template);
167
+ createPbiVizJson(visualPath, visualOptions, options.template);
168
+ resolve(visualPath);
169
+ }
170
+ catch (e) {
171
+ reject(e);
172
+ }
173
+ });
174
+ });
175
+ }
176
+ /**
177
+ * Generates a random GUID for your visual
178
+ */
179
+ static generateVisualGuid() {
180
+ return crypto.randomUUID().replace(/-/g, '').toUpperCase();
181
+ }
182
+ /**
183
+ * Check visual name
184
+ * Using https://github.com/mathiasbynens/mothereff.in/tree/master/js-properties
185
+ *
186
+ * @static
187
+ * @param {string} name Visual name
188
+ * @returns {string} error message
189
+ *
190
+ * @memberof VisualGenerator
191
+ */
192
+ static checkVisualName(name) {
193
+ const regexES3ReservedWord = /^(?:do|if|in|for|int|new|try|var|byte|case|char|else|enum|goto|long|null|this|true|void|with|break|catch|class|const|false|final|float|short|super|throw|while|delete|double|export|import|native|public|return|static|switch|throws|typeof|boolean|default|extends|finally|package|private|abstract|continue|debugger|function|volatile|interface|protected|transient|implements|instanceof|synchronized)$/;
194
+ const regexNumber = /^(?![+-])([0-9\+\-\.]+)/;
195
+ const regexZeroWidth = /\u200c|\u200d/;
196
+ const regexpWrongSymbols = /^[a-zA-Z0-9]+$/;
197
+ const valueAsUnescapedString = name.replace(/\\u([a-fA-F0-9]{4})|\\u\{([0-9a-fA-F]{1,})\}/g, ($0, $1, $2) => {
198
+ const codePoint = parseInt($2 || $1, 16);
199
+ // If it’s a surrogate…
200
+ if (codePoint >= 0xD800 && codePoint <= 0xDFFF) {
201
+ // Return a character that is never valid in an identifier.
202
+ // This prevents the surrogate from pairing with another.
203
+ return '\0';
204
+ }
205
+ return String.fromCodePoint(codePoint);
206
+ });
207
+ if (regexNumber.test(name)) {
208
+ return `The visual name can't begin with a number digit`;
209
+ }
210
+ else if (!regexpWrongSymbols.test(name)) {
211
+ return `The visual name can contain only letters and numbers`;
212
+ }
213
+ else if (regexES3ReservedWord.test(valueAsUnescapedString)) {
214
+ return `The visual name cannot be equal to a reserved JavaScript keyword.
215
+ More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords`;
216
+ }
217
+ else if (regexZeroWidth.test(valueAsUnescapedString)) {
218
+ return `The visual name can't be empty`;
219
+ }
220
+ }
221
+ }