datapackage-init
Advanced tools
Comparing version 0.1.0 to 0.2.0
99
index.js
@@ -1,3 +0,2 @@ | ||
var initpkgjson = require('init-package-json') | ||
, fs = require('fs') | ||
var fs = require('fs') | ||
, path = require('path') | ||
@@ -9,24 +8,45 @@ , jtsinfer = require('jts-infer') | ||
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_; | ||
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; | ||
} | ||
if (!cb) cb = function(er) { | ||
if (er) { | ||
console.error('\n' + er.message); | ||
// 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); | ||
}); | ||
var configData = {} | ||
exports.createResourceEntries(dir, function(err, resources) { | ||
configData.resources = resources; | ||
initpkgjson(dir, promptFile, configData, cb); | ||
}); | ||
} | ||
// ======================================================== | ||
// Helpers | ||
// locate potential data files in this directory | ||
@@ -102,2 +122,6 @@ exports.findDataFiles = function(dir) { | ||
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; | ||
@@ -108,1 +132,40 @@ 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 '' | ||
} | ||
} |
{ | ||
"name": "datapackage-init", | ||
"description": "Initialize and create Data Packages", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
@@ -25,8 +25,8 @@ "author": { | ||
"dependencies": { | ||
"init-package-json": "", | ||
"jts-infer": "" | ||
}, | ||
"devDependencies": { | ||
"mocha": "" | ||
"mocha": "", | ||
"rimraf": "" | ||
} | ||
} |
@@ -18,7 +18,15 @@ datapackage-init | ||
Following assume you've imported the module as follows: | ||
``` | ||
var init = require('datapackage-init'); | ||
var dpinit = 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)); | ||
``` | ||
@@ -28,1 +36,26 @@ * `path`: optional path to where your data package is (will use this to search | ||
## 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 | ||
@@ -1,6 +0,45 @@ | ||
var dpm = require('../index') | ||
, util = require('../util') | ||
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(){ | ||
@@ -7,0 +46,0 @@ it('findDataFiles CSV OK', function() { |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Bad dependency semver
QualityPackage has dependencies with an invalid semantic version. This could be a sign of beta, low quality, or unmaintained dependencies.
Found 1 instance in 1 package
13585
1
10
348
3
60
2
1
- Removedinit-package-json@