-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(twilio-run): restructure configuration (#198)
This PR aims to restructure the way configuration is handled by dropping .twilio-functions files in favor of a dedicated config file and a deploy info cache file. BREAKING CHANGE: Drops support for .twilio-functions files and internally restructures activate files to promote fix #166
- Loading branch information
Showing
44 changed files
with
1,525 additions
and
648 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,5 +69,6 @@ dist/ | |
|
||
*.tsbuildinfo | ||
.twilio-functions | ||
*.twiliodeployinfo | ||
|
||
packages/serverless-api/docs/ |
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,62 @@ | ||
# Configuration | ||
|
||
## Examples | ||
|
||
### Functions with Preprocessor like TypeScript or Babel | ||
|
||
By default the Serverless Toolkit will be looking for Functions inside a `functions/` or `src/` directory and for assets for an `assets/` or `static/` directory. | ||
|
||
If you are using a pre-processor such as TypeScript or Babel, your structure might look different. For example maybe you have a structure where `src/` contains all TypeScript files and `dist/` contains the output. | ||
|
||
In which case you'd want a config that looks similar to this: | ||
|
||
```json | ||
{ | ||
"functionsFolderName": "dist" | ||
} | ||
``` | ||
|
||
### Using different `.env` files for different environments | ||
|
||
If you are deploying to different environments you might want different environment variables for your application. | ||
|
||
You can specify environment specific configurations inside the config file by using the domain suffix of your environment. | ||
|
||
If you are using the `--production` flag you'll need to use the environment: `*`. | ||
|
||
For example: | ||
|
||
```json | ||
{ | ||
"environments": { | ||
"dev": { | ||
"env": ".env" | ||
}, | ||
"stage": { | ||
"env": ".env.stage" | ||
}, | ||
"*": { | ||
"env": ".env.prod" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Deploy to specific services on different accounts | ||
|
||
There might be a scenario where you want to deploy the same project to different Twilio accounts or projects with different services. | ||
|
||
Inside the config you can define which service the project should be deployed to depending on the Twilio Account SID. | ||
|
||
```json | ||
{ | ||
"projects": { | ||
"AC11111111111111111111111111111111": { | ||
"serviceSid": "ZSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
}, | ||
"AC22222222222222222222222222222222": { | ||
"serviceSid": "ZSbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" | ||
} | ||
} | ||
} | ||
``` |
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,41 @@ | ||
# Migration Guides | ||
|
||
## Migrating from Serverless Toolkit V2 to V3 | ||
|
||
What changed? The main changes are the following: | ||
|
||
### 1. No more `.twilio-functions` file. | ||
|
||
The `.twilio-functions` file is being replaced by a `.twilioserverlessrc` file for any configuration you want to do and a `.twiliodeployinfo` file. | ||
|
||
The `.twilioserverlessrc` file can be checked into your project and helps especially with more complex applications. Check out the [Configuration documentation](CONFIGURATION.md) for more information. | ||
|
||
The `.twiliodeployinfo` file can be commited to your project but is primarily intended to make commands more convenient for you. This file gets updated as part of deployments. | ||
|
||
To transition your `.twilio-functions` file we provide a convenience script that takes care of some of the work. You can run it by running: | ||
|
||
```bash | ||
npx -p twilio-run@3 twilio-upgrade-config | ||
``` | ||
|
||
## FAQ | ||
|
||
### How do I know which version I'm using? | ||
|
||
There are two ways you can consume the Serverless Toolkit and the ways to determine your version are different. | ||
|
||
**Scenario 1**: If you are using the Twilio CLI and `@twilio-labs/plugin-serverless` run the following command: | ||
|
||
```bash | ||
twilio plugins | ||
``` | ||
|
||
Your output should contain something like: `@twilio-labs/plugin-serverless 1.9.0`. In this case your version of `@twilio-labs/plugin-serverless` is version 1.9.0 and to get your Serverless Toolkit version increment the first number by one. So in this case you have Serverless Toolkit V2. | ||
|
||
**Scenario 2**: If you are using `twilio-run` directly. | ||
|
||
Run the following command. The first number will be your Serverless Toolkit version: | ||
|
||
```bash | ||
npx twilio-run --version | ||
``` |
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
48 changes: 48 additions & 0 deletions
48
packages/twilio-run/__tests__/checks/legacy-config.test.ts
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 { mocked } from 'ts-jest/utils'; | ||
import checkLegacyConfig, { | ||
printConfigWarning, | ||
} from '../../src/checks/legacy-config'; | ||
import { logger } from '../../src/utils/logger'; | ||
|
||
jest.mock('../../src/utils/fs', () => ({ | ||
fileExistsSync: jest.fn(() => { | ||
return SHOULD_FILE_EXIST; | ||
}), | ||
})); | ||
|
||
jest.mock('../../src/utils/logger', () => ({ | ||
logger: { | ||
warn: jest.fn(), | ||
}, | ||
})); | ||
|
||
var SHOULD_FILE_EXIST = false; | ||
|
||
describe('printLegacyConfig', () => { | ||
it('should log a message with the correct title', () => { | ||
printConfigWarning(); | ||
expect(logger.warn).toHaveBeenCalledTimes(1); | ||
expect( | ||
mocked(logger.warn).mock.calls[0][0].includes( | ||
'We found a .twilio-functions file' | ||
) | ||
).toBeTruthy(); | ||
expect(mocked(logger.warn).mock.calls[0][1]).toEqual( | ||
'Legacy Configuration Detected' | ||
); | ||
}); | ||
}); | ||
|
||
describe('checkLegacyConfig', () => { | ||
it('should print a warning if the config file exists', () => { | ||
SHOULD_FILE_EXIST = true; | ||
checkLegacyConfig(); | ||
expect(logger.warn).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not print a warning if the file does not exist', () => { | ||
SHOULD_FILE_EXIST = false; | ||
checkLegacyConfig(); | ||
expect(logger.warn).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,131 @@ | ||
jest.mock('../../src/config/utils/configLoader'); | ||
|
||
import { readSpecializedConfig } from '../../src/config/global'; | ||
import { ConfigurationFile } from '../../src/types/config'; | ||
const { | ||
__setTestConfig, | ||
}: { | ||
__setTestConfig: (config: Partial<ConfigurationFile>) => void; | ||
} = require('../../src/config/utils/configLoader'); | ||
|
||
describe('readSpecializedConfig', () => { | ||
test('returns the right base config', () => { | ||
__setTestConfig({ | ||
serviceSid: 'ZS11112222111122221111222211112222', | ||
env: '.env.example', | ||
}); | ||
|
||
expect( | ||
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy') | ||
).toEqual({ | ||
serviceSid: 'ZS11112222111122221111222211112222', | ||
env: '.env.example', | ||
}); | ||
}); | ||
|
||
test('merges command-specific config', () => { | ||
__setTestConfig({ | ||
serviceSid: 'ZS11112222111122221111222211112222', | ||
commands: { | ||
deploy: { | ||
functionsFolder: '/tmp/functions', | ||
}, | ||
}, | ||
}); | ||
|
||
expect( | ||
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy') | ||
).toEqual({ | ||
serviceSid: 'ZS11112222111122221111222211112222', | ||
functionsFolder: '/tmp/functions', | ||
}); | ||
}); | ||
|
||
test('ignores other command-specific config', () => { | ||
__setTestConfig({ | ||
serviceSid: 'ZS11112222111122221111222211112222', | ||
commands: { | ||
deploy: { | ||
functionsFolder: '/tmp/functions', | ||
}, | ||
start: { | ||
functionsFolder: '/tmp/src', | ||
}, | ||
}, | ||
}); | ||
|
||
expect( | ||
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy') | ||
).toEqual({ | ||
serviceSid: 'ZS11112222111122221111222211112222', | ||
functionsFolder: '/tmp/functions', | ||
}); | ||
}); | ||
|
||
test('environments override other config', () => { | ||
__setTestConfig({ | ||
serviceSid: 'ZS11112222111122221111222211112222', | ||
env: '.env.example', | ||
commands: { | ||
deploy: { | ||
functionsFolder: '/tmp/functions', | ||
}, | ||
}, | ||
environments: { | ||
stage: { | ||
env: '.env.stage', | ||
}, | ||
'*': { | ||
env: '.env.prod', | ||
}, | ||
}, | ||
}); | ||
|
||
expect( | ||
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy', { | ||
environmentSuffix: 'stage', | ||
}) | ||
).toEqual({ | ||
serviceSid: 'ZS11112222111122221111222211112222', | ||
functionsFolder: '/tmp/functions', | ||
env: '.env.stage', | ||
}); | ||
}); | ||
|
||
test('account config overrides every other config', () => { | ||
__setTestConfig({ | ||
serviceSid: 'ZS11112222111122221111222211112222', | ||
env: '.env.example', | ||
commands: { | ||
deploy: { | ||
functionsFolder: '/tmp/functions', | ||
}, | ||
}, | ||
environments: { | ||
stage: { | ||
serviceSid: 'ZS11112222111122221111222211112223', | ||
env: '.env.stage', | ||
}, | ||
'*': { | ||
env: '.env.prod', | ||
}, | ||
}, | ||
projects: { | ||
AC11112222111122221111222211114444: { | ||
serviceSid: 'ZS11112222111122221111222211114444', | ||
}, | ||
}, | ||
}); | ||
|
||
expect( | ||
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy', { | ||
environmentSuffix: 'stage', | ||
accountSid: 'AC11112222111122221111222211114444', | ||
}) | ||
).toEqual({ | ||
serviceSid: 'ZS11112222111122221111222211114444', | ||
functionsFolder: '/tmp/functions', | ||
env: '.env.stage', | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.