Skip to content

Commit 027b98a

Browse files
serverless#170, serverless#219 - Replaced unlinkSync to removeSync to remove node_modules when packaging serverless project
1 parent 0c2025e commit 027b98a

File tree

1 file changed

+54
-25
lines changed

1 file changed

+54
-25
lines changed

Diff for: src/index.ts

+54-25
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ export class TypeScriptPlugin {
6060
await this.copyExtras()
6161
await this.copyDependencies()
6262
if (this.isWatching) {
63-
emitedFiles.forEach(filename => {
64-
const module = require.resolve(path.resolve(this.originalServicePath, filename))
63+
emitedFiles.forEach((filename) => {
64+
const module = require.resolve(
65+
path.resolve(this.originalServicePath, filename)
66+
)
6567
delete require.cache[module]
6668
})
6769
}
@@ -71,7 +73,7 @@ export class TypeScriptPlugin {
7173
this.watchFunction()
7274
this.serverless.cli.log('Waiting for changes...')
7375
}
74-
}
76+
},
7577
}
7678
}
7779

@@ -81,7 +83,7 @@ export class TypeScriptPlugin {
8183

8284
if (options.function) {
8385
return {
84-
[options.function]: service.functions[this.options.function]
86+
[options.function]: service.functions[this.options.function],
8587
}
8688
}
8789

@@ -103,11 +105,14 @@ export class TypeScriptPlugin {
103105
fn.package = fn.package || {
104106
exclude: [],
105107
include: [],
106-
patterns: []
108+
patterns: [],
107109
}
108110

109111
// Add plugin to excluded packages or an empty array if exclude is undefined
110-
fn.package.exclude = _.uniq([...fn.package.exclude || [], 'node_modules/serverless-plugin-typescript'])
112+
fn.package.exclude = _.uniq([
113+
...(fn.package.exclude || []),
114+
'node_modules/serverless-plugin-typescript',
115+
])
111116
}
112117
}
113118

@@ -132,7 +137,11 @@ export class TypeScriptPlugin {
132137
this.serverless.cli.log(`Watching typescript files...`)
133138

134139
this.isWatching = true
135-
watchFiles(this.rootFileNames, this.originalServicePath, this.compileTs.bind(this))
140+
watchFiles(
141+
this.rootFileNames,
142+
this.originalServicePath,
143+
this.compileTs.bind(this)
144+
)
136145
}
137146

138147
async compileTs(): Promise<string[]> {
@@ -143,14 +152,19 @@ export class TypeScriptPlugin {
143152
// Save original service path and functions
144153
this.originalServicePath = this.serverless.config.servicePath
145154
// Fake service path so that serverless will know what to zip
146-
this.serverless.config.servicePath = path.join(this.originalServicePath, BUILD_FOLDER)
155+
this.serverless.config.servicePath = path.join(
156+
this.originalServicePath,
157+
BUILD_FOLDER
158+
)
147159
}
148160
let tsConfigFileLocation: string | undefined
149161
if (
150-
this.serverless.service.custom !== undefined
151-
&& this.serverless.service.custom.serverlessPluginTypescript !== undefined
162+
this.serverless.service.custom !== undefined &&
163+
this.serverless.service.custom.serverlessPluginTypescript !== undefined
152164
) {
153-
tsConfigFileLocation = this.serverless.service.custom.serverlessPluginTypescript.tsConfigFileLocation
165+
tsConfigFileLocation =
166+
this.serverless.service.custom.serverlessPluginTypescript
167+
.tsConfigFileLocation
154168
}
155169
const tsconfig = typescript.getTypescriptConfig(
156170
this.originalServicePath,
@@ -169,7 +183,10 @@ export class TypeScriptPlugin {
169183
async copyExtras() {
170184
const { service } = this.serverless
171185

172-
const patterns = [...(service.package.include || []), ...(service.package.patterns || [])]
186+
const patterns = [
187+
...(service.package.include || []),
188+
...(service.package.patterns || []),
189+
]
173190
// include any "extras" from the "include" section
174191
if (patterns.length > 0) {
175192
const files = await globby(patterns)
@@ -183,7 +200,10 @@ export class TypeScriptPlugin {
183200
}
184201

185202
if (!fs.existsSync(destFileName)) {
186-
fs.copySync(path.resolve(filename), path.resolve(path.join(BUILD_FOLDER, filename)))
203+
fs.copySync(
204+
path.resolve(filename),
205+
path.resolve(path.join(BUILD_FOLDER, filename))
206+
)
187207
}
188208
}
189209
}
@@ -196,12 +216,14 @@ export class TypeScriptPlugin {
196216
*/
197217
async copyDependencies(isPackaging = false) {
198218
const outPkgPath = path.resolve(path.join(BUILD_FOLDER, 'package.json'))
199-
const outModulesPath = path.resolve(path.join(BUILD_FOLDER, 'node_modules'))
219+
const outModulesPath = path.resolve(
220+
path.join(BUILD_FOLDER, 'node_modules')
221+
)
200222

201223
// copy development dependencies during packaging
202224
if (isPackaging) {
203225
if (fs.existsSync(outModulesPath)) {
204-
fs.unlinkSync(outModulesPath)
226+
fs.removeSync(outModulesPath)
205227
}
206228

207229
fs.copySync(
@@ -210,7 +232,11 @@ export class TypeScriptPlugin {
210232
)
211233
} else {
212234
if (!fs.existsSync(outModulesPath)) {
213-
await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction')
235+
await this.linkOrCopy(
236+
path.resolve('node_modules'),
237+
outModulesPath,
238+
'junction'
239+
)
214240
}
215241
}
216242

@@ -244,7 +270,7 @@ export class TypeScriptPlugin {
244270

245271
if (service.package.individually) {
246272
const functionNames = service.getAllFunctions()
247-
functionNames.forEach(name => {
273+
functionNames.forEach((name) => {
248274
service.functions[name].package.artifact = path.join(
249275
this.originalServicePath,
250276
SERVERLESS_FOLDER,
@@ -273,14 +299,17 @@ export class TypeScriptPlugin {
273299
* Attempt to symlink a given path or directory and copy if it fails with an
274300
* `EPERM` error.
275301
*/
276-
private async linkOrCopy(srcPath: string, dstPath: string, type?: fs.FsSymlinkType): Promise<void> {
277-
return fs.symlink(srcPath, dstPath, type)
278-
.catch(error => {
279-
if (error.code === 'EPERM' && error.errno === -4048) {
280-
return fs.copy(srcPath, dstPath)
281-
}
282-
throw error
283-
})
302+
private async linkOrCopy(
303+
srcPath: string,
304+
dstPath: string,
305+
type?: fs.FsSymlinkType
306+
): Promise<void> {
307+
return fs.symlink(srcPath, dstPath, type).catch((error) => {
308+
if (error.code === 'EPERM' && error.errno === -4048) {
309+
return fs.copy(srcPath, dstPath)
310+
}
311+
throw error
312+
})
284313
}
285314
}
286315

0 commit comments

Comments
 (0)