Skip to content

Commit

Permalink
[#2][l]: substantial refactor to improve and simplify.
Browse files Browse the repository at this point in the history
* new defaults methods to generate default values
* use these from init method
* prompt.js is now obsolete and removed (and also dependency on init-package-json)
  • Loading branch information
rufuspollock committed Jan 18, 2014
1 parent bbb8531 commit 3cee602
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 98 deletions.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,45 @@ npm install datapackage-init

# Usage

Following assume you've imported the module as follows:

```
var dpinit = require('datapackage-init');
```
var init = require('datapackage-init');

init.init(path, callback(err, datapackageJson));
## init.init

Create a datapackage.json at the path specified.

```
dpinit.init(path, callback(err, datapackageJson));
```

* `path`: optional path to where your data package is (will use this to search
for data to add, for an existing datapackage.json to update etc)

## init.simpleDefaults

Generate simple defaults for a `datapackage.json`

```
var defaults = dpinit.simpleDefaults();
```

## init.defaultsForLocalPackage

Get defaults based on a local file-system data package

```
dpinit.defaultsForLocalPackage(path_, cb)
```

* `path_`: path to the data package directory

Defaults here will include things like:

- Generating a name based on the directory
- Generating a description based on a README (if present)
- Searching for data files (csv and geojson at present) and adding them to the
resources

99 changes: 81 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
var initpkgjson = require('init-package-json')
, fs = require('fs')
var fs = require('fs')
, path = require('path')
, jtsinfer = require('jts-infer')
, assert = require('assert')
;

exports.init = function(path_, cb) {
var self = this;
var promptFile = path.join(__dirname, 'prompt.js');
if (typeof path_ === 'string') {
dir = path_;
} else {
var dir = process.cwd();
cb = path_;
}

if (!cb) cb = function(er) {
if (er) {
console.error('\n' + er.message);
var dpjsonPath = path.join(path_, 'datapackage.json');
exports.defaultsForLocalPackage(path_, function(err, dpjson) {
if (err) {
cb(err)
} else {
var fdata = JSON.stringify(dpjson, null, 2);
fs.writeFileSync(dpjsonPath, fdata, {encoding: 'utf8'});
cb(err, dpjson);
}
});
}

exports.simpleDefaults = function() {
var out = {
"name" : 'my-data-package',
"version" : '0.1.0',
"licenses" : [{
type: 'ODC-PDDL',
url: 'http://opendatacommons.org/licenses/pddl/1.0/'
}]
}
return out;
}

var configData = {}
exports.createResourceEntries(dir, function(err, resources) {
configData.resources = resources;
initpkgjson(dir, promptFile, configData, cb);
// get defaults based on a file path (assumed to be directory)
exports.defaultsForLocalPackage = function(path_, cb) {
var dpjson = exports.simpleDefaults();
dpjson.name = path.basename(path_).replace(/^node-|[.-]js$/g, '');
dpjson.description = _getDescriptionFromReadme(path_);
dpjson.repository = _getGitRepo(path_);
exports.createResourceEntries(path_, function(err, resources) {
if (err) {
console.error(err)
}
dpjson.resources = resources;
cb(null, dpjson);
});

}

// ========================================================
// Helpers

// locate potential data files in this directory
exports.findDataFiles = function(dir) {
var dir = dir || '.';
Expand Down Expand Up @@ -98,8 +118,51 @@ exports.createResourceEntries = function(dir, cb) {
}
dataFiles.forEach(function(fp, idx) {
exports.createResourceEntry(fp, function(err, resource) {
// fix path in resource to be relative
if (resource.path) {
resource.path = path.relative(dir, resource.path);
}
resources[idx] = resource;
done();
});
});
}

_getGitRepo = function(dir) {
var path_ = path.join(dir, '.git', 'config');
try {
var gconf = fs.readFileSync(path_).toString()
gconf = gconf.split(/\r?\n/)
var i = gconf.indexOf('[remote "origin"]')
if (i !== -1) {
var u = gconf[i + 1]
if (!u.match(/^\s*url =/)) u = gconf[i + 2]
if (!u.match(/^\s*url =/)) u = null
else u = u.replace(/^\s*url = /, '')
}
if (u && u.match(/^git@github.com:/)) {
u = u.replace(/^git@github.com:/, 'git://github.com/')
}
return u;
}
catch (e) {
}
}

var _getDescriptionFromReadme = function(dir) {
var readmePath = path.join(dir, 'README.md');
try {
var src = fs.readFileSync(readmePath, 'utf8');
var description = src.split('\n').filter(function (line) {
return /\s+/.test(line)
&& !line.trim().match(/^#/)
;
})[0]
.trim()
.replace(/\.$/, '')
;
return description;
} catch(e) {
return ''
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
],
"main": "./index",
"dependencies": {
"init-package-json": "",
"jts-infer": ""
},
"devDependencies": {
"mocha": ""
"mocha": "",
"rimraf": ""
}
}
75 changes: 0 additions & 75 deletions prompt.js

This file was deleted.

4 changes: 4 additions & 0 deletions test/data/dp1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a data package.

Stuff to ignore.

42 changes: 41 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
var dpm = require('../index')
var path = require('path')
, fs = require('fs')
, rimraf = require('rimraf')
, assert = require('assert')
, dpm = require('../index')
;

describe('init', function(){
it('init OK', function(done) {
var path_ = '/tmp/test-data-package-init'
if (fs.existsSync(path_)) {
rimraf.sync(path_);
}
fs.mkdirSync(path_);
var dpjsonPath = path.join(path_, 'datapackage.json');
dpm.init(path_, function(err, dpjson) {
assert(fs.existsSync(dpjsonPath));
var outdp = JSON.parse(fs.readFileSync(dpjsonPath));
assert.equal(outdp.name, 'test-data-package-init');
done();
});
});
});

describe('defaults', function(){
it('getDefaults OK', function(done) {
var dpjson = dpm.simpleDefaults();
assert.equal(dpjson.name, 'my-data-package');
assert.equal(dpjson.version, '0.1.0');
assert.equal(dpjson.licenses[0].type, 'ODC-PDDL');
done();
});
it('getDefaultsForFilePath OK', function(done) {
dpm.defaultsForLocalPackage('test/data/dp1', function(err, dpjson) {
assert.equal(dpjson.version, '0.1.0');
assert.equal(dpjson.name, 'dp1');
assert.equal(dpjson.description, 'This is a data package');
assert.equal(dpjson.resources[0].path, 'data.csv');
// TODO: test git stuff etc
done();
});
});
});

describe('basics', function(){
it('findDataFiles CSV OK', function() {
var out = dpm.findDataFiles('test/data/dp1');
Expand Down

0 comments on commit 3cee602

Please sign in to comment.