From 2a892b7a41dbc93c2bfd681794fa907e01eceacb Mon Sep 17 00:00:00 2001 From: vsavkin Date: Wed, 3 Jan 2018 09:34:27 -0500 Subject: [PATCH] fix(schematics): fix format command to handle large number of files --- e2e/schematics/command-line.test.ts | 31 ++++++++++++++++++- .../schematics/src/command-line/format.ts | 21 +++++++------ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/e2e/schematics/command-line.test.ts b/e2e/schematics/command-line.test.ts index 6384570022..777e6ce2c9 100644 --- a/e2e/schematics/command-line.test.ts +++ b/e2e/schematics/command-line.test.ts @@ -88,7 +88,21 @@ describe('Command line', () => { updateFile( 'apps/myapp/src/main.ts', ` - const x = 3232; + const x = 1111; + ` + ); + + updateFile( + 'apps/myapp/src/app/app.module.ts', + ` + const y = 1111; + ` + ); + + updateFile( + 'apps/myapp/src/app/app.component.ts', + ` + const z = 1111; ` ); @@ -97,7 +111,22 @@ describe('Command line', () => { fail('boom'); } catch (e) { expect(e.stdout.toString()).toContain('apps/myapp/src/main.ts'); + expect(e.stdout.toString()).toContain('apps/myapp/src/app/app.module.ts'); + expect(e.stdout.toString()).toContain('apps/myapp/src/app/app.component.ts'); } + runCommand( + 'npm run format:write -- --files="apps/myapp/src/app/app.module.ts,apps/myapp/src/app/app.component.ts"' + ); + + try { + runCommand('npm run -s format:check'); + fail('boom'); + } catch (e) { + expect(e.stdout.toString()).toContain('apps/myapp/src/main.ts'); + expect(e.stdout.toString()).not.toContain('apps/myapp/src/app/app.module.ts'); + expect(e.stdout.toString()).not.toContain('apps/myapp/src/app/app.component.ts'); + } + runCommand('npm run format:write'); expect(runCommand('npm run -s format:check')).toEqual(''); }, diff --git a/packages/schematics/src/command-line/format.ts b/packages/schematics/src/command-line/format.ts index ed8e5c76b0..9dea444078 100644 --- a/packages/schematics/src/command-line/format.ts +++ b/packages/schematics/src/command-line/format.ts @@ -14,18 +14,18 @@ switch (command) { break; } -function parseFiles() { +function parseFiles(): string[] { const args = process.argv.slice(3); if (args.length === 0) { - return '"{apps,libs}/**/*.ts"'; + return ['"{apps,libs}/**/*.ts"']; } const dashDashFiles = args.filter(a => a.startsWith('--files='))[0]; if (dashDashFiles) { args.splice(args.indexOf(dashDashFiles), 1); - return `"${parseDashDashFiles(dashDashFiles).join(',')}"`; + return parseDashDashFiles(dashDashFiles).map(t => `\"${t}\"`); } else { const withoutShahs = args.slice(2); - return `"${getFilesFromShash(args[0], args[1]).join(',')}"`; + return getFilesFromShash(args[0], args[1]).map(t => `\"${t}\"`); } } @@ -42,19 +42,22 @@ function getFilesFromShash(sha1: string, sha2: string): string[] { .toString('utf-8') .split('\n') .map(a => a.trim()) - .filter(a => a.length > 0); + .filter(a => a.length > 0) + .filter(a => path.extname(a) === '.ts'); } -function write(files: string) { - execSync(`node ./node_modules/prettier/bin/prettier.js --single-quote --print-width 120 --write ${files}`, { +function write(files: string[]) { + execSync(`node ./node_modules/prettier/bin/prettier.js --single-quote --print-width 120 --write ${files.join(' ')}`, { stdio: [0, 1, 2] }); } -function check(files: string) { +function check(files: string[]) { try { execSync( - `node ./node_modules/prettier/bin/prettier.js --single-quote --print-width 120 --list-different ${files}`, + `node ./node_modules/prettier/bin/prettier.js --single-quote --print-width 120 --list-different ${files.join( + ' ' + )}`, { stdio: [0, 1, 2] }