Skip to content

Commit

Permalink
Merge pull request #51 from digidem/feat/zip
Browse files Browse the repository at this point in the history
feat: zip
  • Loading branch information
tomasciccola authored Mar 7, 2024
2 parents 3775475 + 9c430e7 commit 5bdd16f
Show file tree
Hide file tree
Showing 3 changed files with 2,252 additions and 1,028 deletions.
66 changes: 38 additions & 28 deletions commands/build_lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var fs = require('fs')
var path = require('path')
var run = require('run-series')
var presetsBuilder = require('id-presets-builder')
var tar = require('tar-stream')
var yazl = require('yazl')
var exists = require('fs-exists-sync')
var jsonschema = require('jsonschema')
var pump = require('pump')
Expand Down Expand Up @@ -68,6 +68,19 @@ module.exports = function ({ output, lang, timeout }, sourceDir, { lint } = { li
var pngIcons = results[3]
var translations = results[4]

var zip = new yazl.ZipFile()

const safelyZipBuffer = (bufOrString, name) => {
const buf = typeof bufOrString === 'string'
? Buffer.from(bufOrString)
: bufOrString
try{
zip.addBuffer(buf, name)
}catch(e){
throw new Error(`error zipping file ${name} with error ${e.message}`)
}
}

if (exists(imageryFile)) {
try {
var imagery = fs.readFileSync(imageryFile)
Expand Down Expand Up @@ -120,25 +133,23 @@ module.exports = function ({ output, lang, timeout }, sourceDir, { lint } = { li
return log(log.chalk.bold(log.symbols.ok + ' Presets are valid'))
}

var pack = tar.pack()
pack.on('error', done)
pack.entry({ name: 'presets.json' }, stringify(presets))
pack.entry({ name: 'translations.json' }, stringify(translations))
safelyZipBuffer(stringify(presets), 'presets.json')
safelyZipBuffer(stringify(translations), 'translations.json')
if (svgSprite) {
pack.entry({ name: 'icons.svg' }, svgSprite)
safelyZipBuffer(svgSprite, 'icons.svg')
}
if (pngSprite) {
pack.entry({ name: 'icons.png' }, pngSprite)
pack.entry({ name: 'icons.json' }, JSON.stringify(pngLayout, null, 2))
safelyZipBuffer(pngSprite, 'icons.png')
safelyZipBuffer(JSON.stringify(pngLayout, null, 2), 'icons.json')
}
if (imagery) {
pack.entry({ name: 'imagery.json' }, imagery)
safelyZipBuffer(stringify(imagery), 'imagery.json')
}
if (exists(styleFile)) {
pack.entry({ name: 'style.css' }, fs.readFileSync(styleFile))
safelyZipBuffer(fs.readFileSync(styleFile), 'style.css')
}
if (exists(layersFile)) {
pack.entry({ name: 'layers.json' }, fs.readFileSync(layersFile))
safelyZipBuffer(fs.readFileSync(layersFile), 'layers.json')
}
metadata.name = metadata.name || pak.name
metadata.version = metadata.version || pak.version
Expand All @@ -158,28 +169,27 @@ module.exports = function ({ output, lang, timeout }, sourceDir, { lint } = { li
log.error('Error parsing syncServer:', e.message)
}
}

pack.entry({ name: 'metadata.json' }, stringify(metadata))
safelyZipBuffer(stringify(metadata), 'metadata.json')
if (pngIcons) {
pngIcons.forEach(icon => {
pack.entry({ name: `icons/${icon.filename}` }, icon.png)
safelyZipBuffer(icon.png, `icons/${icon.filename}`)
})
}
pack.entry({ name: 'VERSION' }, pkg.version)
pack.finalize()
safelyZipBuffer(pkg.version, 'VERSION')
var outputStream = output ? fs.createWriteStream(output) : process.stdout
pump(pack, outputStream, err => {
if (err) log.error(`Error writing file ${output}`)
else {
log(
`${log.chalk.bold(
log.symbols.ok + ' Successfully created file'
)} '${log.chalk.italic(output)}' ${log.chalk.gray(
`(total ${Date.now() - start}ms)`
)}`
)
}
})
zip.end()
pump(zip.outputStream, outputStream, err => {
if (err) log.error(`Error writing file ${output}`)
else {
log(
`${log.chalk.bold(
log.symbols.ok + ' Successfully created file'
)} '${log.chalk.italic(output)}' ${log.chalk.gray(
`(total ${Date.now() - start}ms)`
)}`
)
}
})
}
)
}
Expand Down
Loading

0 comments on commit 5bdd16f

Please sign in to comment.