Skip to content

Commit d00e93d

Browse files
MI-85: Use stdio inherit for preserving cli colours
1 parent 7898b79 commit d00e93d

File tree

6 files changed

+42
-33
lines changed

6 files changed

+42
-33
lines changed

bin/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ async function main() {
6868
`${baseCommand} --stage ${stage} --aws-profile ${profile} ${verbose}`
6969
);
7070

71-
await runCLICommand(commands);
71+
await runCLICommand(commands, bitbucketCloneDir);
7272

7373
deploymentStatus = true;
7474
} catch (error) {
75-
console.error(logSymbols.error, chalk.red(error));
75+
if (error instanceof Error) {
76+
console.error(logSymbols.error, chalk.redBright(error.message));
77+
}
7678
console.error(
7779
logSymbols.error,
78-
chalk.red(
80+
chalk.redBright(
7981
'Deployment failed! Please check the logs for more details.'
8082
)
8183
);

lib/cmd.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import chalk from 'chalk';
2-
import * as cp from 'child_process';
2+
import { SpawnOptions, spawn } from 'child_process';
33
import logSymbols from 'log-symbols';
44
import { env } from './env';
55

@@ -20,29 +20,22 @@ function splitCommandAndArgs(command: string): Command {
2020
// Wrap spawn in a promise
2121
function asyncSpawn(
2222
command: string,
23-
args?: ReadonlyArray<string>,
24-
options?: cp.SpawnOptionsWithoutStdio
23+
args: ReadonlyArray<string>,
24+
options: SpawnOptions
2525
): Promise<number | null> {
2626
return new Promise(function (resolve, reject) {
2727
if (env.debug) {
2828
const commandWithArgs = `${command} ${args?.join(' ')}`;
2929
const optionsStr = JSON.stringify(options);
3030
console.log(
31-
chalk.white(
32-
`ℹ️ Executing command: ${commandWithArgs} with options: ${optionsStr}`
31+
logSymbols.info,
32+
chalk.whiteBright(
33+
`Executing command: ${commandWithArgs} with options: ${optionsStr}`
3334
)
3435
);
3536
}
3637

37-
const process = cp.spawn(command, args, options);
38-
39-
process.stdout.on('data', (data) => {
40-
console.log(logSymbols.info, chalk.white(data.toString()));
41-
});
42-
43-
process.stderr.on('data', (data) => {
44-
console.error(logSymbols.error, chalk.red(data.toString()));
45-
});
38+
const process = spawn(command, args, options);
4639

4740
process.on('exit', function (code) {
4841
if (code !== 0) reject(code);
@@ -57,15 +50,23 @@ function asyncSpawn(
5750

5851
function runCommandString(
5952
command: string,
60-
workDir?: string
53+
workDir: string
6154
): Promise<number | null> {
62-
console.log(logSymbols.info, chalk.white(`Running command: ${command}`));
55+
console.log(
56+
logSymbols.info,
57+
chalk.whiteBright(`Running command: ${command}`)
58+
);
6359
const cmd = splitCommandAndArgs(command);
64-
return asyncSpawn(cmd.command, cmd.args, { cwd: workDir });
60+
return asyncSpawn(cmd.command, cmd.args, {
61+
cwd: workDir,
62+
stdio: ['pipe', 'inherit', 'inherit'],
63+
});
6564
}
6665

67-
export async function runCLICommand(commandStr: Array<string>) {
68-
const workDir = process.env.BITBUCKET_CLONE_DIR;
66+
export async function runCLICommand(
67+
commandStr: Array<string>,
68+
workDir: string
69+
) {
6970
console.log(logSymbols.info, chalk.white(`Running commands in ${workDir}`));
7071

7172
for (const cmd of commandStr) {

lib/findServerlessYaml.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export async function findServerlessYaml(basePath: string) {
88

99
console.log(
1010
logSymbols.info,
11-
chalk.white(
11+
chalk.whiteBright(
1212
`Fetching serverless configuration with pattern ${globPattern}`
1313
)
1414
);
@@ -18,7 +18,7 @@ export async function findServerlessYaml(basePath: string) {
1818
for (const file of files) {
1919
console.log(
2020
logSymbols.info,
21-
chalk.white('Found serverless.yml at: ', file)
21+
chalk.whiteBright('Found serverless.yml at:', file)
2222
);
2323
}
2424

lib/injectCfnRole.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function injectCfnRole(
1919
if (env.debug) {
2020
console.log(
2121
logSymbols.info,
22-
chalk.white(JSON.stringify(serverless, null, 2))
22+
chalk.whiteBright(JSON.stringify(serverless, null, 2))
2323
);
2424
}
2525

@@ -35,22 +35,22 @@ export async function injectCfnRole(
3535
) {
3636
console.warn(
3737
logSymbols.warning,
38-
chalk.yellow(
38+
chalk.yellowBright(
3939
'It looks like serverless.yaml already defines a CFN role.'
4040
)
4141
);
4242

4343
if (cfnRole) {
4444
console.log(
4545
logSymbols.warning,
46-
chalk.yellow(
46+
chalk.yellowBright(
4747
'This can now be injected by deploy pipe and removed from serverless.yaml'
4848
)
4949
);
5050
} else {
5151
console.log(
5252
logSymbols.warning,
53-
chalk.yellow(
53+
chalk.yellowBright(
5454
`This will be overwritten with ${cfnRole}. Please remove from serverless.yaml`
5555
)
5656
);
@@ -63,7 +63,7 @@ export async function injectCfnRole(
6363
if (!cfnRole) {
6464
console.warn(
6565
logSymbols.warning,
66-
chalk.yellow('Please provide a CFN role for deployment')
66+
chalk.yellowBright('Please provide a CFN role for deployment')
6767
);
6868
return;
6969
}
@@ -79,10 +79,12 @@ export async function injectCfnRole(
7979
await writeFile(serverlessYamlPath, modifiedYaml, 'utf8');
8080
console.log(
8181
logSymbols.success,
82-
chalk.green(`Injected CFN role ${cfnRole} at ${serverlessYamlPath}`)
82+
chalk.greenBright(
83+
`Injected CFN role ${cfnRole} at ${serverlessYamlPath}`
84+
)
8385
);
8486
} catch (error) {
85-
console.error(logSymbols.error, chalk.red(error));
87+
console.error(logSymbols.error, chalk.redBright(error));
8688
throw new Error(
8789
`Failed to inject CFN role at path ${serverlessYamlPath}`
8890
);

lib/serverlessProjectType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export async function isNxServerlessMonorepo(directoryPath: string) {
77

88
const serverlessYmlFiles = await glob('serverless.{yml,yaml}', {
99
cwd: directoryPath,
10+
ignore: ['**/node_modules/**'],
1011
});
1112
const isServerlessFileExist = serverlessYmlFiles.length > 0;
1213

lib/uploadDeploymentBadge.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export async function uploadDeploymentBadge(
2929

3030
try {
3131
if (!uploadBadge) {
32-
console.log(logSymbols.info, chalk.white('Skipping badge upload'));
32+
console.log(
33+
logSymbols.info,
34+
chalk.whiteBright('Skipping badge upload')
35+
);
3336
return statusCode;
3437
}
3538

@@ -59,7 +62,7 @@ export async function uploadDeploymentBadge(
5962
);
6063
return statusCode;
6164
} catch (error) {
62-
console.error(logSymbols.error, chalk.red(error));
65+
console.error(logSymbols.error, chalk.redBright(error));
6366
return 1;
6467
}
6568
}

0 commit comments

Comments
 (0)