From e335d65f14728ac32cea34ad3e08a26349793ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 11 Jul 2019 10:11:46 +0200 Subject: [PATCH] fix: don't copy node_modules in templates --- .../commands/init/__tests__/template.test.js | 4 +- packages/cli/src/commands/init/template.js | 4 +- .../__tests__/__fixtures__/binary.keystore | Bin 0 -> 2257 bytes .../__tests__/__fixtures__/extraDir/file3 | 0 .../src/tools/__tests__/__fixtures__/file1.js | 1 + .../tools/__tests__/__fixtures__/file2.txt | 1 + .../cli/src/tools/__tests__/copyFiles.test.js | 61 ++++++++++++++++++ packages/cli/src/tools/copyFiles.js | 14 +++- 8 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 packages/cli/src/tools/__tests__/__fixtures__/binary.keystore create mode 100644 packages/cli/src/tools/__tests__/__fixtures__/extraDir/file3 create mode 100644 packages/cli/src/tools/__tests__/__fixtures__/file1.js create mode 100644 packages/cli/src/tools/__tests__/__fixtures__/file2.txt create mode 100644 packages/cli/src/tools/__tests__/copyFiles.test.js 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 0000000000000000000000000000000000000000..364e105ed39fbfd62001429a68140672b06ec0de GIT binary patch literal 2257 zcmchYXEfYt8;7T1^dLH$VOTZ%2NOdOH5j5LYLtZ0q7x-V8_6gU5)#7dkq{HTmsfNq zB3ZqcAxeY^G10@?efK?Q&)M(qInVv!xjx+IKEL}p*K@LYvIzo#AZG>st5|P)KF1_Z;y){W{<7K{nl!CPuE z_^(!C(Ol0n8 zK13*rzAtW>(wULKPRYLd7G18F8#1P`V*9`(Poj26eOXYyBVZPno~Cvvhx7vPjAuZo zF?VD!zB~QG(!zbw#qsxT8%BSpqMZ4f70ZPn-3y$L8{EVbbN9$H`B&Z1quk9tgp5FM zuxp3pJ0b8u|3+#5bkJ4SRnCF2l7#DyLYXYY8*?OuAwK4E6J{0N=O3QNVzQ$L#FKkR zi-c@&!nDvezOV$i$Lr}iF$XEcwnybQ6WZrMKuw8gCL^U#D;q3t&HpTbqyD%vG=TeDlzCT~MXUPC|Leb-Uk+ z=vnMd(|>ld?Fh>V8poP;q;;nc@en$|rnP0ytzD&fFkCeUE^kG9Kx4wUh!!rpjwKDP zyw_e|a^x_w3E zP}}@$g>*LLJ4i0`Gx)qltL}@;mDv}D*xR^oeWcWdPkW@Uu)B^X&4W1$p6}ze!zudJ zyiLg@uggoMIArBr*27EZV7djDg@W1MaL+rcZ-lrANJQ%%>u8)ZMWU@R2qtnmG(acP z0d_^!t>}5W zpT`*2NR+0+SpTHb+6Js4b;%LJB;B_-ChhnU5py}iJtku*hm5F0!iql8Hrpcy1aYbT z1*dKC5ua6pMX@@iONI?Hpr%h;&YaXp9n!ND7-=a%BD7v&g zOO41M6EbE24mJ#S$Ui0-brR5ML%@|ndz^)YLMMV1atna{Fw<;TF@>d&F|!Z>8eg>>hkFrV)W+uv=`^F9^e zzzM2*oOjT9%gLoub%(R57p-`TXFe#oh1_{&N-YN z<}artH|m=d8TQuKSWE)Z%puU|g|^^NFwC#N=@dPhasyYjoy(fdEVfKR@cXKHZV-`06HsP`|Ftx;8(YD$fFXumLWbGnu$GMqRncXYY9mwz9$ap zQtfZB^_BeNYITh^hA7+(XNFox5WMeG_LtJ%*Q}$8VKDI_p8^pqX)}NMb`0e|wgF7D zuQACY_Ua<1ri{;Jwt@_1sW9zzdgnyh_O#8y+C;LcZq6=4e^cs6KvmK@$vVpKFGbQ= z$)Eux5C|Fx;Gtmv9^#Y-g@7Rt7*eLp5n!gJmn7&B_L$G?NCN`AP>cXQEz}%F%K;vUs{+l4Q{}eWW;ATe2 zqvXzxoIDy(u;F2q1JH7Sf;{jy_j})F+cKlIOmNfjBGHoG^CN zM|Ho&&X|L-36f}Q-obEACz`sI%2f&k>z5c$2TyTSj~vmO)BW~+N^kt`Jt@R|s!){H ze1_eCrlNaPkJQhL$WG&iRvF*YG=gXd1IyYQ9ew|iYn7r~g!wOnw;@n42>enAxBv*A zEmV*N#sxdicyNM=A4|yaOC5MByts}s_Hpfj|y<6G=o=!3S@eIFKDdpR7|FY>L&Wat&oW&cm&X~ z5Bt>Fcq(fgnvlvLSYg&o6>&fY`ODg4`V^lWWD=%oJ#Kbad2u~! zLECFS*??>|vDsNR&pH=Ze0Eo`sC_G`OjoEKVHY|wmwlX&(XBE<@sx3Hd^gtd-fNwUHsylg06p`U2y_={u}Bc { + 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,