forked from sanity-io/sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportFromFolder.js
40 lines (31 loc) · 1.46 KB
/
importFromFolder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const path = require('path')
const fse = require('fs-extra')
const globby = require('globby')
const getFileUrl = require('file-url')
const debug = require('debug')('sanity:import:folder')
module.exports = async function importFromFolder(fromDir, options, importers) {
debug('Importing from folder %s', fromDir)
const dataFiles = await globby('*.ndjson', {cwd: fromDir, absolute: true})
if (dataFiles.length === 0) {
throw new Error(`No .ndjson file found in ${fromDir}`)
}
if (dataFiles.length > 1) {
throw new Error(`More than one .ndjson file found in ${fromDir} - only one is supported`)
}
const assetMap = await fse.readJson(path.join(fromDir, 'assets.json')).catch(() => ({}))
const dataFile = dataFiles[0]
debug('Importing from file %s', dataFile)
const stream = fse.createReadStream(dataFile)
const images = await globby('images/*', {cwd: fromDir, absolute: true})
const files = await globby('files/*', {cwd: fromDir, absolute: true})
const unreferencedAssets = []
.concat(images.map((imgPath) => `image#${getFileUrl(imgPath, {resolve: false})}`))
.concat(files.map((filePath) => `file#${getFileUrl(filePath, {resolve: false})}`))
debug('Queueing %d assets', unreferencedAssets.length)
const streamOptions = {...options, unreferencedAssets, assetsBase: fromDir, assetMap}
const result = await importers.fromStream(stream, streamOptions, importers)
if (options.deleteOnComplete) {
await fse.remove(fromDir)
}
return result
}