Skip to content

Commit

Permalink
[pkg][s]: removed export syntax that is not supported in node 8 - refs
Browse files Browse the repository at this point in the history
  • Loading branch information
anuveyatsu committed Aug 9, 2017
1 parent 24bba31 commit a03d40f
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 29 deletions.
18 changes: 13 additions & 5 deletions lib/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const mdTable = require('markdown-table')
const CSV = require('csv-string')
const XLSX = require('xlsx')

export const dumpAscii = async function (resource) {
const dumpAscii = async function (resource) {
const rows = await toArray(await resource.rows())

// Process.stdout.columns not defined when piping so we assume 100
Expand All @@ -26,24 +26,32 @@ export const dumpAscii = async function (resource) {
return table.toString()
}

export const dumpCsv = async function (resource) {
const dumpCsv = async function (resource) {
const rows = await toArray(await resource.rows())
return CSV.stringify(rows)
}

export const dumpMarkdown = async function (resource) {
const dumpMarkdown = async function (resource) {
const rows = await toArray(await resource.rows())
return mdTable(rows)
}

export const dumpXlsx = async function (resource) {
const dumpXlsx = async function (resource) {
const rows = await toArray(await resource.rows())
return XLSX.utils.aoa_to_sheet(rows)
}

export const dumpers = {
const dumpers = {
ascii: dumpAscii,
csv: dumpCsv,
md: dumpMarkdown,
xlsx: dumpXlsx
}

module.exports = {
dumpAscii,
dumpCsv,
dumpMarkdown,
dumpXlsx,
dumpers
}
9 changes: 7 additions & 2 deletions lib/info.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {dumpers} = require('./cat')

export const infoPackage = pkg => {
const infoPackage = pkg => {
let firstParagraphReadme
const readme = pkg.readme || 'No readme is provided'

Expand Down Expand Up @@ -33,7 +33,12 @@ ${readme}
return out
}

export const infoResource = async resource => {
const infoResource = async resource => {
const out = await dumpers.ascii(resource)
return out
}

module.exports = {
infoPackage,
infoResource
}
9 changes: 7 additions & 2 deletions lib/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = require('./utils/config')

const port = 3000

export const login = async function (apiUrl, authUrl) {
const login = async function (apiUrl, authUrl) {
opn(authUrl, {wait: false})
// Now enter a wait loop
const urlWithToken = await runServer()
Expand All @@ -22,7 +22,7 @@ export const login = async function (apiUrl, authUrl) {
}

// Do authentication here: if authenticated returns userInfo, if not returns login providers
export const authenticate = async (apiUrl, token) => {
const authenticate = async (apiUrl, token) => {
const url = `${apiUrl}/auth/check?jwt=${token}&next=http://localhost:${port}`
const res = await fetch(url)
const out = await res.json()
Expand Down Expand Up @@ -52,3 +52,8 @@ const runServer = async function () {
})
})
}

module.exports = {
login,
authenticate
}
5 changes: 4 additions & 1 deletion lib/utils/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const fetch = require('node-fetch')
* @return {Function} fetch
*/

export class Agent {
class Agent {
constructor(url, {tls = true, debug} = {}) {
this._url = url
const parsed = parse(url)
Expand Down Expand Up @@ -82,3 +82,6 @@ export class Agent {
}
}

module.exports = {
Agent
}
31 changes: 22 additions & 9 deletions lib/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const DEFAULT_ENCODING = 'utf-8'
* size and hash are direct properties as they are lazily evaluated (if not already specified)
*/
// TODO: support initializing with data
export class Resource {
class Resource {

static load(pathOrDescriptor, {basePath} = {}) {
let descriptor = null
Expand Down Expand Up @@ -118,7 +118,7 @@ export class Resource {
}
}

export class ResourceLocal extends Resource {
class ResourceLocal extends Resource {
get path() {
return this._basePath ? path.join(this._basePath, this.descriptor.path) : this.descriptor.path
}
Expand All @@ -142,7 +142,7 @@ export class ResourceLocal extends Resource {
}
}

export class ResourceRemote extends Resource {
class ResourceRemote extends Resource {
get path() {
return this._basePath ? urljoin(this._basePath, this.descriptor.path) : this.descriptor.path
}
Expand All @@ -159,7 +159,7 @@ export class ResourceRemote extends Resource {
}
}

export class ResourceInline extends Resource {
class ResourceInline extends Resource {
constructor(descriptor, {basePath} = {}) {
super(descriptor, {basePath})

Expand Down Expand Up @@ -215,7 +215,7 @@ const parserDatabase = {
// List of formats that are known as tabular
const knownTabularFormats = ['csv', 'tsv', 'dsv']

export const parsePath = (path_, basePath = null) => {
const parsePath = (path_, basePath = null) => {
const isItUrl = isUrl(path_) || isUrl(basePath)
// eslint-disable-next-line no-useless-escape
const fileName = path_.replace(/^.*[\\\/]/, '')
Expand All @@ -229,19 +229,19 @@ export const parsePath = (path_, basePath = null) => {
}
}

export const parsePackageIdentifier = path_ => {
const parsePackageIdentifier = path_ => {
return {
path: path_,
type: isUrl(path_) ? 'remote' : 'local'
}
}

export const isUrl = path_ => {
const isUrl = path_ => {
const r = new RegExp('^(?:[a-z]+:)?//', 'i')
return r.test(path_)
}

export const isPackage = path_ => {
const isPackage = path_ => {
// If it is a path to file we assume it is not a Package
// Only exception is 'datapackage.json':
if (path_.endsWith('datapackage.json')) {
Expand Down Expand Up @@ -273,7 +273,7 @@ export const isPackage = path_ => {
*
* Under the hood it stores metadata in data package format.
*/
export class Package {
class Package {
// TODO: handle owner
constructor(descriptor = {}, identifier = {path: null, owner: null}) {
if (!lodash.isPlainObject(descriptor)) {
Expand Down Expand Up @@ -391,3 +391,16 @@ export class Package {
}
}
}


module.exports = {
Resource,
ResourceLocal,
ResourceRemote,
ResourceInline,
parsePath,
parsePackageIdentifier,
isUrl,
isPackage,
Package
}
6 changes: 5 additions & 1 deletion lib/utils/datahub.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {Resource} = require('./data')
// get dedicated auth token for the rawstore
// common error handling for fetch stuff ... (?)

export class DataHub extends EventEmitter {
class DataHub extends EventEmitter {
constructor({apiUrl, token, ownerid, owner, debug = false}) {
super()
this.apiUrl = apiUrl
Expand Down Expand Up @@ -229,3 +229,7 @@ async function responseError(res) {

return err
}

module.exports = {
DataHub
}
9 changes: 7 additions & 2 deletions lib/utils/identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const urljoin = require('url-join')
const identifier = require('datapackage-identifier')

// TODO: 2017-07-08 set this in config and use from there
export const PACKAGE_STORE_URL = 'https://pkg.datahub.io'
const PACKAGE_STORE_URL = 'https://pkg.datahub.io'

export const parseIdentifier = dpId => {
const parseIdentifier = dpId => {
if (dpId && dpId !== '.' && dpId.indexOf('http') === -1) {
const dpIdArray = dpId.split('/')
// Remove `/` in the beginning and in the end
Expand Down Expand Up @@ -59,3 +59,8 @@ export const parseIdentifier = dpId => {
delete idObject.originalType
return idObject
}

module.exports = {
PACKAGE_STORE_URL,
parseIdentifier
}
9 changes: 7 additions & 2 deletions lib/utils/parser/csv.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const parse = require('csv-parse')

export const csvParser = async (resource, keyed = false) => {
const csvParser = async (resource, keyed = false) => {
const stream = await resource.stream()
const parseOptions = getParseOptions(resource.descriptor.dialect, keyed)
return stream.pipe(parse(parseOptions))
}

export const getParseOptions = (dialect, keyed) => {
const getParseOptions = (dialect, keyed) => {
const parseOptions = {
columns: keyed ? true : null,
ltrim: true
Expand All @@ -22,3 +22,8 @@ export const getParseOptions = (dialect, keyed) => {

return parseOptions
}

module.exports = {
csvParser,
getParseOptions
}
6 changes: 5 additions & 1 deletion lib/utils/parser/xlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const parse = require('csv-parse')

const {getParseOptions} = require('./csv')

export const xlsxParser = async (resource, keyed = false) => {
const xlsxParser = async (resource, keyed = false) => {
const buffer = await resource.buffer
const workbook = XLSX.read(buffer, {type: 'buffer'})
// For now we handle only first sheet
Expand All @@ -17,3 +17,7 @@ export const xlsxParser = async (resource, keyed = false) => {
const parseOptions = getParseOptions(resource.descriptor.dialect, keyed)
return stream.pipe(parse(parseOptions))
}

module.exports = {
xlsxParser
}
15 changes: 11 additions & 4 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {Table} = require('tableschema')

const {Resource, isUrl} = require('./utils/data')

export async function validate(descriptor, basePath) {
async function validate(descriptor, basePath) {
try {
await validateMetadata(descriptor)
for (let i = 0; i < descriptor.resources.length; i++) {
Expand All @@ -23,14 +23,14 @@ export async function validate(descriptor, basePath) {
}
}

export async function validateData(schema, absPath) {
async function validateData(schema, absPath) {
// TODO: handle inlined data resources
const table = await Table.load(absPath, {schema})
await table.read()
return true
}

export async function validateMetadata(descriptor) {
async function validateMetadata(descriptor) {
// If descriptor has a profile property then use it
// Else use the latest schema
const defaultProfile = descriptor.profile || 'data-package'
Expand All @@ -42,7 +42,7 @@ export async function validateMetadata(descriptor) {
}

// Profile class extracted from datapackage-js library
export class Profile {
class Profile {

static async load(profile) {
let jsonschema = _cache[profile]
Expand Down Expand Up @@ -113,3 +113,10 @@ export class Profile {
// Internal

const _cache = {}

module.exports = {
validate,
validateData,
validateMetadata,
Profile
}

0 comments on commit a03d40f

Please sign in to comment.