Skip to content

Support 'deploy function' command #22

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

Merged
merged 1 commit into from
Jun 20, 2017
Merged
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
33 changes: 21 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'fs-p'
import * as _ from 'lodash'
import * as globby from 'globby'

import { ServerlessOptions, ServerlessInstance } from './types'
import { ServerlessOptions, ServerlessInstance, ServerlessFunction } from './types'
import * as typescript from './typescript'

// Folders
Expand All @@ -13,6 +13,7 @@ const buildFolder = '.build'
class ServerlessPlugin {

private originalServicePath: string
private originalFunctions: { [key: string]: ServerlessFunction } | {}

serverless: ServerlessInstance
options: ServerlessOptions
Expand All @@ -25,8 +26,10 @@ class ServerlessPlugin {

this.hooks = {
'before:offline:start:init': this.beforeCreateDeploymentArtifacts.bind(this),
'before:package:createDeploymentArtifacts': this.beforeCreateDeploymentArtifacts.bind(this),
'after:package:createDeploymentArtifacts': this.afterCreateDeploymentArtifacts.bind(this),
'before:package:createDeploymentArtifacts': this.beforeCreateDeploymentArtifacts.bind(this, 'service'),
'after:package:createDeploymentArtifacts': this.afterCreateDeploymentArtifacts.bind(this, 'service'),
'before:deploy:function:packageFunction': this.beforeCreateDeploymentArtifacts.bind(this, 'function'),
'after:deploy:function:packageFunction': this.afterCreateDeploymentArtifacts.bind(this, 'function'),
'before:invoke:local:invoke': this.beforeCreateDeploymentArtifacts.bind(this),
'after:invoke:local:invoke': this.cleanup.bind(this),
}
Expand Down Expand Up @@ -55,24 +58,27 @@ class ServerlessPlugin {
}
}

async beforeCreateDeploymentArtifacts(): Promise<void> {
async beforeCreateDeploymentArtifacts(type: string): Promise<void> {
this.serverless.cli.log('Compiling with Typescript...')

// Save original service path and functions
this.originalServicePath = this.serverless.config.servicePath
this.originalFunctions = type === 'function'
? _.pick(this.serverless.service.functions, [this.options.function])
: this.serverless.service.functions

// Fake service path so that serverless will know what to zip
this.serverless.config.servicePath = path.join(this.originalServicePath, buildFolder)

const tsFileNames = typescript.extractFileNames(this.serverless.service.functions)
const tsFileNames = typescript.extractFileNames(this.originalFunctions)
const tsconfig = typescript.getTypescriptConfig(this.originalServicePath)

for (const fnName in this.serverless.service.functions) {
const fn = this.serverless.service.functions[fnName]
for (const fnName in this.originalFunctions) {
const fn = this.originalFunctions[fnName]
fn.package = fn.package || {
exclude: [],
include: [],
}
exclude: [],
include: [],
}
fn.package.exclude = _.uniq([...fn.package.exclude, 'node_modules/serverless-plugin-typescript'])
}

Expand Down Expand Up @@ -104,14 +110,17 @@ class ServerlessPlugin {
}
}

async afterCreateDeploymentArtifacts(): Promise<void> {
async afterCreateDeploymentArtifacts(type: string): Promise<void> {
// Copy .build to .serverless
await fs.copy(
path.join(this.originalServicePath, buildFolder, serverlessFolder),
path.join(this.originalServicePath, serverlessFolder)
)

this.serverless.service.package.artifact = path.join(this.originalServicePath, serverlessFolder, path.basename(this.serverless.service.package.artifact))
const basename = type === 'function'
? path.basename(this.originalFunctions[this.options.function].artifact)
: path.basename(this.serverless.service.package.artifact)
this.serverless.service.package.artifact = path.join(this.originalServicePath, serverlessFolder, basename)

// Cleanup after everything is copied
await this.cleanup()
Expand Down