musora-content-services 1.2.5 → 1.3.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.
@@ -1,19 +1,19 @@
1
- const {fetchUserPermissions} = require("../src/services/userPermissions");
2
- const {initializeTestService} = require("./initializeTests");
1
+ const { fetchUserPermissions } = require('../src/services/userPermissions')
2
+ const { initializeTestService } = require('./initializeTests')
3
3
 
4
4
  describe('userPermissions', function () {
5
- beforeEach(() => {
6
- initializeTestService();
7
- });
5
+ beforeEach(() => {
6
+ initializeTestService()
7
+ })
8
8
 
9
- test('fetchUserPermissions', async () => {
10
- let result = await fetchUserPermissions(); //fetch from server
11
- let result2 = await fetchUserPermissions(); //fetch locally
9
+ test('fetchUserPermissions', async () => {
10
+ let result = await fetchUserPermissions() //fetch from server
11
+ let result2 = await fetchUserPermissions() //fetch locally
12
12
 
13
- //This breaks when running tests in parallel
14
- //expect(railContentModule.fetchUserPermissionsData).toHaveBeenCalledTimes(1);
15
- expect(result.permissions).toStrictEqual([78,91,92]);
16
- expect(result.isAdmin).toStrictEqual(false);
17
- expect(result).toBe(result2);
18
- });
19
- });
13
+ //This breaks when running tests in parallel
14
+ //expect(railContentModule.fetchUserPermissionsData).toHaveBeenCalledTimes(1);
15
+ expect(result.permissions).toStrictEqual([78, 91, 92])
16
+ expect(result.isAdmin).toStrictEqual(false)
17
+ expect(result).toBe(result2)
18
+ })
19
+ })
@@ -1,111 +1,108 @@
1
- /**
2
- * Tool script to generate index.js and index.d.ts files based on exports from files in our services directory.
3
- * This scans all files from the src/services directory and retrieves any functions that are declared with
4
- * `export function` or `export async function`. It also retrieves ES module exports through `module.exports`. *
5
- */
6
-
7
- const fs = require('fs');
8
- const path = require('path');
9
- const fileExports = {};
10
-
11
- /**
12
- * Helper function to extract function names from ES module and CommonJS exports
13
- *
14
- * @param {string} filePath
15
- * @returns {string[]}
16
- */
17
- function extractExportedFunctions(filePath) {
18
- const fileContent = fs.readFileSync(filePath, 'utf-8');
19
-
20
- const exportFunctionRegex = /export\s+(async\s+)?function\s+(\w+)/g;
21
- const exportVariableRegex = /export\s+(let|const|var)\s+(globalConfig)\s+/g;
22
- const moduleExportsRegex = /module\.exports\s*=\s*{\s*([\s\S]+?)\s*};/g;
23
-
24
- let matches = [...fileContent.matchAll(exportFunctionRegex)].map(match => match[2]);
25
-
26
- // Match `globalConfig` variable
27
- const variableMatches = [...fileContent.matchAll(exportVariableRegex)].map(match => match[2]);
28
- matches = matches.concat(variableMatches);
29
-
30
-
31
- const moduleExportsMatch = moduleExportsRegex.exec(fileContent);
32
- if (moduleExportsMatch) {
33
- const exportsList = moduleExportsMatch[1]
34
- .split(',')
35
- .map(exp => exp.split(':')[0].trim());
36
- matches = matches.concat(exportsList);
37
- }
38
-
39
- const excludedFunctions = getExclusionList(fileContent);
40
- matches = matches.filter(fn => !excludedFunctions.includes(fn));
41
-
42
- return matches.sort();
43
- }
44
-
45
- /**
46
- * Helper function to find the list of exclusions from the file's exports
47
- *
48
- * @param {string} fileContent
49
- * @returns {string[]}
50
- */
51
- function getExclusionList(fileContent) {
52
- const excludeRegex = /const\s+excludeFromGeneratedIndex\s*=\s*\[(.*?)\];/;
53
- const excludeMatch = fileContent.match(excludeRegex);
54
- let excludedFunctions = [];
55
- if (excludeMatch) {
56
- excludedFunctions = excludeMatch[1]
57
- .split(',')
58
- .map(name => name.trim().replace(/['"`]/g, ''));
59
- }
60
- return excludedFunctions;
61
- }
62
-
63
- // get all files in the services directory
64
- const servicesDir = path.join(__dirname, '../src/services');
65
- const files = fs.readdirSync(servicesDir);
66
-
67
- files.forEach((file) => {
68
- const filePath = path.join(servicesDir, file);
69
- const functionNames = extractExportedFunctions(filePath);
70
-
71
- if (functionNames.length > 0) {
72
- fileExports[file] = functionNames;
73
- }
74
- });
75
-
76
- // populate the index.js content string with the import/export of all functions
77
- let content = '/*** This file was generated automatically. To recreate, please run `npm run build-index`. ***/\n';
78
-
79
- Object.entries(fileExports).forEach(([file, functionNames]) => {
80
- content += `\nimport {\n\t${functionNames.join(',\n\t')}\n} from './services/${file}';\n`;
81
- });
82
-
83
- const allFunctionNames = Object.values(fileExports).flat().sort();
84
- content += '\nexport {\n';
85
- content += `\t${allFunctionNames.join(',\n\t')},\n`;
86
- content += '};\n';
87
-
88
- // write the generated content to index.js
89
- const outputPath = path.join(__dirname, '../src/index.js');
90
- fs.writeFileSync(outputPath, content);
91
-
92
- console.log('index.js generated successfully!');
93
-
94
- // populate the index.d.ts content string with the import and module export of all functions
95
- let dtsContent = '/*** This file was generated automatically. To recreate, please run `npm run build-index`. ***/\n';
96
-
97
- Object.entries(fileExports).forEach(([file, functionNames]) => {
98
- dtsContent += `\nimport {\n\t${functionNames.join(',\n\t')}\n} from './services/${file}';\n`;
99
- });
100
-
101
- dtsContent += '\ndeclare module \'musora-content-services\' {\n';
102
- dtsContent += '\texport {\n';
103
- dtsContent += `\t\t${allFunctionNames.join(',\n\t\t')},\n`;
104
- dtsContent += '\t}\n';
105
- dtsContent += '}\n';
106
-
107
- // write the generated content to index.d.ts
108
- const outputDtsPath = path.join(__dirname, '../src/index.d.ts');
109
- fs.writeFileSync(outputDtsPath, dtsContent);
110
-
111
- console.log('index.d.ts generated successfully!');
1
+ /**
2
+ * Tool script to generate index.js and index.d.ts files based on exports from files in our services directory.
3
+ * This scans all files from the src/services directory and retrieves any functions that are declared with
4
+ * `export function` or `export async function`. It also retrieves ES module exports through `module.exports`. *
5
+ */
6
+
7
+ const fs = require('fs')
8
+ const path = require('path')
9
+ const fileExports = {}
10
+
11
+ /**
12
+ * Helper function to extract function names from ES module and CommonJS exports
13
+ *
14
+ * @param {string} filePath
15
+ * @returns {string[]}
16
+ */
17
+ function extractExportedFunctions(filePath) {
18
+ const fileContent = fs.readFileSync(filePath, 'utf-8')
19
+
20
+ const exportFunctionRegex = /export\s+(async\s+)?function\s+(\w+)/g
21
+ const exportVariableRegex = /export\s+(let|const|var)\s+(globalConfig)\s+/g
22
+ const moduleExportsRegex = /module\.exports\s*=\s*{\s*([\s\S]+?)\s*};/g
23
+
24
+ let matches = [...fileContent.matchAll(exportFunctionRegex)].map((match) => match[2])
25
+
26
+ // Match `globalConfig` variable
27
+ const variableMatches = [...fileContent.matchAll(exportVariableRegex)].map((match) => match[2])
28
+ matches = matches.concat(variableMatches)
29
+
30
+ const moduleExportsMatch = moduleExportsRegex.exec(fileContent)
31
+ if (moduleExportsMatch) {
32
+ const exportsList = moduleExportsMatch[1].split(',').map((exp) => exp.split(':')[0].trim())
33
+ matches = matches.concat(exportsList)
34
+ }
35
+
36
+ const excludedFunctions = getExclusionList(fileContent)
37
+ matches = matches.filter((fn) => !excludedFunctions.includes(fn))
38
+
39
+ return matches.sort()
40
+ }
41
+
42
+ /**
43
+ * Helper function to find the list of exclusions from the file's exports
44
+ *
45
+ * @param {string} fileContent
46
+ * @returns {string[]}
47
+ */
48
+ function getExclusionList(fileContent) {
49
+ const excludeRegex = /const\s+excludeFromGeneratedIndex\s*=\s*\[(.*?)\];/
50
+ const excludeMatch = fileContent.match(excludeRegex)
51
+ let excludedFunctions = []
52
+ if (excludeMatch) {
53
+ excludedFunctions = excludeMatch[1].split(',').map((name) => name.trim().replace(/['"`]/g, ''))
54
+ }
55
+ return excludedFunctions
56
+ }
57
+
58
+ // get all files in the services directory
59
+ const servicesDir = path.join(__dirname, '../src/services')
60
+ const files = fs.readdirSync(servicesDir)
61
+
62
+ files.forEach((file) => {
63
+ const filePath = path.join(servicesDir, file)
64
+ const functionNames = extractExportedFunctions(filePath)
65
+
66
+ if (functionNames.length > 0) {
67
+ fileExports[file] = functionNames
68
+ }
69
+ })
70
+
71
+ // populate the index.js content string with the import/export of all functions
72
+ let content =
73
+ '/*** This file was generated automatically. To recreate, please run `npm run build-index`. ***/\n'
74
+
75
+ Object.entries(fileExports).forEach(([file, functionNames]) => {
76
+ content += `\nimport {\n\t${functionNames.join(',\n\t')}\n} from './services/${file}';\n`
77
+ })
78
+
79
+ const allFunctionNames = Object.values(fileExports).flat().sort()
80
+ content += '\nexport {\n'
81
+ content += `\t${allFunctionNames.join(',\n\t')},\n`
82
+ content += '};\n'
83
+
84
+ // write the generated content to index.js
85
+ const outputPath = path.join(__dirname, '../src/index.js')
86
+ fs.writeFileSync(outputPath, content)
87
+
88
+ console.log('index.js generated successfully!')
89
+
90
+ // populate the index.d.ts content string with the import and module export of all functions
91
+ let dtsContent =
92
+ '/*** This file was generated automatically. To recreate, please run `npm run build-index`. ***/\n'
93
+
94
+ Object.entries(fileExports).forEach(([file, functionNames]) => {
95
+ dtsContent += `\nimport {\n\t${functionNames.join(',\n\t')}\n} from './services/${file}';\n`
96
+ })
97
+
98
+ dtsContent += "\ndeclare module 'musora-content-services' {\n"
99
+ dtsContent += '\texport {\n'
100
+ dtsContent += `\t\t${allFunctionNames.join(',\n\t\t')},\n`
101
+ dtsContent += '\t}\n'
102
+ dtsContent += '}\n'
103
+
104
+ // write the generated content to index.d.ts
105
+ const outputDtsPath = path.join(__dirname, '../src/index.d.ts')
106
+ fs.writeFileSync(outputDtsPath, dtsContent)
107
+
108
+ console.log('index.d.ts generated successfully!')