Skip to content

Instantly share code, notes, and snippets.

@danwbyrne
Last active October 11, 2018 21:11
Show Gist options
  • Save danwbyrne/7eb3c54cb751b2f56963060c146f362e to your computer and use it in GitHub Desktop.
Save danwbyrne/7eb3c54cb751b2f56963060c146f362e to your computer and use it in GitHub Desktop.
export interface Foo {}
export class Bar {
public constructor() {
//
}
public do(): Foo {
return {};
}
}
export class Baz {}
export * from './Bar';
export * from './Baz';
{
"compilerOptions": {
"module": "commonjs",
"removeComments": true,
"outFile": "test.d.ts"
},
"files": [
"Bar.d.ts",
"Baz.d.ts",
"index.d.ts"
]
}
import { testDirectoryPath } from '../__data__/helpers';
import { concatenate } from '../concatenate';
describe.only('Concatenator Testing Suite Mk 1.3b.73', () => {
test('CONCATENTENTENTATION', () => {
const baselinePath = testDirectoryPath('baseline');
const result = concatenate(baselinePath);
console.log(result === undefined ? ':thinking:' : result.text);
});
});
import { tsUtils } from '@neo-one/ts-utils';
import * as fs from 'fs-extra';
import path from 'path';
import ts from 'typescript';
import { Concatenator } from './Concatenator';
export function concatenate(entry: string) {
const servicesHost: ts.LanguageServiceHost = {
getScriptFileNames: () => [...entry],
getScriptVersion: () => '0',
getScriptSnapshot: (fileName) => {
// tslint:disable-next-line no-non-null-assertion
if (!servicesHost.fileExists!(fileName)) {
return undefined;
}
// tslint:disable-next-line no-non-null-assertion
return ts.ScriptSnapshot.fromString(servicesHost.readFile!(fileName)!);
},
getCurrentDirectory: () => process.cwd(),
getCompilationSettings: () => {
const configPath = ts.findConfigFile(entry, ts.sys.fileExists);
if (configPath === undefined) {
return ts.getDefaultCompilerOptions();
}
console.log(configPath);
const text = fs.readFileSync(configPath, 'utf8');
const parseResult = ts.parseConfigFileTextToJson(configPath, text);
return ts.parseJsonConfigFileContent(
parseResult.config,
{
useCaseSensitiveFileNames: true,
readDirectory: ts.sys.readDirectory,
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
},
path.dirname(configPath),
undefined,
undefined,
).options;
},
getDefaultLibFileName: (opts) => ts.getDefaultLibFilePath(opts),
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
getNewLine: () => ts.sys.newLine,
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory,
};
const languageService = ts.createLanguageService(servicesHost);
const program = languageService.getProgram();
if (program === undefined) {
throw new Error('hmmm');
}
const typeChecker = program.getTypeChecker();
const getSymbol = (node: ts.Node) => {
const symbol = tsUtils.node.getSymbol(typeChecker, node);
if (symbol === undefined) {
return undefined;
}
const aliased = tsUtils.symbol.getAliasedSymbol(typeChecker, symbol);
if (aliased !== undefined) {
return aliased;
}
return symbol;
};
const isIgnoreFile = () => false;
const isGlobalIdentifier = () => false;
const isGlobalFile = () => false;
const isGlobalSymbol = () => false;
const sources = program.getSourceFiles();
sources.forEach((source) => console.log(tsUtils.file.getFilePath(source)));
const sourceFile = tsUtils.file.getSourceFileOrThrow(program, entry);
console.log(sourceFile);
const concatenator = new Concatenator({
context: {
typeChecker,
program,
languageService,
getSymbol,
isIgnoreFile,
isGlobalIdentifier,
isGlobalFile,
isGlobalSymbol,
},
sourceFile,
});
const sourceFiles = concatenator.sourceFiles;
if (sourceFiles.length === 0 || sourceFiles.length === 1) {
return undefined;
}
return tsUtils.printBundle(program, sourceFiles, concatenator.substituteNode);
}
import { normalizePath } from '@neo-one/utils';
import appRootDir from 'app-root-dir';
import * as path from 'path';
export const testDirectoryPath = (directory: string) =>
normalizePath(
path.resolve(
appRootDir.get(),
'packages',
'neo-one-typescript-concatenator',
'src',
'__data__',
'test-directories',
directory,
'index.d.ts',
),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment