@microlight/core 0.6.1 → 0.7.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.
@@ -2,6 +2,10 @@ import { glob } from 'glob';
2
2
  import path from 'path';
3
3
  // import { join, relative, dirname } from 'path';
4
4
  import fs from 'fs';
5
+ import { parse } from '@babel/parser';
6
+ import * as babel from '@babel/core';
7
+ import { fileURLToPath } from 'url';
8
+ import { dirname } from 'path';
5
9
  const tasksDir = path.join(process.cwd(), 'src', 'tasks');
6
10
 
7
11
  // Create tasks directory if it doesn't exist
@@ -25,7 +29,8 @@ const getTaskFiles = function () {
25
29
  const generateImportSwitch = async tasks => {
26
30
  let cases = await Promise.all(tasks.map(async file => {
27
31
  const filePath = path.resolve(tasksDir, file);
28
- const task = await import(filePath);
32
+ // const task = await import(filePath);
33
+ const task = await parseTaskFile(filePath);
29
34
  // console.log(task);
30
35
  const taskName = path.basename(file, '.task.js');
31
36
  const taskSlug = task?.default?.slug;
@@ -47,6 +52,77 @@ ${cases}
47
52
  }
48
53
  };`.trim();
49
54
  };
55
+ const parseTaskFile = filePath => {
56
+ // Read the contents of the file as UTF-8 text
57
+ const content = fs.readFileSync(filePath, 'utf-8');
58
+ try {
59
+ // Parse the file content into an Abstract Syntax Tree (AST)
60
+ // sourceType: 'module' allows ES6 import/export syntax
61
+ // plugins enable parsing of JSX and TypeScript syntax
62
+ const ast = parse(content, {
63
+ sourceType: 'module',
64
+ plugins: ['jsx', 'typescript']
65
+ });
66
+
67
+ // Search through the top-level statements (ast.program.body)
68
+ // to find the 'export default' statement
69
+ // Find the export default declaration
70
+ const exportDecl = ast.program.body.find(node => node.type === 'ExportDefaultDeclaration');
71
+
72
+ // If no export default is found, return null
73
+ if (!exportDecl) return null;
74
+
75
+ // Get the exported object, either direct or via identifier
76
+ // Get what's being exported (the declaration)
77
+ let objectAst = exportDecl.declaration;
78
+ // If the export is a variable (e.g., export default taskConfig)
79
+ if (objectAst.type === 'Identifier') {
80
+ // Find the variable declaration in the file
81
+ // (e.g., const taskConfig = { ... })
82
+ const varDecl = ast.program.body.find(node => node.type === 'VariableDeclaration' && node.declarations.some(d => d.id.name === objectAst.name));
83
+ if (varDecl) {
84
+ // Get the actual object from the variable declaration
85
+ objectAst = varDecl.declarations[0].init;
86
+ }
87
+ }
88
+
89
+ // Convert AST to string, replacing function values with null
90
+ // Convert the AST object to a string, with a custom replacer function
91
+ const cleanObject = JSON.stringify(objectAst, (key, value) => {
92
+ // If the value is a function (any type of function), replace it with null
93
+ if (value?.type === 'FunctionExpression' || value?.type === 'ArrowFunctionExpression' || value?.type === 'FunctionDeclaration') {
94
+ return null;
95
+ }
96
+ // Keep all other values as is
97
+ return value;
98
+ });
99
+
100
+ // Parse the cleaned object to get the final result
101
+ // Parse the string back to an object
102
+ // Replace AST-specific property names with prefixed versions
103
+ // This prevents these technical properties from conflicting with task properties
104
+ const taskConfig = JSON.parse(cleanObject.replace(/"type":|"start":|"end":|"loc":|"range":/g, '"_$1":'));
105
+
106
+ // Extract only the properties we care about
107
+ // Remove all the AST-specific properties using destructuring
108
+ // ...finalConfig contains all the remaining properties we want to keep
109
+ const {
110
+ _type,
111
+ _start,
112
+ _end,
113
+ _loc,
114
+ _range,
115
+ ...finalConfig
116
+ } = taskConfig;
117
+
118
+ // Return the clean task configuration object
119
+ return finalConfig;
120
+ } catch (err) {
121
+ // If any error occurs during parsing, log it and return null
122
+ console.error(`Error parsing task file ${filePath}:`, err);
123
+ return null;
124
+ }
125
+ };
50
126
  export async function prepareTasks() {
51
127
  console.log('Preparing tasks now');
52
128
  await prepareTasksIndex();
@@ -58,7 +134,7 @@ export async function prepareTasksIndex() {
58
134
  // Import all task files dynamically
59
135
  const tasks = await Promise.all(taskFiles.map(async file => {
60
136
  const filePath = path.resolve(tasksDir, file);
61
- const task = await import(filePath);
137
+ const task = await parseTaskFile(filePath);
62
138
  const taskName = path.basename(filePath);
63
139
  let folderName = file.split('/');
64
140
  folderName.pop();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microlight/core",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {
@@ -26,6 +26,7 @@
26
26
  "microlight-core": "./bin/microlight-core.js"
27
27
  },
28
28
  "dependencies": {
29
+ "@babel/parser": "^7.26.10",
29
30
  "@mui/joy": "^5.0.0-beta.51",
30
31
  "async": "^3.2.6",
31
32
  "commander": "^13.1.0",