Skip to content

Commit

Permalink
test(csv-ts-demo-node16): demo assiated with PR #341
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jul 10, 2022
1 parent e443b22 commit a5c5ea9
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 0 deletions.
32 changes: 32 additions & 0 deletions demo/ts-module-node16/lib/stringify.ts
@@ -0,0 +1,32 @@

import assert from 'assert'
import { stringify, Stringifier } from 'csv-stringify';

let output: string = '';
// Create the parser
const stringifier: Stringifier = stringify({
delimiter: ':',
encoding: 'utf8'
});
// Use the readable stream api to consume records
stringifier.on('readable', function(){
let record; while ((record = stringifier.read()) !== null) {
output += record
}
});
// Catch any error
stringifier.on('error', function(err){
console.error(err.message)
});
// Test that the parsed records matched what's expected
stringifier.on('end', function(){
assert.deepStrictEqual(
output,
'a:b:c\n1:2:3\n'
)
});
// Write data to the stream
stringifier.write(["a", "b", "c"]);
stringifier.write([1, 2, 3]);
// Close the readable stream
stringifier.end();
31 changes: 31 additions & 0 deletions demo/ts-module-node16/package.json
@@ -0,0 +1,31 @@
{
"name": "csv-ts-demo-node16",
"version": "0.0.0",
"main": "index.js",
"license": "MIT",
"type": "module",
"private": true,
"devDependencies": {
"@types/node": "^18.0.3",
"coffeescript": "^2.7.0",
"mocha": "^10.0.0",
"should": "^13.2.3",
"ts-node": "^10.8.2",
"typescript": "^4.7.4"
},
"mocha": {
"inline-diffs": true,
"loader": "./test/loaders/all.js",
"recursive": true,
"reporter": "spec",
"require": [
"should"
],
"throw-deprecation": true,
"timeout": 40000
},
"scripts": {
"test": "mocha 'test/**/*.coffee'",
"test:legacy": "mocha --loader=./test/loaders/legacy/all.js 'test/**/*.{coffee,ts}'"
}
}
16 changes: 16 additions & 0 deletions demo/ts-module-node16/test/loaders/all.js
@@ -0,0 +1,16 @@

import * as coffee from './coffee.js'
import * as ts from 'ts-node/esm'

const coffeeRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;
const tsRegex = /\.ts$/;

export function load(url, context, next) {
if (coffeeRegex.test(url)) {
return coffee.load.apply(this, arguments)
}
if (tsRegex.test(url)) {
return ts.load.apply(this, arguments)
}
return next(url, context, next);
}
20 changes: 20 additions & 0 deletions demo/ts-module-node16/test/loaders/coffee.js
@@ -0,0 +1,20 @@
import CoffeeScript from 'coffeescript';

// See https://github.com/nodejs/node/issues/36396
const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;

export async function load(url, context, next) {
if (extensionsRegex.test(url)) {
const format = 'module';
const { source: rawSource } = await next(url, { format });
const source = CoffeeScript.compile(rawSource.toString(), {
bare: true,
inlineMap: true,
filename: url,
header: false,
sourceMap: false,
});
return {format, source};
}
return next(url, context);
}
37 changes: 37 additions & 0 deletions demo/ts-module-node16/test/loaders/legacy/all.js
@@ -0,0 +1,37 @@

import * as coffee from './coffee.js'
import * as ts from 'ts-node/esm'

const coffeeRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;
const tsRegex = /\.ts$/;

export function resolve(specifier, context, defaultResolve) {
if (coffeeRegex.test(specifier)) {
return coffee.resolve.apply(this, arguments)
}
if (tsRegex.test(specifier)) {
return ts.resolve.apply(this, arguments)
}
return ts.resolve.apply(this, arguments);
}

export function getFormat(url, context, defaultGetFormat) {
if (coffeeRegex.test(url)) {
return coffee.getFormat.apply(this, arguments)
}
if (tsRegex.test(url)) {
return ts.getFormat.apply(this, arguments)
}
return ts.getFormat.apply(this, arguments);
}

export function transformSource(source, context, defaultTransformSource) {
const { url } = context;
if (coffeeRegex.test(url)) {
return coffee.transformSource.apply(this, arguments)
}
if (tsRegex.test(url)) {
return ts.transformSource.apply(this, arguments)
}
return ts.transformSource.apply(this, arguments);
}
50 changes: 50 additions & 0 deletions demo/ts-module-node16/test/loaders/legacy/coffee.js
@@ -0,0 +1,50 @@
// coffeescript-loader.mjs
import { URL, pathToFileURL } from 'url';
import CoffeeScript from 'coffeescript';
import { cwd } from 'process';

const baseURL = pathToFileURL(`${cwd()}/`).href;

// CoffeeScript files end in .coffee, .litcoffee or .coffee.md.
const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;

export function resolve(specifier, context, defaultResolve) {
const { parentURL = baseURL } = context;
// Node.js normally errors on unknown file extensions, so return a URL for
// specifiers ending in the CoffeeScript file extensions.
if (extensionsRegex.test(specifier)) {
return {
url: new URL(specifier, parentURL).href,
stop: true
};
}
// Let Node.js handle all other specifiers.
return defaultResolve(specifier, context, defaultResolve);
}

export function getFormat(url, context, defaultGetFormat) {
// Now that we patched resolve to let CoffeeScript URLs through, we need to
// tell Node.js what format such URLs should be interpreted as. For the
// purposes of this loader, all CoffeeScript URLs are ES modules.
if (extensionsRegex.test(url)) {
return {
format: 'module',
stop: true
};
}
// Let Node.js handle all other URLs.
return defaultGetFormat(url, context, defaultGetFormat);
}

export function transformSource(source, context, defaultTransformSource) {
const { url, format } = context;

if (extensionsRegex.test(url)) {
return {
source: CoffeeScript.compile(String(source), { bare: true })
};
}

// Let Node.js handle all other sources.
return defaultTransformSource(source, context, defaultTransformSource);
}
26 changes: 26 additions & 0 deletions demo/ts-module-node16/test/samples.coffee
@@ -0,0 +1,26 @@

import fs from 'fs'
import path from 'path'
import { exec } from 'child_process'

import { fileURLToPath } from 'url'
__dirname = path.dirname fileURLToPath import.meta.url
dir = path.resolve __dirname, '../lib'
samples = fs.readdirSync dir

describe 'Samples', ->

samples
.filter (sample) ->
return false unless /\.(js|ts)?$/.test sample
true
.map (sample) ->
it "Sample #{sample}", (callback) ->
ext = /\.(\w+)?$/.exec(sample)[0]
bin = switch ext
when '.js'
'node'
when '.ts'
'node --loader ts-node/esm'
exec "#{bin} #{path.resolve dir, sample}", (err, stdout, stderr) ->
callback err
7 changes: 7 additions & 0 deletions demo/ts-module-node16/tsconfig.json
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "Node16",
"strict": true,
}
}

0 comments on commit a5c5ea9

Please sign in to comment.