diff --git a/.gitignore b/.gitignore index af46769..0cba35f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ node_modules build dist +.env +.env.local package-lock.json -**/*.tgz +**/*.tgz \ No newline at end of file diff --git a/README.MD b/README.MD index b08a910..800b457 100644 --- a/README.MD +++ b/README.MD @@ -10,6 +10,9 @@ ### Package Structure ``` +├─build +│ ├─cjs +│ └─esm ├─dist │ ├── haina-npm-0.1.0.tgz │ ├── hooks-0.1.0.tgz @@ -43,11 +46,6 @@ npm run pack:all #### FTP -#### Github Action - -``` -https://github.com/hainatravel-it/hai-npm/releases/download/v0.1.0/commons-0.1.0.tgz -``` ## Usage @@ -82,7 +80,7 @@ import { commons, request } from '@haina/utils'; ```sh npm install http://xxxx/npm/utils-commons-0.1.0.tgz -npm install https://package.mycht.cn/npmjs/commons-0.1.0.tgz +npm install https://research.hainatravel.com/npm/utils-commons-0.1.1.tgz ``` ```js diff --git a/scripts/pack-modules.js b/scripts/pack-modules.js index 02103b5..52e7bad 100644 --- a/scripts/pack-modules.js +++ b/scripts/pack-modules.js @@ -134,8 +134,11 @@ function packDirectory(dirName, dirPath) { 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); + // const destPath2 = path.join(distDir, `${dirName}-latest.tgz`); + fs.copyFileSync(srcPath, destPath); + // fs.copyFileSync(srcPath, destPath2); console.log(`Created: ${path.basename(destPath)}`); + // console.log(`Created: ${path.basename(destPath2)}`); }); } @@ -233,8 +236,11 @@ function packIndividualFile(fileName, parentDir, parentPath) { 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); + // const destPath2 = path.join(distDir, `${parentDir}-${moduleName}-latest.tgz`); + fs.copyFileSync(srcPath, destPath); + // fs.copyFileSync(srcPath, destPath2); console.log(`Created: ${path.basename(destPath)}`); + // console.log(`Created: ${path.basename(destPath2)}`); }); } @@ -258,4 +264,32 @@ function copyDir(src, dest) { // Clean up temporary directory rimraf.sync(tempDir); +// Generate index.html with version information and list of packed files +const packageVersion = require('../package.json').version; +const indexPath = path.join(distDir, 'index.html'); + +// Get list of all packed files in dist directory +const packedFiles = fs.readdirSync(distDir).filter(file => file.endsWith(packageVersion+'.tgz')); + +let fileListHtml = '\n'; + +const htmlContent = ` + + + Package Information + + +

Latest Version: ${packageVersion.replace(/v/, '')}

+

Available Packages:

+ ${fileListHtml} + +`; +fs.writeFileSync(indexPath, htmlContent); +console.log(`Generated index.html with version ${packageVersion.replace(/v/, '')} and ${packedFiles.length} packed files listed`); + console.log('Module packing completed!'); diff --git a/scripts/winscp-upload.bat b/scripts/winscp-upload.bat new file mode 100644 index 0000000..666eee4 --- /dev/null +++ b/scripts/winscp-upload.bat @@ -0,0 +1,104 @@ +@echo off +setlocal enabledelayedexpansion + +REM Load environment variables from .env file if it exists +if exist "..\.env" ( + for /f "tokens=*" %%a in ('type "..\.env" ^| findstr /v "^#"') do ( + for /f "tokens=1,2 delims==" %%b in ("%%a") do ( + set "%%b=%%c" + ) + ) +) + +REM Use environment variables with defaults +if not defined FTP_HOST ( + set FTP_HOST=myserver.com + echo Warning: FTP_HOST not set, using default value +) +if not defined FTP_USER ( + set FTP_USER=myuser + echo Warning: FTP_USER not set, using default value +) +if not defined FTP_PASSP ( + set FTP_PASSP=mypass + echo Warning: FTP_PASSP not set, using default value +) +if not defined FTP_PORT set FTP_PORT=21 +if not defined FTP_REMOTE_PATH set FTP_REMOTE_PATH=/packages + +REM Read package version from package.json +if not exist "..\package.json" ( + echo Error: package.json not found + pause + exit /b 1 +) + +REM Use PowerShell to extract the version from package.json +for /f "usebackq tokens=*" %%i in (`powershell -command "(Get-Content -Raw ..\package.json | ConvertFrom-Json).version"`) do ( + set PACKAGE_VERSION=%%i +) + +echo Detected package version: !PACKAGE_VERSION! + +REM Check if WinSCP is installed and accessible +where winscp.com >nul 2>&1 +if errorlevel 1 ( + echo Error: WinSCP is not found in PATH. Please install WinSCP and add it to your PATH. + echo Download: https://winscp.net/ + pause + exit /b 1 +) + +REM Get the dist directory path +set DIST_DIR=%~dp0..\dist + +REM Check if dist directory exists +if not exist "%DIST_DIR%" ( + echo Error: Dist directory does not exist. Please run build/pack command first. + pause + exit /b 1 +) + +REM Check if there are any .tgz files matching the current version to upload +set COUNT=0 +for %%f in ("%DIST_DIR%\*!PACKAGE_VERSION!*.tgz") do set /a COUNT+=1 +for %%f in ("%DIST_DIR%\*-latest.tgz") do set /a COUNT+=1 +if !COUNT! EQU 0 ( + echo No .tgz files found in dist directory matching version !PACKAGE_VERSION! And named `latest` + pause + exit /b 0 +) + +echo Found !COUNT! package files to upload for version !PACKAGE_VERSION! + +@REM exit /b 0 + +REM Create a temporary script file with actual values +set TEMP_SCRIPT=%TEMP%\winscp_script_%RANDOM%.txt +echo option batch abort > "%TEMP_SCRIPT%" +echo option confirm off >> "%TEMP_SCRIPT%" +echo open ftp://!FTP_USER!:!FTP_PASSP!@!FTP_HOST!:!FTP_PORT! >> "%TEMP_SCRIPT%" +echo cd !FTP_REMOTE_PATH! >> "%TEMP_SCRIPT%" +echo put "%DIST_DIR%\*!PACKAGE_VERSION!*.tgz" >> "%TEMP_SCRIPT%" +echo put "%DIST_DIR%\index.html" >> "%TEMP_SCRIPT%" +echo close >> "%TEMP_SCRIPT%" +echo exit >> "%TEMP_SCRIPT%" + +REM Run WinSCP with the temporary script file +echo Uploading packages via WinSCP... +winscp.com /script="%TEMP_SCRIPT%" + +set RESULT=%ERRORLEVEL% + +REM Clean up the temporary script file +del "%TEMP_SCRIPT%" + +if !RESULT! neq 0 ( + echo Upload failed! + pause + exit /b 1 +) + +echo Upload completed successfully! +@REM pause +exit /b 0