@awesomeness-js/utils 1.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.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # Nothing Special
2
+
3
+ Just some utils...
4
+
5
+ example usage:
6
+ ```javascript
7
+ import { build } from '@awesomeness-js/utils';
8
+
9
+ const built = build({
10
+ src: './src',
11
+ dest: ['./', 'index.js']
12
+ });
13
+
14
+ console.log({ built });
15
+
16
+ ```
package/build.js ADDED
@@ -0,0 +1,6 @@
1
+ import build from './src/build.js';
2
+
3
+ build({
4
+ src: './src',
5
+ dest: ['./', 'index.js']
6
+ });
package/index.js ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * This file is auto-generated by the build script.
3
+ * It consolidates API functions for use in the application.
4
+ * Do not edit manually.
5
+ */
6
+ import _build from './build.js';
7
+
8
+ export default {
9
+ /**
10
+ * Generates a output file that consolidates all src functions.
11
+ *
12
+ * @param {Object} [options] - The options for generating the output file.
13
+ * @param {string} [options.src='./src'] - The source directory.
14
+ * @param {array} [options.dest=['./', 'index.js']] - The destination file.
15
+ * @returns {bool} - Returns true if the output file is generated successfully.
16
+ */
17
+ build: _build
18
+ };
package/license ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2024 Scott Forte
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@awesomeness-js/utils",
3
+ "version": "1.0.0",
4
+ "description": "Awesomeness - Utils",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/awesomeness-js/utils.git"
8
+ },
9
+ "type": "module",
10
+ "main": "index.js",
11
+ "author": "Scott Forte",
12
+ "license": "MIT",
13
+ "publishConfig": {
14
+ "access": "public"
15
+ }
16
+ }
17
+
package/src/build.js ADDED
@@ -0,0 +1,128 @@
1
+ import { readdirSync, statSync, writeFileSync, readFileSync } from 'fs';
2
+ import { join, sep } from 'path';
3
+
4
+ /**
5
+ * Generates a output file that consolidates all src functions.
6
+ *
7
+ * @param {Object} [options] - The options for generating the output file.
8
+ * @param {string} [options.src='./src'] - The source directory.
9
+ * @param {array} [options.dest=['./', 'index.js']] - The destination file.
10
+ * @returns {bool} - Returns true if the output file is generated successfully.
11
+ */
12
+
13
+ function getAllFiles(base, dir, files = []) {
14
+ const directory = join(base, dir);
15
+ let sortedFiles = readdirSync(directory).sort();
16
+ sortedFiles.forEach(file => {
17
+ const fullPath = join(directory, file);
18
+
19
+ if (
20
+ statSync(fullPath).isDirectory() &&
21
+ file !== '_template'
22
+ ) {
23
+ files = getAllFiles(base, join(dir, file), files);
24
+ } else if (file.endsWith('.js') && !file.match(/\..*\./)) { // Exclude files with two or more dots
25
+ files.push(join(dir, file));
26
+ }
27
+ });
28
+ return files;
29
+ }
30
+
31
+
32
+ function objectToString(obj, allComments, indent = 4, level = 1) {
33
+ const spaces = ' '.repeat(indent * level);
34
+ const entries = Object.entries(obj).map(([key, value]) => {
35
+
36
+ if (typeof value === 'object') {
37
+ return `${spaces}${key}: ${objectToString(value, allComments, indent, level + 1)}`;
38
+ }
39
+
40
+ if(key.startsWith('___')){
41
+ let realKey = key.replace('___', '');
42
+
43
+ // split comment based on lines and insert spaces
44
+ let comment = allComments[value];
45
+ comment = comment.split('\n').map((line) => `${spaces.replace(' ', '')} ${line}`).join('\n');
46
+
47
+ return `${comment}\n${spaces}${realKey}: ${value}`;
48
+ } else {
49
+ return `${spaces}${key}: ${value}`;
50
+ }
51
+
52
+ });
53
+
54
+ return `{\n${entries.join(',\n')}\n${' '.repeat(indent * (level - 1))}}`;
55
+
56
+ }
57
+
58
+
59
+ function extractJSDocComment(filePath) {
60
+ const fileContent = readFileSync(filePath, 'utf8');
61
+ const match = fileContent.match(/\/\*\*([\s\S]*?)\*\//);
62
+ return match ? `/**${match[1]}*/` : '';
63
+ }
64
+
65
+
66
+ function generateExports(src) {
67
+ const allFiles = [];
68
+ const fnFiles = getAllFiles(src, '.');
69
+ allFiles.push(...fnFiles);
70
+
71
+ let imports = '';
72
+ let apiObject = {};
73
+
74
+ let allComments = {};
75
+
76
+
77
+ for (const file of allFiles) {
78
+ const parts = file.split(sep).filter(p => p !== '.');
79
+ const functionName = parts.pop().replace('.js', '');
80
+ const namespace = parts.join('_');
81
+ const importPath = './' + file.replace(/\.js$/, '').replace(/\\/g, '/');
82
+ const filePath = join(src, file);
83
+
84
+ // Extract JSDoc comment, if present
85
+ let hasComment = '';
86
+ const jsDocComment = extractJSDocComment(filePath);
87
+ if(jsDocComment){
88
+ allComments[`${namespace}_${functionName}`] = jsDocComment
89
+ hasComment = '___'
90
+ }
91
+
92
+
93
+ // Generate import statement with JSDoc comment if available
94
+ imports += `import ${namespace}_${functionName} from '${importPath}.js';\n`;
95
+
96
+ // Populate the API object structure
97
+ let current = apiObject;
98
+ for (const part of parts) {
99
+ current[part] = current[part] || {};
100
+ current = current[part];
101
+ }
102
+ current[`${hasComment}${functionName}`] = `${namespace}_${functionName}`;
103
+ }
104
+
105
+ const apiContent = 'export default ' + objectToString(apiObject, allComments) + ';';
106
+
107
+ // Add a header comment
108
+ const headerComment = `/**
109
+ * This file is auto-generated by the build script.
110
+ * It consolidates API functions for use in the application.
111
+ * Do not edit manually.
112
+ */
113
+ `;
114
+
115
+ return headerComment + imports + '\n' + apiContent;
116
+ }
117
+
118
+
119
+ async function build({
120
+ src = './src',
121
+ dest = ['./', 'index.js']
122
+ } = {}) {
123
+ const indexContent = generateExports(src);
124
+ writeFileSync(join(...dest), indexContent);
125
+ return true;
126
+ }
127
+
128
+ export default build;