Skip to content

Commit

Permalink
Merge pull request #75 from serverless-stack/fix-stage-option
Browse files Browse the repository at this point in the history
For the npm run -- option add helper warning and docs
  • Loading branch information
jayair committed Feb 1, 2021
2 parents dc40822 + 302356c commit 4f78840
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
54 changes: 50 additions & 4 deletions packages/cli/bin/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const fs = require("fs-extra");
const yargs = require("yargs");
const chalk = require("chalk");
const spawn = require("cross-spawn");
const { initializeLogger } = require("@serverless-stack/core");
const { logger, initializeLogger } = require("@serverless-stack/core");

const packageJson = require("../package.json");
const paths = require("../scripts/util/paths");
Expand Down Expand Up @@ -64,7 +64,7 @@ function getCliInfo() {
cdkOptions: {
...cdkOptions,
verbose: argv.verbose ? 2 : 0,
noColor: process.env.NO_COLOR === 'true',
noColor: process.env.NO_COLOR === "true",
},
};
}
Expand All @@ -88,6 +88,48 @@ function addOptions(currentCmd) {
};
}

/**
* If `npm run` is used to execute these commands, you need to add `--` before
* the options. If it's not used, the command will run but the options will not be
* set correctly. The region or the stage might get set as the stack. This
* function simply checks if the stack is set to a common stage name or a region.
* And shows a warning.
*/
function checkNpmScriptArgs() {
const commonStageAndRegions = [
"qa",
"dev",
"prod",
"stage",
"staging",
"preprod",
"production",
"development",
"eu-west-1",
"eu-west-2",
"sa-east-1",
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
"ap-south-1",
"ca-central-1",
"eu-central-1",
"ap-northeast-2",
"ap-southeast-1",
"ap-southeast-2",
"ap-northeast-1",
];

if (commonStageAndRegions.indexOf(argv.stack) !== -1) {
logger.warn(
chalk.yellow(
`\nWarning: It looks like you might be setting the stack option to "${argv.stack}" by mistake. If you are using "npm run", make sure to add "--" before the options. For example, "npm run deploy -- --stage prod".\n`
)
);
}
}

const argv = yargs
.parserConfiguration({ "boolean-negation": false })

Expand Down Expand Up @@ -175,12 +217,12 @@ const argv = yargs

// Disable color
if (!process.stdout.isTTY || argv.noColor) {
process.env.NO_COLOR = 'true';
process.env.NO_COLOR = "true";
chalk.level = 0;
}

if (argv.verbose) {
process.env.DEBUG = 'true';
process.env.DEBUG = "true";
}

// Empty and recreate the .build directory
Expand All @@ -195,6 +237,10 @@ switch (script) {
case cmd.remove: {
const cliInfo = getCliInfo();

if (cliInfo.npm) {
checkNpmScriptArgs();
}

// Prepare app
prepareCdk(argv, cliInfo).then(({ config }) =>
internals[script](argv, config, cliInfo)
Expand Down
6 changes: 6 additions & 0 deletions www/docs/deploying-your-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ This uses your **default AWS Profile**. And the **region** and **stage** specifi
AWS_PROFILE=my-profile npx sst deploy --stage prod --region eu-west-1
```

Just note that if you are using `npm run` to deploy, you'll need to be careful while setting the stage or region. You'll need to use an extra `--` for the options. For example:

```bash
npm run deploy -- --stage prod --region eu-west-1
```

## Removing an app

Finally, you can remove all your stacks and their resources from AWS using.
Expand Down
19 changes: 18 additions & 1 deletion www/docs/packages/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Runs your tests using Jest. Takes all the [Jest CLI options](https://jestjs.io/d

### `cdk`

The sst CLI comes with <a href={ config.forkedCdk }>a forked version of AWS CDK</a> that it uses internally. This command gives direct access to it. To use this command you'll need to pass in the location of the CDK app. In our cases this is going to be generated in `build/run.js`. For example, to run the CDK `list` command you'll need to.
The SST CLI comes with <a href={ config.forkedCdk }>a forked version of AWS CDK</a> that it uses internally. This command gives direct access to it. To use this command you'll need to pass in the location of the CDK app. In our cases this is going to be generated in `build/run.js`. For example, to run the CDK `list` command you'll need to.

```bash
npx sst cdk --app=build/run.js list
Expand All @@ -136,3 +136,20 @@ AWS_PROFILE=production npx sst deploy
```

Where `production` is a profile defined locally in your `~/.aws/credentials`.

## Package scripts

If you used the `create-serverless-stack` CLI to create your app, the above commands (`start`, `build`, `deploy`, and `remove`) are also available in your `package.json`. So you can run them using.

```bash
# With npm
npm run <command>
# Or with Yarn
yarn run <command>
```

Just note that for `npm run`, you'll need to use an extra `--` for the options. For example:

```bash
npm run build -- --stage alpha
```
17 changes: 0 additions & 17 deletions www/docs/working-locally.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,6 @@ yarn test

Internally, SST uses [Jest](https://jestjs.io/). You'll just need to add your tests to the `test/` directory.

## Package scripts

The above commands (`start`, `build`, `deploy`, and `remove`) are also available in your `package.json`. So you can run them using.

```bash
# With npm
npm run <command>
# Or with Yarn
yarn run <command>
```

Just note that for `npm run`, you'll need to use an extra `--` for the options. For example:

```bash
npm run build -- --stage alpha
```

## Linting, type checking

Your code is automatically linted when building or deploying. If you'd like to customize the lint rules, add a `.eslintrc.json` in your project root. If you'd like to turn off linting, add `*` to an `.eslintignore` file in your project root.
Expand Down

0 comments on commit 4f78840

Please sign in to comment.