@plugjs/typescript 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+
26
+ // report.ts
27
+ var report_exports = {};
28
+ __export(report_exports, {
29
+ updateReport: () => updateReport
30
+ });
31
+ module.exports = __toCommonJS(report_exports);
32
+ var import_typescript = __toESM(require("typescript"));
33
+ var import_logging = require("@plugjs/plug/logging");
34
+ var import_paths = require("@plugjs/plug/paths");
35
+ function convertMessageChain(chain, indent = 0) {
36
+ const message = `${"".padStart(indent * 2)}${chain.messageText}`;
37
+ if (chain.next) {
38
+ const next = chain.next.map((c) => convertMessageChain(c, indent + 1));
39
+ return [message, ...next.flat(1)];
40
+ } else {
41
+ return [message];
42
+ }
43
+ }
44
+ function convertDiagnostics(diagnostics, directory) {
45
+ return diagnostics.map((diagnostic) => {
46
+ let level;
47
+ switch (diagnostic.category) {
48
+ case import_typescript.default.DiagnosticCategory.Error:
49
+ level = import_logging.ERROR;
50
+ break;
51
+ case import_typescript.default.DiagnosticCategory.Warning:
52
+ level = import_logging.WARN;
53
+ break;
54
+ default:
55
+ level = import_logging.NOTICE;
56
+ }
57
+ let message;
58
+ if (typeof diagnostic.messageText === "string") {
59
+ message = diagnostic.messageText;
60
+ } else {
61
+ message = convertMessageChain(diagnostic.messageText);
62
+ }
63
+ const tags = `TS${diagnostic.code}`;
64
+ if (diagnostic.file) {
65
+ const { file: sourceFile, start, length } = diagnostic;
66
+ const file = (0, import_paths.resolveAbsolutePath)(directory, sourceFile.fileName);
67
+ const source = sourceFile.getFullText();
68
+ if (start !== void 0) {
69
+ const position = sourceFile.getLineAndCharacterOfPosition(start);
70
+ let { line, character: column } = position;
71
+ column += 1;
72
+ line += 1;
73
+ return { level, message, tags, file, source, line, column, length };
74
+ } else {
75
+ return { level, message, tags, file, source };
76
+ }
77
+ } else {
78
+ return { level, message, tags };
79
+ }
80
+ });
81
+ }
82
+ function updateReport(report, diagnostics, directory) {
83
+ const records = convertDiagnostics(diagnostics, directory);
84
+ report.add(...records);
85
+ }
86
+ // Annotate the CommonJS export names for ESM import in node:
87
+ 0 && (module.exports = {
88
+ updateReport
89
+ });
90
+ //# sourceMappingURL=report.cjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/report.ts"],
4
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAe;AACf,qBAAoC;AACpC,mBAAoC;AAMpC,SAAS,oBAAoB,OAAkC,SAAS,GAAa;AACnF,QAAM,UAAU,GAAG,GAAG,SAAS,SAAS,CAAC,IAAI,MAAM;AAEnD,MAAI,MAAM,MAAM;AACd,UAAM,OAAO,MAAM,KAAK,IAAI,CAAC,MAAM,oBAAoB,GAAG,SAAS,CAA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typescript';
2
+ import type { Report } from '@plugjs/plug/logging';
3
+ import type { AbsolutePath } from '@plugjs/plug/paths';
4
+ /** Update a report, adding records from an array of {@link ts.Diagnostic} */
5
+ export declare function updateReport(report: Report, diagnostics: readonly ts.Diagnostic[], directory: AbsolutePath): void;
@@ -0,0 +1,59 @@
1
+ // report.ts
2
+ import ts from "typescript";
3
+ import { ERROR, NOTICE, WARN } from "@plugjs/plug/logging";
4
+ import { resolveAbsolutePath } from "@plugjs/plug/paths";
5
+ function convertMessageChain(chain, indent = 0) {
6
+ const message = `${"".padStart(indent * 2)}${chain.messageText}`;
7
+ if (chain.next) {
8
+ const next = chain.next.map((c) => convertMessageChain(c, indent + 1));
9
+ return [message, ...next.flat(1)];
10
+ } else {
11
+ return [message];
12
+ }
13
+ }
14
+ function convertDiagnostics(diagnostics, directory) {
15
+ return diagnostics.map((diagnostic) => {
16
+ let level;
17
+ switch (diagnostic.category) {
18
+ case ts.DiagnosticCategory.Error:
19
+ level = ERROR;
20
+ break;
21
+ case ts.DiagnosticCategory.Warning:
22
+ level = WARN;
23
+ break;
24
+ default:
25
+ level = NOTICE;
26
+ }
27
+ let message;
28
+ if (typeof diagnostic.messageText === "string") {
29
+ message = diagnostic.messageText;
30
+ } else {
31
+ message = convertMessageChain(diagnostic.messageText);
32
+ }
33
+ const tags = `TS${diagnostic.code}`;
34
+ if (diagnostic.file) {
35
+ const { file: sourceFile, start, length } = diagnostic;
36
+ const file = resolveAbsolutePath(directory, sourceFile.fileName);
37
+ const source = sourceFile.getFullText();
38
+ if (start !== void 0) {
39
+ const position = sourceFile.getLineAndCharacterOfPosition(start);
40
+ let { line, character: column } = position;
41
+ column += 1;
42
+ line += 1;
43
+ return { level, message, tags, file, source, line, column, length };
44
+ } else {
45
+ return { level, message, tags, file, source };
46
+ }
47
+ } else {
48
+ return { level, message, tags };
49
+ }
50
+ });
51
+ }
52
+ function updateReport(report, diagnostics, directory) {
53
+ const records = convertDiagnostics(diagnostics, directory);
54
+ report.add(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AER,eAAO,EAAE,OAAO,SAAS,MAAM,MAAM,QAAQ,MAAM,QAAQ,OAAO;AAAA,MACpE,OAAO;AACL,eAAO,EAAE,OAAO,SAAS,MAAM,MAAM,OAAO;AAAA,MAC9C;AAAA,IACF,OAAO;AACL,aAAO,EAAE,OAAO,SAAS,KAAK;AAAA,IAChC;AAAA,EACF,CAAC;AACH;AAGO,SAAS,aACZ,QACA,aACA,WACI;AACN,QAAM,UAAU,mBAAmB,aAAa,SAAS;AACzD,SAAO,IAAI,GAAG,OAAO;AACvB;",
5
+ "names": []
6
+ }
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+
26
+ // typescript.ts
27
+ var typescript_exports = {};
28
+ __export(typescript_exports, {
29
+ Tsc: () => Tsc
30
+ });
31
+ module.exports = __toCommonJS(typescript_exports);
32
+ var import_typescript = __toESM(require("typescript"));
33
+ var import_asserts = require("@plugjs/plug/asserts");
34
+ var import_files = require("@plugjs/plug/files");
35
+ var import_logging = require("@plugjs/plug/logging");
36
+ var import_paths = require("@plugjs/plug/paths");
37
+ var import_utils = require("@plugjs/plug/utils");
38
+ var import_compiler = require("./compiler.cjs");
39
+ var import_options = require("./options.cjs");
40
+ var import_report = require("./report.cjs");
41
+ var Tsc = class {
42
+ _tsconfig;
43
+ _options;
44
+ constructor(...args) {
45
+ const { params: [tsconfig], options } = (0, import_utils.parseOptions)(args, {});
46
+ this._tsconfig = tsconfig;
47
+ this._options = options;
48
+ }
49
+ async pipe(files, context) {
50
+ const baseDir = context.resolve(".");
51
+ const report = context.log.report("TypeScript Report");
52
+ const { extraTypesDir, ...overrides } = { ...this._options };
53
+ const sourcesConfig = (0, import_paths.resolveFile)(files.directory, "tsconfig.json");
54
+ const tsconfig = this._tsconfig ? context.resolve(this._tsconfig) : sourcesConfig || (0, import_paths.resolveFile)(context.resolve("tsconfig.json"));
55
+ let rootDir;
56
+ if (overrides.rootDir) {
57
+ rootDir = overrides.rootDir = context.resolve(overrides.rootDir);
58
+ } else {
59
+ rootDir = overrides.rootDir = files.directory;
60
+ }
61
+ let outDir;
62
+ if (overrides.outDir) {
63
+ outDir = overrides.outDir = context.resolve(overrides.outDir);
64
+ } else {
65
+ outDir = overrides.outDir = rootDir;
66
+ }
67
+ if (overrides.rootDirs) {
68
+ overrides.rootDirs = overrides.rootDirs.map((dir) => context.resolve(dir));
69
+ }
70
+ if (overrides.baseUrl)
71
+ overrides.baseUrl = context.resolve(overrides.baseUrl);
72
+ if (overrides.outFile)
73
+ overrides.outFile = context.resolve(overrides.outFile);
74
+ const { errors, options } = await (0, import_options.getCompilerOptions)(
75
+ tsconfig,
76
+ overrides
77
+ );
78
+ (0, import_report.updateReport)(report, errors, baseDir);
79
+ if (report.errors)
80
+ report.done(true);
81
+ const paths = [...files.absolutePaths()];
82
+ for (const path of paths)
83
+ context.log.trace(`Compiling "${(0, import_logging.$p)(path)}"`);
84
+ context.log.info("Compiling", paths.length, "files");
85
+ if (extraTypesDir) {
86
+ const directory = context.resolve(extraTypesDir);
87
+ for await (const file of (0, import_utils.walk)(directory, ["**/*.d.ts"])) {
88
+ const path = (0, import_paths.resolveAbsolutePath)(directory, file);
89
+ context.log.debug(`Including extra type file "${(0, import_logging.$p)(path)}"`);
90
+ paths.push(path);
91
+ }
92
+ }
93
+ context.log.debug("Compliation options", options);
94
+ const host = new import_compiler.TypeScriptHost(rootDir);
95
+ const program = import_typescript.default.createProgram(paths, options, host, void 0, errors);
96
+ const diagnostics = import_typescript.default.getPreEmitDiagnostics(program);
97
+ (0, import_report.updateReport)(report, diagnostics, rootDir);
98
+ if (report.errors)
99
+ report.done(true);
100
+ const builder = import_files.Files.builder(outDir);
101
+ const promises = [];
102
+ const result = program.emit(void 0, (fileName, code) => {
103
+ promises.push(builder.write(fileName, code).then((file) => {
104
+ context.log.trace("Written", (0, import_logging.$p)(file));
105
+ }).catch((error) => {
106
+ const outFile = (0, import_paths.resolveAbsolutePath)(outDir, fileName);
107
+ context.log.error("Error writing to", (0, import_logging.$p)(outFile), error);
108
+ throw import_asserts.BuildFailure.fail();
109
+ }));
110
+ });
111
+ await (0, import_asserts.assertPromises)(promises);
112
+ (0, import_report.updateReport)(report, result.diagnostics, rootDir);
113
+ if (report.errors)
114
+ report.done(true);
115
+ const outputs = builder.build();
116
+ context.log.info("TSC produced", outputs.length, "files into", (0, import_logging.$p)(outputs.directory));
117
+ return outputs;
118
+ }
119
+ };
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ Tsc
123
+ });
124
+ //# sourceMappingURL=typescript.cjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/typescript.ts"],
4
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAAe;AACf,qBAA6C;AAC7C,mBAAsB;AACtB,qBAAmB;AACnB,mBAA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cAAM,+BAAe,QAAQ;AAG7B,oCAAa,QAAQ,OAAO,aAAa,OAAO;AAEhD,QAAI,OAAO;AAAQ,aAAO,KAAK,IAAI;AAGnC,UAAM,UAAU,QAAQ,MAAM;AAC9B,YAAQ,IAAI,KAAK,gBAAgB,QAAQ,QAAQ,kBAAc,mBAAG,QAAQ,SAAS,CAAC;AACpF,WAAO;AAAA,EACT;AACF;",
5
+ "names": ["ts"]
6
+ }
@@ -0,0 +1,9 @@
1
+ /// <reference path="index.d.ts" />
2
+ import { Files } from '@plugjs/plug/files';
3
+ import type { Context, PipeParameters, Plug } from '@plugjs/plug/pipe';
4
+ export declare class Tsc implements Plug<Files> {
5
+ private readonly _tsconfig?;
6
+ private readonly _options;
7
+ constructor(...args: PipeParameters<'tsc'>);
8
+ pipe(files: Files, context: Context): Promise<Files>;
9
+ }
@@ -0,0 +1,93 @@
1
+ // typescript.ts
2
+ import ts from "typescript";
3
+ import { assertPromises, BuildFailure } from "@plugjs/plug/asserts";
4
+ import { Files } from "@plugjs/plug/files";
5
+ import { $p } from "@plugjs/plug/logging";
6
+ import { resolveAbsolutePath, resolveFile } from "@plugjs/plug/paths";
7
+ import { parseOptions, walk } from "@plugjs/plug/utils";
8
+ import { TypeScriptHost } from "./compiler.mjs";
9
+ import { getCompilerOptions } from "./options.mjs";
10
+ import { updateReport } from "./report.mjs";
11
+ var Tsc = class {
12
+ _tsconfig;
13
+ _options;
14
+ constructor(...args) {
15
+ const { params: [tsconfig], options } = parseOptions(args, {});
16
+ this._tsconfig = tsconfig;
17
+ this._options = options;
18
+ }
19
+ async pipe(files, context) {
20
+ const baseDir = context.resolve(".");
21
+ const report = context.log.report("TypeScript Report");
22
+ const { extraTypesDir, ...overrides } = { ...this._options };
23
+ const sourcesConfig = resolveFile(files.directory, "tsconfig.json");
24
+ const tsconfig = this._tsconfig ? context.resolve(this._tsconfig) : sourcesConfig || resolveFile(context.resolve("tsconfig.json"));
25
+ let rootDir;
26
+ if (overrides.rootDir) {
27
+ rootDir = overrides.rootDir = context.resolve(overrides.rootDir);
28
+ } else {
29
+ rootDir = overrides.rootDir = files.directory;
30
+ }
31
+ let outDir;
32
+ if (overrides.outDir) {
33
+ outDir = overrides.outDir = context.resolve(overrides.outDir);
34
+ } else {
35
+ outDir = overrides.outDir = rootDir;
36
+ }
37
+ if (overrides.rootDirs) {
38
+ overrides.rootDirs = overrides.rootDirs.map((dir) => context.resolve(dir));
39
+ }
40
+ if (overrides.baseUrl)
41
+ overrides.baseUrl = context.resolve(overrides.baseUrl);
42
+ if (overrides.outFile)
43
+ overrides.outFile = context.resolve(overrides.outFile);
44
+ const { errors, options } = await getCompilerOptions(
45
+ tsconfig,
46
+ overrides
47
+ );
48
+ updateReport(report, errors, baseDir);
49
+ if (report.errors)
50
+ report.done(true);
51
+ const paths = [...files.absolutePaths()];
52
+ for (const path of paths)
53
+ context.log.trace(`Compiling "${$p(path)}"`);
54
+ context.log.info("Compiling", paths.length, "files");
55
+ if (extraTypesDir) {
56
+ const directory = context.resolve(extraTypesDir);
57
+ for await (const file of walk(directory, ["**/*.d.ts"])) {
58
+ const path = resolveAbsolutePath(directory, file);
59
+ context.log.debug(`Including extra type file "${$p(path)}"`);
60
+ paths.push(path);
61
+ }
62
+ }
63
+ context.log.debug("Compliation options", options);
64
+ const host = new TypeScriptHost(rootDir);
65
+ const program = ts.createProgram(paths, options, host, void 0, errors);
66
+ const diagnostics = ts.getPreEmitDiagnostics(program);
67
+ updateReport(report, diagnostics, rootDir);
68
+ if (report.errors)
69
+ report.done(true);
70
+ const builder = Files.builder(outDir);
71
+ const promises = [];
72
+ const result = program.emit(void 0, (fileName, code) => {
73
+ promises.push(builder.write(fileName, code).then((file) => {
74
+ context.log.trace("Written", $p(file));
75
+ }).catch((error) => {
76
+ const outFile = resolveAbsolutePath(outDir, fileName);
77
+ context.log.error("Error writing to", $p(outFile), error);
78
+ throw BuildFailure.fail();
79
+ }));
80
+ });
81
+ await assertPromises(promises);
82
+ updateReport(report, result.diagnostics, rootDir);
83
+ if (report.errors)
84
+ report.done(true);
85
+ const outputs = builder.build();
86
+ context.log.info("TSC produced", outputs.length, "files into", $p(outputs.directory));
87
+ return outputs;
88
+ }
89
+ };
90
+ export {
91
+ Tsc
92
+ };
93
+ //# sourceMappingURL=typescript.mjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/typescript.ts"],
4
+ "mappings": ";AAGA,OAAO,QAAQ;AACf,SAAS,gBAAgB,oBAAoB;AAC7C,SAAS,aAAa;AACtB,SAAS,UAAU;AACnB,SAAS,qBAAqB,mBAAmB;AACjD,SAAS,cAAc,YAAY;AAEnC,SAAS,sBAAsB;AAC/B,SAAS,0BAA0B;AACnC,SAAS,oBAAoB;AAWtB,IAAM,MAAN,MAAiC;AAAA,EACrB;AAAA,EACA;AAAA,EAEjB,eAAe,MAA6B;AAC1C,UAAM,EAAE,QAAQ,CAAE,QAAS,GAAG,QAAQ,IAAI,aAAa,MAAM,CAAC,CAAC;AAC/D,SAAK,YAAY;AACjB,SAAK,WAAW;AAAA,EAClB;AAAA,EAEA,MAAM,KAAK,OAAc,SAAkC;AACzD,UAAM,UAAU,QAAQ,QAAQ,GAAG;AACnC,UAAM,SAAS,QAAQ,IAAI,OAAO,mBAAmB;AACrD,UAAM,EAAE,kBAAkB,UAAU,IAAI,EAAE,GAAG,KAAK,SAAS;AAM3D,UAAM,gBAAgB,YAAY,MAAM,WAAW,eAAe;AAClE,UAAM,WAAW,KAAK,YAAY,QAAQ,QAAQ,KAAK,SAAS,IAC9D,iBAAiB,YAAY,QAAQ,QAAQ,eAAe,CAAC;AAG/D,QAAI;AACJ,QAAI,UAAU,SAAS;AACrB,gBAAU,UAAU,UAAU,QAAQ,QAAQ,UAAU,OAAO;AAAA,IACjE,OAAO;AACL,gBAAU,UAAU,UAAU,MAAM;AAAA,IACtC;AAGA,QAAI;AACJ,QAAI,UAAU,QAAQ;AACpB,eAAS,UAAU,SAAS,QAAQ,QAAQ,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AQ,KAAK,QAAW,CAAC,UAAU,SAAS;AACzD,eAAS,KAAK,QAAQ,MAAM,UAAU,IAAI,EAAE,KAAK,CAAC,SAAS;AACzD,gBAAQ,IAAI,MAAM,WAAW,GAAG,IAAI,CAAC;AAAA,MACvC,CAAC,EAAE,MAAiC,CAAC,UAAU;AAC7C,cAAM,UAAU,oBAAoB,QAAQ,QAAQ;AACpD,gBAAQ,IAAI,MAAM,oBAAoB,GAAG,OAAO,GAAG,KAAK;AACxD,cAAM,aAAa,KAAK;AAAA,MAC1B,CAAC,CAAC;AAAA,IACJ,CAAC;AAGD,UAAM,eAAe,QAAQ;AAG7B,iBAAa,QAAQ,OAAO,aAAa,OAAO;AAEhD,QAAI,OAAO;AAAQ,aAAO,KAAK,IAAI;AAGnC,UAAM,UAAU,QAAQ,MAAM;AAC9B,YAAQ,IAAI,KAAK,gBAAgB,QAAQ,QAAQ,cAAc,GAAG,QAAQ,SAAS,CAAC;AACpF,WAAO;AAAA,EACT;AACF;",
5
+ "names": []
6
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@plugjs/typescript",
3
+ "version": "0.1.0",
4
+ "type": "commonjs",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "require": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.cjs"
13
+ },
14
+ "import": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.mjs"
17
+ }
18
+ },
19
+ "./typescript": {
20
+ "require": {
21
+ "types": "./dist/typescript.d.ts",
22
+ "default": "./dist/typescript.cjs"
23
+ },
24
+ "import": {
25
+ "types": "./dist/typescript.d.ts",
26
+ "default": "./dist/typescript.mjs"
27
+ }
28
+ }
29
+ },
30
+ "scripts": {
31
+ "build": "plug"
32
+ },
33
+ "author": "Juit Developers <developers@juit.com>",
34
+ "license": "Apache-2.0",
35
+ "devDependencies": {
36
+ "@plugjs/cov8": "^0.1.0",
37
+ "@plugjs/eslint": "^0.1.0",
38
+ "@plugjs/eslint-plugin": "^0.1.0",
39
+ "@plugjs/mocha": "^0.1.0",
40
+ "@plugjs/plug": "^0.1.0",
41
+ "@types/chai": "^4.3.3",
42
+ "@types/node": "<17",
43
+ "chai": "^4.3.6"
44
+ },
45
+ "dependencies": {
46
+ "typescript": "^4.8.4"
47
+ },
48
+ "peerDependencies": {
49
+ "@plugjs/plug": "^0.1.0"
50
+ },
51
+ "files": [
52
+ "*.md",
53
+ "dist/",
54
+ "src/"
55
+ ]
56
+ }
@@ -0,0 +1,70 @@
1
+ import ts from 'typescript' // TypeScript does NOT support ESM modules
2
+ import { resolveAbsolutePath } from '@plugjs/plug/paths'
3
+
4
+ import type { AbsolutePath } from '@plugjs/plug/paths'
5
+
6
+ export class TypeScriptHost
7
+ implements ts.CompilerHost {
8
+ constructor(directory: AbsolutePath)
9
+ constructor(private readonly _directory: AbsolutePath) {}
10
+
11
+ /* ======================================================================== */
12
+
13
+ /** Get a source file parsing one of our virtual files */
14
+ getSourceFile(
15
+ fileName: string,
16
+ languageVersion: ts.ScriptTarget,
17
+ ): ts.SourceFile | undefined {
18
+ const code = this.readFile(fileName)
19
+ if (code == null) return undefined // loose "undefined" check
20
+ return ts.createSourceFile(fileName, code, languageVersion)
21
+ }
22
+
23
+ /* coverage ignore next */
24
+ /** Never write any files */
25
+ writeFile(fileName: string): void {
26
+ throw new Error(`Cowardly refusing to write "${fileName}"`)
27
+ }
28
+
29
+ /** Get the default library associated with the given options */
30
+ getDefaultLibFileName(options: ts.CompilerOptions): string {
31
+ return ts.getDefaultLibFilePath(options)
32
+ }
33
+
34
+ /** Check for filesystem case sensitivity */
35
+ useCaseSensitiveFileNames(): boolean {
36
+ return ts.sys.useCaseSensitiveFileNames
37
+ }
38
+
39
+ /** Check for the existence of a given file */
40
+ fileExists(fileName: string): boolean {
41
+ return ts.sys.fileExists(resolveAbsolutePath(this.getCurrentDirectory(), fileName))
42
+ }
43
+
44
+ /** Read the file if it exists, otherwise return undefined */
45
+ readFile(fileName: string): string | undefined {
46
+ return ts.sys.readFile(resolveAbsolutePath(this.getCurrentDirectory(), fileName))
47
+ }
48
+
49
+ /** Return the current working directory */
50
+ getCurrentDirectory(): AbsolutePath {
51
+ return this._directory
52
+ }
53
+
54
+ /** Return the canonical name for the specified file */
55
+ getCanonicalFileName(fileName: string): string {
56
+ /* coverage ignore if */
57
+ if (ts.sys.useCaseSensitiveFileNames) return fileName
58
+
59
+ // Lifted from TypeScript sources
60
+ const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_. ]+/g
61
+ return fileNameLowerCaseRegExp.test(fileName) ?
62
+ fileName.replace(fileNameLowerCaseRegExp, (s) => s.toLowerCase()) :
63
+ fileName
64
+ }
65
+
66
+ /** Return the new line sequence used by this platform */
67
+ getNewLine(): string {
68
+ return ts.sys.newLine
69
+ }
70
+ }
package/src/index.ts ADDED
@@ -0,0 +1,58 @@
1
+ import { installForking } from '@plugjs/plug/fork'
2
+ import { requireResolve } from '@plugjs/plug/paths'
3
+
4
+ import type { CompilerOptions } from 'typescript'
5
+
6
+ /** TypeScript Compiler options with some additional properties */
7
+ export interface ExtendedCompilerOptions extends CompilerOptions {
8
+ /**
9
+ * An additional directory containing a set of `.d.ts` files which will
10
+ * be part of the compilation input, but not of the output.
11
+ *
12
+ * This can be useful when requiring (or fixing) specific types while
13
+ * compiling a project, but the definition of those types does not affect
14
+ * the resulting files (e.g. used only internally).
15
+ */
16
+ extraTypesDir?: string | undefined
17
+ }
18
+
19
+ declare module '@plugjs/plug' {
20
+ export interface Pipe {
21
+ /**
22
+ * Run the {@link https://www.typescriptlang.org/ TypeScript Compiler}
23
+ * over the input source files, using the default `tsconfig.json` file.
24
+ */
25
+ tsc(): Pipe
26
+
27
+ /**
28
+ * Run the {@link https://www.typescriptlang.org/ TypeScript Compiler}
29
+ * over the input source files, specifying the `tsconfig.json` file.
30
+ *
31
+ * @param configFile The `tsconfig.json` file to use.
32
+ */
33
+ tsc(configFile: string): Pipe
34
+
35
+ /**
36
+ * Run the {@link https://www.typescriptlang.org/ TypeScript Compiler}
37
+ * over the input source files, using the default `tsconfig.json` file
38
+ * and overriding some options
39
+ *
40
+ * @param options {@link ExtendedCompilerOptions | Options} overriding
41
+ * the contents of the default `tsconfig.json`.
42
+ */
43
+ tsc(options: ExtendedCompilerOptions): Pipe
44
+
45
+ /**
46
+ * Run the {@link https://www.typescriptlang.org/ TypeScript Compiler}
47
+ * over the input source files, specifying the `tsconfig.json` file
48
+ * and overriding some options
49
+ *
50
+ * @param configFile The `tsconfig.json` file to use.
51
+ * @param options {@link ExtendedCompilerOptions | Options} overriding
52
+ * the contents of the specified `tsconfig.json`.
53
+ */
54
+ tsc(configFile: string, options: ExtendedCompilerOptions): Pipe
55
+ }
56
+ }
57
+
58
+ installForking('tsc', requireResolve(__fileurl, './typescript'), 'Tsc')