@asyncapi/generator 2.8.4 → 3.0.0

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 (82) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/docs/api.md +2 -51
  3. package/docs/asyncapi-document.md +1 -1
  4. package/docs/configuration-file.md +0 -4
  5. package/docs/file-templates.md +1 -59
  6. package/docs/generator-template-java.md +0 -2
  7. package/docs/generator-template.md +1 -4
  8. package/docs/index.md +2 -2
  9. package/docs/jsdoc2md-handlebars/main-index/global-index/global-index-dl.hbs +13 -0
  10. package/docs/jsdoc2md-handlebars/member-index-grouped.hbs +6 -0
  11. package/docs/jsdoc2md-handlebars/member-index-list.hbs +6 -0
  12. package/docs/jsdoc2md-handlebars/members.hbs +1 -0
  13. package/docs/react-render-engine.md +5 -37
  14. package/docs/template-development.md +2 -7
  15. package/docs/template.md +2 -2
  16. package/docs/typescript-support.md +3 -5
  17. package/jest.config.js +3 -6
  18. package/lib/__mocks__/utils.js +0 -3
  19. package/lib/generator.js +4 -20
  20. package/lib/templates/bakedInTemplates/core-template-client-kafka-java-quarkus/.ageneratorrc +2 -4
  21. package/lib/templates/bakedInTemplates/core-template-client-kafka-java-quarkus/__transpiled/jest.config.js +13 -0
  22. package/lib/templates/bakedInTemplates/core-template-client-kafka-java-quarkus/__transpiled/jest.config.js.map +1 -0
  23. package/lib/templates/bakedInTemplates/core-template-client-kafka-java-quarkus/jest.config.js +14 -0
  24. package/lib/templates/bakedInTemplates/core-template-client-kafka-java-quarkus/package.json +3 -17
  25. package/lib/templates/bakedInTemplates/core-template-client-websocket-dart/.ageneratorrc +0 -1
  26. package/lib/templates/bakedInTemplates/core-template-client-websocket-dart/__transpiled/jest.config.js +13 -0
  27. package/lib/templates/bakedInTemplates/core-template-client-websocket-dart/__transpiled/jest.config.js.map +1 -0
  28. package/lib/templates/bakedInTemplates/core-template-client-websocket-dart/jest.config.js +14 -0
  29. package/lib/templates/bakedInTemplates/core-template-client-websocket-dart/package.json +3 -17
  30. package/lib/templates/bakedInTemplates/core-template-client-websocket-java-quarkus/.ageneratorrc +0 -2
  31. package/lib/templates/bakedInTemplates/core-template-client-websocket-java-quarkus/__transpiled/jest.config.js +13 -0
  32. package/lib/templates/bakedInTemplates/core-template-client-websocket-java-quarkus/__transpiled/jest.config.js.map +1 -0
  33. package/lib/templates/bakedInTemplates/core-template-client-websocket-java-quarkus/jest.config.js +14 -0
  34. package/lib/templates/bakedInTemplates/core-template-client-websocket-java-quarkus/package.json +4 -18
  35. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/.ageneratorrc +8 -1
  36. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/ClientClass.js +5 -1
  37. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/ClientClass.js.map +1 -1
  38. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/CompileOperationSchemas.js +48 -0
  39. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/CompileOperationSchemas.js.map +1 -0
  40. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/Constructor.js +7 -1
  41. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/Constructor.js.map +1 -1
  42. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/jest.config.js +13 -0
  43. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/jest.config.js.map +1 -0
  44. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/template/README.md.js +5 -153
  45. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/template/README.md.js.map +1 -1
  46. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/template/client.js.js +52 -3
  47. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/template/client.js.js.map +1 -1
  48. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/components/ClientClass.js +4 -2
  49. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/components/CompileOperationSchemas.js +40 -0
  50. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/components/Constructor.js +7 -1
  51. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/jest.config.js +14 -0
  52. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/package.json +7 -20
  53. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/template/README.md.js +2 -103
  54. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/template/client.js.js +5 -1
  55. package/lib/templates/bakedInTemplates/core-template-client-websocket-python/.ageneratorrc +0 -1
  56. package/lib/templates/bakedInTemplates/core-template-client-websocket-python/__transpiled/jest.config.js +13 -0
  57. package/lib/templates/bakedInTemplates/core-template-client-websocket-python/__transpiled/jest.config.js.map +1 -0
  58. package/lib/templates/bakedInTemplates/core-template-client-websocket-python/__transpiled/template/README.md.js +18 -0
  59. package/lib/templates/bakedInTemplates/core-template-client-websocket-python/__transpiled/template/README.md.js.map +1 -0
  60. package/lib/templates/bakedInTemplates/core-template-client-websocket-python/jest.config.js +14 -0
  61. package/lib/templates/bakedInTemplates/core-template-client-websocket-python/package.json +3 -17
  62. package/lib/templates/bakedInTemplates/core-template-client-websocket-python/template/README.md.js +5 -0
  63. package/lib/templates/config/validator.js +3 -3
  64. package/lib/utils.js +0 -21
  65. package/package.json +7 -14
  66. package/Dockerfile +0 -20
  67. package/cli.js +0 -205
  68. package/docs/migration-cli.md +0 -70
  69. package/docs/migration-nunjucks-react.md +0 -144
  70. package/docs/nunjucks-render-engine.md +0 -83
  71. package/lib/filtersRegistry.js +0 -134
  72. package/lib/renderer/nunjucks.js +0 -42
  73. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/AvailableOperations.js +0 -34
  74. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/AvailableOperations.js.map +0 -1
  75. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/MessageExamples.js +0 -29
  76. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/MessageExamples.js.map +0 -1
  77. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/OperationHeader.js +0 -19
  78. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/__transpiled/components/OperationHeader.js.map +0 -1
  79. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/components/AvailableOperations.js +0 -20
  80. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/components/MessageExamples.js +0 -25
  81. package/lib/templates/bakedInTemplates/core-template-client-websocket-javascript/components/OperationHeader.js +0 -12
  82. package/lib/watcher.js +0 -147
@@ -1,42 +0,0 @@
1
- const Nunjucks = require('nunjucks');
2
- const nunjucksExport = module.exports;
3
-
4
- /**
5
- * Configures Nunjucks templating system
6
- * @deprecated This method is deprecated. For more details, see the release notes: https://github.com/asyncapi/generator/releases/tag/%40asyncapi%2Fgenerator%402.6.0
7
- * @private
8
- * @param {boolean} debug flag
9
- * @param {string} templateDir path
10
- */
11
- nunjucksExport.configureNunjucks = (debug, templateDir) => {
12
- const config = {};
13
- if (debug) config.dev = true;
14
-
15
- return new Nunjucks.Environment(new Nunjucks.FileSystemLoader(templateDir), config);
16
- };
17
-
18
- /**
19
- * Renders the template with nunjucks and returns a string.
20
- * @deprecated This method is deprecated. For more details, see the release notes: https://github.com/asyncapi/generator/releases/tag/%40asyncapi%2Fgenerator%402.6.0
21
- * @param {import('@asyncapi/parser').AsyncAPIDocument} asyncapiDocument
22
- * @param {string} templateString template filecontent to be rendered with nunjucks
23
- * @param {string} filePath path to the template file
24
- * @param {Object} extraTemplateData Extra data to pass to the template.
25
- * @param {Object} templateParams provided template parameters
26
- * @param {string} originalAsyncAPI
27
- * @param {} nunjucks instance
28
- * @return {Promise}
29
- */
30
- nunjucksExport.renderNunjucks = (asyncapiDocument, templateString, filePath, extraTemplateData, templateParams, originalAsyncAPI, nunjucks) => {
31
- return new Promise((resolve, reject) => {
32
- nunjucks.renderString(templateString, {
33
- asyncapi: asyncapiDocument,
34
- params: templateParams,
35
- originalAsyncAPI,
36
- ...extraTemplateData
37
- }, { path: filePath }, (err, result) => {
38
- if (err) return reject(err);
39
- resolve(result);
40
- });
41
- });
42
- };
@@ -1,34 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- require('source-map-support/register');
6
- var generatorReactSdk = require('@asyncapi/generator-react-sdk');
7
- var OperationHeader = require('./OperationHeader.js');
8
- var MessageExamples = require('./MessageExamples.js');
9
- var jsxRuntime = require('/home/runner/work/generator/generator/node_modules/react/cjs/react-jsx-runtime.production.min.js');
10
- require('@asyncapi/generator-helpers');
11
-
12
- function AvailableOperations({
13
- operations
14
- }) {
15
- if (!operations || operations.length === 0) {
16
- return null;
17
- }
18
- return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
19
- children: [/*#__PURE__*/jsxRuntime.jsx(generatorReactSdk.Text, {
20
- newLines: 2,
21
- children: "### Available Operations"
22
- }), operations.map(operation => /*#__PURE__*/jsxRuntime.jsxs(generatorReactSdk.Text, {
23
- newLines: 2,
24
- children: [/*#__PURE__*/jsxRuntime.jsx(OperationHeader, {
25
- operation: operation
26
- }), /*#__PURE__*/jsxRuntime.jsx(MessageExamples, {
27
- operation: operation
28
- })]
29
- }))]
30
- });
31
- }
32
-
33
- exports.AvailableOperations = AvailableOperations;
34
- //# sourceMappingURL=AvailableOperations.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AvailableOperations.js","sources":["../../../../../../../../packages/templates/clients/websocket/javascript/components/AvailableOperations.js"],"sourcesContent":["import { Text } from '@asyncapi/generator-react-sdk';\nimport OperationHeader from './OperationHeader';\nimport MessageExamples from './MessageExamples';\n\nexport function AvailableOperations({ operations }) {\n if (!operations || operations.length === 0) {\n return null;\n }\n return (\n <>\n <Text newLines={2}>### Available Operations</Text>\n {operations.map((operation) => (\n <Text newLines={2}>\n <OperationHeader operation={operation} />\n <MessageExamples operation={operation} />\n </Text>\n ))}\n </>\n );\n}\n"],"names":["AvailableOperations","operations","length","_jsxs","_Fragment","children","_jsx","Text","newLines","map","operation","OperationHeader","MessageExamples"],"mappings":";;;;;;;;;;;AAIO,SAASA,mBAAmBA,CAAC;AAAEC,EAAAA,UAAAA;AAAW,CAAC,EAAE;EAClD,IAAI,CAACA,UAAU,IAAIA,UAAU,CAACC,MAAM,KAAK,CAAC,EAAE;AAC1C,IAAA,OAAO,IAAI,CAAA;AACb,GAAA;EACA,oBACEC,eAAA,CAAAC,mBAAA,EAAA;IAAAC,QAAA,EAAA,cACEC,cAAA,CAACC,sBAAI,EAAA;AAACC,MAAAA,QAAQ,EAAE,CAAE;AAAAH,MAAAA,QAAA,EAAC,0BAAA;KAA8B,CAAC,EACjDJ,UAAU,CAACQ,GAAG,CAAEC,SAAS,iBACxBP,eAAA,CAACI,sBAAI,EAAA;AAACC,MAAAA,QAAQ,EAAE,CAAE;MAAAH,QAAA,EAAA,cAChBC,cAAA,CAACK,eAAe,EAAA;AAACD,QAAAA,SAAS,EAAEA,SAAAA;AAAU,OAAE,CAAC,eACzCJ,cAAA,CAACM,eAAe,EAAA;AAACF,QAAAA,SAAS,EAAEA,SAAAA;AAAU,OAAE,CAAC,CAAA;AAAA,KACrC,CACP,CAAC,CAAA;AAAA,GACF,CAAC,CAAA;AAEP;;;;"}
@@ -1,29 +0,0 @@
1
- 'use strict';
2
-
3
- require('source-map-support/register');
4
- var generatorHelpers = require('@asyncapi/generator-helpers');
5
- var generatorReactSdk = require('@asyncapi/generator-react-sdk');
6
- var jsxRuntime = require('/home/runner/work/generator/generator/node_modules/react/cjs/react-jsx-runtime.production.min.js');
7
-
8
- function MessageExamples({
9
- operation
10
- }) {
11
- const operationId = operation.id();
12
- const messageExamples = [];
13
- const messages = generatorHelpers.getOperationMessages(operation) || [];
14
- messages.forEach(message => {
15
- const examples = generatorHelpers.getMessageExamples(message) || [];
16
- examples.forEach(example => {
17
- const payload = example.payload();
18
- messageExamples.push(`\n\n**Example:**\n\`\`\`javascript\nclient.${operationId}(${JSON.stringify(payload, null, 2)});\n\`\`\``);
19
- });
20
- });
21
- return /*#__PURE__*/jsxRuntime.jsx(generatorReactSdk.Text, {
22
- children: messageExamples.map(example => /*#__PURE__*/jsxRuntime.jsx(generatorReactSdk.Text, {
23
- children: example
24
- }))
25
- });
26
- }
27
-
28
- module.exports = MessageExamples;
29
- //# sourceMappingURL=MessageExamples.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"MessageExamples.js","sources":["../../../../../../../../packages/templates/clients/websocket/javascript/components/MessageExamples.js"],"sourcesContent":["import { getMessageExamples, getOperationMessages } from '@asyncapi/generator-helpers';\nimport { Text } from '@asyncapi/generator-react-sdk';\n\nexport default function MessageExamples({operation}) {\n const operationId = operation.id();\n const messageExamples = [];\n const messages = getOperationMessages(operation) || [];\n\n messages.forEach((message) => {\n const examples = getMessageExamples(message) || [];\n examples.forEach((example) => {\n const payload = example.payload();\n messageExamples.push(`\\n\\n**Example:**\\n\\`\\`\\`javascript\\nclient.${operationId}(${JSON.stringify(payload, null, 2)});\\n\\`\\`\\``);\n });\n });\n return (\n <Text>\n {messageExamples.map(example => (\n <Text>\n {example}\n </Text>\n ))}\n </Text>\n );\n}"],"names":["MessageExamples","operation","operationId","id","messageExamples","messages","getOperationMessages","forEach","message","examples","getMessageExamples","example","payload","push","JSON","stringify","_jsx","Text","children","map"],"mappings":";;;;;;;AAGe,SAASA,eAAeA,CAAC;AAACC,EAAAA,SAAAA;AAAS,CAAC,EAAE;AACnD,EAAA,MAAMC,WAAW,GAAGD,SAAS,CAACE,EAAE,EAAE,CAAA;EAClC,MAAMC,eAAe,GAAG,EAAE,CAAA;AAC1B,EAAA,MAAMC,QAAQ,GAAGC,qCAAoB,CAACL,SAAS,CAAC,IAAI,EAAE,CAAA;AAEtDI,EAAAA,QAAQ,CAACE,OAAO,CAAEC,OAAO,IAAK;AAC5B,IAAA,MAAMC,QAAQ,GAAGC,mCAAkB,CAACF,OAAO,CAAC,IAAI,EAAE,CAAA;AAClDC,IAAAA,QAAQ,CAACF,OAAO,CAAEI,OAAO,IAAK;AAC5B,MAAA,MAAMC,OAAO,GAAGD,OAAO,CAACC,OAAO,EAAE,CAAA;AACjCR,MAAAA,eAAe,CAACS,IAAI,CAAC,CAA8CX,2CAAAA,EAAAA,WAAW,IAAIY,IAAI,CAACC,SAAS,CAACH,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,YAAY,CAAC,CAAA;AACjI,KAAC,CAAC,CAAA;AACJ,GAAC,CAAC,CAAA;EACF,oBACEI,cAAA,CAACC,sBAAI,EAAA;IAAAC,QAAA,EACFd,eAAe,CAACe,GAAG,CAACR,OAAO,iBAC1BK,cAAA,CAACC,sBAAI,EAAA;AAAAC,MAAAA,QAAA,EACFP,OAAAA;AAAO,KACJ,CACP,CAAA;AAAC,GACE,CAAC,CAAA;AAEX;;;;"}
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- require('source-map-support/register');
4
- var generatorReactSdk = require('@asyncapi/generator-react-sdk');
5
- var jsxRuntime = require('/home/runner/work/generator/generator/node_modules/react/cjs/react-jsx-runtime.production.min.js');
6
-
7
- function OperationHeader({
8
- operation
9
- }) {
10
- const operationId = operation.id();
11
- return /*#__PURE__*/jsxRuntime.jsx(generatorReactSdk.Text, {
12
- children: `#### \`${operationId}(payload)\`
13
- ${operation.hasSummary() ? operation.summary() : ''}
14
- ${operation.hasDescription() ? `\n${operation.description()}` : ''}`
15
- });
16
- }
17
-
18
- module.exports = OperationHeader;
19
- //# sourceMappingURL=OperationHeader.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"OperationHeader.js","sources":["../../../../../../../../packages/templates/clients/websocket/javascript/components/OperationHeader.js"],"sourcesContent":["import { Text } from '@asyncapi/generator-react-sdk';\n\nexport default function OperationHeader({operation}) {\n const operationId = operation.id();\n return (\n <Text>\n {`#### \\`${operationId}(payload)\\`\n${operation.hasSummary() ? operation.summary() : ''}\n${operation.hasDescription() ? `\\n${operation.description()}` : ''}`}\n </Text>\n );\n}"],"names":["OperationHeader","operation","operationId","id","_jsx","Text","children","hasSummary","summary","hasDescription","description"],"mappings":";;;;;;AAEe,SAASA,eAAeA,CAAC;AAACC,EAAAA,SAAAA;AAAS,CAAC,EAAE;AACnD,EAAA,MAAMC,WAAW,GAAGD,SAAS,CAACE,EAAE,EAAE,CAAA;EAClC,oBACEC,cAAA,CAACC,sBAAI,EAAA;IAAAC,QAAA,EACF,UAAUJ,WAAW,CAAA;AAC5B,EAAED,SAAS,CAACM,UAAU,EAAE,GAAIN,SAAS,CAACO,OAAO,EAAE,GAAG,EAAE,CAAA;AACpD,EAAEP,SAAS,CAACQ,cAAc,EAAE,GAAG,CAAA,EAAA,EAAKR,SAAS,CAACS,WAAW,EAAE,CAAA,CAAE,GAAG,EAAE,CAAA,CAAA;AAAE,GAC1D,CAAC,CAAA;AAEX;;;;"}
@@ -1,20 +0,0 @@
1
- import { Text } from '@asyncapi/generator-react-sdk';
2
- import OperationHeader from './OperationHeader';
3
- import MessageExamples from './MessageExamples';
4
-
5
- export function AvailableOperations({ operations }) {
6
- if (!operations || operations.length === 0) {
7
- return null;
8
- }
9
- return (
10
- <>
11
- <Text newLines={2}>### Available Operations</Text>
12
- {operations.map((operation) => (
13
- <Text newLines={2}>
14
- <OperationHeader operation={operation} />
15
- <MessageExamples operation={operation} />
16
- </Text>
17
- ))}
18
- </>
19
- );
20
- }
@@ -1,25 +0,0 @@
1
- import { getMessageExamples, getOperationMessages } from '@asyncapi/generator-helpers';
2
- import { Text } from '@asyncapi/generator-react-sdk';
3
-
4
- export default function MessageExamples({operation}) {
5
- const operationId = operation.id();
6
- const messageExamples = [];
7
- const messages = getOperationMessages(operation) || [];
8
-
9
- messages.forEach((message) => {
10
- const examples = getMessageExamples(message) || [];
11
- examples.forEach((example) => {
12
- const payload = example.payload();
13
- messageExamples.push(`\n\n**Example:**\n\`\`\`javascript\nclient.${operationId}(${JSON.stringify(payload, null, 2)});\n\`\`\``);
14
- });
15
- });
16
- return (
17
- <Text>
18
- {messageExamples.map(example => (
19
- <Text>
20
- {example}
21
- </Text>
22
- ))}
23
- </Text>
24
- );
25
- }
@@ -1,12 +0,0 @@
1
- import { Text } from '@asyncapi/generator-react-sdk';
2
-
3
- export default function OperationHeader({operation}) {
4
- const operationId = operation.id();
5
- return (
6
- <Text>
7
- {`#### \`${operationId}(payload)\`
8
- ${operation.hasSummary() ? operation.summary() : ''}
9
- ${operation.hasDescription() ? `\n${operation.description()}` : ''}`}
10
- </Text>
11
- );
12
- }
package/lib/watcher.js DELETED
@@ -1,147 +0,0 @@
1
- const fs = require('fs');
2
- const chokidar = require('chokidar');
3
-
4
- /**
5
- * Class to watch for change in certain file(s)
6
- */
7
- class Watcher {
8
- constructor(paths, ignorePaths) {
9
- if (Array.isArray(paths)) {
10
- this.paths = paths;
11
- } else {
12
- this.paths = [paths];
13
- }
14
- //Ensure all backwards slashes are replaced with forward slash based on the requirement from chokidar
15
- for (const pathIndex in this.paths) {
16
- const path = this.paths[pathIndex];
17
- this.paths[pathIndex] = path.replace(/[\\]/g, '/');
18
- }
19
- this.fsWait = false;
20
- this.watchers = {};
21
- this.filesChanged = {};
22
- this.ignorePaths = ignorePaths;
23
- }
24
-
25
- /**
26
- * Initiates watch on a path.
27
- * @param {*} path The path the watcher is listening on.
28
- * @param {*} changeCallback Callback to call when changed occur.
29
- * @param {*} errorCallback Calback to call when it is no longer possible to watch a file.
30
- */
31
- initiateWatchOnPath(path, changeCallback, errorCallback) {
32
- const watcher = chokidar.watch(path, {ignoreInitial: true, ignored: this.ignorePaths});
33
- watcher.on('all', (eventType, changedPath) => this.fileChanged(path, changedPath, eventType, changeCallback, errorCallback));
34
- this.watchers[path] = watcher;
35
- }
36
-
37
- /**
38
- * This method initiate the watch for change in all files
39
- * @param {*} callback called when the file(s) change
40
- */
41
- async watch(changeCallback, errorCallback) {
42
- for (const index in this.paths) {
43
- const path = this.paths[index];
44
- this.initiateWatchOnPath(path, changeCallback, errorCallback);
45
- }
46
- }
47
-
48
- /**
49
- * Should be called when a file has changed one way or another.
50
- * @param {*} listenerPath The path the watcher is listening on.
51
- * @param {*} changedPath The file/dir that was changed
52
- * @param {*} eventType What kind of change
53
- * @param {*} changeCallback Callback to call when changed occur.
54
- * @param {*} errorCallback Calback to call when it is no longer possible to watch a file.
55
- */
56
- fileChanged(listenerPath, changedPath, eventType, changeCallback, errorCallback) {
57
- try {
58
- if (fs.existsSync(listenerPath)) {
59
- const newEventType = this.convertEventType(eventType);
60
- this.filesChanged[changedPath] = { eventType: newEventType, path: changedPath};
61
- // Since multiple changes can occur at the same time, lets wait a bit before processing.
62
- if (this.fsWait) return;
63
- this.fsWait = setTimeout(async () => {
64
- await changeCallback(this.filesChanged);
65
- this.filesChanged = {};
66
- this.fsWait = false;
67
- }, 500);
68
- }
69
- } catch (e) {
70
- // File was not, find all files that are missing..
71
- const unknownPaths = this.getAllNonExistingPaths();
72
- this.closeWatchers();
73
- errorCallback(unknownPaths);
74
- }
75
- }
76
-
77
- /**
78
- * Convert the event type to a more usefull one.
79
- * @param {*} currentEventType The current event type (from chokidar)
80
- */
81
- convertEventType(currentEventType) {
82
- let newEventType = currentEventType;
83
- //Change the naming of the event type
84
- switch (newEventType) {
85
- case 'unlink':
86
- case 'unlinkDir':
87
- newEventType = 'removed';
88
- break;
89
- case 'addDir':
90
- case 'add':
91
- newEventType = 'added';
92
- break;
93
- case 'change':
94
- newEventType = 'changed';
95
- break;
96
- case 'rename':
97
- newEventType = 'renamed';
98
- break;
99
- default:
100
- newEventType = `unknown (${currentEventType})`;
101
- }
102
- return newEventType;
103
- }
104
-
105
- /**
106
- * Get all paths which no longer exists
107
- */
108
- getAllNonExistingPaths() {
109
- const unknownPaths = [];
110
- for (const index in this.paths) {
111
- const path = this.paths[index];
112
- if (!fs.existsSync(path)) {
113
- unknownPaths.push(path);
114
- }
115
- }
116
- return unknownPaths;
117
- }
118
-
119
- /**
120
- * Closes all active watchers down.
121
- */
122
- closeWatchers() {
123
- this.filesChanged = {};
124
- for (const index in this.paths) {
125
- const path = this.paths[index];
126
- this.closeWatcher(path);
127
- }
128
- }
129
-
130
- /**
131
- * Closes an active watcher down.
132
- * @param {*} path The path to close the watcher for.
133
- */
134
- closeWatcher(path) {
135
- // Ensure if called before `watch` to do nothing
136
- if (path !== null) {
137
- const watcher = this.watchers[path];
138
- if (watcher !== null) {
139
- watcher.close();
140
- this.watchers[path] = null;
141
- } else {
142
- //Watcher not found for path
143
- }
144
- }
145
- }
146
- }
147
- module.exports = Watcher;