Skip to content
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
4 changes: 3 additions & 1 deletion packages/cli/src/commands/init/__tests__/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ test('copyTemplate', async () => {
TEMPLATE_NAME,
TEMPLATE_DIR,
);
expect(copyFiles.default).toHaveBeenCalledWith(expect.any(String), CWD);
expect(copyFiles.default).toHaveBeenCalledWith(expect.any(String), CWD, {
exclude: [expect.any(RegExp)],
});
});

test('executePostInitScript', async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/commands/init/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export async function copyTemplate(

logger.debug(`Copying template from ${templatePath}`);

await copyFiles(templatePath, process.cwd());
await copyFiles(templatePath, process.cwd(), {
exclude: [new RegExp(path.resolve(templatePath, 'node_modules'))],
});
}

export function executePostInitScript(
Expand Down
Binary file not shown.
Empty file.
1 change: 1 addition & 0 deletions packages/cli/src/tools/__tests__/__fixtures__/file1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 1;
1 change: 1 addition & 0 deletions packages/cli/src/tools/__tests__/__fixtures__/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file2
61 changes: 61 additions & 0 deletions packages/cli/src/tools/__tests__/copyFiles.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// @flow
import fs from 'fs';
import path from 'path';
import copyFiles from '../copyFiles';
import {cleanup, getTempDirectory} from '../../../../../jest/helpers';

const DIR = getTempDirectory('copyFiles-test');

beforeEach(() => {
cleanup(DIR);
fs.mkdirSync(DIR);
});

afterEach(() => {
cleanup(DIR);
});

test('copies text and binary files from source to destination', async () => {
const src = path.resolve(__dirname, './__fixtures__');
await copyFiles(src, DIR);

expect(fs.readdirSync(DIR)).toMatchInlineSnapshot(`
Array [
"binary.keystore",
"extraDir",
"file1.js",
"file2.txt",
]
`);

['binary.keystore', 'file1.js', 'file2.txt'].forEach(file => {
expect(fs.readFileSync(path.join(src, file))).toEqual(
fs.readFileSync(path.join(DIR, file)),
);
});

expect(fs.readdirSync(path.join(DIR, 'extraDir'))).toMatchInlineSnapshot(`
Array [
"file3",
]
`);

expect(fs.readFileSync(path.join(src, 'extraDir', 'file3'))).toEqual(
fs.readFileSync(path.join(DIR, 'extraDir', 'file3')),
);
});

test('copies files from source to destination excluding directory', async () => {
const src = path.resolve(__dirname, './__fixtures__');
await copyFiles(src, DIR, {
exclude: [new RegExp(path.join(src, 'extraDir'))],
});

expect(fs.readdirSync(DIR)).toMatchInlineSnapshot(`
Array [
"binary.keystore",
"file1.js",
"file2.txt",
]
`);
});
14 changes: 13 additions & 1 deletion packages/cli/src/tools/copyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ import fs from 'fs';
import path from 'path';
import walk from './walk';

type Options = {
exclude?: Array<RegExp>,
};

/**
* Copy files (binary included) recursively.
*/
async function copyFiles(srcPath: string, destPath: string) {
async function copyFiles(
srcPath: string,
destPath: string,
options?: Options = {},
) {
return Promise.all(
walk(srcPath).map(async absoluteSrcFilePath => {
const exclude = options.exclude;
if (exclude && exclude.some(p => p.test(absoluteSrcFilePath))) {
return;
}
const relativeFilePath = path.relative(srcPath, absoluteSrcFilePath);
await copyFile(
absoluteSrcFilePath,
Expand Down