From 22032e45e0d7fccd6bee4a3c4c7930b04ca802cc Mon Sep 17 00:00:00 2001 From: Lei OT Date: Fri, 26 Dec 2025 09:54:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=86=E5=88=AB=E6=89=93=E5=8C=85:?= =?UTF-8?q?=20=E6=96=87=E4=BB=B6=E5=A4=B9=E5=92=8Cutils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- README.MD | 17 +- package.json | 7 +- scripts/generate-indexes.js | 72 +++++++ scripts/pack-modules.js | 369 +++++++++++++++++++-------------- src/hooks/index.js | 2 + src/hooks/useHTLanguageSets.js | 20 ++ src/index.js | 8 +- src/utils/index.js | 3 + 9 files changed, 330 insertions(+), 170 deletions(-) create mode 100644 scripts/generate-indexes.js create mode 100644 src/hooks/index.js create mode 100644 src/hooks/useHTLanguageSets.js create mode 100644 src/utils/index.js diff --git a/.gitignore b/.gitignore index 2444b14..af46769 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ node_modules -build/ +build dist package-lock.json diff --git a/README.MD b/README.MD index e720bfe..4e065fc 100644 --- a/README.MD +++ b/README.MD @@ -7,7 +7,15 @@ ## 📦 Installation ```sh +# 安装整个包 npm install http://xxxx/npm/haina-npm-0.1.0.tgz + +# 安装单个模块 +npm install http://xxxx/npm/utils-0.1.0.tgz + + # 安装Utils的子模块 +npm install http://xxxx/npm/utils-commons-0.1.0.tgz + ``` ## 🚀 Usage @@ -15,10 +23,15 @@ npm install http://xxxx/npm/haina-npm-0.1.0.tgz ```js // Import everything import { commons, request } from '@haina/npm'; - // Import only common utilities import { isEmpty } from '@haina/npm/esm/utils/commons.js'; - // Import only request utilities import { fetchJSON } from '@haina/npm/esm/utils/request.js'; + +// Import only common utilities +import { isEmpty } from '@haina/utils-commons'; + +// Import only common utilities +import { commons } from '@haina/utils'; + ``` diff --git a/package.json b/package.json index e20eee1..eb3476c 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,9 @@ ], "scripts": { "clean": "rimraf build", - "build:cjs": "babel src --out-dir build/cjs --plugins=@babel/plugin-transform-modules-commonjs", - "build:esm": "babel src --out-dir build/esm", + "generate-indexes": "node scripts/generate-indexes.js", + "build:cjs": "npm run generate-indexes && babel src --out-dir build/cjs --plugins=@babel/plugin-transform-modules-commonjs", + "build:esm": "npm run generate-indexes && babel src --out-dir build/esm", "build": "npm run clean && npm run build:cjs && npm run build:esm", "pack": "npm run build && node scripts/pack-modules.js", "pack:all": "npm run build && npm pack --pack-destination ./dist && node scripts/pack-modules.js", @@ -28,4 +29,4 @@ "@babel/preset-env": "^7.28.5", "rimraf": "^6.1.2" } -} \ No newline at end of file +} diff --git a/scripts/generate-indexes.js b/scripts/generate-indexes.js new file mode 100644 index 0000000..e0f2658 --- /dev/null +++ b/scripts/generate-indexes.js @@ -0,0 +1,72 @@ +const fs = require('fs'); +const path = require('path'); + +function generateIndexFile(dirPath) { + const files = fs.readdirSync(dirPath).filter(file => + file.endsWith('.js') && file !== 'index.js' + ); + + if (files.length === 0) return; + + let content = '\/\/ Auto Generated index.js\n'; + + // Import each file and export it as a namespace + files.forEach(file => { + const moduleName = path.basename(file, '.js'); + content += `export * as ${moduleName} from './${file}';\n`; + }); + + const indexPath = path.join(dirPath, 'index.js'); + fs.writeFileSync(indexPath, content); + console.log(`Generated index.js for: ${dirPath}`); +} + +// Generate index for each subdirectory in src +const srcDir = path.join(__dirname, '..', 'src'); +const srcItems = fs.readdirSync(srcDir); + +srcItems.forEach(item => { + const itemPath = path.join(srcDir, item); + if (fs.statSync(itemPath).isDirectory()) { + generateIndexFile(itemPath); + } +}); + +// Generate index for src directory that imports subdirectories +let srcContent = '\/\/ Auto Generated index.js\n'; + +// Add imports for each subdirectory +srcItems.forEach(item => { + const itemPath = path.join(srcDir, item); + if (fs.statSync(itemPath).isDirectory() ) { // && item !== 'utils' + // Check if the subdirectory has JS files to determine if we should export it + const subDirFiles = fs.readdirSync(itemPath).filter(file => + file.endsWith('.js') && file !== 'index.js' + ); + + if (subDirFiles.length > 0) { + srcContent += `export * as ${item} from './${item}/index.js';\n`; + } + } +}); + +// Add imports for files in the utils directory +const utilsDir = path.join(srcDir, 'utils'); +if (fs.existsSync(utilsDir)) { + generateIndexFile(utilsDir); + + // Export individual files from utils + const utilsFiles = fs.readdirSync(utilsDir).filter(file => + file.endsWith('.js') && file !== 'index.js' + ); + + utilsFiles.forEach(file => { + const moduleName = path.basename(file, '.js'); + srcContent += `export * as ${moduleName} from './utils/${file}';\n`; + }); +} + +// Write the src index.js file +const srcIndexPath = path.join(srcDir, 'index.js'); +fs.writeFileSync(srcIndexPath, srcContent); +console.log(`Generated index.js for: ${srcDir}`); diff --git a/scripts/pack-modules.js b/scripts/pack-modules.js index c0a3470..d0d7978 100644 --- a/scripts/pack-modules.js +++ b/scripts/pack-modules.js @@ -3,15 +3,9 @@ const path = require('path'); const { execSync } = require('child_process'); const rimraf = require('rimraf'); -// Get all files in src/utils directory -const utilsDir = path.join(__dirname, '..', 'src', 'utils'); -const files = fs.readdirSync(utilsDir); - -// Also get files directly in src directory (excluding utils directory) +// Get all items in src directory const srcDir = path.join(__dirname, '..', 'src'); -const srcFiles = fs.readdirSync(srcDir).filter(file => - file !== 'utils' && file.endsWith('.js') -); +const srcItems = fs.readdirSync(srcDir); // Create dist directory if it doesn't exist const distDir = path.join(__dirname, '..', 'dist'); @@ -31,165 +25,218 @@ fs.mkdirSync(tempDir, { recursive: true }); console.log('Building main package...'); execSync('npm run build', { stdio: 'inherit' }); -// Process each module file in utils -files.forEach(file => { - if (file.endsWith('.js')) { - const moduleName = path.basename(file, '.js'); - const moduleDir = path.join(tempDir, moduleName); - - // Create module-specific directory - fs.mkdirSync(moduleDir, { recursive: true }); - - // Create a module-specific package.json - const packageJson = { - name: `@haina/${moduleName}`, - version: require('../package.json').version, - description: `Haina ${moduleName} module - part of @haina/npm utility library`, - main: `cjs/utils/${file}`, - module: `esm/utils/${file}`, - type: 'module', - files: [ - "build" - ], - scripts: { - postinstall: "node -e \"console.log('This is a sub-module of @haina/npm. For full documentation, see https://github.com/your-repo/haina-npm')\"" - } - }; - - fs.writeFileSync( - path.join(moduleDir, 'package.json'), - JSON.stringify(packageJson, null, 2) - ); - - // Copy README if exists - const readmePath = path.join(__dirname, '..', 'README.MD'); - if (fs.existsSync(readmePath)) { - const readmeContent = fs.readFileSync(readmePath, 'utf8'); - // Add module-specific information to README - const moduleReadmeContent = `# @haina/${moduleName}\n\n${readmeContent}\n\n## Note\n\nThis is a standalone module from the \`@haina/npm\` package. \n\nFor more information about the full package, visit the [original repository](https://github.com/your-repo/haina-npm).\n`; - fs.writeFileSync(path.join(moduleDir, 'README.MD'), moduleReadmeContent); - } - - // Create build directories and copy built files - const moduleBuildDir = path.join(moduleDir, 'build'); - fs.mkdirSync(moduleBuildDir, { recursive: true }); - - // Copy the built ESM and CJS versions of the module - const esmSrcDir = path.join(__dirname, '..', 'build', 'esm', 'utils'); - const cjsSrcDir = path.join(__dirname, '..', 'build', 'cjs', 'utils'); - const esmDestDir = path.join(moduleBuildDir, 'esm', 'utils'); - const cjsDestDir = path.join(moduleBuildDir, 'cjs', 'utils'); - - fs.mkdirSync(esmDestDir, { recursive: true }); - fs.mkdirSync(cjsDestDir, { recursive: true }); - - // Copy the specific module file - fs.copyFileSync( - path.join(esmSrcDir, file), - path.join(esmDestDir, file) - ); - - fs.copyFileSync( - path.join(cjsSrcDir, file), - path.join(cjsDestDir, file) - ); - - // Run npm pack in the module directory - console.log(`Packing module: ${moduleName}`); - const result = execSync(`cd "${moduleDir}" && npm pack`, { encoding: 'utf8' }); - console.log(result); - - // Move the resulting .tgz file to the main dist directory - const tgzFiles = fs.readdirSync(moduleDir).filter(f => f.endsWith('.tgz')); - tgzFiles.forEach(tgzFile => { - const srcPath = path.join(moduleDir, tgzFile); - const destPath = path.join(distDir, `${moduleName}-${require('../package.json').version.replace(/v/, '')}.tgz`); - fs.renameSync(srcPath, destPath); - console.log(`Created: ${path.basename(destPath)}`); - }); +// Process each item in src directory +srcItems.forEach(item => { + const itemPath = path.join(srcDir, item); + + // If it's a directory (like utils), pack the whole directory as a module + if (fs.statSync(itemPath).isDirectory() ) { // && item !== 'utils' + // Special case: if the directory is 'utils', we'll handle individual files inside it later + packDirectory(item, itemPath); } }); -// Process each file directly in src directory -srcFiles.forEach(file => { - if (file.endsWith('.js')) { - const moduleName = path.basename(file, '.js'); - const moduleDir = path.join(tempDir, moduleName); - - // Create module-specific directory - fs.mkdirSync(moduleDir, { recursive: true }); - - // Create a module-specific package.json - const packageJson = { - name: `@haina/${moduleName}`, - version: require('../package.json').version, - description: `Haina ${moduleName} module - part of @haina/npm utility library`, - main: `cjs/${file}`, - module: `esm/${file}`, - type: 'module', - files: [ - "build" - ], - scripts: { - postinstall: "node -e \"console.log('This is a sub-module of @haina/npm. For full documentation, see https://github.com/your-repo/haina-npm')\"" - } - }; - - fs.writeFileSync( - path.join(moduleDir, 'package.json'), - JSON.stringify(packageJson, null, 2) - ); - - // Copy README if exists - const readmePath = path.join(__dirname, '..', 'README.MD'); - if (fs.existsSync(readmePath)) { - const readmeContent = fs.readFileSync(readmePath, 'utf8'); - // Add module-specific information to README - const moduleReadmeContent = `# @haina/${moduleName}\n\n${readmeContent}\n\n## Note\n\nThis is a standalone module from the \`@haina/npm\` package. \n\nFor more information about the full package, visit the [original repository](https://github.com/your-repo/haina-npm).\n`; - fs.writeFileSync(path.join(moduleDir, 'README.MD'), moduleReadmeContent); +// Process individual files in utils directory +const utilsDir = path.join(__dirname, '..', 'src', 'utils'); +if (fs.existsSync(utilsDir)) { + const utilsFiles = fs.readdirSync(utilsDir); + + utilsFiles.forEach(file => { + if (file.endsWith('.js') && file !== 'index.js') { + packIndividualFile(file, 'utils', utilsDir); + } + }); +} + +// // Process each individual file in src directory (not in subdirectories) +// const srcFiles = fs.readdirSync(srcDir).filter(file => { +// const filePath = path.join(srcDir, file); +// return fs.statSync(filePath).isFile() && file.endsWith('.js'); +// }); + +// srcFiles.forEach(file => { +// if (file.endsWith('.js')) { +// packIndividualFile(file, 'src', srcDir); +// } +// }); + +// Function to pack an entire directory as a module +function packDirectory(dirName, dirPath) { + const moduleDir = path.join(tempDir, dirName); + + // Create module-specific directory + fs.mkdirSync(moduleDir, { recursive: true }); + + // Create a module-specific package.json + const packageJson = { + name: `@haina/${dirName}`, + version: require('../package.json').version, + description: `Haina ${dirName} module - part of @haina/npm utility library`, + main: `build/esm/${dirName}/index.js`, // Assuming there's an index.js in the directory + module: `build/esm/${dirName}/index.js`, + type: 'module', + files: [ + "build" + ], + scripts: { + postinstall: "node -e \"console.log('This is a sub-module of @haina/npm. For full documentation, see https://github.com/your-repo/haina-npm')\"" } - - // Create build directories and copy built files - const moduleBuildDir = path.join(moduleDir, 'build'); - fs.mkdirSync(moduleBuildDir, { recursive: true }); - - // Copy the built ESM and CJS versions of the module - const esmSrcDir = path.join(__dirname, '..', 'build', 'esm'); - const cjsSrcDir = path.join(__dirname, '..', 'build', 'cjs'); - const esmDestDir = path.join(moduleBuildDir, 'esm'); - const cjsDestDir = path.join(moduleBuildDir, 'cjs'); - - fs.mkdirSync(esmDestDir, { recursive: true }); - fs.mkdirSync(cjsDestDir, { recursive: true }); - - // Copy the specific module file - fs.copyFileSync( - path.join(esmSrcDir, file), - path.join(esmDestDir, file) - ); - - fs.copyFileSync( - path.join(cjsSrcDir, file), - path.join(cjsDestDir, file) - ); - - // Run npm pack in the module directory - console.log(`Packing module: ${moduleName}`); - const result = execSync(`cd "${moduleDir}" && npm pack`, { encoding: 'utf8' }); - console.log(result); - - // Move the resulting .tgz file to the main dist directory - const tgzFiles = fs.readdirSync(moduleDir).filter(f => f.endsWith('.tgz')); - tgzFiles.forEach(tgzFile => { - const srcPath = path.join(moduleDir, tgzFile); - const destPath = path.join(distDir, `${moduleName}-${require('../package.json').version.replace(/v/, '')}.tgz`); - fs.renameSync(srcPath, destPath); - console.log(`Created: ${path.basename(destPath)}`); - }); + }; + + fs.writeFileSync( + path.join(moduleDir, 'package.json'), + JSON.stringify(packageJson, null, 2) + ); + + // Copy README if exists + const readmePath = path.join(__dirname, '..', 'README.MD'); + if (fs.existsSync(readmePath)) { + const readmeContent = fs.readFileSync(readmePath, 'utf8'); + // Add module-specific information to README + const moduleReadmeContent = `# @haina/${dirName}\n\n${readmeContent}\n\n## Note\n\nThis is a standalone module from the \`@haina/npm\` package. \n\nFor more information about the full package, visit the [original repository](https://github.com/your-repo/haina-npm).\n`; + fs.writeFileSync(path.join(moduleDir, 'README.MD'), moduleReadmeContent); } -}); + + // Create build directories and copy built files + const moduleBuildDir = path.join(moduleDir, 'build'); + fs.mkdirSync(moduleBuildDir, { recursive: true }); + + // Copy the built ESM and CJS versions of the directory + const esmSrcDir = path.join(__dirname, '..', 'build', 'esm', dirName); + const cjsSrcDir = path.join(__dirname, '..', 'build', 'cjs', dirName); + const esmDestDir = path.join(moduleBuildDir, 'esm', dirName); + const cjsDestDir = path.join(moduleBuildDir, 'cjs', dirName); + + // Only copy if source directories exist + if (fs.existsSync(esmSrcDir)) { + copyDir(esmSrcDir, esmDestDir); + } + if (fs.existsSync(cjsSrcDir)) { + copyDir(cjsSrcDir, cjsDestDir); + } + + // Run npm pack in the module directory + console.log(`Packing directory module: ${dirName}`); + const result = execSync(`cd "${moduleDir}" && npm pack`, { encoding: 'utf8' }); + console.log(result); + + // Move the resulting .tgz file to the main dist directory + const tgzFiles = fs.readdirSync(moduleDir).filter(f => f.endsWith('.tgz')); + tgzFiles.forEach(tgzFile => { + const srcPath = path.join(moduleDir, tgzFile); + const destPath = path.join(distDir, `${dirName}-${require('../package.json').version.replace(/v/, '')}.tgz`); + fs.renameSync(srcPath, destPath); + console.log(`Created: ${path.basename(destPath)}`); + }); +} + +// Function to pack an individual file as a module +function packIndividualFile(fileName, parentDir, parentPath) { + const moduleName = path.basename(fileName, '.js'); + const moduleDir = path.join(tempDir, moduleName); + + // Create module-specific directory + fs.mkdirSync(moduleDir, { recursive: true }); + + // Create a module-specific package.json + const packageJson = { + name: `@haina/${parentDir}-${moduleName}`, + version: require('../package.json').version, + description: `Haina ${moduleName} module - part of @haina/npm utility library`, + main: parentDir === 'src' ? `build/esm/${fileName}` : `build/esm/${parentDir}/${fileName}`, + module: parentDir === 'src' ? `build/esm/${fileName}` : `build/esm/${parentDir}/${fileName}`, + type: 'module', + files: [ + "build" + ], + scripts: { + postinstall: "node -e \"console.log('This is a sub-module of @haina/npm. For full documentation, see https://github.com/your-repo/haina-npm')\"" + } + }; + + fs.writeFileSync( + path.join(moduleDir, 'package.json'), + JSON.stringify(packageJson, null, 2) + ); + + // Copy README if exists + const readmePath = path.join(__dirname, '..', 'README.MD'); + if (fs.existsSync(readmePath)) { + const readmeContent = fs.readFileSync(readmePath, 'utf8'); + // Add module-specific information to README + const moduleReadmeContent = `# @haina/${moduleName}\n\n${readmeContent}\n\n## Note\n\nThis is a standalone module from the \`@haina/npm\` package. \n\nFor more information about the full package, visit the [original repository](https://github.com/your-repo/haina-npm).\n`; + fs.writeFileSync(path.join(moduleDir, 'README.MD'), moduleReadmeContent); + } + + // Create build directories and copy built files + const moduleBuildDir = path.join(moduleDir, 'build'); + fs.mkdirSync(moduleBuildDir, { recursive: true }); + + // Copy the built ESM and CJS versions of the module + const esmSrcDir = parentDir === 'src' + ? path.join(__dirname, '..', 'build', 'esm') + : path.join(__dirname, '..', 'build', 'esm', parentDir); + + const cjsSrcDir = parentDir === 'src' + ? path.join(__dirname, '..', 'build', 'cjs') + : path.join(__dirname, '..', 'build', 'cjs', parentDir); + + const esmDestDir = parentDir === 'src' + ? path.join(moduleBuildDir, 'esm') + : path.join(moduleBuildDir, 'esm', parentDir); + + const cjsDestDir = parentDir === 'src' + ? path.join(moduleBuildDir, 'cjs') + : path.join(moduleBuildDir, 'cjs', parentDir); + + fs.mkdirSync(esmDestDir, { recursive: true }); + fs.mkdirSync(cjsDestDir, { recursive: true }); + + // Copy the specific module file + const srcFile = fileName; + fs.copyFileSync( + path.join(esmSrcDir, srcFile), + path.join(esmDestDir, srcFile) + ); + + fs.copyFileSync( + path.join(cjsSrcDir, srcFile), + path.join(cjsDestDir, srcFile) + ); + + // Run npm pack in the module directory + console.log(`Packing individual file module: ${moduleName}`); + const result = execSync(`cd "${moduleDir}" && npm pack`, { encoding: 'utf8' }); + console.log(result); + + // Move the resulting .tgz file to the main dist directory + const tgzFiles = fs.readdirSync(moduleDir).filter(f => f.endsWith('.tgz')); + tgzFiles.forEach(tgzFile => { + const srcPath = path.join(moduleDir, tgzFile); + const destPath = path.join(distDir, `${parentDir}-${moduleName}-${require('../package.json').version.replace(/v/, '')}.tgz`); + fs.renameSync(srcPath, destPath); + console.log(`Created: ${path.basename(destPath)}`); + }); +} + +// Helper function to copy directory recursively +function copyDir(src, dest) { + fs.mkdirSync(dest, { recursive: true }); + const items = fs.readdirSync(src); + + items.forEach(item => { + const srcPath = path.join(src, item); + const destPath = path.join(dest, item); + + if (fs.statSync(srcPath).isDirectory()) { + copyDir(srcPath, destPath); + } else { + fs.copyFileSync(srcPath, destPath); + } + }); +} // Clean up temporary directory rimraf.sync(tempDir); -console.log('Module packing completed!'); \ No newline at end of file +console.log('Module packing completed!'); diff --git a/src/hooks/index.js b/src/hooks/index.js new file mode 100644 index 0000000..b30a985 --- /dev/null +++ b/src/hooks/index.js @@ -0,0 +1,2 @@ +// Auto Generated index.js +export * as useHTLanguageSets from './useHTLanguageSets.js'; diff --git a/src/hooks/useHTLanguageSets.js b/src/hooks/useHTLanguageSets.js new file mode 100644 index 0000000..cd11190 --- /dev/null +++ b/src/hooks/useHTLanguageSets.js @@ -0,0 +1,20 @@ +export const useHTLanguageSets = () => { + const newData = [ + { key: '1', value: '1', label: 'English' }, + { key: '2', value: '2', label: 'Chinese (中文)' }, + { key: '3', value: '3', label: 'Japanese (日本語)' }, + { key: '4', value: '4', label: 'German (Deutsch)' }, + { key: '5', value: '5', label: 'French (Français)' }, + { key: '6', value: '6', label: 'Spanish (Español)' }, + { key: '7', value: '7', label: 'Russian (Русский)' }, + { key: '8', value: '8', label: 'Italian (Italiano)' }, + ]; + + return newData; +}; + +export const useHTLanguageSetsMapVal = () => { + const stateSets = useHTLanguageSets(); + const stateMapVal = stateSets.reduce((r, c) => ({ ...r, [`${c.value}`]: c }), {}); + return stateMapVal; +}; diff --git a/src/index.js b/src/index.js index e7f2f2d..54c4fb8 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,5 @@ - -export * as commons from './utils/commons.js'; -export * as request from './utils/request.js'; +// Auto Generated index.js +export * as hooks from './hooks/index.js'; +export * as utils from './utils/index.js'; +export * as commons from './utils/commons.js'; +export * as request from './utils/request.js'; diff --git a/src/utils/index.js b/src/utils/index.js new file mode 100644 index 0000000..b798c78 --- /dev/null +++ b/src/utils/index.js @@ -0,0 +1,3 @@ +// Auto Generated index.js +export * as commons from './commons.js'; +export * as request from './request.js';