Skip to content

Commit 5e36866

Browse files
authored
Some refactoring (#562)
* refactor: remove unnecessary 'else' * refactor: change _buildAndArchive function to async-await
1 parent 93bb55a commit 5e36866

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

lib/main.js

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -530,28 +530,27 @@ Emulate only the body of the API Gateway event.
530530
resolve()
531531
})
532532
})
533-
} else {
534-
return new Promise((resolve, reject) => {
535-
fs.readdir(codeDirectory, (err, files) => {
536-
if (err) return reject(err)
533+
}
534+
return new Promise((resolve, reject) => {
535+
fs.readdir(codeDirectory, (err, files) => {
536+
if (err) return reject(err)
537537

538-
Promise.all(files.map(file => {
539-
return new Promise((resolve, reject) => {
540-
if (keepNodeModules && file === 'node_modules') {
538+
Promise.all(files.map(file => {
539+
return new Promise((resolve, reject) => {
540+
if (keepNodeModules && file === 'node_modules') {
541+
resolve()
542+
} else {
543+
fs.remove(path.join(codeDirectory, file), err => {
544+
if (err) return reject(err)
541545
resolve()
542-
} else {
543-
fs.remove(path.join(codeDirectory, file), err => {
544-
if (err) return reject(err)
545-
resolve()
546-
})
547-
}
548-
})
549-
})).then(() => {
550-
resolve()
546+
})
547+
}
551548
})
549+
})).then(() => {
550+
resolve()
552551
})
553552
})
554-
}
553+
})
555554
}
556555

557556
_setRunTimeEnvironmentVars (program) {
@@ -664,7 +663,7 @@ Emulate only the body of the API Gateway event.
664663
})
665664
}
666665

667-
_buildAndArchive (program) {
666+
async _buildAndArchive (program) {
668667
if (!fs.existsSync('.env')) {
669668
console.warn('[Warning] `.env` file does not exist.')
670669
console.info('Execute `node-lambda setup` as necessary and set it up.')
@@ -683,24 +682,16 @@ they may not work as expected in the Lambda environment.
683682
const codeDirectory = this._codeDirectory()
684683
const lambdaSrcDirectory = program.sourceDirectory ? program.sourceDirectory.replace(/\/$/, '') : '.'
685684

686-
return Promise.resolve().then(() => {
687-
return this._cleanDirectory(codeDirectory, program.keepNodeModules)
688-
}).then(() => {
689-
console.log('=> Moving files to temporary directory')
690-
return this._fileCopy(program, lambdaSrcDirectory, codeDirectory, true)
691-
}).then(() => {
692-
if (program.keepNodeModules) {
693-
return Promise.resolve()
694-
} else {
695-
console.log(`=> Running npm ${this._shouldUseNpmCi(codeDirectory) ? 'ci' : 'install'} --production`)
696-
return this._npmInstall(program, codeDirectory)
697-
}
698-
}).then(() => {
699-
return this._postInstallScript(program, codeDirectory)
700-
}).then(() => {
701-
console.log('=> Zipping deployment package')
702-
return this._zip(program, codeDirectory)
703-
})
685+
await this._cleanDirectory(codeDirectory, program.keepNodeModules)
686+
console.log('=> Moving files to temporary directory')
687+
await this._fileCopy(program, lambdaSrcDirectory, codeDirectory, true)
688+
if (!program.keepNodeModules) {
689+
console.log(`=> Running npm ${this._shouldUseNpmCi(codeDirectory) ? 'ci' : 'install'} --production`)
690+
await this._npmInstall(program, codeDirectory)
691+
}
692+
await this._postInstallScript(program, codeDirectory)
693+
console.log('=> Zipping deployment package')
694+
return this._zip(program, codeDirectory)
704695
}
705696

706697
_listEventSourceMappings (lambda, params) {

0 commit comments

Comments
 (0)