Coder Social home page Coder Social logo

timmikeladze / use-file-system Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 2.01 MB

A set of React hooks to interact with the File System API. Watch a directory for changes and return a map of filepaths & contents when a file is added, modified or removed.

License: MIT License

JavaScript 12.55% Shell 1.90% TypeScript 85.55%
file-system file-system-watcher file-watcher filesystem react directory-changes use-watch-directory watch-directory

use-file-system's Introduction

๐Ÿ—‚๏ธ use-file-system

A set of React hooks to interact with the File System API.

Watch a directory for changes and return a map of filepaths & contents when a file is added, modified or removed.

Check out the ๐Ÿ“– Storybook for a live example.

๐Ÿšง Under active development. Expect breaking changes until v1.0.0.

๐Ÿ“ก Install

npm install use-file-system

yarn add use-file-system

pnpm add use-file-system

๐Ÿ‘‹ Hello there! Follow me @linesofcode or visit linesofcode.dev for more cool projects like this one.

๐Ÿš€ Getting Started

import React, { useEffect } from 'react';
import { commonFilters, useFileSystem } from 'use-file-system';

export const Example = () => {
  const { onDirectorySelection, files, isBrowserSupported } = useFileSystem({
    filters: commonFilters, // filters out .gitignore paths and output paths like node_modules or dist, etc
    onFilesAdded: (newFiles, previousFiles) => {
      console.log('onFilesAdded', newFiles, previousFiles);
    },
    onFilesChanged: (changedFiles, previousFiles) => {
      console.log('onFilesChanged', changedFiles, previousFiles);
    },
    onFilesDeleted: (deletedFiles, previousFiles) => {
      console.log('onFilesDeleted', deletedFiles, previousFiles);
    },
  });

  const [renderCount, setRenderCount] = React.useState(0);

 useEffect(() => {
    setRenderCount((count) => count + 1);
  }, [files]);

  return (
    <div
      style={{
        display: 'flex',
        flexDirection: 'column',
        gap: '1rem',
      }}
    >
      {!isBrowserSupported && (
        <div
          style={{
            color: 'red',
          }}
        >
          Your browser does not support the File System Access API. Please try
          again in a different browser, such as Chrome.
        </div>
      )}
      <div>
        Select a directory on your file-system to watch for changes. The files
        will be listed below. If you have a .gitignore file in the directory,
        the files will be filtered according to the rules in that file.
      </div>
      <div>
        Component will re-render when files are added, changed, or deleted.
      </div>
      <div>
        <div>Number of renders: {renderCount}</div>
      </div>
      <div>
        <div>Number of files: {files.size}</div>
      </div>
      <div>
        <button type="button" onClick={onDirectorySelection}>
          Click here
        </button>
      </div>
      <div>
        {Array.from(files).map(([filePath]) => (
          <div key={filePath}>
            <div>{filePath}</div>
          </div>
        ))}
      </div>
    </div>
  );
};

๐Ÿงฐ Functions

โš™๏ธ gitFilter

Function Type
gitFilter FilterFn

โš™๏ธ distFilter

Function Type
distFilter FilterFn

โš™๏ธ miscFilter

Function Type
miscFilter FilterFn

โš™๏ธ processDirectory

Function Type
processDirectory (directoryHandle: FileSystemDirectoryHandle, directoryPath: string, filters: Filter[], includeFiles: Map<string, FileSystemFileHandle>, ignoreFilePaths: Set<...>) => Promise<...>

โš™๏ธ useFileSystem

Function Type
useFileSystem (props: UseFileHandlingHookProps) => { handles: Map<string, FileSystemFileHandle>; onDirectorySelection: () => Promise<void>; files: Map<...>; isProcessing: boolean; isBrowserSupported: boolean; }

๐Ÿ”ง Constants

โš™๏ธ commonFilters

Constant Type
commonFilters FilterFn[]

use-file-system's People

Contributors

timmikeladze avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

use-file-system's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Warning

These dependencies are deprecated:

Datasource Name Replacement PR?
npm @storybook/testing-library Unavailable
npm trim Unavailable

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency @testing-library/react to v16
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency eslint-plugin-unused-imports to v4
  • chore(deps): update storybook monorepo to v8 (major) (@storybook/addon-essentials, @storybook/addon-interactions, @storybook/addon-links, @storybook/blocks, @storybook/react, @storybook/react-webpack5, storybook)
  • chore(deps): update typescript-eslint monorepo to v7 (major) (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • chore(deps): update vitest monorepo to v2 (major) (@vitest/coverage-v8, vitest)
  • chore(deps): update wagoid/commitlint-github-action action to v6
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/main.yml
  • actions/checkout v3
  • actions/setup-node v3
  • wagoid/commitlint-github-action v5
.github/workflows/publish.yml
  • actions/checkout v3
  • actions/setup-node v3
npm
package.json
  • ignore ^5.2.4
  • @babel/core 7.22.11
  • @babel/preset-env 7.22.14
  • @babel/preset-react 7.22.5
  • @babel/preset-typescript 7.22.11
  • @commitlint/cli 17.7.1
  • @commitlint/config-conventional 17.7.0
  • @ryansonshine/commitizen 4.2.8
  • @ryansonshine/cz-conventional-changelog 3.3.4
  • @storybook/addon-essentials 7.4.0
  • @storybook/addon-interactions 7.4.0
  • @storybook/addon-links 7.4.0
  • @storybook/blocks 7.4.0
  • @storybook/react 7.4.0
  • @storybook/react-webpack5 7.4.0
  • @storybook/storybook-deployer 2.8.16
  • @storybook/testing-library 0.2.0
  • @testing-library/jest-dom 6.0.0
  • @testing-library/react 14.0.0
  • @types/node 20.5.9
  • @types/react 18.2.21
  • @types/react-dom 18.2.7
  • @types/react-test-renderer 18.0.0
  • @typescript-eslint/eslint-plugin 6.5.0
  • @typescript-eslint/parser 6.5.0
  • @vitejs/plugin-react 4.0.3
  • @vitest/coverage-v8 0.34.0
  • babel-loader 9.1.3
  • concurrently 8.2.1
  • eslint 8.48.0
  • eslint-config-airbnb 19.0.4
  • eslint-config-prettier 9.0.0
  • eslint-plugin-import 2.28.1
  • eslint-plugin-jsx-a11y 6.7.1
  • eslint-plugin-prefer-arrow 1.2.3
  • eslint-plugin-prettier 5.0.0
  • eslint-plugin-react 7.33.2
  • eslint-plugin-react-hooks 4.6.0
  • eslint-plugin-sort-class-members 1.18.0
  • eslint-plugin-storybook 0.6.13
  • eslint-plugin-typescript-sort-keys 3.0.0
  • eslint-plugin-unused-imports 3.0.0
  • husky 8.0.3
  • jsdom 22.1.0
  • lint-staged 14.0.1
  • prettier 3.0.3
  • prop-types 15.8.1
  • react 18.2.0
  • react-dom 18.2.0
  • react-test-renderer 18.2.0
  • release-it 16.1.5
  • storybook 7.4.0
  • tsdoc-markdown 0.0.4
  • tsup 7.2.0
  • typescript 5.2.2
  • vitest 0.34.0
  • yalc 1.0.0-pre.53
  • react >=17
  • react-dom >=17
  • node >=14.0.0
  • glob-parent >=5.1.2
  • parse-url >=8.1.0
  • semver >=7.5.2
  • trim >=0.0.3
  • trim-newlines >=3.0.1
  • yaml >=2.2.2

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.