@awesomeness-js/utils 1.0.14 → 1.0.15
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/{build.js → build/build.js} +3 -3
- package/build/postBuild.js +11 -0
- package/index.js +34 -1
- package/package.json +6 -1
- package/src/build.js +14 -171
- package/src/getAllFiles.js +23 -22
- package/src/utils/buildExportsTree.js +19 -0
- package/src/utils/buildFileDataList.js +20 -0
- package/src/utils/extractJSDocComment.js +11 -0
- package/src/utils/generateFile.js +34 -0
- package/src/utils/generateFlatExportLines.js +18 -0
- package/src/utils/generateImportStatements.js +9 -0
- package/src/utils/generateNamedExports.js +11 -0
- package/src/utils/generateNamespaceCode.js +25 -0
- package/src/utils/generateNamespaceExportLines.js +10 -0
- package/src/utils/shouldIgnore.js +17 -0
- package/tsconfig.json +16 -0
- package/types/build.d.ts +9 -0
- package/types/combineFiles.d.ts +5 -0
- package/types/convertBytes.d.ts +8 -0
- package/types/each.d.ts +1 -0
- package/types/eachAsync.d.ts +1 -0
- package/types/getAllFiles.d.ts +1 -0
- package/types/index.d.ts +85 -0
- package/types/isUUID.d.ts +1 -0
- package/types/md5.d.ts +2 -0
- package/types/setLocalEnvs.d.ts +2 -0
- package/types/toPennies.d.ts +1 -0
- package/types/utils/buildExportsTree.d.ts +4 -0
- package/types/utils/buildFileDataList.d.ts +8 -0
- package/types/utils/extractJSDocComment.d.ts +1 -0
- package/types/utils/generateFile.d.ts +1 -0
- package/types/utils/generateFlatExportLines.d.ts +1 -0
- package/types/utils/generateImportStatements.d.ts +1 -0
- package/types/utils/generateNamedExports.d.ts +1 -0
- package/types/utils/generateNamespaceCode.d.ts +1 -0
- package/types/utils/generateNamespaceExportLines.d.ts +1 -0
- package/types/utils/shouldIgnore.d.ts +1 -0
- package/types/uuid.d.ts +2 -0
- package/src/namespaceExample/deep/deep.js +0 -10
- package/src/namespaceExample/example.js +0 -3
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import build from '
|
|
2
|
-
|
|
1
|
+
import build from '../src/build.js';
|
|
3
2
|
build({
|
|
4
3
|
src: './src',
|
|
5
4
|
dest: './index.js',
|
|
5
|
+
dts: false,
|
|
6
6
|
ignore: [
|
|
7
7
|
'ignoreMe.js',
|
|
8
8
|
'ignoreFolder/*',
|
|
9
|
-
'namespaceExample/*',
|
|
9
|
+
//'namespaceExample/*',
|
|
10
10
|
],
|
|
11
11
|
});
|
package/index.js
CHANGED
|
@@ -13,6 +13,16 @@ import _isUUID from './src/isUUID.js';
|
|
|
13
13
|
import _md5 from './src/md5.js';
|
|
14
14
|
import _setLocalEnvs from './src/setLocalEnvs.js';
|
|
15
15
|
import _toPennies from './src/toPennies.js';
|
|
16
|
+
import _utils_buildExportsTree from './src/utils/buildExportsTree.js';
|
|
17
|
+
import _utils_buildFileDataList from './src/utils/buildFileDataList.js';
|
|
18
|
+
import _utils_extractJSDocComment from './src/utils/extractJSDocComment.js';
|
|
19
|
+
import _utils_generateFile from './src/utils/generateFile.js';
|
|
20
|
+
import _utils_generateFlatExportLines from './src/utils/generateFlatExportLines.js';
|
|
21
|
+
import _utils_generateImportStatements from './src/utils/generateImportStatements.js';
|
|
22
|
+
import _utils_generateNamedExports from './src/utils/generateNamedExports.js';
|
|
23
|
+
import _utils_generateNamespaceCode from './src/utils/generateNamespaceCode.js';
|
|
24
|
+
import _utils_generateNamespaceExportLines from './src/utils/generateNamespaceExportLines.js';
|
|
25
|
+
import _utils_shouldIgnore from './src/utils/shouldIgnore.js';
|
|
16
26
|
import _uuid from './src/uuid.js';
|
|
17
27
|
|
|
18
28
|
export { _build as build };
|
|
@@ -28,7 +38,18 @@ export { _toPennies as toPennies };
|
|
|
28
38
|
export { _uuid as uuid };
|
|
29
39
|
|
|
30
40
|
export default {
|
|
31
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Builds a file from the specified source directory and writes it to the destination file.
|
|
43
|
+
*
|
|
44
|
+
* @param {Object} options - The options for the build process.
|
|
45
|
+
* @param {string} [options.src='./src'] - The source directory to build from.
|
|
46
|
+
* @param {string} [options.dest='./index.js'] - The destination file to write the built content to.
|
|
47
|
+
* @param {boolean} [options.exportRoots=true] - Whether to export root files.
|
|
48
|
+
* @param {string[]} [options.ignore=[]] - An array of file patterns to ignore.
|
|
49
|
+
* @param {boolean} [options.includeComments=true] - Whether to include comments in the generated file.
|
|
50
|
+
* @param {boolean} [options.dts=false] - Whether to generate TypeScript declaration files.
|
|
51
|
+
* @returns {Promise<boolean>} A promise that resolves to true when the build is complete.
|
|
52
|
+
*/
|
|
32
53
|
build: _build,
|
|
33
54
|
combineFiles: _combineFiles,
|
|
34
55
|
/**
|
|
@@ -47,4 +68,16 @@ export default {
|
|
|
47
68
|
setLocalEnvs: _setLocalEnvs,
|
|
48
69
|
toPennies: _toPennies,
|
|
49
70
|
uuid: _uuid,
|
|
71
|
+
utils: {
|
|
72
|
+
buildExportsTree: _utils_buildExportsTree,
|
|
73
|
+
buildFileDataList: _utils_buildFileDataList,
|
|
74
|
+
extractJSDocComment: _utils_extractJSDocComment,
|
|
75
|
+
generateFile: _utils_generateFile,
|
|
76
|
+
generateFlatExportLines: _utils_generateFlatExportLines,
|
|
77
|
+
generateImportStatements: _utils_generateImportStatements,
|
|
78
|
+
generateNamedExports: _utils_generateNamedExports,
|
|
79
|
+
generateNamespaceCode: _utils_generateNamespaceCode,
|
|
80
|
+
generateNamespaceExportLines: _utils_generateNamespaceExportLines,
|
|
81
|
+
shouldIgnore: _utils_shouldIgnore,
|
|
82
|
+
},
|
|
50
83
|
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@awesomeness-js/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "Awesomeness - Utils",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/awesomeness-js/utils.git"
|
|
8
8
|
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"prebuild": "node ./build/build.js",
|
|
11
|
+
"build": "tsc && npm run postBuild",
|
|
12
|
+
"postBuild": "node ./build/postBuild.js"
|
|
13
|
+
},
|
|
9
14
|
"type": "module",
|
|
10
15
|
"main": "index.js",
|
|
11
16
|
"author": "Scott Forte",
|
package/src/build.js
CHANGED
|
@@ -1,184 +1,27 @@
|
|
|
1
|
-
import { readdirSync, statSync, writeFileSync, readFileSync } from 'fs';
|
|
2
|
-
import { join, sep } from 'path';
|
|
3
|
-
|
|
4
|
-
function shouldIgnore(filePath, ignorePatterns) {
|
|
5
|
-
const ignore = ignorePatterns.some(pattern => {
|
|
6
|
-
const normalizedPath = filePath.replace(/\\/g, '/');
|
|
7
|
-
const normalizedPattern = pattern.replace(/\\/g, '/');
|
|
8
|
-
if (normalizedPath === normalizedPattern) return true;
|
|
9
|
-
if (normalizedPattern.endsWith('/*')) {
|
|
10
|
-
const baseDir = normalizedPattern.slice(0, -2);
|
|
11
|
-
return normalizedPath.startsWith(baseDir + '/');
|
|
12
|
-
}
|
|
13
|
-
if (normalizedPattern.endsWith('/')) {
|
|
14
|
-
return normalizedPath === normalizedPattern.slice(0, -1) ||
|
|
15
|
-
normalizedPath.startsWith(normalizedPattern);
|
|
16
|
-
}
|
|
17
|
-
return false;
|
|
18
|
-
});
|
|
19
|
-
//console.log('shouldIgnore', filePath, ignore);
|
|
20
|
-
return ignore;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function getAllFiles(base, dir, files = [], ignore = []) {
|
|
24
|
-
const directory = join(base, dir);
|
|
25
|
-
const normalizedDir = dir.replace(/\\/g, '/');
|
|
26
|
-
if (ignore.some(pattern => normalizedDir.startsWith(pattern.replace(/\/\*$/, '')))) {
|
|
27
|
-
//.log('Ignoring folder:', normalizedDir);
|
|
28
|
-
return files;
|
|
29
|
-
}
|
|
30
|
-
let sortedFiles = readdirSync(directory).sort();
|
|
31
|
-
sortedFiles.forEach(file => {
|
|
32
|
-
const fullPath = join(directory, file);
|
|
33
|
-
const relativePath = join(dir, file).replace(/\\/g, '/');
|
|
34
|
-
if (shouldIgnore(relativePath, ignore)) {
|
|
35
|
-
//console.log('Ignoring file:', relativePath);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
if (statSync(fullPath).isDirectory() && file !== '_template') {
|
|
39
|
-
getAllFiles(base, join(dir, file), files, ignore);
|
|
40
|
-
} else if (file.endsWith('.js') && !file.match(/\..*\./)) {
|
|
41
|
-
files.push(relativePath);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
return files;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function extractJSDocComment(filePath) {
|
|
48
|
-
const fileContent = readFileSync(filePath, 'utf8');
|
|
49
|
-
const match = fileContent.match(/\/\*\*([\s\S]*?)\*\//);
|
|
50
|
-
return match ? `/**${match[1]}*/` : '';
|
|
51
|
-
}
|
|
52
|
-
|
|
53
1
|
/**
|
|
54
|
-
*
|
|
2
|
+
* Builds a file from the specified source directory and writes it to the destination file.
|
|
55
3
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
4
|
+
* @param {Object} options - The options for the build process.
|
|
5
|
+
* @param {string} [options.src='./src'] - The source directory to build from.
|
|
6
|
+
* @param {string} [options.dest='./index.js'] - The destination file to write the built content to.
|
|
7
|
+
* @param {boolean} [options.exportRoots=true] - Whether to export root files.
|
|
8
|
+
* @param {string[]} [options.ignore=[]] - An array of file patterns to ignore.
|
|
9
|
+
* @param {boolean} [options.includeComments=true] - Whether to include comments in the generated file.
|
|
10
|
+
* @param {boolean} [options.dts=false] - Whether to generate TypeScript declaration files.
|
|
11
|
+
* @returns {Promise<boolean>} A promise that resolves to true when the build is complete.
|
|
58
12
|
*/
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
let importStatements = '';
|
|
62
|
-
let flatExports = [];
|
|
63
|
-
let nestedExports = {}; // Build a tree for nested exports
|
|
64
|
-
|
|
65
|
-
// Create file info objects
|
|
66
|
-
const fileDataList = allFiles.map(file => {
|
|
67
|
-
const normalizedFile = file.replace(/\\/g, '/');
|
|
68
|
-
const parts = normalizedFile.split('/');
|
|
69
|
-
const fileName = parts.pop();
|
|
70
|
-
const functionName = fileName.replace(/\.js$/, '');
|
|
71
|
-
const namespaceParts = parts;
|
|
72
|
-
const importVarName = namespaceParts.length > 0
|
|
73
|
-
? '_' + [...namespaceParts, functionName].join('_')
|
|
74
|
-
: '_' + functionName;
|
|
75
|
-
const importPath = src + '/' + normalizedFile.replace(/\.js$/, '');
|
|
76
|
-
const jsDocComment = includeComments ? extractJSDocComment(join(src, normalizedFile)) : '';
|
|
77
|
-
return { normalizedFile, parts: namespaceParts, functionName, importVarName, importPath, jsDocComment };
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// Generate import statements (without comments)
|
|
81
|
-
fileDataList.forEach(({ importVarName, importPath }) => {
|
|
82
|
-
importStatements += `import ${importVarName} from '${importPath}.js';\n`;
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// Build flat exports and nested export tree.
|
|
86
|
-
fileDataList.forEach(({ parts, functionName, importVarName, jsDocComment }) => {
|
|
87
|
-
if (parts.length === 0) {
|
|
88
|
-
flatExports.push({ functionName, importVarName, jsDocComment });
|
|
89
|
-
} else {
|
|
90
|
-
let current = nestedExports;
|
|
91
|
-
parts.forEach(part => {
|
|
92
|
-
if (!current[part]) {
|
|
93
|
-
current[part] = {};
|
|
94
|
-
}
|
|
95
|
-
current = current[part];
|
|
96
|
-
});
|
|
97
|
-
// Store the leaf export along with its JSDoc comment.
|
|
98
|
-
current[functionName] = { importVarName, jsDocComment };
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
// Generate flat export statements for default export object.
|
|
103
|
-
let flatExportLines = '';
|
|
104
|
-
flatExports.forEach(({ functionName, importVarName, jsDocComment }) => {
|
|
105
|
-
if (exportRoots) {
|
|
106
|
-
if (includeComments && jsDocComment) {
|
|
107
|
-
// Indent each comment line by 4 spaces.
|
|
108
|
-
const indentedComment = jsDocComment.split('\n').map(line => ' ' + line).join('\n');
|
|
109
|
-
flatExportLines += indentedComment + '\n';
|
|
110
|
-
}
|
|
111
|
-
flatExportLines += ` ${functionName}: ${importVarName},\n`;
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
// Recursively generate code for nested namespaces,
|
|
116
|
-
// placing JSDoc comments above each leaf key.
|
|
117
|
-
function generateNamespaceCode(nsObj, indentLevel) {
|
|
118
|
-
const indent = ' '.repeat(indentLevel);
|
|
119
|
-
let lines = ['{'];
|
|
120
|
-
for (const key in nsObj) {
|
|
121
|
-
const value = nsObj[key];
|
|
122
|
-
if (typeof value === 'object' && value.hasOwnProperty('importVarName')) {
|
|
123
|
-
// Leaf node.
|
|
124
|
-
if (includeComments && value.jsDocComment) {
|
|
125
|
-
const indentedComment = value.jsDocComment.split('\n').map(line => indent + ' ' + line).join('\n');
|
|
126
|
-
lines.push(indentedComment);
|
|
127
|
-
}
|
|
128
|
-
lines.push(`${indent} ${key}: ${value.importVarName},`);
|
|
129
|
-
} else {
|
|
130
|
-
// Nested namespace.
|
|
131
|
-
const nestedCode = generateNamespaceCode(value, indentLevel + 1);
|
|
132
|
-
lines.push(`${indent} ${key}: ${nestedCode},`);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
lines.push(indent + '}');
|
|
136
|
-
return lines.join('\n');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Generate the default export object.
|
|
140
|
-
let namespaceExportLines = '';
|
|
141
|
-
for (const ns in nestedExports) {
|
|
142
|
-
const nsCode = generateNamespaceCode(nestedExports[ns], 1);
|
|
143
|
-
namespaceExportLines += ` ${ns}: ${nsCode},\n`;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const defaultExportCode = 'export default {\n' +
|
|
147
|
-
flatExportLines +
|
|
148
|
-
namespaceExportLines +
|
|
149
|
-
'};';
|
|
150
|
-
|
|
151
|
-
// Generate individual flat export statements.
|
|
152
|
-
let flatExportStatements = '';
|
|
153
|
-
flatExports.forEach(({ functionName, importVarName }) => {
|
|
154
|
-
if (exportRoots) {
|
|
155
|
-
flatExportStatements += `export { ${importVarName} as ${functionName} };\n`;
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
const headerComment = `/**
|
|
160
|
-
* This file is auto-generated by the build script.
|
|
161
|
-
* It consolidates API functions for use in the application.
|
|
162
|
-
* Do not edit manually.
|
|
163
|
-
*/
|
|
164
|
-
`;
|
|
165
|
-
|
|
166
|
-
return headerComment +
|
|
167
|
-
importStatements + '\n' +
|
|
168
|
-
flatExportStatements +
|
|
169
|
-
'\n' +
|
|
170
|
-
defaultExportCode;
|
|
171
|
-
}
|
|
13
|
+
import { writeFileSync } from 'fs';
|
|
14
|
+
import generateFile from './utils/generateFile.js';
|
|
172
15
|
|
|
173
16
|
async function build({
|
|
174
17
|
src = './src',
|
|
175
18
|
dest = './index.js',
|
|
176
19
|
exportRoots = true,
|
|
177
20
|
ignore = [],
|
|
178
|
-
includeComments = true
|
|
21
|
+
includeComments = true,
|
|
22
|
+
dts = false
|
|
179
23
|
} = {}) {
|
|
180
|
-
|
|
181
|
-
writeFileSync(dest, indexContent);
|
|
24
|
+
writeFileSync(dest, generateFile(src, exportRoots, ignore, includeComments, dts));
|
|
182
25
|
return true;
|
|
183
26
|
}
|
|
184
27
|
|
package/src/getAllFiles.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { readdirSync, statSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import shouldIgnore from './utils/shouldIgnore.js';
|
|
3
4
|
|
|
4
|
-
function getAllFiles(
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
export default function getAllFiles(base, dir, files = [], ignore = []) {
|
|
6
|
+
const directory = join(base, dir);
|
|
7
|
+
const normalizedDir = dir.replace(/\\/g, '/');
|
|
8
|
+
if (ignore.some(pattern => normalizedDir.startsWith(pattern.replace(/\/\*$/, '')))) {
|
|
9
|
+
return files;
|
|
10
|
+
}
|
|
11
|
+
const sortedFiles = readdirSync(directory).sort();
|
|
12
|
+
sortedFiles.forEach(file => {
|
|
13
|
+
const fullPath = join(directory, file);
|
|
14
|
+
const relativePath = join(dir, file).replace(/\\/g, '/');
|
|
15
|
+
if (shouldIgnore(relativePath, ignore)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (statSync(fullPath).isDirectory()) {
|
|
19
|
+
getAllFiles(base, join(dir, file), files, ignore);
|
|
20
|
+
} else if (file.endsWith('.js') && !file.match(/\..*\./)) {
|
|
21
|
+
files.push(relativePath);
|
|
22
|
+
}
|
|
18
23
|
});
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export default getAllFiles;
|
|
24
|
+
return files;
|
|
25
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default function buildExportsTree(fileDataList) {
|
|
2
|
+
const flatExports = [];
|
|
3
|
+
const nestedExports = {};
|
|
4
|
+
fileDataList.forEach(({ parts, functionName, importVarName, jsDocComment }) => {
|
|
5
|
+
if (parts.length === 0) {
|
|
6
|
+
flatExports.push({ functionName, importVarName, jsDocComment });
|
|
7
|
+
} else {
|
|
8
|
+
let current = nestedExports;
|
|
9
|
+
parts.forEach(part => {
|
|
10
|
+
if (!current[part]) {
|
|
11
|
+
current[part] = {};
|
|
12
|
+
}
|
|
13
|
+
current = current[part];
|
|
14
|
+
});
|
|
15
|
+
current[functionName] = { importVarName, jsDocComment };
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return { flatExports, nestedExports };
|
|
19
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import getAllFiles from '../getAllFiles.js';
|
|
2
|
+
import extractJSDocComment from './extractJSDocComment.js';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
|
|
5
|
+
export default function buildFileDataList(src, ignore, includeComments) {
|
|
6
|
+
const allFiles = getAllFiles(src, '.', [], ignore);
|
|
7
|
+
return allFiles.map(file => {
|
|
8
|
+
const normalizedFile = file.replace(/\\/g, '/');
|
|
9
|
+
const parts = normalizedFile.split('/');
|
|
10
|
+
const fileName = parts.pop();
|
|
11
|
+
const functionName = fileName.replace(/\.js$/, '');
|
|
12
|
+
const namespaceParts = parts;
|
|
13
|
+
const importVarName = namespaceParts.length > 0
|
|
14
|
+
? '_' + [...namespaceParts, functionName].join('_')
|
|
15
|
+
: '_' + functionName;
|
|
16
|
+
const importPath = src + '/' + normalizedFile.replace(/\.js$/, '');
|
|
17
|
+
const jsDocComment = includeComments ? extractJSDocComment(join(src, normalizedFile)) : '';
|
|
18
|
+
return { normalizedFile, parts: namespaceParts, functionName, importVarName, importPath, jsDocComment };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
|
|
3
|
+
export default function extractJSDocComment(filePath) {
|
|
4
|
+
const fileContent = readFileSync(filePath, 'utf8');
|
|
5
|
+
const match = fileContent.match(/\/\*\*([\s\S]*?)\*\//);
|
|
6
|
+
// If the match exists and doesn't contain an unresolved template placeholder, return it.
|
|
7
|
+
if (match && match[1] && !match[1].includes('${')) {
|
|
8
|
+
return `/**${match[1]}*/`;
|
|
9
|
+
}
|
|
10
|
+
return '';
|
|
11
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import buildFileDataList from './buildFileDataList.js';
|
|
2
|
+
import buildExportsTree from './buildExportsTree.js';
|
|
3
|
+
import generateImportStatements from './generateImportStatements.js';
|
|
4
|
+
import generateFlatExportLines from './generateFlatExportLines.js';
|
|
5
|
+
import generateNamespaceExportLines from './generateNamespaceExportLines.js';
|
|
6
|
+
import generateNamedExports from './generateNamedExports.js';
|
|
7
|
+
|
|
8
|
+
export default function generateFile(src, exportRoots, ignore, includeComments, dts) {
|
|
9
|
+
const fileDataList = buildFileDataList(src, ignore, includeComments);
|
|
10
|
+
const { flatExports, nestedExports } = buildExportsTree(fileDataList);
|
|
11
|
+
const headerComment = `/**
|
|
12
|
+
* This file is auto-generated by the build script.
|
|
13
|
+
* It consolidates API ${dts ? 'type declarations' : 'functions'} for use in the application.
|
|
14
|
+
* Do not edit manually.
|
|
15
|
+
*/
|
|
16
|
+
`;
|
|
17
|
+
const importStatements = generateImportStatements(fileDataList, dts, src);
|
|
18
|
+
const flatExportLines = generateFlatExportLines(flatExports, exportRoots, includeComments, dts);
|
|
19
|
+
const namespaceExportLines = generateNamespaceExportLines(nestedExports, includeComments, dts);
|
|
20
|
+
const defaultExportCode = dts
|
|
21
|
+
? 'declare const _default: {\n' +
|
|
22
|
+
flatExportLines +
|
|
23
|
+
namespaceExportLines +
|
|
24
|
+
'};\n\nexport default _default;\n'
|
|
25
|
+
: 'export default {\n' +
|
|
26
|
+
flatExportLines +
|
|
27
|
+
namespaceExportLines +
|
|
28
|
+
'};';
|
|
29
|
+
const namedExports = generateNamedExports(flatExports, exportRoots, dts);
|
|
30
|
+
return headerComment +
|
|
31
|
+
importStatements + '\n' +
|
|
32
|
+
namedExports + '\n' +
|
|
33
|
+
defaultExportCode;
|
|
34
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default function generateFlatExportLines(flatExports, exportRoots, includeComments, dts) {
|
|
2
|
+
let lines = '';
|
|
3
|
+
if (exportRoots) {
|
|
4
|
+
flatExports.forEach(({ functionName, importVarName, jsDocComment }) => {
|
|
5
|
+
if (includeComments && jsDocComment) {
|
|
6
|
+
const indentedComment = jsDocComment
|
|
7
|
+
.split('\n')
|
|
8
|
+
.map(line => ' ' + line)
|
|
9
|
+
.join('\n');
|
|
10
|
+
lines += indentedComment + '\n';
|
|
11
|
+
}
|
|
12
|
+
lines += dts
|
|
13
|
+
? ` ${functionName}: typeof ${importVarName};\n`
|
|
14
|
+
: ` ${functionName}: ${importVarName},\n`;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return lines;
|
|
18
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export default function generateImportStatements(fileDataList, dts, src) {
|
|
2
|
+
let statements = '';
|
|
3
|
+
fileDataList.forEach(({ importVarName, importPath }) => {
|
|
4
|
+
statements += dts
|
|
5
|
+
? `import type ${importVarName} from '.${importPath.replace(src,'')}';\n`
|
|
6
|
+
: `import ${importVarName} from '${importPath}.js';\n`;
|
|
7
|
+
});
|
|
8
|
+
return statements;
|
|
9
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function generateNamedExports(flatExports, exportRoots, dts) {
|
|
2
|
+
let lines = '';
|
|
3
|
+
if (exportRoots) {
|
|
4
|
+
flatExports.forEach(({ functionName, importVarName }) => {
|
|
5
|
+
lines += dts
|
|
6
|
+
? `export declare const ${functionName}: typeof ${importVarName};\n`
|
|
7
|
+
: `export { ${importVarName} as ${functionName} };\n`;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
return lines;
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export default function generateNamespaceCode(nsObj, indentLevel, includeComments, dts) {
|
|
2
|
+
const indent = ' '.repeat(indentLevel);
|
|
3
|
+
let lines = ['{'];
|
|
4
|
+
for (const key in nsObj) {
|
|
5
|
+
const value = nsObj[key];
|
|
6
|
+
if (typeof value === 'object' && value.hasOwnProperty('importVarName')) {
|
|
7
|
+
if (includeComments && value.jsDocComment) {
|
|
8
|
+
const indentedComment = value.jsDocComment
|
|
9
|
+
.split('\n')
|
|
10
|
+
.map(line => indent + ' ' + line)
|
|
11
|
+
.join('\n');
|
|
12
|
+
lines.push(indentedComment);
|
|
13
|
+
}
|
|
14
|
+
lines.push(dts
|
|
15
|
+
? `${indent} ${key}: typeof ${value.importVarName},`
|
|
16
|
+
: `${indent} ${key}: ${value.importVarName},`
|
|
17
|
+
);
|
|
18
|
+
} else {
|
|
19
|
+
const nestedCode = generateNamespaceCode(value, indentLevel + 1, includeComments, dts);
|
|
20
|
+
lines.push(`${indent} ${key}: ${nestedCode},`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
lines.push(indent + '}');
|
|
24
|
+
return lines.join('\n');
|
|
25
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import generateNamespaceCode from './generateNamespaceCode.js';
|
|
2
|
+
|
|
3
|
+
export default function generateNamespaceExportLines(nestedExports, includeComments, dts) {
|
|
4
|
+
let lines = '';
|
|
5
|
+
for (const ns in nestedExports) {
|
|
6
|
+
const nsCode = generateNamespaceCode(nestedExports[ns], 1, includeComments, dts);
|
|
7
|
+
lines += ` ${ns}: ${nsCode},\n`;
|
|
8
|
+
}
|
|
9
|
+
return lines;
|
|
10
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default function shouldIgnore(filePath, ignorePatterns) {
|
|
2
|
+
const ignore = ignorePatterns.some(pattern => {
|
|
3
|
+
const normalizedPath = filePath.replace(/\\/g, '/');
|
|
4
|
+
const normalizedPattern = pattern.replace(/\\/g, '/');
|
|
5
|
+
if (normalizedPath === normalizedPattern) return true;
|
|
6
|
+
if (normalizedPattern.endsWith('/*')) {
|
|
7
|
+
const baseDir = normalizedPattern.slice(0, -2);
|
|
8
|
+
return normalizedPath.startsWith(baseDir + '/');
|
|
9
|
+
}
|
|
10
|
+
if (normalizedPattern.endsWith('/')) {
|
|
11
|
+
return normalizedPath === normalizedPattern.slice(0, -1) ||
|
|
12
|
+
normalizedPath.startsWith(normalizedPattern);
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
});
|
|
16
|
+
return ignore;
|
|
17
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"declaration": true, // Generates .d.ts files
|
|
4
|
+
"emitDeclarationOnly": true, // Only output .d.ts files
|
|
5
|
+
"allowJs": true, // Allows JS files
|
|
6
|
+
"outDir": "types", // Output directory for type files
|
|
7
|
+
"moduleResolution": "node",
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"esModuleInterop": true
|
|
10
|
+
},
|
|
11
|
+
"include": ["src/**/*"],
|
|
12
|
+
"exclude": [
|
|
13
|
+
"./src/ignoreFolder/**/*",
|
|
14
|
+
"./src/ignoreMe.js"
|
|
15
|
+
]
|
|
16
|
+
}
|
package/types/build.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a given number of bytes into a more readable string format with appropriate units.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} bytes - The number of bytes to convert.
|
|
5
|
+
* @param {number} [precision=2] - The number of decimal places to include in the result.
|
|
6
|
+
* @returns {string} The converted bytes in a string format with appropriate units.
|
|
7
|
+
*/
|
|
8
|
+
export default function convertBytes(bytes: number, precision?: number): string;
|
package/types/each.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function each(objectOrArray: any, callback: any): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function eachAsync(objectOrArray: any, callback: any): Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function getAllFiles(base: any, dir: any, files?: any[], ignore?: any[]): any[];
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto-generated by the build script.
|
|
3
|
+
* It consolidates API type declarations for use in the application.
|
|
4
|
+
* Do not edit manually.
|
|
5
|
+
*/
|
|
6
|
+
import type _build from './build';
|
|
7
|
+
import type _combineFiles from './combineFiles';
|
|
8
|
+
import type _convertBytes from './convertBytes';
|
|
9
|
+
import type _each from './each';
|
|
10
|
+
import type _eachAsync from './eachAsync';
|
|
11
|
+
import type _getAllFiles from './getAllFiles';
|
|
12
|
+
import type _isUUID from './isUUID';
|
|
13
|
+
import type _md5 from './md5';
|
|
14
|
+
import type _setLocalEnvs from './setLocalEnvs';
|
|
15
|
+
import type _toPennies from './toPennies';
|
|
16
|
+
import type _utils_buildExportsTree from './utils/buildExportsTree';
|
|
17
|
+
import type _utils_buildFileDataList from './utils/buildFileDataList';
|
|
18
|
+
import type _utils_extractJSDocComment from './utils/extractJSDocComment';
|
|
19
|
+
import type _utils_generateFile from './utils/generateFile';
|
|
20
|
+
import type _utils_generateFlatExportLines from './utils/generateFlatExportLines';
|
|
21
|
+
import type _utils_generateImportStatements from './utils/generateImportStatements';
|
|
22
|
+
import type _utils_generateNamedExports from './utils/generateNamedExports';
|
|
23
|
+
import type _utils_generateNamespaceCode from './utils/generateNamespaceCode';
|
|
24
|
+
import type _utils_generateNamespaceExportLines from './utils/generateNamespaceExportLines';
|
|
25
|
+
import type _utils_shouldIgnore from './utils/shouldIgnore';
|
|
26
|
+
import type _uuid from './uuid';
|
|
27
|
+
|
|
28
|
+
export declare const build: typeof _build;
|
|
29
|
+
export declare const combineFiles: typeof _combineFiles;
|
|
30
|
+
export declare const convertBytes: typeof _convertBytes;
|
|
31
|
+
export declare const each: typeof _each;
|
|
32
|
+
export declare const eachAsync: typeof _eachAsync;
|
|
33
|
+
export declare const getAllFiles: typeof _getAllFiles;
|
|
34
|
+
export declare const isUUID: typeof _isUUID;
|
|
35
|
+
export declare const md5: typeof _md5;
|
|
36
|
+
export declare const setLocalEnvs: typeof _setLocalEnvs;
|
|
37
|
+
export declare const toPennies: typeof _toPennies;
|
|
38
|
+
export declare const uuid: typeof _uuid;
|
|
39
|
+
|
|
40
|
+
declare const _default: {
|
|
41
|
+
/**
|
|
42
|
+
* Builds a file from the specified source directory and writes it to the destination file.
|
|
43
|
+
*
|
|
44
|
+
* @param {Object} options - The options for the build process.
|
|
45
|
+
* @param {string} [options.src='./src'] - The source directory to build from.
|
|
46
|
+
* @param {string} [options.dest='./index.js'] - The destination file to write the built content to.
|
|
47
|
+
* @param {boolean} [options.exportRoots=true] - Whether to export root files.
|
|
48
|
+
* @param {string[]} [options.ignore=[]] - An array of file patterns to ignore.
|
|
49
|
+
* @param {boolean} [options.includeComments=true] - Whether to include comments in the generated file.
|
|
50
|
+
* @param {boolean} [options.dts=false] - Whether to generate TypeScript declaration files.
|
|
51
|
+
* @returns {Promise<boolean>} A promise that resolves to true when the build is complete.
|
|
52
|
+
*/
|
|
53
|
+
build: typeof _build;
|
|
54
|
+
combineFiles: typeof _combineFiles;
|
|
55
|
+
/**
|
|
56
|
+
* Converts a given number of bytes into a more readable string format with appropriate units.
|
|
57
|
+
*
|
|
58
|
+
* @param {number} bytes - The number of bytes to convert.
|
|
59
|
+
* @param {number} [precision=2] - The number of decimal places to include in the result.
|
|
60
|
+
* @returns {string} The converted bytes in a string format with appropriate units.
|
|
61
|
+
*/
|
|
62
|
+
convertBytes: typeof _convertBytes;
|
|
63
|
+
each: typeof _each;
|
|
64
|
+
eachAsync: typeof _eachAsync;
|
|
65
|
+
getAllFiles: typeof _getAllFiles;
|
|
66
|
+
isUUID: typeof _isUUID;
|
|
67
|
+
md5: typeof _md5;
|
|
68
|
+
setLocalEnvs: typeof _setLocalEnvs;
|
|
69
|
+
toPennies: typeof _toPennies;
|
|
70
|
+
uuid: typeof _uuid;
|
|
71
|
+
utils: {
|
|
72
|
+
buildExportsTree: typeof _utils_buildExportsTree,
|
|
73
|
+
buildFileDataList: typeof _utils_buildFileDataList,
|
|
74
|
+
extractJSDocComment: typeof _utils_extractJSDocComment,
|
|
75
|
+
generateFile: typeof _utils_generateFile,
|
|
76
|
+
generateFlatExportLines: typeof _utils_generateFlatExportLines,
|
|
77
|
+
generateImportStatements: typeof _utils_generateImportStatements,
|
|
78
|
+
generateNamedExports: typeof _utils_generateNamedExports,
|
|
79
|
+
generateNamespaceCode: typeof _utils_generateNamespaceCode,
|
|
80
|
+
generateNamespaceExportLines: typeof _utils_generateNamespaceExportLines,
|
|
81
|
+
shouldIgnore: typeof _utils_shouldIgnore,
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export default _default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function isUUID(uuid: any): boolean;
|
package/types/md5.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function toPennies(uglyMoney: any): number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function extractJSDocComment(filePath: any): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function generateFile(src: any, exportRoots: any, ignore: any, includeComments: any, dts: any): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function generateFlatExportLines(flatExports: any, exportRoots: any, includeComments: any, dts: any): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function generateImportStatements(fileDataList: any, dts: any, src: any): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function generateNamedExports(flatExports: any, exportRoots: any, dts: any): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function generateNamespaceCode(nsObj: any, indentLevel: any, includeComments: any, dts: any): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function generateNamespaceExportLines(nestedExports: any, includeComments: any, dts: any): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function shouldIgnore(filePath: any, ignorePatterns: any): any;
|
package/types/uuid.d.ts
ADDED