Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skipChildren #44

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ options:

Same as config `exclude`

+ `-sc, --skipChildren`

Skip children of an `excluded` directory. Avoids deep scanning of excluded big folders.

Same as config `skipChildren`.

+ `-do, --deleteOrphaned`

Delete orphaned or `excluded` (when using API) files/folders in target folder. `false` as default.
Expand Down Expand Up @@ -143,6 +149,7 @@ name | description | type | values | default | can be `async` ?
`config.stayHardlink` | only worked when `type: 'hardlink'`. When `stayHardlink: true`, if src file is "src/a.js", the target file "target/a.js" will be a hardlink of "src/a.js". | Boolean | - | `true` | -
`config.exclude` | Priority: `forceSync > exclude`. Filter which src files should not be synced. | RegExp / String / Array (item is RegExp / String) | - | null | -
`config.forceSync` | Priority: `forceSync > exclude`. Force sync some files even though they are `excluded`. | RegExp / String / Array (item is RegExp / String) | - | `(file) => { return false }` | No
`config.skipChildren` | skip children of an `excluded` directory. | Boolean | - | `false` | -
`config.onError` | callback function when something wrong | Function | - | `(err) => { throw new Error(err) }` | Yes when `syncDirectory.async()`

#### Some confusing params
Expand Down
8 changes: 6 additions & 2 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const commander = require('commander');
const isAbsoluteUrl = require('is-absolute');
const run = require('./index').sync;

const actor = function ({ from, to, watch, deleteOrphaned, supportSymlink, type, quiet, exclude }) {
const actor = function ({ from, to, watch, deleteOrphaned, supportSymlink, type, quiet, exclude, skipChildren }) {
const cwd = process.cwd();

if (!from) {
Expand Down Expand Up @@ -42,6 +42,7 @@ const actor = function ({ from, to, watch, deleteOrphaned, supportSymlink, type,
console.log(' - deleteOrphaned:', deleteOrphaned);
console.log(' - type: ', type);
console.log(' - exclude: ', exclude);
console.log(' - skipChildren: ', skipChildren);
console.log(' - supportSymlink: ', supportSymlink);
console.log('');
}
Expand All @@ -51,6 +52,7 @@ const actor = function ({ from, to, watch, deleteOrphaned, supportSymlink, type,
type,
deleteOrphaned,
exclude,
skipChildren
afterEachSync({ eventType, relativePath }) {
if (!quiet) {
console.log(`${eventType}: `, relativePath);
Expand All @@ -69,8 +71,9 @@ commander
.option('-c, --copy', 'Sync with type `copy`, `copy` as default')
.option('-hardlink, --hardlink', 'Sync with type `hardlink`, `copy` as default')
.option('-e, --exclude <strings...>', 'Filter which src files should not be synced')
.option('-sc, --skipChildren', 'Skip children of an excluded directory')
.action((from, to, options) => {
const { watch, deleteOrphaned, symlink, hardlink, quiet, exclude } = options;
const { watch, deleteOrphaned, symlink, hardlink, quiet, exclude, skipChildren } = options;

const params = {
from,
Expand All @@ -80,6 +83,7 @@ commander
supportSymlink: !!symlink,
quiet,
exclude,
skipChildren: !!skipChildren,
type: hardlink ? 'hardlink' : 'copy',
};

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const formatParams = (srcDir, targetDir, customOptions) => {
include: null,
exclude: null,
forceSync: null,
skipChildren: false,
onError: (err) => {
const e = new Error(err.message);
e.stack = err.stack;
Expand Down
16 changes: 8 additions & 8 deletions lib/local-syncfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ const matchUtil = require('./match-util');
const readdirEnhanced = require('readdir-enhanced');
const { ignoredSymlinkDirs } = require('./config');

const readdirSync = (dir, filter) => {
const readdirSync = (dir, skip, filter) => {
return readdirEnhanced.sync(dir, {
deep: true,
deep: skip ? filter : true,
filter,
basePath: dir
});
};

const readdirAsync = async (dir, filter) => {
const readdirAsync = async (dir, skip, filter) => {
return await readdirEnhanced.async(dir, {
deep: true,
deep: skip ? filter : true,
filter,
basePath: dir
});
Expand Down Expand Up @@ -83,8 +83,8 @@ const removeFile = filePath => {
}
};

const syncProcessor = (srcDir, targetDir, { type, exclude, forceSync, afterSync, deleteOrphaned, staySymlink, include }) => {
const srcFiles = readdirSync(srcDir, stats => {
const syncProcessor = (srcDir, targetDir, { type, exclude, forceSync, skipChildren, afterSync, deleteOrphaned, staySymlink, include }) => {
const srcFiles = readdirSync(srcDir, skipChildren, stats => {
const filePath = stats.path;
const isDirectory = isDirectoryUtil(filePath);
const relativePath = `${filePath.replace(srcDir, '')}${isDirectory ? '/' : ''}`;
Expand Down Expand Up @@ -180,8 +180,8 @@ const syncProcessor = (srcDir, targetDir, { type, exclude, forceSync, afterSync,
};

// same as syncProcessor
const asyncProcessor = async (srcDir, targetDir, { type, exclude, forceSync, afterSync, deleteOrphaned, staySymlink, include }) => {
const srcFiles = await readdirAsync(srcDir, stats => {
const asyncProcessor = async (srcDir, targetDir, { type, exclude, forceSync, skipChildren, afterSync, deleteOrphaned, staySymlink, include }) => {
const srcFiles = await readdirAsync(srcDir, skipChildren, stats => {
const filePath = stats.path;
const isDirectory = isDirectoryUtil(filePath);
const relativePath = `${filePath.replace(srcDir, '')}${isDirectory ? '/' : ''}`;
Expand Down
33 changes: 29 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,23 +462,48 @@ describe('options', function () {

const t = syncDirectory => async function () {
const exclude = sinon.spy(p =>
p.indexOf('/Dccc1/')>=0 || p === '/fccc1');
//p === '/Dccc1/' || p === '/fccc1');
p.indexOf('/Dccc1/') === 0 || p === '/fccc1');

await syncDirectory(srcDir, targetDir, {
type: 'copy',
exclude,
});

assert(exclude.calledWith('/Dccc1/'));
// assert(exclude.neverCalledWith('/Dccc1/fccc2'));
// assert(exclude.neverCalledWith('/Dccc1/Dccc2/'));
assert(exclude.calledWith('/Dccc1/fccc2'));
assert(exclude.calledWith('/Dccc1/Dccc2/'));
assertDirTree(testDir, treeAfter);
};

it('copy new/changed except exclude function (sync)', t(syncDirectory.sync));
it('copy new/changed except exclude function (async)', t(syncDirectory.async));
});

describe('skipChildren', function () {
const treeAfter = {
srcDir: treeBefore.srcDir,
targetDir: { ...tree.ab, ...tree.d },
};

const t = syncDirectory => async function () {
const exclude = sinon.spy(p =>
p === '/Dccc1/' || p === '/fccc1');

await syncDirectory(srcDir, targetDir, {
type: 'copy',
exclude,
skipChildren: true,
});

assert(exclude.calledWith('/Dccc1/'));
assert(exclude.neverCalledWith('/Dccc1/fccc2'));
assert(exclude.neverCalledWith('/Dccc1/Dccc2/'));
assertDirTree(testDir, treeAfter);
};

it('copy and skip exclude dir (sync)', t(syncDirectory.sync));
it('copy and skip exclude dir (async)', t(syncDirectory.async));
});
});

describe('forceSync', function () {
Expand Down