@ -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,22 +25,55 @@ 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 ) ;
// 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 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/ ${ moduleName } ` ,
name : ` @haina/ ${ dir Name} ` ,
version : require ( '../package.json' ) . version ,
description : ` Haina ${ moduleName } module - part of @haina/npm utility library ` ,
main : ` cjs/utils/ ${ file } ` ,
module : ` esm/utils/ ${ file } ` ,
description : ` Haina ${ dir Name} 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"
@ -66,7 +93,7 @@ files.forEach(file => {
if ( fs . existsSync ( readmePath ) ) {
const readmeContent = fs . readFileSync ( readmePath , 'utf8' ) ;
// Add module-specific information to README
const moduleReadmeContent = ` # @haina/ ${ module Name} \n \n ${ readmeContent } \n \n ## Note \n \n This is a standalone module from the \` @haina/npm \` package. \n \n For more information about the full package, visit the [original repository](https://github.com/your-repo/haina-npm). \n ` ;
const moduleReadmeContent = ` # @haina/ ${ dir Name} \n \n ${ readmeContent } \n \n ## Note \n \n This is a standalone module from the \` @haina/npm \` package. \n \n For 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 ) ;
}
@ -74,28 +101,22 @@ files.forEach(file => {
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 )
) ;
// 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 ) ;
fs . copyFileSync (
path . join ( cjsSrcDir , file ) ,
path . join ( cjsDestDir , file )
) ;
// 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 module: ${ moduleName } ` ) ;
console . log ( ` Packing directory module: ${ dir Name} ` ) ;
const result = execSync ( ` cd " ${ moduleDir } " && npm pack ` , { encoding : 'utf8' } ) ;
console . log ( result ) ;
@ -103,17 +124,15 @@ files.forEach(file => {
const tgzFiles = fs . readdirSync ( moduleDir ) . filter ( f => f . endsWith ( '.tgz' ) ) ;
tgzFiles . forEach ( tgzFile => {
const srcPath = path . join ( moduleDir , tgzFile ) ;
const destPath = path . join ( distDir , ` ${ module Name} - ${ require ( '../package.json' ) . version . replace ( /v/ , '' ) } .tgz ` ) ;
const destPath = path . join ( distDir , ` ${ dir Name} - ${ 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' ) ;
// 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
@ -121,11 +140,11 @@ srcFiles.forEach(file => {
// Create a module-specific package.json
const packageJson = {
name : ` @haina/ ${ moduleName } ` ,
name : ` @haina/ ${ parentDir } - ${ moduleName } ` ,
version : require ( '../package.json' ) . version ,
description : ` Haina ${ moduleName } module - part of @haina/npm utility library ` ,
main : ` cjs/ ${ fil e} ` ,
module : ` esm/${ fil e} ` ,
main : parentDir === 'src' ? ` build/esm/ ${ fileName } ` : ` build/esm/ ${ parentDir } / ${ fileNam e} ` ,
module : parentDir === 'src' ? ` build/ esm/${ fil eName} ` : ` build/esm/ ${ parentDir } / ${ fileNam e} ` ,
type : 'module' ,
files : [
"build"
@ -154,27 +173,39 @@ srcFiles.forEach(file => {
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' ) ;
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 , file ) ,
path . join ( esmDestDir , file )
path . join ( esmSrcDir , srcF ile) ,
path . join ( esmDestDir , srcF ile)
) ;
fs . copyFileSync (
path . join ( cjsSrcDir , f ile) ,
path . join ( cjsDestDir , f ile)
path . join ( cjsSrcDir , srcF ile) ,
path . join ( cjsDestDir , srcF ile)
) ;
// Run npm pack in the module directory
console . log ( ` Packing module: ${ moduleName } ` ) ;
console . log ( ` Packing individual file module: ${ moduleName } ` ) ;
const result = execSync ( ` cd " ${ moduleDir } " && npm pack ` , { encoding : 'utf8' } ) ;
console . log ( result ) ;
@ -182,12 +213,28 @@ srcFiles.forEach(file => {
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 ` ) ;
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 ) ;