Socket
Socket
Sign inDemoInstall

datapackage-init

Package Overview
Dependencies
Maintainers
3
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

datapackage-init - npm Package Compare versions

Comparing version 0.2.1 to 0.2.3

demo/app.js

46

index.js
var fs = require('fs')
, path = require('path')
, jtsinfer = require('jts-infer')
, jtsinfer = require('json-table-schema-infer')
, assert = require('assert')
;
// init datapackage.json on disk
exports.init = function(path_, cb) {
path_ = path_.replace(/datapackage.json$/, '');
var dpjsonPath = path.join(path_, 'datapackage.json');
exports.defaultsForLocalPackage(path_, function(err, dpjson) {
exports.create(path_, function(err, dpjson) {
if (err) {

@@ -20,2 +22,38 @@ cb(err)

// create Data Package JSON
exports.create = function(path_, cb) {
path_ = path_.replace(/datapackage.json$/, '');
var dpjsonPath = path.join(path_, 'datapackage.json');
existing = fs.existsSync(dpjsonPath) ?
existing = JSON.parse(fs.readFileSync(dpjsonPath, 'utf8'))
:
{ resources: [] }
;
exports.defaultsForLocalPackage(path_, function(err, dpjson) {
if (err) {
cb(err)
} else {
// update existing but do not overwrite existing data in it
for(k in dpjson) {
if (k == 'resources') {
var existingResPaths = existing['resources'].map(function(res) {
return res['path']
});
dpjson['resources'].forEach(function(res) {
if (existingResPaths.indexOf(res['path']) === -1) {
existing['resources'].push(res);
}
});
} else {
// if key not present add o/w do nothing
if (!(k in existing)) {
existing[k] = dpjson[k];
}
}
}
cb(err, existing);
}
});
}
exports.simpleDefaults = function() {

@@ -25,5 +63,6 @@ var out = {

"title": '',
"description": '',
"homepage": '',
"version" : '0.1.0',
"license" : 'PDDL-1.0',
"license" : 'ODC-PDDL-1.0',
"resources": []

@@ -48,3 +87,2 @@

});
}

@@ -51,0 +89,0 @@

4

package.json
{
"name": "datapackage-init",
"description": "Initialize and create Data Packages",
"version": "0.2.1",
"version": "0.2.3",
"license": "MIT",

@@ -25,3 +25,3 @@ "author": {

"dependencies": {
"jts-infer": ""
"json-table-schema-infer": ""
},

@@ -28,0 +28,0 @@ "devDependencies": {

@@ -18,3 +18,3 @@ datapackage-init

Following assume you've imported the module as follows:
Start off by requiring the module as follows:

@@ -25,3 +25,3 @@ ```

## init.init
## init

@@ -34,7 +34,19 @@ Create a datapackage.json at the path specified.

* `path`: optional path to where your data package is (will use this to search
* `path`: 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
## create
Create a Data Package JSON and return it in the callback.
```
dpinit.create(path_, callback(err, datapackageJson));
```
* `path`: path to where your data package data is located. This is used to
search for data to add, for an existing datapackage.json to update etc)
## simpleDefaults
Generate simple defaults for a `datapackage.json`

@@ -46,3 +58,3 @@

## init.defaultsForLocalPackage
## defaultsForLocalPackage

@@ -64,1 +76,19 @@ Get defaults based on a local file-system data package

## createResourceEntry
```
dpinit.createResourceEntry(filepath, cb)
```
Create a resource entry in a Data Package for file at `filepath` returning the
data in the callback.
## createResourceEntries
```
dpinit.createResourceEntries(dir, cb)
```
Create a set of resource entries for a Data Package for all suitable files in
`dir` and its child directories.

@@ -25,2 +25,16 @@ var path = require('path')

describe('create', function(){
it('update existing datapackage.json', function(done) {
var path_ = 'test/fixtures/dp1';
dpm.create(path_, function(err, dpjson) {
console.log(dpjson);
assert.equal(dpjson.name, 'dp1-test-it');
assert.equal(dpjson.license, 'ODC-PDDL-1.0');
assert.equal(dpjson.resources.length, 2);
assert.equal(dpjson.resources[1].path, 'data.csv');
done();
});
});
});
describe('defaults', function(){

@@ -31,7 +45,7 @@ it('getDefaults OK', function(done) {

assert.equal(dpjson.version, '0.1.0');
assert.equal(dpjson.license, 'PDDL-1.0');
assert.equal(dpjson.license, 'ODC-PDDL-1.0');
done();
});
it('getDefaultsForFilePath OK', function(done) {
dpm.defaultsForLocalPackage('test/data/dp1', function(err, dpjson) {
dpm.defaultsForLocalPackage('test/fixtures/dp1', function(err, dpjson) {
assert.equal(dpjson.version, '0.1.0');

@@ -49,11 +63,11 @@ assert.equal(dpjson.name, 'dp1');

it('findDataFiles CSV OK', function() {
var out = dpm.findDataFiles('test/data/dp1');
var out = dpm.findDataFiles('test/fixtures/dp1');
assert.deepEqual(['data.csv'], out);
});
it('findDataFiles GeoJSON OK', function() {
var out = dpm.findDataFiles('test/data/dp2');
var out = dpm.findDataFiles('test/fixtures/dp2');
assert.deepEqual(['data.geojson'], out);
});
it('createResourceEntry OK', function(done) {
var fp = 'test/data/dp1/data.csv';
var fp = 'test/fixtures/dp1/data.csv';
dpm.createResourceEntry(fp, function(err, out) {

@@ -86,3 +100,3 @@ var exp = {

it('createResourceEntry GeoJSON OK', function(done) {
var fp = 'test/data/dp2/data.geojson';
var fp = 'test/fixtures/dp2/data.geojson';
dpm.createResourceEntry(fp, function(err, out) {

@@ -101,3 +115,3 @@ var exp = {

it('createResourceEntries OK', function(done) {
dpm.createResourceEntries('test/data/dp1', function(err, out) {
dpm.createResourceEntries('test/fixtures/dp1', function(err, out) {
assert.equal(out.length, 1);

@@ -104,0 +118,0 @@ assert.equal(out[0].name, 'data');

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc