Skip to content

Commit 19a4124

Browse files
committed
feat: add config support for list,activate,deploy (#15)
This commit adds config file support for the list, activate and deploy command. It also deprecates the --functions-env flag BREAKING CHANGE: Deprecating --functions-env as an option fix: #15, fix: #27
1 parent bc66edf commit 19a4124

File tree

10 files changed

+396
-339
lines changed

10 files changed

+396
-339
lines changed

package-lock.json

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/activate.ts

+4-89
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,18 @@
11
import {
2-
ActivateConfig as ApiActivateConfig,
2+
ActivateConfig,
33
TwilioServerlessApiClient,
44
} from '@twilio-labs/serverless-api';
55
import chalk from 'chalk';
66
import debug from 'debug';
7-
import dotenv from 'dotenv';
87
import ora, { Ora } from 'ora';
9-
import path from 'path';
10-
import { Arguments, Argv } from 'yargs';
8+
import { Argv } from 'yargs';
119
import { checkConfigForCredentials } from '../checks/check-credentials';
12-
import checkForValidServiceSid from '../checks/check-service-sid';
13-
import { getFunctionServiceSid } from '../serverless-api/utils';
14-
import { fileExists, readFile } from '../utils/fs';
15-
import { sharedCliOptions, SharedFlags } from './shared';
10+
import { ActivateCliFlags, getConfigFromFlags } from '../config/activate';
11+
import { sharedCliOptions } from './shared';
1612
import { CliInfo } from './types';
1713

1814
const log = debug('twilio-run:activate');
1915

20-
type ActivateConfig = ApiActivateConfig & {
21-
cwd: string;
22-
accountSid?: string;
23-
authToken?: string;
24-
};
25-
26-
export type ActivateCliFlags = Arguments<
27-
SharedFlags & {
28-
cwd?: string;
29-
serviceSid?: string;
30-
buildSid?: string;
31-
sourceEnvironment?: string;
32-
environment: string;
33-
createEnvironment: boolean;
34-
force: boolean;
35-
env?: string;
36-
}
37-
> & {
38-
_cliDefault?: {
39-
username: string;
40-
password: string;
41-
};
42-
};
43-
44-
async function getConfigFromFlags(
45-
flags: ActivateCliFlags
46-
): Promise<ActivateConfig> {
47-
const cwd = flags.cwd ? path.resolve(flags.cwd) : process.cwd();
48-
let { accountSid: rawAccountSid, authToken: rawAuthToken } = flags;
49-
50-
let accountSid = '';
51-
if (typeof rawAccountSid === 'string') {
52-
accountSid = rawAccountSid;
53-
}
54-
55-
let authToken = '';
56-
if (typeof rawAuthToken === 'string') {
57-
authToken = rawAuthToken;
58-
}
59-
60-
if (!accountSid || !authToken) {
61-
const envPath = path.resolve(cwd, flags.env || '.env');
62-
let contentEnvFile;
63-
if (!(await fileExists(envPath))) {
64-
contentEnvFile = '';
65-
} else {
66-
contentEnvFile = await readFile(envPath, 'utf8');
67-
}
68-
69-
const localEnv = dotenv.parse(contentEnvFile);
70-
accountSid =
71-
accountSid ||
72-
localEnv.ACCOUNT_SID ||
73-
(flags._cliDefault && flags._cliDefault.username) ||
74-
'';
75-
authToken =
76-
authToken ||
77-
localEnv.AUTH_TOKEN ||
78-
(flags._cliDefault && flags._cliDefault.password) ||
79-
'';
80-
}
81-
82-
const readServiceSid =
83-
flags.serviceSid ||
84-
(await getFunctionServiceSid(cwd, flags.config, 'activateConfig'));
85-
86-
const serviceSid = checkForValidServiceSid(readServiceSid);
87-
88-
return {
89-
cwd,
90-
accountSid,
91-
authToken,
92-
serviceSid,
93-
force: flags.force,
94-
createEnvironment: flags.createEnvironment,
95-
buildSid: flags.buildSid,
96-
targetEnvironment: flags.environment,
97-
sourceEnvironment: flags.sourceEnvironment,
98-
};
99-
}
100-
10116
function logError(msg: string) {
10217
console.error(chalk`{red.bold ERROR} ${msg}`);
10318
}

src/commands/deploy.ts

+11-120
Original file line numberDiff line numberDiff line change
@@ -5,140 +5,25 @@ import {
55
import chalk from 'chalk';
66
import { stripIndent } from 'common-tags';
77
import debug from 'debug';
8-
import dotenv from 'dotenv';
98
import ora, { Ora } from 'ora';
109
import path from 'path';
11-
import { Arguments, Argv } from 'yargs';
10+
import { Argv } from 'yargs';
1211
import { checkConfigForCredentials } from '../checks/check-credentials';
1312
import checkProjectStructure from '../checks/project-structure';
13+
import { DeployCliFlags, getConfigFromFlags } from '../config/deploy';
1414
import { printConfigInfo, printDeployedResources } from '../printers/deploy';
1515
import { errorMessage } from '../printers/utils';
1616
import {
1717
ApiErrorResponse,
18-
getFunctionServiceSid,
1918
HttpError,
2019
saveLatestDeploymentData,
2120
} from '../serverless-api/utils';
22-
import { EnvironmentVariablesWithAuth } from '../types/generic';
23-
import { fileExists, readFile } from '../utils/fs';
24-
import { sharedCliOptions, SharedFlags } from './shared';
21+
import { sharedCliOptions } from './shared';
2522
import { CliInfo } from './types';
26-
import { deprecateProjectName, getFullCommand } from './utils';
23+
import { getFullCommand } from './utils';
2724

2825
const log = debug('twilio-run:deploy');
2926

30-
export type DeployCliFlags = Arguments<
31-
SharedFlags & {
32-
cwd?: string;
33-
serviceSid?: string;
34-
functionsEnv: string;
35-
projectName?: string;
36-
serviceName?: string;
37-
accountSid?: string;
38-
authToken?: string;
39-
env?: string;
40-
overrideExistingProject: boolean;
41-
force: boolean;
42-
functions: boolean;
43-
assets: boolean;
44-
assetsFolder?: string;
45-
functionsFolder?: string;
46-
}
47-
> & {
48-
_cliDefault?: {
49-
username: string;
50-
password: string;
51-
};
52-
};
53-
54-
async function getConfigFromFlags(
55-
flags: DeployCliFlags
56-
): Promise<DeployLocalProjectConfig> {
57-
const cwd = flags.cwd ? path.resolve(flags.cwd) : process.cwd();
58-
59-
let accountSid = '';
60-
let authToken = '';
61-
let localEnv: EnvironmentVariablesWithAuth = {};
62-
63-
const envPath = path.resolve(cwd, flags.env || '.env');
64-
65-
if (await fileExists(envPath)) {
66-
const contentEnvFile = await readFile(envPath, 'utf8');
67-
localEnv = dotenv.parse(contentEnvFile);
68-
69-
accountSid =
70-
flags.accountSid ||
71-
localEnv.ACCOUNT_SID ||
72-
(flags._cliDefault && flags._cliDefault.username) ||
73-
'';
74-
authToken =
75-
flags.authToken ||
76-
localEnv.AUTH_TOKEN ||
77-
(flags._cliDefault && flags._cliDefault.password) ||
78-
'';
79-
} else if (flags.env) {
80-
throw new Error(`Failed to find .env file at "${envPath}"`);
81-
}
82-
83-
const serviceSid =
84-
flags.serviceSid ||
85-
(await getFunctionServiceSid(cwd, flags.config, 'deployConfig'));
86-
87-
const pkgJsonPath = path.join(cwd, 'package.json');
88-
if (!(await fileExists(pkgJsonPath))) {
89-
throw new Error('Failed to find package.json file');
90-
}
91-
92-
const pkgContent = await readFile(pkgJsonPath, 'utf8');
93-
const pkgJson = JSON.parse(pkgContent);
94-
95-
const env = {
96-
...localEnv,
97-
};
98-
99-
for (let key of Object.keys(env)) {
100-
const val = env[key];
101-
if (typeof val === 'string' && val.length === 0) {
102-
delete env[key];
103-
}
104-
}
105-
106-
delete env.ACCOUNT_SID;
107-
delete env.AUTH_TOKEN;
108-
109-
let serviceName: string | undefined = flags.serviceName || pkgJson.name;
110-
if (typeof flags.projectName !== 'undefined') {
111-
deprecateProjectName();
112-
if (!serviceName) {
113-
serviceName = flags.projectName;
114-
}
115-
}
116-
117-
if (!serviceName) {
118-
throw new Error(
119-
'Please pass --service-name or add a "name" field to your package.json'
120-
);
121-
}
122-
123-
return {
124-
cwd,
125-
envPath,
126-
accountSid,
127-
authToken,
128-
env,
129-
serviceSid,
130-
pkgJson,
131-
overrideExistingService: flags.overrideExistingProject,
132-
force: flags.force,
133-
serviceName,
134-
functionsEnv: flags.functionsEnv,
135-
functionsFolderName: flags.functionsFolder,
136-
assetsFolderName: flags.assetsFolder,
137-
noAssets: !flags.assets,
138-
noFunctions: !flags.functions,
139-
};
140-
}
141-
14227
function logError(msg: string) {
14328
console.error(chalk`{red.bold ERROR} ${msg}`);
14429
}
@@ -246,7 +131,13 @@ export const cliInfo: CliInfo = {
246131
},
247132
'functions-env': {
248133
type: 'string',
249-
describe: 'The environment name you want to use',
134+
describe: 'DEPRECATED: Use --environment instead',
135+
hidden: true,
136+
},
137+
environment: {
138+
type: 'string',
139+
describe:
140+
'The environment name and domain suffix you want to use for your deployment',
250141
default: 'dev',
251142
},
252143
'service-name': {

0 commit comments

Comments
 (0)