@build-script/autoindex 0.0.1

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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +29 -0
  3. package/autoindex.schema.json +63 -0
  4. package/config/rig.json +5 -0
  5. package/lib/bin.d.ts +2 -0
  6. package/lib/bin.d.ts.map +1 -0
  7. package/lib/bin.js +102 -0
  8. package/lib/bin.js.map +1 -0
  9. package/lib/common/cli.d.ts +22 -0
  10. package/lib/common/cli.d.ts.map +1 -0
  11. package/lib/common/cli.js +92 -0
  12. package/lib/common/cli.js.map +1 -0
  13. package/lib/common/config.d.ts +15 -0
  14. package/lib/common/config.d.ts.map +1 -0
  15. package/lib/common/config.js +79 -0
  16. package/lib/common/config.js.map +1 -0
  17. package/lib/common/create.d.ts +22 -0
  18. package/lib/common/create.d.ts.map +1 -0
  19. package/lib/common/create.js +123 -0
  20. package/lib/common/create.js.map +1 -0
  21. package/lib/common/tsconfig-loader.d.ts +14 -0
  22. package/lib/common/tsconfig-loader.d.ts.map +1 -0
  23. package/lib/common/tsconfig-loader.js +94 -0
  24. package/lib/common/tsconfig-loader.js.map +1 -0
  25. package/lib/common/typescript.d.ts +7 -0
  26. package/lib/common/typescript.d.ts.map +1 -0
  27. package/lib/common/typescript.js +23 -0
  28. package/lib/common/typescript.js.map +1 -0
  29. package/lib/plugin.d.ts +2 -0
  30. package/lib/plugin.d.ts.map +1 -0
  31. package/lib/plugin.js +3 -0
  32. package/lib/plugin.js.map +1 -0
  33. package/lib/tsconfig.tsbuildinfo +1 -0
  34. package/loader/bin.devel.js +3 -0
  35. package/loader/bin.js +8 -0
  36. package/package.json +49 -0
  37. package/src/bin.ts +123 -0
  38. package/src/common/cli.ts +118 -0
  39. package/src/common/config.ts +105 -0
  40. package/src/common/create.ts +168 -0
  41. package/src/common/tsconfig-loader.ts +119 -0
  42. package/src/common/typescript.ts +26 -0
  43. package/src/plugin.ts +1 -0
  44. package/src/tsconfig.json +9 -0
package/src/bin.ts ADDED
@@ -0,0 +1,123 @@
1
+ import { startChokidar } from '@idlebox/chokidar';
2
+ import { createRootLogger, logger } from '@idlebox/logger';
3
+ import type { IgnoreFiles } from '@idlebox/typescript-surface-analyzer';
4
+ import { channelClient } from '@mpis/client';
5
+ import { glob } from 'glob';
6
+ import { resolve } from 'node:path';
7
+ import { parseArgs } from './common/cli.js';
8
+ import { createContext } from './common/config.js';
9
+ import { createIndex } from './common/create.js';
10
+ import { loadTsConfigJson } from './common/tsconfig-loader.js';
11
+ import { loadTypescript } from './common/typescript.js';
12
+
13
+ createRootLogger('autoindex');
14
+
15
+ let tsData: Awaited<ReturnType<typeof loadTypescript>>;
16
+ async function loadTypescriptOnce() {
17
+ if (!tsData) {
18
+ tsData = await loadTypescript(context);
19
+ }
20
+ return tsData;
21
+ }
22
+
23
+ async function main() {
24
+ let outputs = '';
25
+ logger.stream.on('data', (data) => {
26
+ outputs += data.toString();
27
+ });
28
+
29
+ channelClient.start();
30
+
31
+ const { ts, file: tsconfigFile } = await loadTypescriptOnce();
32
+
33
+ try {
34
+ const { command, configFiles } = loadTsConfigJson(ts, tsconfigFile, {
35
+ exclude: context.excludePatterns,
36
+ include: context.includePatterns,
37
+ });
38
+
39
+ if (context.verboseMode) {
40
+ logger.debug('options:', command.options);
41
+ logger.debug('command:');
42
+ for (const file of command.fileNames) {
43
+ logger.debug(' - %s', file);
44
+ }
45
+ }
46
+
47
+ const rootDir = command.options.rootDir;
48
+ if (!rootDir) {
49
+ throw logger.fatal('无法确定rootDir,请添加tsconfig.json中的compilerOptions.rootDir设置。');
50
+ }
51
+
52
+ logger.debug('rootDir=%s', rootDir);
53
+
54
+ const outputFile = resolve(rootDir, context.outputFile + '.ts');
55
+
56
+ if (!outputFile.startsWith(rootDir)) {
57
+ throw logger.fatal(`输出文件 ${outputFile} 路径异常,离开rootDir`);
58
+ }
59
+
60
+ const r = await createIndex({
61
+ ts,
62
+ project: command,
63
+ outputFileAbs: outputFile,
64
+ logger,
65
+ absoluteImport: context.absoluteImport,
66
+ stripTags: context.stripTags,
67
+ extraExcludes: context.excludePatterns,
68
+ });
69
+
70
+ r.watchFiles.push(...configFiles);
71
+
72
+ channelClient.success('autoindex success', outputs);
73
+ return r;
74
+ } catch (e) {
75
+ channelClient.failed('autoindex failed', outputs);
76
+ throw e;
77
+ }
78
+ }
79
+
80
+ const args = await parseArgs();
81
+ logger.debug`arguments: ${args}`;
82
+
83
+ const context = await createContext(args, logger);
84
+
85
+ if (!context.project) {
86
+ logger.log`没有任务需要执行`;
87
+ if (context.watchMode) {
88
+ setInterval(() => {
89
+ // x
90
+ }, 10000);
91
+ }
92
+
93
+ channelClient.success('no task to execute');
94
+ } else if (context.watchMode) {
95
+ let lastExecuteIgnore: IgnoreFiles | undefined;
96
+ async function exec() {
97
+ const r = await main();
98
+
99
+ lastExecuteIgnore = r.ignores;
100
+
101
+ watcher.add(r.watchFiles);
102
+ }
103
+
104
+ function testAndRun(changes: readonly string[]) {
105
+ if (lastExecuteIgnore && lastExecuteIgnore.filter(changes).length === 0) {
106
+ logger.debug('%s file changes, but all ignored.', changes.length);
107
+ return;
108
+ }
109
+ logger.log('rebuild due to %s file changes: %s', changes.length, changes[0]);
110
+ return exec();
111
+ }
112
+
113
+ const watcher = startChokidar(testAndRun, {
114
+ watchingEvents: ['add', 'change', 'unlink', 'addDir'],
115
+ });
116
+
117
+ const content = await glob('**/', { absolute: true, cwd: context.project, ignore: ['**/node_modules/**'] });
118
+ watcher.add(content);
119
+
120
+ await exec();
121
+ } else {
122
+ await main();
123
+ }
@@ -0,0 +1,118 @@
1
+ import { createArgsReader } from '@idlebox/args';
2
+ import { EnableLogLevel, logger } from '@idlebox/logger';
3
+ import { findUpUntilSync } from '@idlebox/node';
4
+ import { resolve } from 'node:path';
5
+
6
+ export function printUsage() {
7
+ console.log(
8
+ `Usage: autoindex [options] <tsconfig.json>
9
+ Options:
10
+ -w, --watch 监视模式
11
+ -d, --debug 显示调试输出
12
+ -C, --config 读取 config/autoindex.json 配置文件
13
+ 如果设置了该选项,但是配置文件不存在,则直接退出,什么也不做
14
+ -o, --output <file> 输出文件路径(默认: autoindex.generated,相对于tsconfig.json所在目录)
15
+ --exclude <pattern> 额外排除的文件或目录
16
+ --include <pattern> 额外包含的文件或目录
17
+ -a, --absolute <path> 绝对导入路径前缀
18
+ --skip-tag <tag> 忽略被 @tag 注释的符号
19
+ --no-config 忽略 "config/autoindex.json" 文件
20
+ -h, --help 显示帮助信息
21
+ `.trim(),
22
+ );
23
+ }
24
+
25
+ export enum ConfigKind {
26
+ DISABLE = 'disable',
27
+ IMPLICIT = 'implicit',
28
+ EXPLICIT = 'explicit',
29
+ }
30
+
31
+ export interface ICliArgs {
32
+ readonly configType: ConfigKind;
33
+ readonly watchMode: boolean;
34
+ readonly debugMode: boolean;
35
+ readonly outputFile?: string;
36
+ readonly excludePatterns: string[];
37
+ readonly includePatterns: string[];
38
+ readonly absoluteImport?: string;
39
+ readonly skipTags: string[];
40
+ readonly project?: string;
41
+ readonly verboseMode: boolean;
42
+ }
43
+
44
+ export async function parseArgs() {
45
+ try {
46
+ return await _parseArgs();
47
+ } catch (e: any) {
48
+ printUsage();
49
+
50
+ throw logger.fatal(e.message);
51
+ }
52
+ }
53
+ async function _parseArgs(): Promise<ICliArgs> {
54
+ const argv = createArgsReader(process.argv.slice(2));
55
+
56
+ if (argv.flag(['-h', '--help']) > 0) {
57
+ printUsage();
58
+ process.exit(0);
59
+ }
60
+
61
+ const watchMode = argv.flag(['-w', '--watch']) > 0;
62
+ const debugMode = argv.flag(['-d', '--debug']) > 0;
63
+ const verboseMode = argv.flag(['-d', '--debug']) > 1;
64
+ const outputFile = argv.single(['-o', '--output']);
65
+ const excludePatterns = argv.multiple(['--exclude']);
66
+ const includePatterns = argv.multiple(['--include']);
67
+ const absoluteImport = argv.single(['-a', '--absolute']);
68
+ const stripTags = argv.multiple(['--skip-tag']);
69
+ const projVal = argv.range(0, 1)[0];
70
+
71
+ let configType = ConfigKind.IMPLICIT;
72
+ if (argv.flag(['--config']) > 0) {
73
+ configType = ConfigKind.EXPLICIT;
74
+ } else if (argv.flag(['--config']) < 0) {
75
+ configType = ConfigKind.DISABLE;
76
+ }
77
+
78
+ if (verboseMode) {
79
+ logger.enable(EnableLogLevel.verbose);
80
+ } else if (debugMode) {
81
+ logger.enable(EnableLogLevel.debug);
82
+ }
83
+
84
+ if (argv.unused().length) {
85
+ logger.fatal`未知的命令行参数: ${argv.unused().join(', ')}`;
86
+ }
87
+
88
+ if (absoluteImport && absoluteImport[0] !== '#') {
89
+ throw logger.fatal(`绝对导入路径必须以'#'开头: ${absoluteImport}`);
90
+ }
91
+
92
+ if (stripTags.length === 0) {
93
+ stripTags.push('internal');
94
+ }
95
+
96
+ const project = projVal ? resolve(process.cwd(), projVal) : undefined;
97
+
98
+ return {
99
+ configType,
100
+ watchMode,
101
+ debugMode,
102
+ outputFile,
103
+ excludePatterns,
104
+ includePatterns,
105
+ absoluteImport,
106
+ skipTags: stripTags,
107
+ project,
108
+ verboseMode,
109
+ };
110
+ }
111
+
112
+ export type ICliCtx = ReturnType<typeof parseArgs>;
113
+
114
+ const _schemaFile = findUpUntilSync({ file: 'autoindex.schema.json', from: import.meta.dirname });
115
+ if (!_schemaFile) {
116
+ throw new Error('missing schema file: autoindex.schema.json');
117
+ }
118
+ export const schemaFile = _schemaFile;
@@ -0,0 +1,105 @@
1
+ import { NotFoundError, ProjectConfig } from '@build-script/rushstack-config-loader';
2
+ import type { IMyLogger } from '@idlebox/logger';
3
+ import { findUpUntilSync } from '@idlebox/node';
4
+ import { resolve } from 'node:path';
5
+ import { ConfigKind, schemaFile, type ICliArgs } from './cli.js';
6
+
7
+ interface IConfigFile {
8
+ project?: string;
9
+ output?: string;
10
+ include?: string[];
11
+ exclude?: string[];
12
+ stripTags?: string[];
13
+ absolute?: string;
14
+ }
15
+
16
+ export interface IContext {
17
+ watchMode: boolean;
18
+ debugMode: boolean;
19
+ outputFile: string;
20
+ excludePatterns: string[];
21
+ includePatterns: string[];
22
+ absoluteImport: undefined | string;
23
+ stripTags: string[];
24
+ project: string;
25
+ verboseMode: boolean;
26
+ }
27
+
28
+ async function loadConfigFile(configType: ConfigKind, context: Partial<IContext>, logger: IMyLogger) {
29
+ if (configType === ConfigKind.DISABLE) {
30
+ logger.debug`由于命令行参数,跳过配置文件: config/autoindex.json`;
31
+ return;
32
+ }
33
+
34
+ logger.debug`即将加载配置文件: config/autoindex.json`;
35
+ logger.verbose`使用schema文件: ${schemaFile}`;
36
+
37
+ const packageJsonFile = findUpUntilSync({ file: 'package.json', from: context.project ?? process.cwd() });
38
+ if (!packageJsonFile) {
39
+ throw logger.fatal`无法找到项目根目录,请确保在正确的目录下运行。`;
40
+ }
41
+ logger.debug`项目package: ${packageJsonFile}`;
42
+ const projectRoot = resolve(packageJsonFile, '..');
43
+
44
+ const config = new ProjectConfig(projectRoot, undefined, logger);
45
+
46
+ try {
47
+ const configFileData = config.loadSingleJson<IConfigFile>('autoindex', schemaFile);
48
+ logger.verbose`内容: ${configFileData}`;
49
+
50
+ if (!context.project) {
51
+ if (!configFileData.project) {
52
+ throw logger.fatal`配置文件中未指定项目路径,必须传入参数`;
53
+ }
54
+ context.project = configFileData.project;
55
+ }
56
+
57
+ if (!context.absoluteImport && configFileData.absolute) context.absoluteImport = configFileData.absolute;
58
+
59
+ if (!context.outputFile && configFileData.output) context.outputFile = configFileData.output;
60
+
61
+ if (!context.includePatterns) context.includePatterns = [];
62
+ if (configFileData.include?.length) context.includePatterns.push(...configFileData.include);
63
+
64
+ if (!context.excludePatterns) context.excludePatterns = [];
65
+ if (configFileData.exclude?.length) context.excludePatterns.push(...configFileData.exclude);
66
+
67
+ if (!context.stripTags) context.stripTags = [];
68
+ if (configFileData.stripTags?.length) context.stripTags.push(...configFileData.stripTags);
69
+ } catch (e: unknown) {
70
+ if (e instanceof NotFoundError) {
71
+ logger.verbose`由于文件不存在,未使用配置文件(${e.message})`;
72
+ } else {
73
+ throw e;
74
+ }
75
+ }
76
+ }
77
+
78
+ export async function createContext(args: ICliArgs, logger: IMyLogger): Promise<IContext> {
79
+ const context: Partial<IContext> = {
80
+ watchMode: args.watchMode,
81
+ debugMode: args.debugMode,
82
+ outputFile: args.outputFile,
83
+ excludePatterns: args.excludePatterns,
84
+ includePatterns: args.includePatterns,
85
+ absoluteImport: undefined,
86
+ stripTags: args.skipTags,
87
+ project: args.project,
88
+ verboseMode: args.verboseMode,
89
+ };
90
+
91
+ await loadConfigFile(args.configType, context, logger);
92
+
93
+ if (!context.outputFile) {
94
+ context.outputFile = './autoindex.generated';
95
+ }
96
+ if (!context.project) {
97
+ if (args.configType === ConfigKind.IMPLICIT) {
98
+ // 如果是隐式配置,则必须在命令行中指定项目路径
99
+ logger.fatal`未指定项目路径,需要额外参数或在配置文件中指定`;
100
+ }
101
+ }
102
+
103
+ logger.verbose`最终配置: ${context}`;
104
+ return context as IContext;
105
+ }
@@ -0,0 +1,168 @@
1
+ import { camelCase, ucfirst } from '@idlebox/common';
2
+ import type { IMyLogger } from '@idlebox/logger';
3
+ import { ensureParentExists, relativePath, writeFileIfChange } from '@idlebox/node';
4
+ import {
5
+ ExportKind,
6
+ IgnoreFiles,
7
+ TypescriptProject,
8
+ type IIdentifierResult,
9
+ } from '@idlebox/typescript-surface-analyzer';
10
+ import { basename, dirname } from 'node:path';
11
+ import type TypeScriptApi from 'typescript';
12
+
13
+ export function idToString(ts: typeof TypeScriptApi, id: TypeScriptApi.StringLiteral | TypeScriptApi.Identifier) {
14
+ if (ts.isIdentifier(id)) {
15
+ return id.escapedText.toString();
16
+ }
17
+ return id.text;
18
+ }
19
+
20
+ export interface ICreateIndexContext {
21
+ absoluteImport?: string;
22
+ stripTags?: readonly string[];
23
+ extraExcludes?: readonly string[];
24
+ ts: typeof TypeScriptApi;
25
+ project: TypeScriptApi.ParsedCommandLine;
26
+ outputFileAbs: string;
27
+ logger: IMyLogger;
28
+ verbose?: boolean;
29
+ }
30
+
31
+ export function createStandaloneIgnore(logger: IMyLogger, outputFileAbs: string, extraExcludes?: readonly string[]) {
32
+ const ignores = new IgnoreFiles(logger);
33
+ applyIgnores(ignores, outputFileAbs, extraExcludes);
34
+ return ignores;
35
+ }
36
+
37
+ function applyIgnores(ignores: IgnoreFiles, outputFileAbs: string, extraExcludes?: readonly string[]) {
38
+ ignores.add(function ignoreIndexItSelf(f: string) {
39
+ return f === outputFileAbs;
40
+ });
41
+ ignores.add('**/*.test.ts');
42
+ ignores.add('**/*.test.tsx');
43
+ ignores.add('**/*.test.d/**');
44
+ ignores.add('**/node_modules/**');
45
+ if (extraExcludes) {
46
+ for (const exclude of extraExcludes) {
47
+ ignores.add(exclude);
48
+ }
49
+ }
50
+ }
51
+
52
+ interface IRet {
53
+ watchFiles: string[];
54
+ ignores: IgnoreFiles;
55
+ }
56
+
57
+ export async function createIndex({
58
+ logger,
59
+ outputFileAbs,
60
+ project,
61
+ ts,
62
+ absoluteImport,
63
+ extraExcludes = [],
64
+ stripTags = ['internal'],
65
+ }: ICreateIndexContext): Promise<IRet> {
66
+ if (!project.options.rootDir || !project.options.configFilePath) {
67
+ logger.error('%o', project.options);
68
+ throw new Error(`missing rootDir and {internal}configFilePath`);
69
+ }
70
+ if (!project.options.rootDir) {
71
+ logger.error('%o', project.options);
72
+ throw new Error(`missing rootDir: ${project.options.configFilePath}`);
73
+ }
74
+
75
+ const p = new TypescriptProject(ts, project, logger);
76
+ logger.log('creating index file: %s', outputFileAbs);
77
+ applyIgnores(p.additionalIgnores, outputFileAbs, extraExcludes);
78
+
79
+ const list = p.execute(stripTags);
80
+
81
+ const indexDir = `./${relativePath(
82
+ project.options.rootDir || dirname(project.options.configFilePath as string),
83
+ dirname(outputFileAbs),
84
+ )
85
+ .split('/')
86
+ .filter((e) => e && e !== '.')
87
+ .map(() => '..')
88
+ .join('/')}`;
89
+ logger.debug('rootDir: %s', project.options.rootDir);
90
+ logger.debug('configFilePath: %s', project.options.configFilePath);
91
+ logger.debug('index dir: %s', indexDir);
92
+ const header = ['// DO NOT EDIT THIS FILE', '// @ts-ignore', '/* eslint-disable */'];
93
+ const content = [];
94
+ const input_files = [];
95
+ for (const file of list) {
96
+ if (file.absolutePath === outputFileAbs) {
97
+ throw new Error(`override output file: ${outputFileAbs} (are you import xxx from ${basename(outputFileAbs)}?)`);
98
+ }
99
+
100
+ const path = importSpec(absoluteImport ?? indexDir, file.relativePath);
101
+ content.push(`/* ${file.relativePath} */`);
102
+
103
+ if (file.identifiers) {
104
+ content.push('\t// Identifiers');
105
+ for (const def of file.identifiers.values()) {
106
+ if (def.reference) {
107
+ if (!def.reference.id && def.reference.type === 'file') {
108
+ // 从本包另一个文件export,并且没有改名(x as y)
109
+ continue;
110
+ }
111
+ }
112
+ content.push(`\texport ${typeTag(def)}{ ${idToString(ts, def.id)} } from "${path}";`);
113
+ }
114
+ }
115
+ if (file.references.length) {
116
+ content.push('\t// References');
117
+ for (const { reference } of file.references) {
118
+ if (reference.type === 'file') {
119
+ content.push(`\texport * from "${importSpec(indexDir, reference.relativeFromRoot)}";`);
120
+ } else {
121
+ content.push(`\texport * from "${reference.name}";`);
122
+ }
123
+ }
124
+ }
125
+ if (file.defaultExport) {
126
+ content.push('\t// Default');
127
+ let id = '';
128
+ if (file.defaultExport.id) {
129
+ id = idToString(ts, file.defaultExport.id);
130
+ } else {
131
+ id = camelCase(file.relativePath);
132
+ if (file.defaultExport.kind === ExportKind.Class || file.defaultExport.kind === ExportKind.Type) {
133
+ id = ucfirst(id);
134
+ }
135
+ }
136
+ content.push(`\texport { default as ${id} } from "${path}";`);
137
+ }
138
+
139
+ input_files.push(file.absolutePath);
140
+ }
141
+
142
+ await ensureParentExists(outputFileAbs);
143
+ const r = await writeFileIfChange(outputFileAbs, `${header.join('\n')}\n\n${content.join('\n')}`);
144
+
145
+ if (r) {
146
+ logger.log('index create ok.');
147
+ } else {
148
+ logger.log('index create ok. (unchange)');
149
+ }
150
+
151
+ return {
152
+ watchFiles: input_files,
153
+ ignores: p.additionalIgnores,
154
+ };
155
+ }
156
+
157
+ const tsExt = /\.tsx?$/;
158
+ const prefxUneedDot = /^\.\/\.\.\//;
159
+ function importSpec(indexDir: string, target: string) {
160
+ return `${indexDir}/${target.replace(tsExt, '.js')}`.replace(/\/\//g, '/').replace(prefxUneedDot, '../');
161
+ }
162
+
163
+ function typeTag(def: IIdentifierResult) {
164
+ if (def.kind === ExportKind.Type) {
165
+ return 'type ';
166
+ }
167
+ return '';
168
+ }
@@ -0,0 +1,119 @@
1
+ import type { IMyLogger } from '@idlebox/logger';
2
+ import { isModuleResolutionError } from '@idlebox/node';
3
+ import { parse, stringify } from 'comment-json';
4
+ import { createRequire } from 'node:module';
5
+ import { normalize, resolve } from 'node:path';
6
+ import type TypeScriptApi from 'typescript';
7
+
8
+ export interface ILoadedConfigFile {
9
+ readonly command: TypeScriptApi.ParsedCommandLine;
10
+ readonly configFiles: readonly string[];
11
+ }
12
+
13
+ interface ILoadTsConfigJsonOptions {
14
+ exclude?: string[];
15
+ include?: string[];
16
+ }
17
+
18
+ export function loadTsConfigJson(
19
+ ts: typeof TypeScriptApi,
20
+ tsconfigJson: string,
21
+ options: ILoadTsConfigJsonOptions = {},
22
+ ): ILoadedConfigFile {
23
+ const { exclude, include } = options;
24
+
25
+ const readFiles: string[] = [];
26
+ function readFile(file: string, encoding?: string) {
27
+ readFiles.push(file);
28
+ const text = ts.sys.readFile(file, encoding);
29
+ if (!text) throw new Error(`failed read file: ${file}`);
30
+ const json = ts.parseConfigFileTextToJson(file, text);
31
+ if (json.error) {
32
+ return text;
33
+ }
34
+ if (Array.isArray(json.config?.exclude)) {
35
+ json.config.exclude = json.config.exclude.filter((l: string) => {
36
+ return !l.endsWith('.generated.ts');
37
+ });
38
+ }
39
+ return JSON.stringify(json.config);
40
+ }
41
+
42
+ const myFormatDiagnosticsHost: TypeScriptApi.FormatDiagnosticsHost = {
43
+ getCurrentDirectory: ts.sys.getCurrentDirectory,
44
+ getCanonicalFileName: normalize,
45
+ getNewLine(): string {
46
+ return ts.sys.newLine;
47
+ },
48
+ };
49
+
50
+ const host: TypeScriptApi.ParseConfigFileHost = {
51
+ onUnRecoverableConfigFileDiagnostic(diagnostic: TypeScriptApi.Diagnostic) {
52
+ throw new Error(ts.formatDiagnostic(diagnostic, myFormatDiagnosticsHost).trim());
53
+ },
54
+ useCaseSensitiveFileNames: true,
55
+ readDirectory: ts.sys.readDirectory,
56
+ getCurrentDirectory: ts.sys.getCurrentDirectory,
57
+ fileExists: ts.sys.fileExists,
58
+ readFile: ts.sys.readFile,
59
+ };
60
+
61
+ let config_patched = true;
62
+ if (exclude?.length || include?.length) {
63
+ config_patched = false;
64
+ Object.assign(host, {
65
+ readFile(file: string, encoding?: string) {
66
+ let text = readFile(file, encoding);
67
+ if (file === tsconfigJson) {
68
+ const content: any = parse(text);
69
+ if (exclude?.length) {
70
+ if (!content.exclude) content.exclude = [];
71
+ content.exclude.push(...exclude);
72
+ }
73
+ if (include?.length) {
74
+ if (!content.include) content.include = [];
75
+ content.include.push(...include);
76
+ }
77
+ text = stringify(content, null, 2);
78
+ config_patched = true;
79
+ }
80
+ return text;
81
+ },
82
+ });
83
+ }
84
+
85
+ const command = ts.getParsedCommandLineOfConfigFile(tsconfigJson, {}, host);
86
+
87
+ if (!command) {
88
+ throw new Error('fatal error, can not continue');
89
+ }
90
+
91
+ if (!config_patched) {
92
+ throw new Error(
93
+ `tsconfig.json file "${tsconfigJson}" has not been patched with exclude/include options, please report issue.`,
94
+ );
95
+ }
96
+
97
+ if (!command.options.rootDir) {
98
+ command.options.rootDir = resolve(tsconfigJson, '..');
99
+ }
100
+
101
+ return { command, configFiles: readFiles };
102
+ }
103
+
104
+ function interop(v: any) {
105
+ return v.default ?? v;
106
+ }
107
+
108
+ export async function getTypescript(tsconfigFile: string, logger?: IMyLogger): Promise<typeof TypeScriptApi> {
109
+ const require = createRequire(tsconfigFile);
110
+ try {
111
+ return require('typescript');
112
+ } catch (e) {
113
+ if (isModuleResolutionError(e)) {
114
+ logger?.error('typescript not found in target project, using bundled one.');
115
+ return interop(await import('typescript'));
116
+ }
117
+ throw e;
118
+ }
119
+ }
@@ -0,0 +1,26 @@
1
+ import { logger } from '@idlebox/logger';
2
+ import { existsSync, statSync } from 'node:fs';
3
+ import { resolve } from 'node:path';
4
+ import type TypeScriptApi from 'typescript';
5
+ import type { IContext } from './config.js';
6
+ import { getTypescript } from './tsconfig-loader.js';
7
+
8
+ export async function loadTypescript(context: IContext) {
9
+ let tsconfigFile: string = context.project;
10
+ if (statSync(tsconfigFile).isDirectory()) {
11
+ tsconfigFile = resolve(tsconfigFile, 'tsconfig.json');
12
+ if (!existsSync(tsconfigFile)) {
13
+ throw logger.fatal(`missing "tsconfig.json" in: ${tsconfigFile}`);
14
+ }
15
+ } else if (!existsSync(tsconfigFile)) {
16
+ throw logger.fatal(`missing tsconfig: ${tsconfigFile}`);
17
+ }
18
+
19
+ const ts: typeof TypeScriptApi = await getTypescript(tsconfigFile, logger);
20
+ logger.log('typescript version: %s', ts.version);
21
+
22
+ return {
23
+ ts,
24
+ file: tsconfigFile,
25
+ } as const;
26
+ }
package/src/plugin.ts ADDED
@@ -0,0 +1 @@
1
+ throw new Error('not implemented');
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@internal/local-rig/profiles/default/tsconfig.json",
3
+ "compilerOptions": {
4
+ "typeRoots": ["../node_modules/@types", "../node_modules"],
5
+ "outDir": "../lib",
6
+ "rootDir": "./",
7
+ "types": ["node"]
8
+ }
9
+ }