Nanoweb Sync is a basic abstract package that provides core functionalities for synchronizing and deploying files. It serves as the foundation for protocol-specific implementations like nanoweb-sync-ftp
and nanoweb-sync-php
.
- Provides a
deploy
function for file synchronization. - Ensures only updated files are uploaded.
- Can be extended for different protocols.
-
nanoweb-sync-ftp
is built on top ofnanoweb-sync
and uses FTP protocol for file synchronization. - Implements the same logic but connects to an FTP server for deployment.
-
nanoweb-sync-php
is built on top ofnanoweb-sync
and communicates with a PHP-based backend. - Uses the same deployment logic but works over HTTP with a PHP API.
Ensure you have Node.js installed, then install dependencies:
npm install
To copy files from a public
directory to a local
directory:
import { deploy } from 'nanoweb-sync'
deploy({ publicDir: './public', local: './local', logger: console })
To use FTP:
npm install nanoweb-sync-ftp
To use PHP:
npm install nanoweb-sync-php
Should copy files from publicDir to local.
import process from 'node:process'
import { resolve } from 'node:path'
const PUBLIC_DIR = resolve(process.cwd(), 'public')
const LOCAL_DIR = resolve(process.cwd(), 'dist')
const logger = console
await deploy({ publicDir: PUBLIC_DIR, local: LOCAL_DIR, logger })
Should log a warning if publicDir is not defined.
import process from 'node:process'
import { resolve } from 'node:path'
const LOCAL_DIR = resolve(process.cwd(), 'dist')
const logger = console
await deploy({ local: LOCAL_DIR, logger })
// Public directory is not defined
Proposed configuration settings for the nanoweb-sync-* drivers.
Check env.example.
SYNC_HOST=ftp.nanoweb.org
SYNC_USER=nanoweb
SYNC_PASS=*********
SYNC_PORT=21
SYNC_SECURE=false
SYNC_MAX_CONNECTIONS=1
SYNC_ACCEPT_UNAUTHORIZED=true
SYNC_STRATEGY=sync
SYNC_LOCAL=dist
SYNC_REMOTE=/
SYNC_PUBLIC=public
SYNC_CACHE=.nanoweb-sync.json
SYNC_IGNORE=^v2/
Nanoweb Sync provides various utility functions to assist with file operations and synchronization.
This function generates an MD5 hash for a given file.
const hash = await getFileHash(join(PUBLIC_DIR, 'file.txt'))
console.log(hash);
// Outputs a 32-character hex string => 9473fdd0d880a43c21b7778d34872157
Retrieves a list of files and directories from a given path.
const result = await getAllFiles(PUBLIC_DIR)
console.log(result)
// Outputs { files: [{ path, size, hash }], directories: [] }
Identifies files that need to be uploaded based on changes.
@todo Add tests for ignore
cmd.addOption('ignore', String, true, 'Ignored paths regular expressions', ['^v2/', '.+/modules/.+'], true);
const mockResult = await findFilesToUpload({ recent: './recent.json', local: LOCAL_DIR, remote: '/remote', logger })
console.log(mockResult)
// Outputs { uploadQueue: Map(1), uploadDirs: [], uploadCount: 1, uploadSize: 12, skipCount: 0, skipSize: 0, skipList: {} }
Logs a friendly message to indicate the process completion.
takeCare(console)
// Outputs 'Take care 🙏'
To run tests:
npm test
Feel free to submit issues or pull requests if you find any improvements.
This project is licensed under the MIT License.