1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.TestWorkspace = exports.compileJsiiForTest = exports.sourceToAssemblyHelper = void 0;
|
11 | const fs = require("node:fs");
|
12 | const os = require("node:os");
|
13 | const path = require("node:path");
|
14 | const spec_1 = require("@jsii/spec");
|
15 | const typescript_1 = require("typescript");
|
16 | const compiler_1 = require("./compiler");
|
17 | const project_info_1 = require("./project-info");
|
18 | const utils_1 = require("./utils");
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function sourceToAssemblyHelper(source, options) {
|
30 | return compileJsiiForTest(source, options).assembly;
|
31 | }
|
32 | exports.sourceToAssemblyHelper = sourceToAssemblyHelper;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function compileJsiiForTest(source, options, compilerOptions) {
|
44 | if (typeof source === 'string') {
|
45 | source = { 'index.ts': source };
|
46 | }
|
47 | const inSomeLocation = isOptionsObject(options) && options.compilationDirectory ? inOtherDir(options.compilationDirectory) : inTempDir;
|
48 |
|
49 |
|
50 | return inSomeLocation(() => {
|
51 | for (const [fileName, content] of Object.entries(source)) {
|
52 | fs.mkdirSync(path.dirname(fileName), { recursive: true });
|
53 | fs.writeFileSync(fileName, content, { encoding: 'utf-8' });
|
54 | }
|
55 | const { projectInfo, packageJson } = makeProjectInfo('index.ts', typeof options === 'function'
|
56 | ? options
|
57 | : (pi) => {
|
58 | Object.assign(pi, options?.packageJson ?? options?.projectInfo ?? {});
|
59 | });
|
60 | const compiler = new compiler_1.Compiler({
|
61 | projectInfo,
|
62 | ...compilerOptions,
|
63 | });
|
64 | const emitResult = compiler.emit();
|
65 | const errors = emitResult.diagnostics.filter((d) => d.category === typescript_1.DiagnosticCategory.Error);
|
66 | for (const error of errors) {
|
67 | console.error((0, utils_1.formatDiagnostic)(error, projectInfo.projectRoot));
|
68 |
|
69 | }
|
70 | if (errors.length > 0 || emitResult.emitSkipped) {
|
71 | throw new Error('There were compiler errors');
|
72 | }
|
73 | const assembly = (0, spec_1.loadAssemblyFromPath)(process.cwd(), false);
|
74 | const files = {};
|
75 | for (const filename of Object.keys(source)) {
|
76 | let jsFile = filename.replace(/\.ts$/, '.js');
|
77 | let dtsFile = filename.replace(/\.ts$/, '.d.ts');
|
78 | if (projectInfo.tsc?.outDir && filename !== 'README.md') {
|
79 | jsFile = path.join(projectInfo.tsc.outDir, jsFile);
|
80 | dtsFile = path.join(projectInfo.tsc.outDir, dtsFile);
|
81 | }
|
82 |
|
83 | files[jsFile] = fs.readFileSync(jsFile, { encoding: 'utf-8' });
|
84 |
|
85 | files[dtsFile] = fs.readFileSync(dtsFile, { encoding: 'utf-8' });
|
86 | const warningsFileName = '.warnings.jsii.js';
|
87 | if (fs.existsSync(warningsFileName)) {
|
88 |
|
89 | files[warningsFileName] = fs.readFileSync(warningsFileName, {
|
90 | encoding: 'utf-8',
|
91 | });
|
92 | }
|
93 | }
|
94 | return {
|
95 | assembly,
|
96 | files,
|
97 | packageJson,
|
98 | compressAssembly: isOptionsObject(options) && options.compressAssembly ? true : false,
|
99 | };
|
100 | });
|
101 | }
|
102 | exports.compileJsiiForTest = compileJsiiForTest;
|
103 | function inTempDir(block) {
|
104 | const origDir = process.cwd();
|
105 | const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsii'));
|
106 | process.chdir(tmpDir);
|
107 | const ret = block();
|
108 | process.chdir(origDir);
|
109 | fs.rmSync(tmpDir, { force: true, recursive: true });
|
110 | return ret;
|
111 | }
|
112 | function inOtherDir(dir) {
|
113 |
|
114 | return (block) => {
|
115 | const origDir = process.cwd();
|
116 | process.chdir(dir);
|
117 | try {
|
118 | return block();
|
119 | }
|
120 | finally {
|
121 | process.chdir(origDir);
|
122 | }
|
123 | };
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | function makeProjectInfo(types, cb) {
|
135 | const packageJson = {
|
136 | types,
|
137 | main: types.replace(/(?:\.d)?\.ts(x?)/, '.js$1'),
|
138 | name: 'testpkg',
|
139 | version: '0.0.1',
|
140 | license: 'Apache-2.0',
|
141 | author: { name: 'John Doe' },
|
142 | repository: { type: 'git', url: 'https://github.com/aws/jsii.git' },
|
143 | jsii: {},
|
144 | };
|
145 | if (cb) {
|
146 | cb(packageJson);
|
147 | }
|
148 | fs.writeFileSync('package.json', JSON.stringify(packageJson, (_, v) => v, 2), 'utf-8');
|
149 | const { projectInfo } = (0, project_info_1.loadProjectInfo)(path.resolve(process.cwd(), '.'));
|
150 | return { projectInfo, packageJson };
|
151 | }
|
152 | function isOptionsObject(x) {
|
153 | return x ? typeof x === 'object' : false;
|
154 | }
|
155 |
|
156 |
|
157 |
|
158 | class TestWorkspace {
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 | static create() {
|
165 | const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsii-testworkspace'));
|
166 | fs.mkdirSync(tmpDir, { recursive: true });
|
167 | return new TestWorkspace(tmpDir);
|
168 | }
|
169 | |
170 |
|
171 |
|
172 | static withWorkspace(block) {
|
173 | const ws = TestWorkspace.create();
|
174 | try {
|
175 | return block(ws);
|
176 | }
|
177 | finally {
|
178 | ws.cleanup();
|
179 | }
|
180 | }
|
181 | constructor(rootDirectory) {
|
182 | this.rootDirectory = rootDirectory;
|
183 | this.installed = new Set();
|
184 | }
|
185 | |
186 |
|
187 |
|
188 | addDependency(dependencyAssembly) {
|
189 | if (this.installed.has(dependencyAssembly.assembly.name)) {
|
190 | throw new Error(`A dependency with name '${dependencyAssembly.assembly.name}' was already installed. Give one a different name.`);
|
191 | }
|
192 | this.installed.add(dependencyAssembly.assembly.name);
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | const modDir = path.join(this.rootDirectory, 'node_modules', dependencyAssembly.assembly.name);
|
200 | fs.mkdirSync(modDir, { recursive: true });
|
201 | (0, spec_1.writeAssembly)(modDir, dependencyAssembly.assembly, {
|
202 | compress: dependencyAssembly.compressAssembly,
|
203 | });
|
204 | fs.writeFileSync(path.join(modDir, 'package.json'), JSON.stringify(dependencyAssembly.packageJson, null, 2), 'utf-8');
|
205 | for (const [fileName, fileContents] of Object.entries(dependencyAssembly.files)) {
|
206 | fs.mkdirSync(path.dirname(path.join(modDir, fileName)), {
|
207 | recursive: true,
|
208 | });
|
209 | fs.writeFileSync(path.join(modDir, fileName), fileContents);
|
210 | }
|
211 | }
|
212 | dependencyDir(name) {
|
213 | if (!this.installed.has(name)) {
|
214 | throw new Error(`No dependency with name '${name}' has been installed`);
|
215 | }
|
216 | return path.join(this.rootDirectory, 'node_modules', name);
|
217 | }
|
218 | cleanup() {
|
219 | fs.rmSync(this.rootDirectory, { force: true, recursive: true });
|
220 | }
|
221 | }
|
222 | exports.TestWorkspace = TestWorkspace;
|
223 |
|
\ | No newline at end of file |