feat: 分别打包
parent
e58db1e5a8
commit
32b1af8b94
@ -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!');
|
||||||
Loading…
Reference in New Issue