Skip to content

Commit

Permalink
[core] Allow importing datasets from directory
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Apr 9, 2018
1 parent 97564ec commit 37bc881
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
1 change: 0 additions & 1 deletion packages/@sanity/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"fs-extra": "^5.0.0",
"get-uri": "^2.0.1",
"json-lexer": "^1.1.1",
"linecount": "^1.0.1",
"lodash": "^4.17.4",
"path-exists": "^3.0.0",
"pretty-ms": "^2.1.0",
Expand Down
27 changes: 16 additions & 11 deletions packages/@sanity/core/src/commands/dataset/importDatasetCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import fse from 'fs-extra'
import sanityImport from '@sanity/import'
import padStart from 'lodash/padStart'
import prettyMs from 'pretty-ms'
import linecount from 'linecount/promise'
import chooseDatasetPrompt from '../../actions/dataset/chooseDatasetPrompt'
import debug from '../../debug'

Expand Down Expand Up @@ -44,16 +43,22 @@ export default {
debug(`Target dataset has been set to "${targetDataset}"`)

const isUrl = /^https?:\/\//i.test(file)
const sourceFile = isUrl ? file : path.resolve(process.cwd(), file)
const inputSource = isUrl ? getUrlStream(sourceFile) : fse.createReadStream(sourceFile)
const inputStream = await inputSource
let inputStream
let sourceIsFolder = false

if (isUrl) {
debug('Input is a URL, streaming from source URL')
inputStream = await getUrlStream(file)
} else {
const sourceFile = path.resolve(process.cwd(), file)
const fileStats = await fse.stat(sourceFile).catch(() => null)
if (!fileStats) {
throw new Error(`${sourceFile} does not exist or is not readable`)
}

const documentCount = isUrl ? 0 : await linecount(sourceFile)
debug(
documentCount
? 'Could not count documents in source'
: `Found ${documentCount} lines in source file`
)
sourceIsFolder = fileStats.isDirectory()
inputStream = sourceIsFolder ? sourceFile : await fse.createReadStream(sourceFile)
}

const importClient = client.clone().config({dataset: targetDataset})

Expand Down Expand Up @@ -116,7 +121,7 @@ export default {
})
currentProgress.text = `[100%] ${currentStep} (${timeSpent})`
currentProgress.succeed()
} else {
} else if (currentProgress) {
currentProgress.fail()
}
}
Expand Down

0 comments on commit 37bc881

Please sign in to comment.