Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@travetto/compiler

Package Overview
Dependencies
Maintainers
1
Versions
300
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@travetto/compiler - npm Package Compare versions

Comparing version 0.0.16 to 0.0.17

test/watch.ts

4

package.json

@@ -8,6 +8,4 @@ {

"@travetto/base": "0.x.x",
"@types/chokidar": "^1.7.5",
"@types/node": "^9.4.7",
"@types/source-map-support": "^0.4.0",
"chokidar": "^2.0.3",
"source-map-support": "^0.5.4",

@@ -25,3 +23,3 @@ "string-hash": "^1.1.3",

"scripts": {},
"version": "0.0.16"
"version": "0.0.17"
}

@@ -5,7 +5,5 @@ import * as fs from 'fs';

import * as path from 'path';
import * as glob from 'glob';
import * as chokidar from 'chokidar';
import { EventEmitter } from 'events';
import { bulkRequire, bulkFindSync, AppEnv, AppInfo } from '@travetto/base';
import { bulkRequire, bulkFindSync, AppEnv, AppInfo, Watcher, Entry } from '@travetto/base';
import { RetargettingHandler } from './proxy';

@@ -30,4 +28,2 @@

static contents = new Map<string, string>();
// static servicesHost: ts.LanguageServiceHost;
// static langaugeService: ts.LanguageService;
static cwd: string;

@@ -39,3 +35,3 @@ static options: ts.CompilerOptions;

static rootFiles: string[];
static fileWatcher: chokidar.FSWatcher;
static fileWatchers: { [key: string]: Watcher } = {};
static events = new EventEmitter();

@@ -48,17 +44,10 @@ static snaphost = new Map<string, ts.IScriptSnapshot | undefined>()

static libraryPath = 'node_modules';
static frameworkWorkingSet = `${Compiler.libraryPath}/@travetto/*/src/**/*.ts`;
static appWorkingSet = `src/**/*.ts`;
static transformerSet = '**/transformer.*.ts';
static transformerPattern = /^.*\/transformer.*[.]ts$/;
static optionalFiles = /\/opt\/[^/]+.ts/;
static definitionFiles = /\.d\.ts$/g;
static devDependencyFiles = AppInfo.DEV_PACKAGES && AppInfo.DEV_PACKAGES.length ?
[new RegExp(`${Compiler.libraryPath}/(${AppInfo.DEV_PACKAGES.join('|')})/`)] : [];
static devDependencyFiles = AppInfo.DEV_PACKAGES
.map(x => new RegExp(`node_modules/${x}/`));
static workingSets = [Compiler.frameworkWorkingSet, Compiler.appWorkingSet];
static invalidWorkingSetFiles = [
Compiler.optionalFiles,
Compiler.definitionFiles,
/transformer\.[^/]+.ts/,
/\.d\.ts$/g, // Definition files
Compiler.transformerPattern,
...Compiler.devDependencyFiles

@@ -77,5 +66,4 @@ ];

static handleLoadError(p: string, e?: any): boolean {
if (!AppEnv.prod || this.optionalFiles.test(p)) { // If attempting to load an optional require
const extra = this.optionalFiles.test(p) ? 'optional ' : '';
console.error(`Unable to import ${extra}require, ${p}, stubbing out`, e);
if (!AppEnv.prod) { // If attempting to load an optional require
console.error(`Unable to import ${p}, stubbing out`, e);
return true;

@@ -93,8 +81,3 @@ } else {

const out = ts.parseJsonSourceFileConfigFileContent(
ts.readJsonConfigFile(`${this.cwd}/${this.configFile}`, x => ts.sys.readFile(x)), {
useCaseSensitiveFileNames: true,
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory
}, this.cwd, {
ts.readJsonConfigFile(`${this.cwd}/${this.configFile}`, ts.sys.readFile), ts.sys, this.cwd, {
rootDir: `${this.cwd}`,

@@ -117,3 +100,3 @@ sourceMap: false,

for (const trns of bulkRequire(this.transformerSet)) {
for (const trns of bulkRequire([this.transformerPattern], this.cwd)) {
for (const key of Object.keys(trns)) {

@@ -151,3 +134,3 @@ const item = trns[key];

const p = Module._resolveFilename(request, parent);
if (p.includes(process.cwd()) && !p.includes(this.libraryPath)) {
if (p.includes(this.cwd) && !p.includes(this.libraryPath)) {
if (!this.modules.has(p)) {

@@ -180,3 +163,6 @@ const handler = new RetargettingHandler(mod);

if (AppEnv.watch) {
this.fileWatcher.add(tsf);
const topLevel = tsf.split(`${this.cwd}/`)[1].split('/')[0];
if (this.fileWatchers[topLevel]) {
this.fileWatchers[topLevel].add([tsf]);
}
}

@@ -243,3 +229,3 @@ // Picking up missed files

static emitFile(fileName: string) {
// let output = this.langaugeService.getEmitOutput(fileName);
console.debug('Emitting', fileName);
const content = ts.sys.readFile(fileName)!;

@@ -251,3 +237,3 @@

if (hash === this.hashes.get(fileName)) {
console.debug('File contents unchanged');
console.debug(`Contents Unchanged: ${fileName}`);
return false;

@@ -259,5 +245,3 @@ }

let output = res.outputText;
if (fileName.match(/\/test\//)) {
// console.debug(fileName, output);
}
const outFileName = toJsName(fileName);

@@ -267,3 +251,3 @@

console.debug(`Compiling ${fileName} failed`);
if (this.handleLoadError(fileName) && this.optionalFiles.test(fileName)) {
if (this.handleLoadError(fileName)) {
output = this.emptyRequire;

@@ -285,52 +269,52 @@ }

static watchFiles(fileNames: string[]) {
// TODO: Replace with something that has fewer dependencies, to decrease overall footprint and load time
const watcher = chokidar.watch(this.appWorkingSet, {
ignored: this.invalidWorkingSetFile,
persistent: true,
cwd: process.cwd(),
private static watcherListener({ event, entry }: { event: string, entry: Entry }) {
if (this.invalidWorkingSetFile(entry.file)) {
return;
}
console.log('Watch', event, entry.file);
if (event === 'added') {
this.rootFiles.push(entry.file);
this.files.set(entry.file, { version: 1 });
if (this.emitFile(entry.file)) {
this.events.emit(event, entry.file);
}
} else if (event === 'changed') {
const changed = this.files.has(entry.file);
if (changed) {
this.snaphost.delete(entry.file);
this.files.get(entry.file)!.version++;
} else {
this.files.set(entry.file, { version: 1 });
this.rootFiles.push(entry.file);
}
if (this.emitFile(entry.file)) {
this.events.emit(changed ? 'changed' : 'added', entry.file);
}
} else if (event === 'removed') {
this.unload(entry.file);
this.events.emit(event, entry.file);
}
}
static buildWatcher(tld: string) {
const watcher = new Watcher({
interval: 250,
ignoreInitial: false,
awaitWriteFinish: { stabilityThreshold: 10 }
cwd: `${this.cwd}/${tld}`
});
watcher.on('ready', () => {
watcher
.on('add', fileName => {
fileName = `${process.cwd()}/${fileName}`;
fileNames.push(fileName);
this.files.set(fileName, { version: 1 });
if (this.emitFile(fileName)) {
this.events.emit('added', fileName);
}
})
.on('change', fileName => {
fileName = `${process.cwd()}/${fileName}`;
const changed = this.files.has(fileName);
if (changed) {
this.snaphost.delete(fileName);
this.files.get(fileName)!.version++;
} else {
this.files.set(fileName, { version: 1 });
fileNames.push(fileName);
}
if (this.emitFile(fileName)) {
this.events.emit(changed ? 'changed' : 'added', fileName);
}
})
.on('unlink', fileName => {
fileName = `${process.cwd()}/${fileName}`;
this.unload(fileName);
this.events.emit('removed', fileName);
});
});
watcher.on('all', this.watcherListener.bind(this))
watcher.add([/.*[.]ts$/]); // Watch ts files
watcher.run(false);
return watcher;
}
static watchFiles() {
const ret = { src: this.buildWatcher('src') };
return ret;
}
static logErrors(fileName: string, diagnostics?: ts.Diagnostic[]) {
// let allDiagnostics = this.langaugeService.getCompilerOptionsDiagnostics()
// .concat(this.langaugeService.getSyntacticDiagnostics(fileName))
// .concat(this.langaugeService.getSemanticDiagnostics(fileName));
if (!diagnostics || !diagnostics.length) {

@@ -354,2 +338,3 @@ return false;

static transpile(input: string, fileName: string) {
// console.debug('Transpiling', fileName);
const output = ts.transpileModule(input, {

@@ -393,27 +378,9 @@ compilerOptions: this.options,

this.rootFiles = bulkFindSync(this.workingSets, undefined, this.invalidWorkingSetFile);
this.rootFiles = bulkFindSync([/[^\/]+\/src\/.*[.]ts$/], `${this.cwd}/${Compiler.libraryPath}/@travetto`)
.concat(bulkFindSync([/.ts/], `${this.cwd}/src`))
.filter(x => !x.stats.isDirectory() && !this.invalidWorkingSetFile(x.file))
.map(x => x.file);
console.debug('Files', this.rootFiles.length);
console.debug(this.workingSets);
/*
this.servicesHost = {
getScriptFileNames: () => this.rootFiles,
getScriptVersion: (fileName) => this.files.has(fileName) ? this.files.get(fileName)!.version.toString() : '',
getScriptSnapshot: (fileName) => this.getSnapshot(fileName),
getCurrentDirectory: () => process.cwd(),
getCompilationSettings: () => this.options,
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
directoryExists: ts.sys.directoryExists,
readDirectory: ts.sys.readDirectory,
getDirectories: ts.sys.getDirectories,
getCustomTransformers: () => this.transformers
};
*/
// Create the language service files
// this.langaugeService = ts.createLanguageService(this.servicesHost, this.registry);
// Prime for type checker

@@ -427,3 +394,3 @@ for (const fileName of this.rootFiles) {

if (AppEnv.watch) {
this.fileWatcher = this.watchFiles(this.rootFiles);
this.fileWatchers = this.watchFiles();
}

@@ -436,3 +403,4 @@

if (AppEnv.watch) {
this.fileWatcher.close();
Object.values(this.fileWatchers).map(x => x.close());
this.fileWatchers = {};
}

@@ -439,0 +407,0 @@ this.contents.clear();

@@ -6,3 +6,3 @@ require('@travetto/base/bootstrap').run()

Compiler.on('changed', require);
require('./stack');
require('./watch');
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc