Skip to content

Commit 6e0ac39

Browse files
committed
Create initial simple test vinyl adapter, verifying that it works to load a local file as a gulp Vinyl file and writes with standard gulp.dest()
1 parent 7030235 commit 6e0ac39

File tree

8 files changed

+107
-26
lines changed

8 files changed

+107
-26
lines changed

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@
4646
"console": "internalConsole",
4747
"env": {"DEBUG_LEVEL":"debug"}
4848
},
49+
{
50+
"name": "TestAdapter Debug",
51+
"type": "node",
52+
"request": "launch",
53+
"program": "${workspaceRoot}/node_modules/gulp/bin/gulp.js",
54+
"stopOnEntry": false,
55+
"args": ["--gulpfile", "./debug/gulpfile.ts", "testAdapter"],
56+
"cwd": "${workspaceRoot}",
57+
"runtimeArgs": [
58+
"--nolazy", "-r", "ts-node/register"
59+
],
60+
"console": "internalConsole",
61+
"env": {"DEBUG_LEVEL":"debug"}
62+
},
4963
{
5064
"name": "csvParseWithoutGulp",
5165
"type": "node",

debug/gulpfile.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let gulp = require('gulp')
2-
import { tapCsv } from '../src/plugin'
2+
import { src as mysqlSrc } from '../src/plugin'
33

44
import * as loglevel from 'loglevel'
55
const log = loglevel.getLogger('gulpfile')
@@ -35,7 +35,7 @@ function runTapCsv(callback: any) {
3535
.on('data', function (file:Vinyl) {
3636
log.info('Starting processing on ' + file.basename)
3737
})
38-
.pipe(tapCsv({raw:true/*, info:true */}))
38+
// .pipe(tapCsv({raw:true/*, info:true */}))
3939
.pipe(rename({
4040
extname: ".ndjson",
4141
}))
@@ -63,5 +63,41 @@ export function csvParseWithoutGulp(callback: any) {
6363

6464
}
6565

66+
67+
68+
export function testAdapter(callback: any) {
69+
log.info('gulp task starting for ' + PLUGIN_NAME)
70+
71+
try {
72+
73+
74+
return mysqlSrc('../testdata/cars.csv',{buffer:gulpBufferMode})
75+
.pipe(errorHandler(function(err:any) {
76+
log.error('Error: ' + err)
77+
callback(err)
78+
}))
79+
.on('data', function (file:Vinyl) {
80+
log.info('Starting processing on ' + file.basename)
81+
})
82+
// .pipe(tapCsv({raw:true/*, info:true */}))
83+
// .pipe(rename({
84+
// extname: ".ndjson",
85+
// }))
86+
.pipe(gulp.dest('../testdata/processed'))
87+
.on('data', function (file:Vinyl) {
88+
log.info('Finished processing on ' + file.basename)
89+
})
90+
.on('end', function () {
91+
log.info('gulp task complete')
92+
callback()
93+
})
94+
95+
}
96+
catch (err) {
97+
log.error(err)
98+
}
99+
100+
}
101+
66102
exports.default = gulp.series(runTapCsv)
67103
exports.runTapCsvBuffer = gulp.series(switchToBuffer, runTapCsv)

package-lock.json

Lines changed: 22 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
],
2222
"homepage": "https://gulpetl.com",
2323
"dependencies": {
24-
"csv-parse": "^4.4.1",
2524
"from2": "^2.3.0",
2625
"gulp-error-handle": "^1.0.1",
2726
"loglevel": "^1.6.1",

src/plugin.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,40 @@ import * as loglevel from 'loglevel'
77
const log = loglevel.getLogger(PLUGIN_NAME) // get a logger instance based on the project name
88
log.setLevel((process.env.DEBUG_LEVEL || 'warn') as log.LogLevelDesc)
99

10-
const parse = require('csv-parse')
10+
const from2 = require('from2');
11+
import * as path from 'path'
12+
1113

1214
/** wrap incoming recordObject in a Singer RECORD Message object*/
1315
function createRecord(recordObject:Object, streamName: string) : any {
1416
return {type:"RECORD", stream:streamName, record:recordObject}
1517
}
1618

19+
// dead-simple stream-mode-only gulp vinyl adapter: loads a file as a stream and wraps it in a vinyl file, then returns vinyl file in a readable stream
20+
export function src(filePath:string, optionsIgnoredForNow:any) {
21+
let fileStream
22+
let vinylFile
23+
try {
24+
fileStream = require('fs').createReadStream(filePath)
25+
// let asdf = (undefined as any).wontWork // create an error for testing
26+
vinylFile = new Vinyl({
27+
base: path.dirname(filePath),
28+
path:filePath,
29+
contents:fileStream
30+
});
31+
}
32+
catch (err) {
33+
throw new PluginError(PLUGIN_NAME, err);
34+
}
35+
36+
return from2.obj([vinylFile])
37+
}
38+
39+
1740
/* This is a gulp-etl plugin. It is compliant with best practices for Gulp plugins (see
1841
https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/guidelines.md#what-does-a-good-plugin-look-like ),
1942
and like all gulp-etl plugins it accepts a configObj as its first parameter */
43+
/*
2044
export function tapCsv(configObj: any) {
2145
if (!configObj) configObj = {}
2246
if (!configObj.columns) configObj.columns = true // we don't allow false for columns; it results in arrays instead of objects for each record
@@ -133,4 +157,6 @@ export function tapCsv(configObj: any) {
133157
})
134158
135159
return strm
136-
}
160+
}
161+
162+
*/

testdata/processed/cars.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
carModel,price,color
2+
"Audi",10000,"blue"
3+
"BMW",15000,"red"
4+
"Mercedes",20000,"yellow"
5+
"Porsche",30000,"green"

testdata/processed/cars.ndjson

Lines changed: 0 additions & 4 deletions
This file was deleted.

testdata/processed/cars2.ndjson

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)