Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): prefix when using --output-style=stream
- Loading branch information
Showing
11 changed files
with
211 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
isNotWindows, | ||
newProject, | ||
readFile, | ||
readJson, | ||
runCLI, | ||
runCLIAsync, | ||
runCommand, | ||
tmpProjPath, | ||
uniq, | ||
updateFile, | ||
updateProjectConfig, | ||
} from '@nrwl/e2e/utils'; | ||
import { renameSync } from 'fs'; | ||
import { packagesWeCareAbout } from 'nx/src/command-line/report'; | ||
|
||
describe('Output Style', () => { | ||
beforeEach(() => newProject()); | ||
|
||
it('should stream output', async () => { | ||
const myapp = uniq('myapp'); | ||
runCLI(`generate @nrwl/web:app ${myapp}`); | ||
updateProjectConfig(myapp, (c) => { | ||
c.targets['counter'] = { | ||
executor: '@nrwl/workspace:counter', | ||
options: { | ||
to: 2, | ||
}, | ||
}; | ||
return c; | ||
}); | ||
|
||
const withPrefixes = runCLI( | ||
`counter ${myapp} --result=true --output-style=stream` | ||
); | ||
expect(withPrefixes).toContain(`[${myapp}`); | ||
|
||
const noPrefixes = runCLI( | ||
`counter ${myapp} --result=true --output-style=stream-without-prefixes` | ||
); | ||
expect(noPrefixes).not.toContain(`[${myapp}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { addPrefixToLines } from './add-command-prefix'; | ||
const stripAnsi = require('strip-ansi'); | ||
|
||
describe('addPrefixToLines', () => { | ||
it('should add project name as a prefix', () => { | ||
const prefixed = addPrefixToLines('myproj', ['one', 'two', 'three']); | ||
expect(prefixed.map(stripAnsi)).toEqual([ | ||
'[myproj ] one', | ||
'[myproj ] two', | ||
'[myproj ] three', | ||
]); | ||
}); | ||
|
||
it('should handle project names which length = 15', () => { | ||
const prefixed = addPrefixToLines('123456789012345', [ | ||
'one', | ||
'two', | ||
'three', | ||
]); | ||
expect(prefixed.map(stripAnsi)).toEqual([ | ||
'[123456789012345] one', | ||
'[123456789012345] two', | ||
'[123456789012345] three', | ||
]); | ||
}); | ||
|
||
it('should handle long project names', () => { | ||
const prefixed = addPrefixToLines('12345678901234567890', [ | ||
'one', | ||
'two', | ||
'three', | ||
]); | ||
expect(prefixed.map(stripAnsi)).toEqual([ | ||
'[...901234567890] one', | ||
'[...901234567890] two', | ||
'[...901234567890] three', | ||
]); | ||
}); | ||
|
||
it('should not prefix last empty line', () => { | ||
const prefixed = addPrefixToLines('myproj', ['one', 'two', '']); | ||
expect(prefixed.map(stripAnsi)).toEqual([ | ||
'[myproj ] one', | ||
'[myproj ] two', | ||
'', | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as chalk from 'chalk'; | ||
|
||
export function addCommandPrefixIfNeeded( | ||
projectName: string, | ||
chunk: any, | ||
encoding: string | ||
) { | ||
if (process.env.NX_PREFIX_OUTPUT === 'true') { | ||
const lines = ( | ||
typeof chunk === 'string' ? chunk : chunk.toString('utf-8') | ||
).split('\n'); | ||
return { | ||
content: addPrefixToLines(projectName, lines).join('\n'), | ||
encoding: 'utf-8', | ||
}; | ||
} else { | ||
return { content: chunk, encoding: encoding }; | ||
} | ||
} | ||
|
||
export function addPrefixToLines(projectName: string, lines: string[]) { | ||
const updatedLines = []; | ||
for (let i = 0; i < lines.length; ++i) { | ||
if (i === lines.length - 1 && lines[i] === '') { | ||
updatedLines.push(''); | ||
} else { | ||
updatedLines.push(`${projectNamePrefix(projectName)} ${lines[i]}`); | ||
} | ||
} | ||
return updatedLines; | ||
} | ||
|
||
const colors = [ | ||
chalk.green, | ||
chalk.greenBright, | ||
chalk.red, | ||
chalk.redBright, | ||
chalk.cyan, | ||
chalk.cyanBright, | ||
chalk.yellow, | ||
chalk.yellowBright, | ||
chalk.magenta, | ||
chalk.magentaBright, | ||
]; | ||
|
||
function projectNamePrefix(projectName: string) { | ||
const n = normalizeProjectName(projectName); | ||
return colors[projectNameToIndex(projectName)](`[${n}]`); | ||
} | ||
|
||
function projectNameToIndex(projectName: string): number { | ||
let code = 0; | ||
for (let i = 0; i < projectName.length; ++i) { | ||
code += projectName.charCodeAt(i); | ||
} | ||
return code % colors.length; | ||
} | ||
|
||
function normalizeProjectName(projectName: string): string { | ||
return projectName.length > 15 | ||
? `...${projectName.substring(projectName.length - 12)}` | ||
: `${projectName} `.substring(0, 15); | ||
} |
844679e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev