Skip to content

Commit 816cd26

Browse files
committed
fix(*): handle no node_modules directory
1 parent 424fe49 commit 816cd26

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/index.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import { watchFiles } from './watchFiles'
99
const SERVERLESS_FOLDER = '.serverless'
1010
const BUILD_FOLDER = '.build'
1111

12+
// handles broken symlinks
13+
const symlinkExistsSync = (path: string) => {
14+
try {
15+
fs.lstatSync(path);
16+
return true;
17+
} catch (e) {
18+
return false;
19+
}
20+
}
21+
1222
export class TypeScriptPlugin {
1323
private originalServicePath: string
1424
private isWatching: boolean
@@ -191,14 +201,17 @@ export class TypeScriptPlugin {
191201

192202
// copy development dependencies during packaging
193203
if (isPackaging) {
194-
if (fs.existsSync(outModulesPath)) {
204+
// symlinkExistsSync handles the broken symlink case
205+
if (fs.existsSync(outModulesPath) || symlinkExistsSync(outModulesPath)) {
195206
fs.unlinkSync(outModulesPath)
196207
}
197208

198-
fs.copySync(
199-
path.resolve('node_modules'),
200-
path.resolve(path.join(BUILD_FOLDER, 'node_modules'))
201-
)
209+
if (fs.existsSync(path.resolve('node_modules'))) {
210+
fs.copySync(
211+
path.resolve('node_modules'),
212+
outModulesPath
213+
)
214+
}
202215
} else {
203216
if (!fs.existsSync(outModulesPath)) {
204217
await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction')

0 commit comments

Comments
 (0)