-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Prompt for dataset on import/export (#337)
- Loading branch information
Showing
8 changed files
with
191 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/@sanity/core/src/actions/dataset/chooseDatasetPrompt.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import debug from '../../debug' | ||
import promptForDatasetName from './datasetNamePrompt' | ||
|
||
module.exports = async (context, options = {}) => { | ||
const {apiClient, prompt} = context | ||
const {message, allowCreation} = options | ||
const client = apiClient() | ||
|
||
const datasets = await client.datasets.list() | ||
const hasProduction = datasets.find(dataset => dataset.name === 'production') | ||
const datasetChoices = datasets.map(dataset => ({value: dataset.name})) | ||
const selected = await prompt.single({ | ||
message: message || 'Select dataset to use', | ||
type: 'list', | ||
choices: allowCreation | ||
? [{value: 'new', name: 'Create new dataset'}, new prompt.Separator(), ...datasetChoices] | ||
: datasetChoices | ||
}) | ||
|
||
if (selected === 'new') { | ||
debug('User wants to create a new dataset, prompting for name') | ||
const newDatasetName = await promptForDatasetName(prompt, { | ||
message: 'Name your dataset:', | ||
default: hasProduction ? undefined : 'production' | ||
}) | ||
await client.datasets.create(newDatasetName) | ||
return newDatasetName | ||
} | ||
|
||
return selected | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import got from 'got' | ||
import simpleGet from 'simple-get' | ||
|
||
export default (client, dataset) => { | ||
// Sanity client doesn't handle streams natively since we want to support node/browser | ||
// with same API. We're just using it here to get hold of URLs and tokens. | ||
const url = client.getUrl(`/data/export/${dataset}`) | ||
return got.stream(url, { | ||
headers: {Authorization: `Bearer ${client.config().token}`} | ||
const headers = {Authorization: `Bearer ${client.config().token}`} | ||
return new Promise((resolve, reject) => { | ||
simpleGet({url, headers}, (err, res) => (err ? reject(err) : resolve(res))) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import fse from 'fs-extra' | ||
import path from 'path' | ||
import os from 'os' | ||
|
||
export async function pathIsEmpty(dir) { | ||
try { | ||
const content = await fse.readdir(absolutify(dir)) | ||
return content.length === 0 | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return true | ||
} | ||
|
||
throw err | ||
} | ||
} | ||
|
||
export function expandHome(filePath) { | ||
if (filePath.charCodeAt(0) === 126 /* ~ */) { | ||
if (filePath.charCodeAt(1) === 43 /* + */) { | ||
return path.join(process.cwd(), filePath.slice(2)) | ||
} | ||
|
||
const home = os.homedir() | ||
return home ? path.join(home, filePath.slice(1)) : filePath | ||
} | ||
|
||
return filePath | ||
} | ||
|
||
export function absolutify(dir) { | ||
const pathName = expandHome(dir) | ||
return path.isAbsolute(pathName) | ||
? pathName | ||
: path.resolve(process.cwd(), pathName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters