feat: upload脚本; 创建index.html

main
Lei OT 3 months ago
parent 896fc93fa0
commit c0bdf426ef

4
.gitignore vendored

@ -1,6 +1,8 @@
node_modules node_modules
build build
dist dist
.env
.env.local
package-lock.json package-lock.json
**/*.tgz **/*.tgz

@ -10,6 +10,9 @@
### Package Structure ### Package Structure
``` ```
├─build
│ ├─cjs
│ └─esm
├─dist ├─dist
│ ├── haina-npm-0.1.0.tgz │ ├── haina-npm-0.1.0.tgz
│ ├── hooks-0.1.0.tgz │ ├── hooks-0.1.0.tgz
@ -43,11 +46,6 @@ npm run pack:all
#### FTP #### FTP
#### Github Action
```
https://github.com/hainatravel-it/hai-npm/releases/download/v0.1.0/commons-0.1.0.tgz
```
## Usage ## Usage
@ -82,7 +80,7 @@ import { commons, request } from '@haina/utils';
```sh ```sh
npm install http://xxxx/npm/utils-commons-0.1.0.tgz 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 ```js

@ -134,8 +134,11 @@ function packDirectory(dirName, dirPath) {
tgzFiles.forEach(tgzFile => { tgzFiles.forEach(tgzFile => {
const srcPath = path.join(moduleDir, tgzFile); const srcPath = path.join(moduleDir, tgzFile);
const destPath = path.join(distDir, `${dirName}-${require('../package.json').version.replace(/v/, '')}.tgz`); 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(destPath)}`);
// console.log(`Created: ${path.basename(destPath2)}`);
}); });
} }
@ -233,8 +236,11 @@ function packIndividualFile(fileName, parentDir, parentPath) {
tgzFiles.forEach(tgzFile => { tgzFiles.forEach(tgzFile => {
const srcPath = path.join(moduleDir, tgzFile); const srcPath = path.join(moduleDir, tgzFile);
const destPath = path.join(distDir, `${parentDir}-${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); // 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(destPath)}`);
// console.log(`Created: ${path.basename(destPath2)}`);
}); });
} }
@ -258,4 +264,32 @@ function copyDir(src, dest) {
// Clean up temporary directory // Clean up temporary directory
rimraf.sync(tempDir); 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 = '<ul>\n';
packedFiles.forEach(file => {
// fileListHtml += ` <li><a href="./${file}">${file}</a></li>\n`;
fileListHtml += ` <li>${file}</li>\n`;
});
fileListHtml += '</ul>\n';
const htmlContent = `<!DOCTYPE html>
<html>
<head>
<title>Package Information</title>
</head>
<body>
<h1>Latest Version: ${packageVersion.replace(/v/, '')}</h1>
<h2>Available Packages:</h2>
${fileListHtml}
</body>
</html>`;
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!'); console.log('Module packing completed!');

@ -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
Loading…
Cancel
Save