Skip to content

Commit e6986c0

Browse files
author
Philip Sarin
committed
store file permissions in the zip package archive
1 parent c932778 commit e6986c0

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

utils.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ const globby = require('globby')
55
const { contains, isNil, last, split, equals, not, pick } = require('ramda')
66
const { readFile, createReadStream, createWriteStream } = require('fs-extra')
77
const { utils } = require('@serverless/core')
8+
const fs = require('fs')
9+
const util = require('util')
810

911
const VALID_FORMATS = ['zip', 'tar']
1012
const isValidFormat = (format) => contains(format, VALID_FORMATS)
1113

14+
const augmentWithFileModes = async (files) => {
15+
const promisingStat = util.promisify(fs.stat)
16+
const promises = files.map((file) => (
17+
promisingStat(file.input).then((stats) => (
18+
Object.assign(file, { mode: (stats.mode & 0o777) })
19+
))
20+
))
21+
return Promise.all(promises)
22+
}
23+
1224
const packDir = async (inputDirPath, outputFilePath, include = [], exclude = [], prefix) => {
1325
const format = last(split('.', outputFilePath))
1426

@@ -22,13 +34,22 @@ const packDir = async (inputDirPath, outputFilePath, include = [], exclude = [],
2234
exclude.forEach((excludedItem) => patterns.push(`!${excludedItem}`))
2335
}
2436

25-
const files = (await globby(patterns, { cwd: inputDirPath, dot: true }))
37+
let files = (await globby(patterns, { cwd: inputDirPath, dot: true }))
2638
.sort() // we must sort to ensure correct hash
2739
.map((file) => ({
2840
input: path.join(inputDirPath, file),
2941
output: prefix ? path.join(prefix, file) : file
3042
}))
3143

44+
if (!isNil(include)) {
45+
files = files.concat(include.map((file) => ({
46+
input: file,
47+
output: path.basename(file)
48+
})))
49+
}
50+
51+
files = await augmentWithFileModes(files)
52+
3253
return new Promise((resolve, reject) => {
3354
const output = createWriteStream(outputFilePath)
3455
const archive = archiver(format, {
@@ -40,16 +61,9 @@ const packDir = async (inputDirPath, outputFilePath, include = [], exclude = [],
4061

4162
// we must set the date to ensure correct hash
4263
files.forEach((file) =>
43-
archive.append(createReadStream(file.input), { name: file.output, date: new Date(0) })
64+
archive.append(createReadStream(file.input), { name: file.output, date: new Date(0), mode: file.mode })
4465
)
4566

46-
if (!isNil(include)) {
47-
include.forEach((file) => {
48-
const stream = createReadStream(file)
49-
archive.append(stream, { name: path.basename(file), date: new Date(0) })
50-
})
51-
}
52-
5367
archive.finalize()
5468
})
5569

0 commit comments

Comments
 (0)