Skip to content

Commit

Permalink
remove all ES6 syntax
Browse files Browse the repository at this point in the history
This part of the effort to create an official TypeScript compiler:
Urigo/angular2-meteor#89
Urigo/angular2-meteor#90
  • Loading branch information
barbatus committed Feb 12, 2016
1 parent 3949d13 commit e6be42a
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 153 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
tests/.cache
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## meteor-typescript

TypeScript wrapped for Meteor.
180 changes: 89 additions & 91 deletions cache.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

const path = require("path");
const fs = require("fs");
const assert = require("assert");
const LRU = require("lru-cache");
const utils = require("./utils");
const pkgVersion = require("./package.json").version;
const random = require("random-js")();
var path = require("path");
var fs = require("fs");
var assert = require("assert");
var LRU = require("lru-cache");
var utils = require("./utils");
var pkgVersion = require("./package.json").version;
var random = require("random-js")();

function ensureCacheDir(cacheDir) {
cacheDir = path.resolve(
Expand All @@ -29,113 +29,111 @@ function ensureCacheDir(cacheDir) {
return cacheDir;
}

class Cache {
function Cache(compileFn, cacheDir) {
assert.ok(this instanceof Cache);
assert.strictEqual(typeof compileFn, "function");

constructor(compileFn, cacheDir) {
assert.strictEqual(typeof compileFn, "function");
this.compileFn = compileFn;
this.cacheDir = ensureCacheDir(cacheDir);

this.compileFn = compileFn;
this.cacheDir = ensureCacheDir(cacheDir);
var maxSize = process.env.TYPESCRIPT_CACHE_SIZE;
this._cache = new LRU({
max: maxSize || 1024 * 10 * 10
});
};

const maxSize = process.env.TYPESCRIPT_CACHE_SIZE;
this._cache = new LRU({
max: maxSize || 1024 * 10 * 10
});
}
exports.Cache = Cache;

get(source, options) {
let cacheKey = utils.deepHash(pkgVersion, source, options);
let compileResult = this._cache.get(cacheKey);
var Cp = Cache.prototype;

if (! compileResult) {
compileResult = this._readCache(cacheKey);
}
Cp.get = function(source, options) {
var cacheKey = utils.deepHash(pkgVersion, source, options);
var compileResult = this._cache.get(cacheKey);

if (! compileResult) {
compileResult = this.compileFn(source, options);
this._cache.set(cacheKey, compileResult);
this._writeCacheAsync(cacheKey, compileResult);
}

return compileResult;
if (! compileResult) {
compileResult = this._readCache(cacheKey);
}

_cacheFilename(cacheKey) {
// We want cacheKeys to be hex so that they work on any FS
// and never end in .cache.
if (!/^[a-f0-9]+$/.test(cacheKey)) {
throw Error('bad cacheKey: ' + cacheKey);
}

return path.join(this.cacheDir, cacheKey + '.cache');
if (! compileResult) {
compileResult = this.compileFn(source, options);
this._cache.set(cacheKey, compileResult);
this._writeCacheAsync(cacheKey, compileResult);
}

_readFileOrNull(filename) {
try {
return fs.readFileSync(filename, 'utf8');
} catch (e) {
if (e && e.code === 'ENOENT')
return null;
throw e;
}
}
return compileResult;
}

_parseJSONOrNull(json) {
try {
return JSON.parse(json);
} catch (e) {
if (e instanceof SyntaxError)
return null;
throw e;
}
Cp._cacheFilename = function(cacheKey) {
// We want cacheKeys to be hex so that they work on any FS
// and never end in .cache.
if (!/^[a-f0-9]+$/.test(cacheKey)) {
throw Error('bad cacheKey: ' + cacheKey);
}

// Returns null if the file does not exist or can't be parsed; otherwise
// returns the parsed compileResult in the file.
_readAndParseCompileResultOrNull(filename) {
var content = this._readFileOrNull(filename);
return this._parseJSONOrNull(content);
}
return path.join(this.cacheDir, cacheKey + '.cache');
}

_readCache(cacheKey) {
if (! this.cacheDir) {
Cp._readFileOrNull = function(filename) {
try {
return fs.readFileSync(filename, 'utf8');
} catch (e) {
if (e && e.code === 'ENOENT')
return null;
}
throw e;
}
}

var cacheFilename = this._cacheFilename(cacheKey);
var compileResult = this._readAndParseCompileResultOrNull(cacheFilename);
if (! compileResult) {
Cp._parseJSONOrNull = function(json) {
try {
return JSON.parse(json);
} catch (e) {
if (e instanceof SyntaxError)
return null;
}
this._cache.set(cacheKey, compileResult);

return compileResult;
throw e;
}
}

// We want to write the file atomically.
// But we also don't want to block processing on the file write.
_writeFileAsync(filename, contents) {
var tempFilename = filename + '.tmp.' + random.uuid4();
fs.writeFile(tempFilename, contents, (err) => {
// ignore errors, it's just a cache
if (err) {
return;
}
fs.rename(tempFilename, filename, (err) => {
// ignore this error too.
});
});
}
// Returns null if the file does not exist or can't be parsed; otherwise
// returns the parsed compileResult in the file.
Cp._readAndParseCompileResultOrNull = function(filename) {
var content = this._readFileOrNull(filename);
return this._parseJSONOrNull(content);
}

_writeCacheAsync(cacheKey, compileResult) {
if (! this.cacheDir) return;
Cp._readCache = function(cacheKey) {
if (! this.cacheDir) {
return null;
}

var cacheFilename = this._cacheFilename(cacheKey);
var cacheContents = JSON.stringify(compileResult);
this._writeFileAsync(cacheFilename, cacheContents);
var cacheFilename = this._cacheFilename(cacheKey);
var compileResult = this._readAndParseCompileResultOrNull(cacheFilename);
if (! compileResult) {
return null;
}
this._cache.set(cacheKey, compileResult);

return compileResult;
}

exports.Cache = Cache;
// We want to write the file atomically.
// But we also don't want to block processing on the file write.
Cp._writeFileAsync = function(filename, contents) {
var tempFilename = filename + '.tmp.' + random.uuid4();
fs.writeFile(tempFilename, contents, function(err) {
// ignore errors, it's just a cache
if (err) {
return;
}
fs.rename(tempFilename, filename, function(err) {
// ignore this error too.
});
});
}

Cp._writeCacheAsync = function(cacheKey, compileResult) {
if (! this.cacheDir) return;

var cacheFilename = this._cacheFilename(cacheKey);
var cacheContents = JSON.stringify(compileResult);
this._writeFileAsync(cacheFilename, cacheContents);
}
37 changes: 28 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
'use strict';

const getDefaultOptions = require("./options").getDefaultOptions;
const tsCompile = require("./typescript").compile;
const Cache = require("./cache").Cache;
var getDefaultCompilerOptions = require("./options").getDefaultCompilerOptions;
var convertCompilerOptionsOrThrow = require("./options").convertCompilerOptionsOrThrow;
var tsCompile = require("./typescript").compile;
var Cache = require("./cache").Cache;
var _ = require("underscore");

function setCacheDir(cacheDir) {
exports.setCacheDir = function setCacheDir(cacheDir) {
if (compileCache && compileCache.cacheDir === cacheDir) {
return;
}

compileCache = new Cache(function(source, options) {
return tsCompile(source, options);
}, cacheDir);
}

exports.setCacheDir = setCacheDir;
};

let compileCache;
var compileCache;
exports.compile = function compile(source, options) {
options = options || {compilerOptions: getDefaultOptions()};
options = options ? convertOptionsOrThrow(options) :
{compilerOptions: getDefaultCompilerOptions()};

if (! options.useCache) {
return tsCompile(source, options);
Expand All @@ -30,3 +31,21 @@ exports.compile = function compile(source, options) {

return compileCache.get(source, options);
};

function convertOptionsOrThrow(options) {
if (! options.compilerOptions) return null;

var compilerOptions = convertCompilerOptionsOrThrow(options.compilerOptions);
var result = _.clone(options);
result.compilerOptions = compilerOptions;

return result;
}

exports.convertOptionsOrThrow = convertOptionsOrThrow;

exports.getDefaultOptions = function getDefaultOptions() {
return {
compilerOptions: getDefaultCompilerOptions()
}
}
60 changes: 55 additions & 5 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const ts = require("typescript");
const _ = require("underscore");
var ts = require("typescript");
var _ = require("underscore");

function getCompilerOptions(customOptions) {
let compilerOptions = ts.getDefaultCompilerOptions();
var compilerOptions = ts.getDefaultCompilerOptions();

_.extend(compilerOptions, customOptions);

Expand Down Expand Up @@ -46,7 +46,7 @@ function getCompilerOptions(customOptions) {
exports.getCompilerOptions = getCompilerOptions;

// Default compiler options.
function getDefaultOptions() {
function getDefaultCompilerOptions() {
return {
module : ts.ModuleKind.None,
target: ts.ScriptTarget.ES5,
Expand All @@ -61,4 +61,54 @@ function getDefaultOptions() {
}
}

exports.getDefaultOptions = getDefaultOptions;
exports.getDefaultCompilerOptions = getDefaultCompilerOptions;

var customOptions = ['useCache'];
function isCustomOption(option) {
return customOptions.indexOf(option) !== -1;
}

function validateCustomOptions(options) {
if ('useCache' in options) {
if (typeof options['useCache'] !== 'boolean') {
throw new Error('useCache should be boolean');
}
}
}

// Validate compiler options and convert them from
// user-friendly format to enum values used by TypeScript:
// 'system' string converted to ts.ModuleKind.System value.
function convertCompilerOptionsOrThrow(options) {
if (! options) return null;

var compilerOptions = _.clone(options);
var customOptions = {};
if (compilerOptions) {
for (var option in compilerOptions) {
if (isCustomOption(option)) {
customOptions[option] = compilerOptions[option];
delete compilerOptions[option];
}
}
}

var testOptions = {};
testOptions.compilerOptions = compilerOptions;
testOptions.files = [];
var result = ts.parseJsonConfigFileContent(testOptions);

if (result.errors && result.errors.length) {
throw new Error(result.errors[0].messageText);
}

validateCustomOptions(customOptions);

// Add converted compiler options plus custom options back.
compilerOptions = _.extend(
result.options, customOptions);

return compilerOptions;
}

exports.convertCompilerOptionsOrThrow = convertCompilerOptionsOrThrow;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "meteor-typescript",
"author": "@barbatus",
"version": "0.1.0",
"version": "0.5.2",
"license": "MIT",
"description": "TypeScript wrapper package for use with Meteor",
"keywords": [
Expand Down
5 changes: 5 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ var assert = require("assert");
var result = meteorTS.compile("export const foo = 600;");
assert.equal(result.code.indexOf("exports.foo = 600;"), 0);

var converted = meteorTS.convertOptionsOrThrow({
compilerOptions: {
module: 'system'
}
});
Loading

0 comments on commit e6be42a

Please sign in to comment.