From 32b1af8b949323bea8169ebd87dd450cedcf3d56 Mon Sep 17 00:00:00 2001 From: Lei OT Date: Thu, 25 Dec 2025 15:36:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=86=E5=88=AB=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 5 +- scripts/pack-modules.js | 195 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 scripts/pack-modules.js diff --git a/package.json b/package.json index 59bb9ee..e20eee1 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "build:cjs": "babel src --out-dir build/cjs --plugins=@babel/plugin-transform-modules-commonjs", "build:esm": "babel src --out-dir build/esm", "build": "npm run clean && npm run build:cjs && npm run build:esm", - "pack": "npm run build && npm pack --pack-destination ./dist", + "pack": "npm run build && node scripts/pack-modules.js", + "pack:all": "npm run build && npm pack --pack-destination ./dist && node scripts/pack-modules.js", "test": "echo \"Error: no test specified\" && exit 1" }, "devDependencies": { @@ -27,4 +28,4 @@ "@babel/preset-env": "^7.28.5", "rimraf": "^6.1.2" } -} +} \ No newline at end of file diff --git a/scripts/pack-modules.js b/scripts/pack-modules.js new file mode 100644 index 0000000..c0a3470 --- /dev/null +++ b/scripts/pack-modules.js @@ -0,0 +1,195 @@ +const fs = require('fs'); +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) +const srcDir = path.join(__dirname, '..', 'src'); +const srcFiles = fs.readdirSync(srcDir).filter(file => + file !== 'utils' && file.endsWith('.js') +); + +// Create dist directory if it doesn't exist +const distDir = path.join(__dirname, '..', 'dist'); +if (!fs.existsSync(distDir)) { + fs.mkdirSync(distDir, { recursive: true }); +} + +// Clean up any previous temporary directories +const tempDir = path.join(__dirname, '..', 'temp_modules'); +if (fs.existsSync(tempDir)) { + rimraf.sync(tempDir); +} + +// Create temporary directory +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 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); + } + + // 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)}`); + }); + } +}); + +// Clean up temporary directory +rimraf.sync(tempDir); + +console.log('Module packing completed!'); \ No newline at end of file