musora-content-services 1.2.4 → 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.
- package/.prettierignore +5 -0
- package/.prettierrc +8 -0
- package/CHANGELOG.md +11 -0
- package/babel.config.cjs +1 -1
- package/jest.config.js +9 -10
- package/jsdoc.json +17 -12
- package/package.json +2 -1
- package/src/contentMetaData.js +1190 -1131
- package/src/contentTypeConfig.js +492 -387
- package/src/filterBuilder.js +163 -145
- package/src/index.d.ts +227 -237
- package/src/index.js +226 -236
- package/src/services/config.js +12 -12
- package/src/services/contentLikes.js +33 -32
- package/src/services/contentProgress.js +233 -200
- package/src/services/dataContext.js +99 -93
- package/src/services/lastUpdated.js +7 -7
- package/src/services/railcontent.js +368 -364
- package/src/services/sanity.js +975 -955
- package/src/services/userPermissions.js +12 -14
- package/test/contentLikes.test.js +89 -86
- package/test/contentProgress.test.js +229 -236
- package/test/initializeTests.js +54 -51
- package/test/lastUpdated.test.js +20 -18
- package/test/live/contentProgressLive.test.js +135 -137
- package/test/live/railcontentLive.test.js +12 -14
- package/test/localStorageMock.js +16 -16
- package/test/log.js +5 -5
- package/test/sanityQueryService.test.js +857 -821
- package/test/userPermissions.test.js +15 -15
- package/tools/generate-index.cjs +108 -111
- package/.yarnrc.yml +0 -1
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
const {fetchUserPermissions} = require(
|
|
2
|
-
const {initializeTestService} = require(
|
|
1
|
+
const { fetchUserPermissions } = require('../src/services/userPermissions')
|
|
2
|
+
const { initializeTestService } = require('./initializeTests')
|
|
3
3
|
|
|
4
4
|
describe('userPermissions', function () {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
initializeTestService()
|
|
7
|
+
})
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
test('fetchUserPermissions', async () => {
|
|
10
|
+
let result = await fetchUserPermissions() //fetch from server
|
|
11
|
+
let result2 = await fetchUserPermissions() //fetch locally
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
+
})
|
package/tools/generate-index.cjs
CHANGED
|
@@ -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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
*
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
Object.
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
content
|
|
85
|
-
|
|
86
|
-
content
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
dtsContent += '\
|
|
102
|
-
dtsContent += '\
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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!')
|
package/.yarnrc.yml
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
nodeLinker: node-modules
|