Skip to content

Commit 5af7c6e

Browse files
committed
Merge branch 'fix-function-package-individually' of github.com:zjye/serverless-plugin-typescript into fix-function-package-individually
2 parents d4cdcbe + a217376 commit 5af7c6e

File tree

1 file changed

+50
-20
lines changed

1 file changed

+50
-20
lines changed

src/index.ts

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import * as fs from 'fs-extra'
44
import * as _ from 'lodash'
55
import * as globby from 'globby'
66

7-
import { ServerlessOptions, ServerlessInstance, ServerlessFunction } from './types'
7+
import {
8+
ServerlessOptions,
9+
ServerlessInstance,
10+
ServerlessFunction
11+
} from './types'
812
import * as typescript from './typescript'
913

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

1620
export class TypeScriptPlugin {
17-
1821
private originalServicePath: string
1922
private isWatching: boolean
2023

@@ -47,7 +50,9 @@ export class TypeScriptPlugin {
4750
const emitedFiles = await this.compileTs()
4851
if (this.isWatching) {
4952
emitedFiles.forEach(filename => {
50-
const module = require.resolve(path.resolve(this.originalServicePath, filename))
53+
const module = require.resolve(
54+
path.resolve(this.originalServicePath, filename)
55+
)
5156
delete require.cache[module]
5257
})
5358
}
@@ -63,12 +68,20 @@ export class TypeScriptPlugin {
6368

6469
get functions() {
6570
return this.options.function
66-
? { [this.options.function] : this.serverless.service.functions[this.options.function] }
71+
? {
72+
[this.options.function]: this.serverless.service.functions[
73+
this.options.function
74+
]
75+
}
6776
: this.serverless.service.functions
6877
}
6978

7079
get rootFileNames() {
71-
return typescript.extractFileNames(this.originalServicePath, this.serverless.service.provider.name, this.functions)
80+
return typescript.extractFileNames(
81+
this.originalServicePath,
82+
this.serverless.service.provider.name,
83+
this.functions
84+
)
7285
}
7386

7487
prepare() {
@@ -78,10 +91,13 @@ export class TypeScriptPlugin {
7891
const fn = functions[fnName]
7992
fn.package = fn.package || {
8093
exclude: [],
81-
include: [],
94+
include: []
8295
}
8396
// Add plugin to excluded packages or an empty array if exclude is undefined
84-
fn.package.exclude = _.uniq([...fn.package.exclude || [], 'node_modules/serverless-plugin-typescript'])
97+
fn.package.exclude = _.uniq([
98+
...(fn.package.exclude || []),
99+
'node_modules/serverless-plugin-typescript'
100+
])
85101
}
86102
}
87103

@@ -119,7 +135,10 @@ export class TypeScriptPlugin {
119135
// Save original service path and functions
120136
this.originalServicePath = this.serverless.config.servicePath
121137
// Fake service path so that serverless will know what to zip
122-
this.serverless.config.servicePath = path.join(this.originalServicePath, buildFolder)
138+
this.serverless.config.servicePath = path.join(
139+
this.originalServicePath,
140+
buildFolder
141+
)
123142
}
124143

125144
const tsconfig = typescript.getTypescriptConfig(
@@ -142,15 +161,22 @@ export class TypeScriptPlugin {
142161
// Link or copy node_modules and package.json to .build so Serverless can
143162
// exlcude devDeps during packaging
144163
if (!fs.existsSync(outModulesPath)) {
145-
await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction')
164+
await this.linkOrCopy(
165+
path.resolve('node_modules'),
166+
outModulesPath,
167+
'junction'
168+
)
146169
}
147170

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

152175
// include any "extras" from the "include" section
153-
if (this.serverless.service.package.include && this.serverless.service.package.include.length > 0) {
176+
if (
177+
this.serverless.service.package.include &&
178+
this.serverless.service.package.include.length > 0
179+
) {
154180
const files = await globby(this.serverless.service.package.include)
155181

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

164190
if (!fs.existsSync(destFileName)) {
165-
fs.copySync(path.resolve(filename), path.resolve(path.join(buildFolder, filename)))
191+
fs.copySync(
192+
path.resolve(filename),
193+
path.resolve(path.join(buildFolder, filename))
194+
)
166195
}
167196
}
168197
}
@@ -177,7 +206,7 @@ export class TypeScriptPlugin {
177206
if (this.options.function) {
178207
const fn = this.serverless.service.functions[this.options.function]
179208
const basename = path.basename(fn.package.artifact)
180-
fn.package.artifact = path.join(
209+
fn.package.artifact = path.join(
181210
this.originalServicePath,
182211
serverlessFolder,
183212
path.basename(fn.package.artifact)
@@ -191,7 +220,9 @@ export class TypeScriptPlugin {
191220
this.serverless.service.functions[name].package.artifact = path.join(
192221
this.originalServicePath,
193222
serverlessFolder,
194-
path.basename(this.serverless.service.functions[name].package.artifact)
223+
path.basename(
224+
this.serverless.service.functions[name].package.artifact
225+
)
195226
)
196227
})
197228
return
@@ -233,13 +264,12 @@ export class TypeScriptPlugin {
233264
dstPath: string,
234265
type?: 'dir' | 'junction' | 'file'
235266
): Promise<void> {
236-
return fs.symlink(srcPath, dstPath, type)
237-
.catch(error => {
238-
if (error.code === 'EPERM' && error.errno === -4048) {
239-
return fs.copy(srcPath, dstPath)
240-
}
241-
throw error
242-
})
267+
return fs.symlink(srcPath, dstPath, type).catch(error => {
268+
if (error.code === 'EPERM' && error.errno === -4048) {
269+
return fs.copy(srcPath, dstPath)
270+
}
271+
throw error
272+
})
243273
}
244274
}
245275

0 commit comments

Comments
 (0)