1.2.0 • Published 4 months ago

nanoweb-sync v1.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

Nanoweb Sync

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.

Features

  • Provides a deploy function for file synchronization.
  • Ensures only updated files are uploaded.
  • Can be extended for different protocols.

Protocol-Specific Implementations

Nanoweb Sync FTP

  • nanoweb-sync-ftp is built on top of nanoweb-sync and uses FTP protocol for file synchronization.
  • Implements the same logic but connects to an FTP server for deployment.

Nanoweb Sync PHP

  • nanoweb-sync-php is built on top of nanoweb-sync and communicates with a PHP-based backend.
  • Uses the same deployment logic but works over HTTP with a PHP API.

Installation

Ensure you have Node.js installed, then install dependencies:

npm install

Usage

Deploy Files

To copy files from a public directory to a local directory:

import { deploy } from 'nanoweb-sync'

deploy({ publicDir: './public', local: './local', logger: console })

Extend for Protocols

To use FTP:

npm install nanoweb-sync-ftp

To use PHP:

npm install nanoweb-sync-php

Deploy public files into local directory

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

Configuration

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/

Utility Functions

Nanoweb Sync provides various utility functions to assist with file operations and synchronization.

getFileHash

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

getAllFiles

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: [] }

findFilesToUpload

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: {} }

takeCare

Logs a friendly message to indicate the process completion.

takeCare(console)
// Outputs 'Take care 🙏'

Testing

To run tests:

npm test

Contributing

Feel free to submit issues or pull requests if you find any improvements.

License

This project is licensed under the MIT License.

1.2.0

4 months ago

1.1.0

4 months ago

1.0.0

4 months ago