npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@definetool/sync-files

v2.1.10

Published

基于 MD5 的文件同步工具,把源目录的文件同步到目标目录中。

Downloads

3

Readme

@definetool/sync-files

基于 MD5 的文件同步工具,把源目录的文件和目录结构同步到目标目录中。

用 MD5 对文件内容进行计算,MD5 值相同的文件被认为是同一个文件,尽管它们的文件名可能不同。
同步后,目标目录的目录结构和文件列表就保持跟源目录完全一致。

同步过程中: 对源目录完全是无伤害的,即不会对源目录有任何的删除、修改、新增文件/目录的操作,仅仅把它当成一个数据源进行读取。
对目标目录可能存在以下操作:

  • 删除多余的文件或目录;当 target 目录有某个文件或目录,而 source 目录没有此文件或目录时。
  • 复制新增的文件或目录;当 source 目录有某个文件或目录,而 target 目录没有此文件或目录时。
  • 重命名已有的文件;当 source 目录有某个文件,target 目录也存在此文件,但对应的目录位置、文件名不同时。

同步过程中,会对源目录和目标目录的文件进行 MD5 计算,为了方便下次可以直接使用本次的计算结果,会在当前的源目录和目标目录创建一个隐藏的子目录 .sync-files 以保存 MD5 的计算结果。

每次计算文件的 MD5 时,都会先从目录 .sync-files 目录读取,如果存在对应的记录,则直接使用,不再重复计算当前文件的 MD5。
如果不存在,则计算当前文件的 MD5 值,并且会把结果缓存起来。 用户可以直接删除 .sync-files 目录,则会重新计算 MD5。

示例

使用默认设置


const { parse, sync, } = require('@definetool/sync-files');

//仅对指定目录进行解析,生成 MD5 元数据库。
parse('/Users/micty/Pictures/Canon');


//进行完整的同步流程,包括解析、同步、清理、校验。
sync({
    //要进行同步的来源目录。
    //不会对此目录有任何的删除、修改等操作,仅仅把它当成数据源进行读取。
    source: '/Users/micty/Pictures/Canon',

    //要进行同步的目标目录。
    //进行同步时,可能会对此目录进行以下操作:
    //一,删除多余的文件;当目标目录有某个文件,而源目录没有此文件时。
    //二,复制新增的文件;当源目录有某个文件,而目标目录没有此文件时。
    //三,重命名已有的文件;当源目录有某个文件,目标目录也存在此文件,但对应的目录位置、文件名不同时。
    target: '/Volumes/3/Canon',
});

自定义方式

const { Task, } = require('@definetool/sync-files');


let config = {
    //解析过程中提取文件 MD5 等元数据后要保存到目录名,建议指定为 `.sync-files/`。
    //如果不指定,则不保存元数据。
    //为了使用下次的解析更快,建议开启缓存。
    //此目录是在 source 和 target 对应的目录中。
    cache: '.sync-files/',

    //要进行同步的来源目录。
    //同步过程中不会对此目录有任何的删除、修改等操作,仅仅把它当成数据源进行读取。
    //如果指定了 cache 字段,则会在此目录中生成 cache 目录。
    source: '/Users/micty/Pictures/Canon',

    //要进行同步的目标目录。
    //进行同步时,可能会对此目录进行以下操作:
    //一,删除多余的文件;当目标目录有某个文件,而源目录没有此文件时。
    //二,复制新增的文件;当源目录有某个文件,而目标目录没有此文件时。
    //三,重命名已有的文件;当源目录有某个文件,目标目录也存在此文件,但对应的目录位置、文件名不同时。
    //如果指定了 cache 字段,则会在此目录中生成 cache 目录。
    target: '/Volumes/3/Canon',

    patterns: [
        // '**/*.*',        //匹配 `文件名.后缀名`,但不匹配 `.后缀名` 和 `文件名`。
        '**/*',             //匹配 `文件名.后缀名`、`文件名`、`文件名.`,但不匹配 `.后缀名`。 即匹配所有含有文件名的文件。
        '**/.*',            //匹配 `.后缀名`,即匹配只含有后缀名的文件。
        '!**/.DS_Store',    //排除 `.DS_Store` 文件。 
        '!**/Thumbs.db',    //排除 `Thumbs.db` 文件。
    ],


    output: {
        //会话过程中产生的日志等临时文件的存放目录。 
        //建议每次都使用一个不同的目录,以方便多次运行后进行查找和对比。
        //如果不指定,则不输出临时文件。
        dir: `./output/2021-12-21/`,  

        //会话过程中产生的日志的文件名称。 
        //如果指定,则输出到此文件中;否则仅在控制台输出。
        //此文件名是在 home 目录中。
        console: 'console.log',

        //同步文件与目录过程,target 目录中要被删除的文件与目录在删除前,
        //可以先复制到此目录作为备份,以避免误删。
        deletes: 'deletes/',

        //调用 task.parse() 方法时要输出的结果路径。
        parse: `parse.{type}.json`,

        //调用 task.compare() 方法时要输出的结果路径。
        compare: `compare.{type}.json`,

        //调用 task.sync() 方法时要输出的结果路径。
        sync: `sync.{type}.json`,

    },
  
};

let task = new Task(config);
let { source, target, } = task.parse();
let compare = task.compare({ source, target, });
let sync = task.sync({ source, target, compare, });

// return { source, target, compare, sync, };