@puya/fh v1.0.5
Puya Folder Hash Utility
@puya/fh is a tool that creates a hash for a directory based on the sub-directories/files located in it.
Story
As developers we all remember the troubles of building/transfering files from a published folder in a dev machine to a folder in a production server.
No matter how sharp we track the files we change during developement so that only copy them to production server not the whole published folder, we are all ended up creating a zip file, transfer it to production server, extract it there in the root of a website and free ourselves.
The slow transfer is painful, but the result is right at target (no errors - God willing -, all old files are replaced by their new updated version).
The issue resolved, but we always asked ourselves at the end of the day, couldn't be there a tool by which we could copy/transfer only the changed files, not the whole publish folder?
@puya/fh was created to help answering exactly this need.
Description
@puya/fh has two main features:
- Generating a
jsonfile for a folder based on its content. - Comparing two json files and generate a change list.
The generated json for a given directory can be used to ...
- compare the directory with another directory
- produce a report based on the differences between two directories
- create a batch file to copy missing file/folders
- copy the changes directly.
- compress changes into a single zip file
With these capabilities at hand, the problem described in Story section can be easily resolved.
- We generate a json for our application folder in the production server.
- Bring the json to our dev machine.
- Compare the json with our local folder and create a compressed file out of the changes
- Transfer the compressed file to production.
- Extract it there at the root of our app.
We can now transfer only the changed files to the production server.
Install
npm i @puya/fh -gUsage
CLI
Base usage:
fh [command] [args] [options]command:
hash: generates json file for a folder based on its content.diff: checks the differences between two foldersapply: copies the differences between two folders to a target directory.
[args] depend on `command.
[options]:
-edor--exclude-dirs: specify a comma separated list of excluded folders that should be ignored.-efor--exclude-files: specify a comma separated list of excluded files that should be ignored.-idor--include-dirs: specify a comma separated list of included folders that should not be ignored.-ifor--include-files: specify a comma separated list of excluded files that should not be ignored.-qor-quiet: quiet mode (do not show output messages in console)-nsor--no-sort: do not sort folder/files-dbmor--debug-mode: debug mode-dpor--deep: show deep details
Other args:
-?or--help: show help-vor--version: show the tool version number
Notes:
includelists have more priority overexcludelists.- If an exclude/include list starts with comma, the list will be added to the default exclude/include list, otherwise it will replace it.
- For each file it generates an
md5hash. - For sub-directories, it generates the
md5based on the content of a sub-directory.
Using @puya/fh in CLI is described in details a little furthur.
Development
import { FolderUtil } from "@puya/fh";
const json = FolderUtil.getHash(dir);
console.log(json.hash);CLI Usage
hash
Args:
-dor--dir: path of directory for which hash should be generated (could be an absolute or relative path).-oor--output: path/name of generated json file.
example 1: create hash for current directory
fhexample 2: create hash for /publish folder in current directory
fh hash -d ./publishexample 3: create hash for /publish folder as hash.json
fh hash -d ./publish -o hash.jsonexample 4: create hash for /publish folder in current directory, do not sort file/folders
fh hash -d ./publish -nsexample 5: create hash for /publish folder in current directory, quiet mode.
fh hash -d ./publish -qdiff
Args:
-for--from: (relative/absolute) path offromorsourcefolder (the folder we are comparing to) or (relative/absolute) path of its json.-tor--to: (relative/absolute) path oftoordestinationfolder (the folder we are comparing) or (relative/absolute) path of its json.-rfor--relative-from: a path forfromfolder that will be used in generated batch orapplycommand (copying files/folders) for source file/dirs.-rtor--relative-to: a path fortofolder that will be used in generated batch orapplycommand (copying files/folders) for destination file/dirs.-kor--kind: kind of operation to be performed on detected changes.cmd: generate a windows.batfile to copy changes.bash: generate a linux.shfile to copy changes.report: report or show changes.
example 1: compare ./dev to ./prod directories, report changes
fh diff -f ./dev -t ./prodexample 2: compare dev.json to prod.json hash files, report changes
fh diff -f dev.json -t prod.jsonexample 3: compare ./dev to prod.json, report changes
fh diff -f ./dev -t prod.jsonexample 4: compare ./dev to ./prod directories, generate cmd batch file
fh diff -f ./dev -t ./prod -k cmdexample 5: compare ./dev to ./prod directories, generate bash file named ch20250512.sh
fh diff -f ./dev -t ./prod -k bash -o ch20250512.shexample 6: compare ./dev to prod.json, generate cmd batch file named ch20250512.bat relative to ./publish dir
fh diff -f ./dev -t prod.json -rt ./publish -k cmd -o ch20250512.batapply
Args:
arguments are the same as those used in diff command.
example 1: compare ./dev to ./prod directories, apply changes from './dev' into './prod'.
fh apply -f ./dev -t ./prodexample 2: compare ./dev to prod.json, copy into -rt
fh apply -f ./dev -t ./prod -rt ./publishexample 3: create zip archive out of changes
fh apply -f ./dev -t ./prod -cIncluded/Excluded files/folders
There are a default list of files and folders that @puya/fh ignores them by default.
default excluded folders
- node_modules
- .git
- tests
- tests
- packages
- wwwroot
- coverage
- .vscode
- .idea
- build
- publish
- .vs
Excluded files:
- thumbs.db
- package.json
- packages.config
- .env
- .gitignore
- .ds_store
- *.log
- *.test.js
- *.spec.js
- *.bak
- *.tmp
- sync.bat
- sync.sh
These lists can be customized through cli arguments.