Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
fix: remove noEnv and add typescript to dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
arantespp committed Mar 22, 2021
1 parent fbb7a2a commit aacde98
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 81 deletions.
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"simple-git": "^2.25.0",
"ts-loader": "^8.0.14",
"ts-node": "^9.1.1",
"typescript": "^4.1.2",
"uglify-js": "^3.13.0",
"webpack": "^5.15.0",
"yargs": "^16.2.0"
Expand All @@ -58,8 +59,7 @@
"@types/yargs": "^16.0.0",
"faker": "^5.1.0",
"jest": "^26.6.3",
"ts-jest": "^26.4.4",
"typescript": "^4.1.2"
"ts-jest": "^26.4.4"
},
"bin": {
"carlin": "./bin/carlin"
Expand Down
146 changes: 78 additions & 68 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,79 +77,89 @@ const cli = () => {
return finalConfig;
};

return yargs
.strict()
.scriptName(NAME)
.env(getEnv())
.options(addGroupToOptions(options, 'Common Options'))
.middleware(((argv: any, { parsed }: any) => {
const { environment, environments } = argv;

return (
yargs
/**
* Create final options with environment and environments.
* It can't be full strict because options may overlap among calin config
* files.
*/
if (environment && environments && environments[environment as string]) {
Object.entries(environments[environment]).forEach(([key, value]) => {
/**
* The case where argv[key] must not have the environment value is
* when such value is passed as option via CLI. For instance,
*
* $ carlin deploy --stack-name SomeName
*
* SomeName must be used as stack name independently of the
* environment values https://github.com/ttoss/carlin/issues/13.
*
* Three cases set argv:
*
* 1. Default.
* 2. Config file.
* 3. CLI
*
* - Case 1 we determine if the parsed.defaulted is true.
* - Case 2 we determine if `argv[key] === finalConfig[key]`.
* - Case 3 if the two above are falsy.
*/
const isKeyFormCli = (() => {
const paramCaseKey = paramCase(key);

.strictCommands()
.scriptName(NAME)
.env(getEnv())
.options(addGroupToOptions(options, 'Common Options'))
.middleware(((argv: any, { parsed }: any) => {
const { environment, environments } = argv;

/**
* Create final options with environment and environments.
*/
if (
environment &&
environments &&
environments[environment as string]
) {
Object.entries(environments[environment]).forEach(([key, value]) => {
/**
* Fixes #16 https://github.com/ttoss/carlin/issues/16
* The case where argv[key] must not have the environment value is
* when such value is passed as option via CLI. For instance,
*
* $ carlin deploy --stack-name SomeName
*
* SomeName must be used as stack name independently of the
* environment values https://github.com/ttoss/carlin/issues/13.
*
* Three cases set argv:
*
* 1. Default.
* 2. Config file.
* 3. CLI
*
* - Case 1 we determine if the parsed.defaulted is true.
* - Case 2 we determine if `argv[key] === finalConfig[key]`.
* - Case 3 if the two above are falsy.
*/
if (parsed?.defaulted?.[paramCaseKey]) {
return false;
const isKeyFormCli = (() => {
const paramCaseKey = paramCase(key);

/**
* Fixes #16 https://github.com/ttoss/carlin/issues/16
*/
if (parsed?.defaulted?.[paramCaseKey]) {
return false;
}

/**
* Fixes #13 https://github.com/ttoss/carlin/issues/13
*/
if (argv[key] === finalConfig[key]) {
return false;
}

return true;
})();

if (!isKeyFormCli) {
argv[key] = value;
}

/**
* Fixes #13 https://github.com/ttoss/carlin/issues/13
*/
if (argv[key] === finalConfig[key]) {
return false;
}

return true;
})();

if (!isKeyFormCli) {
argv[key] = value;
}
});
}
}) as any)
.pkgConf(NAME)
.config(getConfig())
.config('config', (configPath: string) =>
readObjectFile({ path: configPath }),
)
.command({
command: 'print-args',
describe: false,
handler: (argv) => console.log(JSON.stringify(argv, null, 2)),
})
.command(deployCommand)
.epilogue(
'For more information, find our manual at https://carlin.ttoss.dev',
)
.help();
});
}
}) as any)
.pkgConf(NAME)
.config(getConfig())
.config('config', (configPath: string) =>
readObjectFile({ path: configPath }),
)
.command({
command: 'print-args',
describe: false,
handler: (argv) => console.log(JSON.stringify(argv, null, 2)),
})
.command(deployCommand)
.epilogue(
'For more information, find our manual at https://carlin.ttoss.dev',
)
.help()
);
};

export default cli;
32 changes: 23 additions & 9 deletions packages/cli/src/deploy/addDefaults.cloudFormation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const addDefaultsParametersAndTagsToParams = async (
): Promise<CloudFormationParams> => {
const [
branchName,
environment = 'NoEnv',
environment,
packageName,
packageVersion,
projectName,
Expand All @@ -42,13 +42,15 @@ const addDefaultsParametersAndTagsToParams = async (
...params,
Parameters: [
...(params.Parameters || []),
{ ParameterKey: 'Environment', ParameterValue: environment },
...(environment
? [{ ParameterKey: 'Environment', ParameterValue: environment }]
: []),
{ ParameterKey: 'Project', ParameterValue: projectName },
],
Tags: [
...(params.Tags || []),
{ Key: 'Branch', Value: branchName },
{ Key: 'Environment', Value: environment },
...(environment ? [{ Key: 'Environment', Value: environment }] : []),
{ Key: 'Package', Value: packageName },
{ Key: 'Project', Value: projectName },
{ Key: 'Version', Value: packageVersion },
Expand All @@ -64,13 +66,17 @@ const addDefaultParametersToTemplate = async (
getProjectName(),
]);

const newParameters: any = {
Project: { Default: projectName, Type: 'String' },
};

if (environment) {
newParameters.Environment = { Default: environment, Type: 'String' };
}

const newTemplate = {
...template,
Parameters: {
Environment: { Default: environment, Type: 'String' },
Project: { Default: projectName, Type: 'String' },
...template.Parameters,
},
Parameters: { ...newParameters, ...template.Parameters },
};

return newTemplate;
Expand Down Expand Up @@ -140,12 +146,18 @@ const addEnvironmentsToLambdaResources = (
return;
}

if (!environment) {
return;
}

if (!Properties.Environment) {
Properties.Environment = {};
}

if (!Properties.Environment.Variables) {
Properties.Environment.Variables = {};
}

Properties.Environment.Variables.ENVIRONMENT = environment;
}
});
Expand All @@ -163,8 +175,10 @@ export const addDefaults = async ({
addEnvironmentsToLambdaResources,
].reduce(async (acc, addFn) => addFn(await acc), Promise.resolve(template));

return {
const response = {
params: await addDefaultsParametersAndTagsToParams(params),
template: newTemplate,
};

return response;
};
4 changes: 3 additions & 1 deletion packages/cli/src/deploy/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AWS_DEFAULT_REGION } from '../config';
import { addGroupToOptions, getAwsAccountId } from '../utils';

import { deployBaseStackCommand } from './baseStack/command';
import { deployCicdCommand } from './cicd/command';
import { deployCloudFormation, destroyCloudFormation } from './cloudFormation';
import { printStackOutputsAfterDeploy } from './cloudFormation.core';
import { deployLambdaLayerCommand } from './lambdaLayer/command';
Expand Down Expand Up @@ -158,7 +159,8 @@ export const deployCommand: CommandModule<
.command(deployLambdaLayerCommand)
.command(describeDeployCommand)
.command(deployBaseStackCommand)
.command(deployStaticAppCommand);
.command(deployStaticAppCommand)
.command(deployCicdCommand);

return yargsBuilder;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/staticApp/staticApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const getStaticAppBucket = async ({
/**
* Fixes #20 https://github.com/ttoss/carlin/issues/20
*/
export const defaultBuildFolders = ['build', 'out'];
export const defaultBuildFolders = ['build', 'out', 'storybook-static'];

const findDefaultBuildFolder = async () => {
/**
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/deploy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export const handleDeployError = ({
process.exit(1);
};

/**
* @param param.stackName acts as a default stack name.
*/
export const handleDeployInitialization = async ({
logPrefix,
stackName: preDefinedStackName,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/utils/cloudFormationTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface Parameter {
Default?: string | number;
Description?: string;
Type: string;
NoEcho?: boolean;
}

export interface Resource {
Expand Down

0 comments on commit aacde98

Please sign in to comment.