├── .gitignore ├── .npmrc ├── package.json ├── README.md └── copydir.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | registry=https://registry.npmjs.org/ -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sync-copydir", 3 | "version": "1.0.4", 4 | "description": "a copy module for node", 5 | "main": "copydir.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "seawind8888", 10 | "license": "ISC", 11 | "dependencies": { 12 | "fs-extra": "^7.0.1" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sync-copydir 2 | a async copydir utils for nodejs 3 | 4 | ## Installing 5 | 6 | ``` 7 | npm i sync-copydir 8 | ``` 9 | or 10 | 11 | ``` 12 | yarn add sync-copydir 13 | ``` 14 | 15 | ## Usage 16 | 17 | copydir('path/from', 'path/to', config)` 18 | 19 | ## Example 20 | move all of files from 'aaa' folder to 'bbb' folder 21 | move 'bbb' folder to 'ccc' folder 22 | 23 | ### Usage1 24 | 25 | ```javascript 26 | const path = require('path') 27 | const copydir = require('sync-copydir') 28 | 29 | copydir(path.join(__dirname, './aaa'), path.join(__dirname, './bbb'),{ 30 | 31 | }) 32 | 33 | 34 | ``` 35 | 36 | ### Usage2 37 | 38 | ```javascript 39 | const path = require('path') 40 | const copydir = require('sync-copydir') 41 | 42 | copydir('./aaa','./bbb',{ 43 | relative: true 44 | }) 45 | 46 | 47 | ``` 48 | 49 | ## config 50 | | property | type | default | description | 51 | | -------- | ---------- | ------ |------ | 52 | | relative | bool | false | Output static directory(copy('./fileA','./fileB')) | 53 | -------------------------------------------------------------------------------- /copydir.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | 4 | 5 | function copydir(f, t, c = {}) { 6 | let _f = f, _t = t 7 | if(c.relative) { 8 | let _f = path.resolve(process.cwd(), f) 9 | let _t = path.join(process.cwd(), t) 10 | } 11 | _copydir(_f, _t); 12 | } 13 | 14 | function _copydir(f, t) { 15 | try { 16 | fs.accessSync(t); 17 | } catch (e) { 18 | fs.mkdirSync(t); 19 | } 20 | try { 21 | fs.readdirSync(f).forEach(function (p) { 22 | let _f = f + '/' + p; 23 | let _t = t + '/' + p; 24 | try { 25 | let stat = fs.statSync(_f) 26 | if (stat.isFile()) { 27 | fs.writeFileSync(_t, fs.readFileSync(_f)); 28 | } else if (stat.isDirectory()) { 29 | _copydir(_f, _t) 30 | } 31 | } catch (e) { 32 | console.log(e) 33 | } 34 | }) 35 | } catch (e) { 36 | console.log(e) 37 | } 38 | } 39 | 40 | 41 | 42 | module.exports = copydir --------------------------------------------------------------------------------