Skip to content

Commit daf6981

Browse files
committed
[init][s]: implemented interactive mode and refactored init module according to changes in Init module in datahub-client - refs #336 and #335
1 parent efe96a7 commit daf6981

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

bin/data-init.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
const fs = require('fs')
55
const path = require('path')
66
const minimist = require('minimist')
7-
const {init} = require('datahub-client').init
7+
const {Init} = require('datahub-client')
88

99
// Ours
1010
const {customMarked} = require('../lib/utils/tools.js')
11+
const info = require('../lib/utils/output/info.js')
1112

1213
const argv = minimist(process.argv.slice(2), {
1314
string: ['init'],
14-
boolean: ['help'],
15+
boolean: ['help', 'interactive'],
1516
alias: {
16-
help: 'h'
17+
help: 'h',
18+
interactive: 'i'
1719
}
1820
})
1921

@@ -27,4 +29,40 @@ if (argv.help) {
2729
process.exit(0)
2830
}
2931

30-
init()
32+
33+
const checkDpIsThere = (path_ = process.cwd()) => {
34+
const files = fs.readdirSync(path_)
35+
return files.indexOf('datapackage.json') > -1
36+
}
37+
38+
39+
(async() => {
40+
41+
const initializer = new Init({interactive: argv.interactive})
42+
// Listen for events:
43+
initializer.on('message', (message) => {
44+
if (message.constructor.name === 'String') {
45+
info(message)
46+
} else {
47+
info(message.name + ': ' + message.status)
48+
}
49+
})
50+
51+
// Get a descriptor generated:
52+
let descriptor = {}
53+
if (checkDpIsThere()) {
54+
descriptor = await initializer.updateDataset()
55+
} else {
56+
descriptor = await initializer.createDataset()
57+
}
58+
// Now save the generated descriptor:
59+
const content = JSON.stringify(descriptor, null, 2)
60+
fs.writeFile('./datapackage.json', content, 'utf8', err => {
61+
if (err) {
62+
throw new Error(err)
63+
}
64+
const cwd = path.join(process.cwd(), 'datapackage.json')
65+
info(`datapackage.json file is saved in ${cwd}`)
66+
})
67+
68+
})()

docs/init.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
2-
# Initialize Data Package
3-
4-
## Usage:
5-
61
```
72
■ data init
83
```
94

5+
Initialize a Data Package in the current working directory.
6+
It will scan the current working directory and nested directories for the files and generate a `datapackage.json`.
7+
108
## Options:
119

1210
```
1311
-h, --help Output usage information
12+
-i, --interactive Run init in interactive mode
1413
```
1514

1615
## Example:

0 commit comments

Comments
 (0)