Skip to content

Commit defc15f

Browse files
committed
[push][m]: set dataset name and title while pushing single file - refs #204
* Check if `name` property is provided * YES - use it as name * NO - prompt user to confirm auto generated name (default) or he should type new one * Construct title from name and confirm with user
1 parent 6fc2511 commit defc15f

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

bin/data-push.js

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ const prepareDatasetFromFile = async filePath => {
129129
// Prompt user with headers and fieldTypes
130130
const headers = file.descriptor.schema.fields.map(field => field.name)
131131
const fieldTypes = file.descriptor.schema.fields.map(field => field.type)
132-
const questions = [ask('headers', headers), ask('types', fieldTypes)]
132+
const questions = [
133+
ask('headers', headers, 'y', 'yesOrNo'),
134+
ask('types', fieldTypes, 'y', 'yesOrNo')
135+
]
133136
const answers = await inquirer.prompt(questions)
134137

135138
if (answers.headers === 'n' & answers.types === 'n') {
@@ -139,35 +142,58 @@ const prepareDatasetFromFile = async filePath => {
139142
}
140143
}
141144

142-
let dpName = file.descriptor.name.replace(/\s+/g, '-').toLowerCase()
143-
// Add human readable id so that this packge does not conflict with other
144-
// packages (name is coming from the file name which could just be
145-
// data.csv)
146-
dpName += '-' + hri.random()
145+
let dpName, dpTitle
146+
if (argv.name) {
147+
dpName = argv.name
148+
} else {
149+
dpName = file.descriptor.name.replace(/\s+/g, '-').toLowerCase()
150+
// Add human readable id so that this packge does not conflict with other
151+
// packages (name is coming from the file name which could just be
152+
// data.csv)
153+
dpName += '-' + hri.random()
154+
// Confirm dpName with user:
155+
const answer = await inquirer.prompt([ask('name', dpName, dpName, 'nameValidation')])
156+
dpName = answer.name
157+
}
158+
159+
// Make unslugifies version for title:
160+
dpTitle = dpName.replace(/-+/g, ' ')
161+
dpTitle = dpTitle.charAt(0).toUpperCase() + dpTitle.slice(1)
162+
// Confirm title with user:
163+
const answer = await inquirer.prompt([ask('title', dpTitle, dpTitle)])
164+
147165
const metadata = {
148166
name: dpName,
149-
title: '', // TODO: generate from file name (maybe prompt user for it ...)
167+
title: answer.title,
150168
resources: []
151169
}
152170
const dataset = await Dataset.load(metadata)
153171
dataset.addResource(file)
154172
return dataset
155173
}
156174

157-
const ask = (name, data) => {
158-
return {
175+
const validationPatterns = {
176+
yesOrNo: /^[y,n]+$/,
177+
nameValidation: /^([-a-z0-9._\/])+$/
178+
}
179+
180+
const ask = (property, data, defaultValue, validation) => {
181+
const inquirerObj = {
159182
type: 'input',
160-
name,
161-
message: `Are these ${name} correct for this dataset:\n[${data}]\ny/n?`,
183+
name: property,
184+
message: `Please, confirm ${property} for this dataset:\n${data}`,
162185
default: () => {
163-
return 'y'
164-
},
165-
validate: value => {
166-
const pass = value.match(/^[y,n]+$/)
186+
return defaultValue
187+
}
188+
}
189+
if (validation) {
190+
inquirerObj.validate = value => {
191+
const pass = value.match(validationPatterns[validation])
167192
if (pass) {
168193
return true
169194
}
170-
return `Please, provide with following responses 'y' for yes or 'n' for no`
195+
return `Provided value must match following pattern: ${validationPatterns[validation]}`
171196
}
172197
}
198+
return inquirerObj
173199
}

0 commit comments

Comments
 (0)