diff --git a/packages/cli/src/commands/init/__tests__/template.test.js b/packages/cli/src/commands/init/__tests__/template.test.js index 3af48b748..fc41fdce0 100644 --- a/packages/cli/src/commands/init/__tests__/template.test.js +++ b/packages/cli/src/commands/init/__tests__/template.test.js @@ -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 () => { diff --git a/packages/cli/src/commands/init/template.js b/packages/cli/src/commands/init/template.js index 8e2f9dede..918a40be6 100644 --- a/packages/cli/src/commands/init/template.js +++ b/packages/cli/src/commands/init/template.js @@ -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( diff --git a/packages/cli/src/tools/__tests__/__fixtures__/binary.keystore b/packages/cli/src/tools/__tests__/__fixtures__/binary.keystore new file mode 100644 index 000000000..364e105ed Binary files /dev/null and b/packages/cli/src/tools/__tests__/__fixtures__/binary.keystore differ diff --git a/packages/cli/src/tools/__tests__/__fixtures__/extraDir/file3 b/packages/cli/src/tools/__tests__/__fixtures__/extraDir/file3 new file mode 100644 index 000000000..e69de29bb diff --git a/packages/cli/src/tools/__tests__/__fixtures__/file1.js b/packages/cli/src/tools/__tests__/__fixtures__/file1.js new file mode 100644 index 000000000..bd816eaba --- /dev/null +++ b/packages/cli/src/tools/__tests__/__fixtures__/file1.js @@ -0,0 +1 @@ +module.exports = 1; diff --git a/packages/cli/src/tools/__tests__/__fixtures__/file2.txt b/packages/cli/src/tools/__tests__/__fixtures__/file2.txt new file mode 100644 index 000000000..6c493ff74 --- /dev/null +++ b/packages/cli/src/tools/__tests__/__fixtures__/file2.txt @@ -0,0 +1 @@ +file2 diff --git a/packages/cli/src/tools/__tests__/copyFiles.test.js b/packages/cli/src/tools/__tests__/copyFiles.test.js new file mode 100644 index 000000000..477d4721d --- /dev/null +++ b/packages/cli/src/tools/__tests__/copyFiles.test.js @@ -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", + ] + `); +}); diff --git a/packages/cli/src/tools/copyFiles.js b/packages/cli/src/tools/copyFiles.js index 6f8206a05..e2e5fad66 100644 --- a/packages/cli/src/tools/copyFiles.js +++ b/packages/cli/src/tools/copyFiles.js @@ -11,12 +11,24 @@ import fs from 'fs'; import path from 'path'; import walk from './walk'; +type Options = { + exclude?: Array, +}; + /** * 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,