Skip to content

Commit

Permalink
Merge branch 'fix-function-package-individually' of github.com:zjye/s…
Browse files Browse the repository at this point in the history
…erverless-plugin-typescript into fix-function-package-individually
  • Loading branch information
zjye-idealhub committed Apr 30, 2019
2 parents d4cdcbe + a217376 commit 5af7c6e
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import * as fs from 'fs-extra'
import * as _ from 'lodash'
import * as globby from 'globby'

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

import { watchFiles } from './watchFiles'
Expand All @@ -14,7 +18,6 @@ const serverlessFolder = '.serverless'
const buildFolder = '.build'

export class TypeScriptPlugin {

private originalServicePath: string
private isWatching: boolean

Expand Down Expand Up @@ -47,7 +50,9 @@ export class TypeScriptPlugin {
const emitedFiles = await this.compileTs()
if (this.isWatching) {
emitedFiles.forEach(filename => {
const module = require.resolve(path.resolve(this.originalServicePath, filename))
const module = require.resolve(
path.resolve(this.originalServicePath, filename)
)
delete require.cache[module]
})
}
Expand All @@ -63,12 +68,20 @@ export class TypeScriptPlugin {

get functions() {
return this.options.function
? { [this.options.function] : this.serverless.service.functions[this.options.function] }
? {
[this.options.function]: this.serverless.service.functions[
this.options.function
]
}
: this.serverless.service.functions
}

get rootFileNames() {
return typescript.extractFileNames(this.originalServicePath, this.serverless.service.provider.name, this.functions)
return typescript.extractFileNames(
this.originalServicePath,
this.serverless.service.provider.name,
this.functions
)
}

prepare() {
Expand All @@ -78,10 +91,13 @@ export class TypeScriptPlugin {
const fn = functions[fnName]
fn.package = fn.package || {
exclude: [],
include: [],
include: []
}
// Add plugin to excluded packages or an empty array if exclude is undefined
fn.package.exclude = _.uniq([...fn.package.exclude || [], 'node_modules/serverless-plugin-typescript'])
fn.package.exclude = _.uniq([
...(fn.package.exclude || []),
'node_modules/serverless-plugin-typescript'
])
}
}

Expand Down Expand Up @@ -119,7 +135,10 @@ export class TypeScriptPlugin {
// Save original service path and functions
this.originalServicePath = this.serverless.config.servicePath
// Fake service path so that serverless will know what to zip
this.serverless.config.servicePath = path.join(this.originalServicePath, buildFolder)
this.serverless.config.servicePath = path.join(
this.originalServicePath,
buildFolder
)
}

const tsconfig = typescript.getTypescriptConfig(
Expand All @@ -142,15 +161,22 @@ export class TypeScriptPlugin {
// Link or copy node_modules and package.json to .build so Serverless can
// exlcude devDeps during packaging
if (!fs.existsSync(outModulesPath)) {
await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction')
await this.linkOrCopy(
path.resolve('node_modules'),
outModulesPath,
'junction'
)
}

if (!fs.existsSync(outPkgPath)) {
await this.linkOrCopy(path.resolve('package.json'), outPkgPath, 'file')
}

// include any "extras" from the "include" section
if (this.serverless.service.package.include && this.serverless.service.package.include.length > 0) {
if (
this.serverless.service.package.include &&
this.serverless.service.package.include.length > 0
) {
const files = await globby(this.serverless.service.package.include)

for (const filename of files) {
Expand All @@ -162,7 +188,10 @@ export class TypeScriptPlugin {
}

if (!fs.existsSync(destFileName)) {
fs.copySync(path.resolve(filename), path.resolve(path.join(buildFolder, filename)))
fs.copySync(
path.resolve(filename),
path.resolve(path.join(buildFolder, filename))
)
}
}
}
Expand All @@ -177,7 +206,7 @@ export class TypeScriptPlugin {
if (this.options.function) {
const fn = this.serverless.service.functions[this.options.function]
const basename = path.basename(fn.package.artifact)
fn.package.artifact = path.join(
fn.package.artifact = path.join(
this.originalServicePath,
serverlessFolder,
path.basename(fn.package.artifact)
Expand All @@ -191,7 +220,9 @@ export class TypeScriptPlugin {
this.serverless.service.functions[name].package.artifact = path.join(
this.originalServicePath,
serverlessFolder,
path.basename(this.serverless.service.functions[name].package.artifact)
path.basename(
this.serverless.service.functions[name].package.artifact
)
)
})
return
Expand Down Expand Up @@ -233,13 +264,12 @@ export class TypeScriptPlugin {
dstPath: string,
type?: 'dir' | 'junction' | 'file'
): Promise<void> {
return fs.symlink(srcPath, dstPath, type)
.catch(error => {
if (error.code === 'EPERM' && error.errno === -4048) {
return fs.copy(srcPath, dstPath)
}
throw error
})
return fs.symlink(srcPath, dstPath, type).catch(error => {
if (error.code === 'EPERM' && error.errno === -4048) {
return fs.copy(srcPath, dstPath)
}
throw error
})
}
}

Expand Down

0 comments on commit 5af7c6e

Please sign in to comment.