-
-
Notifications
You must be signed in to change notification settings - Fork 804
/
Copy pathdatapackage.js
101 lines (90 loc) · 2.24 KB
/
datapackage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Script to automatically populate the `resources` field in the `datapackage.json` file.
*/
'use strict';
// MODULES //
var resolve = require( 'path' ).resolve;
var writeFileSync = require( 'fs' ).writeFileSync;
var replace = require( '@stdlib/string/replace' );
var copy = require( '@stdlib/utils/copy' );
var FILE_LIST = require( './../data/file_list.json' );
var pkg = require( './../datapackage.json' );
// VARIABLES //
var RE_FILENAME_PUNC = /[\/_.]/g; // eslint-disable-line no-useless-escape
var RE_FILENAME = /^(.*)\/(\d{5})\.(.*)\.txt$/;
var RE_SPAM = /spam/;
var RE_HARD_HAM = /hard/;
var json = {
'name': '',
'title': '',
'description': '',
'format': 'json',
'mediatype': 'application/json',
'encoding': 'UTF-8',
'hash': '',
'path': ''
};
var txt = {
'name': '',
'title': '',
'description': '',
'format': 'text',
'mediatype': 'plain/text',
'encoding': 'UTF-8',
'hash': '',
'path': ''
};
// MAIN //
/**
* Main execution sequence.
*
* @private
*/
function main() {
var fpath;
var parts;
var opts;
var tmp;
var f;
var i;
opts = {
'encoding': 'utf8'
};
pkg.resources = [];
for ( i = 0; i < FILE_LIST.length; i++ ) {
f = FILE_LIST[ i ];
parts = RE_FILENAME.exec( f );
if ( !parts ) {
continue;
}
tmp = copy( txt );
tmp.name = replace( f, RE_FILENAME_PUNC, '-' );
tmp.title = parts[ 1 ] + ':' + parts[ 2 ];
if ( RE_SPAM.test( f ) ) {
tmp.description = 'Spam Assassin spam email.';
} else if ( RE_HARD_HAM.test( f ) ) {
tmp.description = 'Spam Assassin hard ham email.';
} else {
tmp.description = 'Spam Assassin ham email.';
}
tmp.path = './data/'+f;
tmp.hash = parts[ 3 ];
pkg.resources.push( tmp );
tmp = copy( json );
f = replace( f, '.txt', '.json' );
tmp.name = replace( f, RE_FILENAME_PUNC, '-' );
tmp.title = parts[ 1 ] + ':' + parts[ 2 ];
if ( RE_SPAM.test( f ) ) {
tmp.description = 'Spam Assassin spam email.';
} else if ( RE_HARD_HAM.test( f ) ) {
tmp.description = 'Spam Assassin hard ham email.';
} else {
tmp.description = 'Spam Assassin ham email.';
}
tmp.path = './data/'+f;
pkg.resources.push( tmp );
}
fpath = resolve( __dirname, '..', 'datapackage.json' );
writeFileSync( fpath, JSON.stringify( pkg, null, 2 ), opts );
} // end FUNCTION main()
main();