Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(templating): introduce postinstall hook #106

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions __tests__/templating/hooks/run-hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
jest.unmock('twilio');
import { stripIndent } from 'common-tags';
import { runHook } from '../../../src/templating/hooks/run-hook';

const EXAMPLE_HOOK = stripIndent`
async function postinstall(client, env) {
const services = await client.serverless.services.list();
return {
env: {
SERVICE_SID: services[0].sid
}
}
}
module.exports = { postinstall }
`;

describe('runPostInstallHook', () => {
test('runs', async () => {
const output = await runHook(
'postinstall',
EXAMPLE_HOOK,
{
DATABASE_URL: 'http://localhost:3000',
},
console
);
expect(true).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"title": "^3.4.1",
"twilio": "^3.33.0",
"type-fest": "^0.6.0",
"vm2": "^3.8.4",
"window-size": "^1.1.1",
"wrap-ansi": "^5.1.0",
"yargs": "^13.2.2"
Expand Down
10 changes: 8 additions & 2 deletions src/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import { Arguments, Argv } from 'yargs';
import checkProjectStructure from '../checks/project-structure';
import { downloadTemplate, fetchListOfTemplates } from '../templating/actions';
import { setLogLevelByName } from '../utils/logger';
import { baseCliOptions, BaseFlags, ExternalCliOptions } from './shared';
import {
baseCliOptions,
ExternalCliOptions,
SharedFlagsWithCredentials,
} from './shared';
import { CliInfo } from './types';
import { getFullCommand } from './utils';

export type NewCliFlags = Arguments<
BaseFlags & {
SharedFlagsWithCredentials & {
namespace?: string;
template?: string;
}
Expand All @@ -20,6 +24,8 @@ export type NewCliFlags = Arguments<
export type NewConfig = Merge<
NewCliFlags,
{
cwd?: string;
env?: string;
namespace?: string;
template?: string;
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type SharedFlags = BaseFlags & {
cwd?: string;
};

export type SharedFlagsWithCrdentials = SharedFlags & {
export type SharedFlagsWithCredentials = SharedFlags & {
accountSid?: string;
authToken?: string;
env?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/config/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import checkForValidServiceSid from '../checks/check-service-sid';
import { cliInfo } from '../commands/activate';
import {
ExternalCliOptions,
SharedFlagsWithCrdentials,
SharedFlagsWithCredentials,
} from '../commands/shared';
import { getFullCommand } from '../commands/utils';
import { readSpecializedConfig } from './global';
Expand All @@ -19,7 +19,7 @@ type ActivateConfig = ApiActivateConfig & {
};

export type ActivateCliFlags = Arguments<
SharedFlagsWithCrdentials & {
SharedFlagsWithCredentials & {
cwd?: string;
serviceSid?: string;
buildSid?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/config/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Arguments } from 'yargs';
import { cliInfo } from '../commands/deploy';
import {
ExternalCliOptions,
SharedFlagsWithCrdentials,
SharedFlagsWithCredentials,
} from '../commands/shared';
import { deprecateFunctionsEnv } from '../commands/utils';
import { getFunctionServiceSid } from '../serverless-api/utils';
Expand All @@ -18,7 +18,7 @@ import {
import { mergeFlagsAndConfig } from './utils/mergeFlagsAndConfig';

export type DeployCliFlags = Arguments<
SharedFlagsWithCrdentials & {
SharedFlagsWithCredentials & {
serviceSid?: string;
functionsEnv?: string;
environment: string;
Expand Down
4 changes: 2 additions & 2 deletions src/config/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Arguments } from 'yargs';
import { cliInfo } from '../commands/list';
import {
ExternalCliOptions,
SharedFlagsWithCrdentials,
SharedFlagsWithCredentials,
} from '../commands/shared';
import { getFunctionServiceSid } from '../serverless-api/utils';
import { readSpecializedConfig } from './global';
Expand All @@ -21,7 +21,7 @@ export type ListConfig = ApiListConfig & {
};

export type ListCliFlags = Arguments<
SharedFlagsWithCrdentials & {
SharedFlagsWithCredentials & {
types: string;
projectName?: string;
serviceName?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/config/utils/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
ExternalCliOptions,
SharedFlagsWithCrdentials,
SharedFlagsWithCredentials,
} from '../../commands/shared';
import { getDebugFunction } from '../../utils/logger';
import { readLocalEnvFile } from './env';
Expand All @@ -24,7 +24,7 @@ export type Credentials = {
* @param externalCliOptions Any external information for example passed by the Twilio CLI
*/
export async function getCredentialsFromFlags<
T extends SharedFlagsWithCrdentials
T extends SharedFlagsWithCredentials
>(flags: T, externalCliOptions?: ExternalCliOptions): Promise<Credentials> {
// default Twilio CLI credentials (4) or empty string (5)
let accountSid =
Expand Down
3 changes: 3 additions & 0 deletions src/templating/hooks/HookError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class HookError extends Error {
name = 'HookError';
}
11 changes: 11 additions & 0 deletions src/templating/hooks/postinstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EnvironmentVariablesWithAuth } from '../../types/generic';
import { ILogger } from '../../utils/logger';
import { runHook } from './run-hook';

export async function executePostInstallHook(
rawScript: string,
env: EnvironmentVariablesWithAuth,
logger: Console | ILogger
) {
return runHook('postinstall', rawScript, env, logger);
}
46 changes: 46 additions & 0 deletions src/templating/hooks/run-hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as Twilio from 'twilio';
import { NodeVM, VMScript } from 'vm2';
import { EnvironmentVariablesWithAuth } from '../../types/generic';
import { getDebugFunction, ILogger } from '../../utils/logger';
import { HookError } from './HookError';

const debug = getDebugFunction('twilio-run:templating:hooks:run');

export async function runHook(
hookName: string,
rawScript: string,
env: EnvironmentVariablesWithAuth,
logger: Console | ILogger
) {
const vm = new NodeVM({
console: 'off',
sandbox: {
Twilio: Twilio,
console: logger,
process: {
env: env,
},
},
require: {
external: false,
builtin: [],
root: process.cwd(),
},
});
const client = new Twilio.Twilio(
process.env.TWILIO_ACCOUNT_SID || '',
process.env.TWILIO_AUTH_TOKEN || ''
);
const script = new VMScript(rawScript);
const { [hookName]: hookFunction } = vm.run(script);

try {
const output = await hookFunction(client, env);
return output;
} catch (err) {
debug('%O', err);
throw new HookError(
'Hook failed running. Please file an issue at github.com/twilio-labs/function-templates'
);
}
}