@sanity/export
Advanced tools
Comparing version 0.128.3 to 0.128.5
@@ -8,5 +8,11 @@ 'use strict'; | ||
const path = require('path'); | ||
const crypto = require('crypto'); | ||
const fse = require('fs-extra'); | ||
const miss = require('mississippi'); | ||
const PQueue = require('p-queue'); | ||
var _require = require('lodash'); | ||
const omit = _require.omit; | ||
const pkg = require('../package.json'); | ||
@@ -16,2 +22,3 @@ const requestStream = require('./requestStream'); | ||
const EXCLUDE_PROPS = ['_id', '_type', 'assetId', 'extension', 'mimeType', 'path', 'url']; | ||
const ACTION_REMOVE = 'remove'; | ||
@@ -30,3 +37,3 @@ const ACTION_REWRITE = 'rewrite'; | ||
_this.assetsSeen.set(doc._id, type); | ||
_this.queueAssetDownload(doc, filePath); | ||
_this.queueAssetDownload(doc, filePath, type); | ||
callback(); | ||
@@ -138,2 +145,3 @@ return; | ||
this.assetsSeen = new Map(); | ||
this.assetMap = {}; | ||
this.filesWritten = 0; | ||
@@ -156,3 +164,3 @@ this.queueSize = 0; | ||
this.reject = reject; | ||
this.queue.onIdle().then(resolve); | ||
this.queue.onIdle().then(() => resolve(this.assetMap)); | ||
}); | ||
@@ -173,3 +181,3 @@ } | ||
queueAssetDownload(assetDoc, dstPath) { | ||
queueAssetDownload(assetDoc, dstPath, type) { | ||
if (!assetDoc.url) { | ||
@@ -182,9 +190,11 @@ debug('Asset document "%s" does not have a URL property, skipping', assetDoc._id); | ||
this.queueSize++; | ||
this.queue.add(() => this.downloadAsset(assetDoc.url, dstPath)); | ||
this.queue.add(() => this.downloadAsset(assetDoc, dstPath)); | ||
} | ||
downloadAsset(url, dstPath) { | ||
downloadAsset(assetDoc, dstPath) { | ||
var _this2 = this; | ||
return _asyncToGenerator(function* () { | ||
const url = assetDoc.url; | ||
const headers = { 'User-Agent': `${pkg.name}@${pkg.version}` }; | ||
@@ -208,4 +218,11 @@ const stream = yield requestStream({ url, headers }); | ||
debug('Asset stream ready, writing to filesystem at %s', dstPath); | ||
yield writeStream(path.join(_this2.tmpDir, dstPath), stream); | ||
const hash = yield writeHashedStream(path.join(_this2.tmpDir, dstPath), stream); | ||
const type = assetDoc._type === 'sanity.imageAsset' ? 'image' : 'file'; | ||
const id = `${type}-${hash}`; | ||
const metaProps = omit(assetDoc, EXCLUDE_PROPS); | ||
if (Object.keys(metaProps).length > 0) { | ||
_this2.assetMap[id] = metaProps; | ||
} | ||
_this2.filesWritten++; | ||
@@ -251,5 +268,11 @@ })(); | ||
function writeStream(filePath, stream) { | ||
return new Promise((resolve, reject) => miss.pipe(stream, fse.createWriteStream(filePath), err => { | ||
return err ? reject(err) : resolve(); | ||
function writeHashedStream(filePath, stream) { | ||
const hash = crypto.createHash('sha1'); | ||
const hasher = miss.through((chunk, enc, cb) => { | ||
hash.update(chunk); | ||
cb(null, chunk); | ||
}); | ||
return new Promise((resolve, reject) => miss.pipe(stream, hasher, fse.createWriteStream(filePath), err => { | ||
return err ? reject(err) : resolve(hash.digest('hex')); | ||
})); | ||
@@ -256,0 +279,0 @@ } |
@@ -144,3 +144,4 @@ 'use strict'; | ||
try { | ||
yield assetHandler.finish(); | ||
const assetMap = yield assetHandler.finish(); | ||
archive.append(JSON.stringify(assetMap), { name: 'assets.json', prefix }); | ||
clearInterval(progressInterval); | ||
@@ -147,0 +148,0 @@ } catch (assetErr) { |
{ | ||
"name": "@sanity/export", | ||
"version": "0.128.3", | ||
"version": "0.128.5", | ||
"description": "Export Sanity documents and assets", | ||
@@ -37,3 +37,3 @@ "main": "lib/export.js", | ||
"decompress": "^4.2.0", | ||
"jest": "^22.0.5", | ||
"jest": "^22.1.4", | ||
"rimraf": "^2.6.2", | ||
@@ -40,0 +40,0 @@ "string-to-stream": "^1.1.0" |
const path = require('path') | ||
const crypto = require('crypto') | ||
const fse = require('fs-extra') | ||
const miss = require('mississippi') | ||
const PQueue = require('p-queue') | ||
const {omit} = require('lodash') | ||
const pkg = require('../package.json') | ||
@@ -9,2 +11,3 @@ const requestStream = require('./requestStream') | ||
const EXCLUDE_PROPS = ['_id', '_type', 'assetId', 'extension', 'mimeType', 'path', 'url'] | ||
const ACTION_REMOVE = 'remove' | ||
@@ -20,2 +23,3 @@ const ACTION_REWRITE = 'rewrite' | ||
this.assetsSeen = new Map() | ||
this.assetMap = {} | ||
this.filesWritten = 0 | ||
@@ -38,3 +42,3 @@ this.queueSize = 0 | ||
this.reject = reject | ||
this.queue.onIdle().then(resolve) | ||
this.queue.onIdle().then(() => resolve(this.assetMap)) | ||
}) | ||
@@ -50,3 +54,3 @@ } | ||
this.assetsSeen.set(doc._id, type) | ||
this.queueAssetDownload(doc, filePath) | ||
this.queueAssetDownload(doc, filePath, type) | ||
callback() | ||
@@ -84,3 +88,3 @@ return | ||
queueAssetDownload(assetDoc, dstPath) { | ||
queueAssetDownload(assetDoc, dstPath, type) { | ||
if (!assetDoc.url) { | ||
@@ -93,6 +97,7 @@ debug('Asset document "%s" does not have a URL property, skipping', assetDoc._id) | ||
this.queueSize++ | ||
this.queue.add(() => this.downloadAsset(assetDoc.url, dstPath)) | ||
this.queue.add(() => this.downloadAsset(assetDoc, dstPath)) | ||
} | ||
async downloadAsset(url, dstPath) { | ||
async downloadAsset(assetDoc, dstPath) { | ||
const {url} = assetDoc | ||
const headers = {'User-Agent': `${pkg.name}@${pkg.version}`} | ||
@@ -116,4 +121,11 @@ const stream = await requestStream({url, headers}) | ||
debug('Asset stream ready, writing to filesystem at %s', dstPath) | ||
await writeStream(path.join(this.tmpDir, dstPath), stream) | ||
const hash = await writeHashedStream(path.join(this.tmpDir, dstPath), stream) | ||
const type = assetDoc._type === 'sanity.imageAsset' ? 'image' : 'file' | ||
const id = `${type}-${hash}` | ||
const metaProps = omit(assetDoc, EXCLUDE_PROPS) | ||
if (Object.keys(metaProps).length > 0) { | ||
this.assetMap[id] = metaProps | ||
} | ||
this.filesWritten++ | ||
@@ -198,6 +210,12 @@ } | ||
function writeStream(filePath, stream) { | ||
function writeHashedStream(filePath, stream) { | ||
const hash = crypto.createHash('sha1') | ||
const hasher = miss.through((chunk, enc, cb) => { | ||
hash.update(chunk) | ||
cb(null, chunk) | ||
}) | ||
return new Promise((resolve, reject) => | ||
miss.pipe(stream, fse.createWriteStream(filePath), err => { | ||
return err ? reject(err) : resolve() | ||
miss.pipe(stream, hasher, fse.createWriteStream(filePath), err => { | ||
return err ? reject(err) : resolve(hash.digest('hex')) | ||
}) | ||
@@ -204,0 +222,0 @@ ) |
@@ -132,3 +132,4 @@ const os = require('os') | ||
try { | ||
await assetHandler.finish() | ||
const assetMap = await assetHandler.finish() | ||
archive.append(JSON.stringify(assetMap), {name: 'assets.json', prefix}) | ||
clearInterval(progressInterval) | ||
@@ -135,0 +136,0 @@ } catch (assetErr) { |
36571
881