fix(core): initial
This commit is contained in:
9
ts/index.ts
Normal file
9
ts/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export * from "./tsbuild.exports";
|
||||
|
||||
import * as tsbuild from './tsbuild.exports'
|
||||
|
||||
if (process.env.CLI_CALL_TSBUILD === "true") {
|
||||
tsbuild.compileGlobStringObject({
|
||||
"./ts/**/*.ts": "./dist"
|
||||
})
|
||||
}
|
78
ts/tsbuild.classes.compiler.ts
Normal file
78
ts/tsbuild.classes.compiler.ts
Normal file
@ -0,0 +1,78 @@
|
||||
// import all the stuff we need
|
||||
import * as plugins from './tsbuild.plugins';
|
||||
import { CompilerOptions } from 'typescript';
|
||||
export { CompilerOptions, ScriptTarget, ModuleKind } from 'typescript';
|
||||
|
||||
/**
|
||||
* the default typescript compilerOptions
|
||||
*/
|
||||
export const compilerOptionsDefault: CompilerOptions = {
|
||||
declaration: true,
|
||||
emitDecoratorMetadata: true,
|
||||
experimentalDecorators: true,
|
||||
inlineSourceMap: true,
|
||||
noEmitOnError: false,
|
||||
outDir: 'dist/',
|
||||
module: plugins.typescript.ModuleKind.CommonJS,
|
||||
lib: [
|
||||
'es2016',
|
||||
'es2017'
|
||||
],
|
||||
noImplicitAny: false,
|
||||
target: plugins.typescript.ScriptTarget.ES2015
|
||||
};
|
||||
|
||||
/**
|
||||
* merges compilerOptions with the default compiler options
|
||||
*/
|
||||
export const mergeCompilerOptions = function(customTsOptions: CompilerOptions): CompilerOptions {
|
||||
// create merged options
|
||||
let mergedOptions: CompilerOptions = {
|
||||
...compilerOptionsDefault,
|
||||
...customTsOptions
|
||||
};
|
||||
|
||||
return mergedOptions;
|
||||
};
|
||||
|
||||
/**
|
||||
* the internal main compiler function that compiles the files
|
||||
*/
|
||||
export const compiler = (
|
||||
fileNames: string[],
|
||||
options: plugins.typescript.CompilerOptions
|
||||
): Promise<any[]> => {
|
||||
console.log(options);
|
||||
let done = plugins.smartpromise.defer<any[]>();
|
||||
let program = plugins.typescript.createProgram(fileNames, options);
|
||||
let emitResult = program.emit();
|
||||
|
||||
// implement check only
|
||||
/*let emitResult = program.emit(undefined,(args) => {
|
||||
console.log(args)
|
||||
});*/
|
||||
|
||||
let allDiagnostics = plugins.typescript
|
||||
.getPreEmitDiagnostics(program)
|
||||
.concat(emitResult.diagnostics);
|
||||
try {
|
||||
allDiagnostics.forEach(diagnostic => {
|
||||
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
||||
let message = plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
||||
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
|
||||
});
|
||||
} catch (err) {
|
||||
// console.log(allDiagnostics)
|
||||
}
|
||||
|
||||
let exitCode = emitResult.emitSkipped ? 1 : 0;
|
||||
if (exitCode === 0) {
|
||||
console.log('TypeScript emit succeeded!');
|
||||
done.resolve(emitResult.emittedFiles);
|
||||
} else {
|
||||
console.error('TypeScript emit failed. Please investigate!');
|
||||
process.exit(exitCode);
|
||||
}
|
||||
|
||||
return done.promise;
|
||||
};
|
52
ts/tsbuild.exports.ts
Normal file
52
ts/tsbuild.exports.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import * as plugins from './tsbuild.plugins';
|
||||
import { compiler, CompilerOptions, mergeCompilerOptions } from './tsbuild.classes.compiler';
|
||||
|
||||
export * from './tsbuild.classes.compiler';
|
||||
|
||||
/**
|
||||
* compile am array of absolute file paths
|
||||
*/
|
||||
export let compileFileArray = (
|
||||
fileStringArrayArg: string[],
|
||||
compilerOptionsArg: CompilerOptions = {}
|
||||
): Promise<any[]> => {
|
||||
return compiler(fileStringArrayArg, mergeCompilerOptions(compilerOptionsArg));
|
||||
};
|
||||
|
||||
/**
|
||||
* compile advanced glob configurations
|
||||
* @param globStringArrayArg a array of glob strings
|
||||
* {
|
||||
* './some/origin/folder/**\/*.ts': './some/destination/folder'
|
||||
* }
|
||||
*/
|
||||
export let compileGlobStringObject = async (
|
||||
globStringObjectArg: any,
|
||||
tsOptionsArg: CompilerOptions = {},
|
||||
cwdArg: string = process.cwd()
|
||||
) => {
|
||||
let compiledFiles = [];
|
||||
for (let keyArg in globStringObjectArg) {
|
||||
console.log(
|
||||
`TypeScript assignment: transpile from ${keyArg} to ${globStringObjectArg[keyArg]}`
|
||||
);
|
||||
const fileTreeArray = await plugins.smartfile.fs.listFileTree(cwdArg, keyArg);
|
||||
let absoluteFilePathArray: string[] = plugins.smartpath.transform.toAbsolute(
|
||||
fileTreeArray,
|
||||
cwdArg
|
||||
);
|
||||
let destDir: string = plugins.smartpath.transform.toAbsolute(
|
||||
globStringObjectArg[keyArg],
|
||||
cwdArg
|
||||
);
|
||||
tsOptionsArg = {
|
||||
...tsOptionsArg,
|
||||
outDir: destDir
|
||||
};
|
||||
compiledFiles = compiledFiles.concat(
|
||||
compiledFiles,
|
||||
await compileFileArray(absoluteFilePathArray, tsOptionsArg)
|
||||
);
|
||||
}
|
||||
return compiledFiles;
|
||||
};
|
6
ts/tsbuild.plugins.ts
Normal file
6
ts/tsbuild.plugins.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartpath from '@pushrocks/smartpath';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as typescript from 'typescript';
|
||||
|
||||
export { smartfile, smartpath, smartpromise, typescript };
|
Reference in New Issue
Block a user