|
1 | 1 | import * as spec from '@jsii/spec';
|
| 2 | +import { loadAssemblyFromFile, loadAssemblyFromPath, getAssemblyFile, writeAssembly } from '@jsii/spec'; |
2 | 3 | import * as crypto from 'crypto';
|
3 | 4 | import * as fs from 'fs-extra';
|
4 | 5 | import * as path from 'path';
|
@@ -55,35 +56,28 @@ export interface LoadedAssembly {
|
55 | 56 | /**
|
56 | 57 | * Load assemblies by filename or directory
|
57 | 58 | */
|
58 |
| -export async function loadAssemblies( |
| 59 | +export function loadAssemblies( |
59 | 60 | assemblyLocations: readonly string[],
|
60 | 61 | validateAssemblies: boolean,
|
61 |
| -): Promise<readonly LoadedAssembly[]> { |
62 |
| - return Promise.all(assemblyLocations.map(loadAssembly)); |
| 62 | +): readonly LoadedAssembly[] { |
| 63 | + return assemblyLocations.map(loadAssembly); |
63 | 64 |
|
64 |
| - async function loadAssembly(location: string): Promise<LoadedAssembly> { |
65 |
| - const stat = await fs.stat(location); |
| 65 | + function loadAssembly(location: string): LoadedAssembly { |
| 66 | + const stat = fs.statSync(location); |
66 | 67 | if (stat.isDirectory()) {
|
67 |
| - return loadAssembly(path.join(location, '.jsii')); |
| 68 | + return loadAssembly(getAssemblyFile(location)); |
68 | 69 | }
|
69 | 70 |
|
70 | 71 | const directory = path.dirname(location);
|
71 | 72 | const pjLocation = path.join(directory, 'package.json');
|
72 | 73 |
|
73 |
| - const [assembly, packageJson] = await Promise.all([ |
74 |
| - loadAssemblyFromFile(location, validateAssemblies), |
75 |
| - (await fs.pathExists(pjLocation)) ? fs.readJSON(pjLocation, { encoding: 'utf-8' }) : Promise.resolve(undefined), |
76 |
| - ]); |
| 74 | + const assembly = loadAssemblyFromFile(location, validateAssemblies); |
| 75 | + const packageJson = fs.pathExistsSync(pjLocation) ? fs.readJSONSync(pjLocation, { encoding: 'utf-8' }) : undefined; |
77 | 76 |
|
78 | 77 | return { assembly, directory, packageJson };
|
79 | 78 | }
|
80 | 79 | }
|
81 | 80 |
|
82 |
| -async function loadAssemblyFromFile(filename: string, validate: boolean): Promise<spec.Assembly> { |
83 |
| - const contents = await fs.readJSON(filename, { encoding: 'utf-8' }); |
84 |
| - return validate ? spec.validateAssembly(contents) : (contents as spec.Assembly); |
85 |
| -} |
86 |
| - |
87 | 81 | /**
|
88 | 82 | * Load the default tablets for every assembly, if available
|
89 | 83 | *
|
@@ -236,12 +230,12 @@ export async function allTypeScriptSnippets(
|
236 | 230 | * Replaces the file where the original assembly file *should* be found with a new assembly file.
|
237 | 231 | * Recalculates the fingerprint of the assembly to avoid tampering detection.
|
238 | 232 | */
|
239 |
| -export async function replaceAssembly(assembly: spec.Assembly, directory: string): Promise<void> { |
240 |
| - const fileName = path.join(directory, '.jsii'); |
241 |
| - await fs.writeJson(fileName, _fingerprint(assembly), { |
242 |
| - encoding: 'utf8', |
243 |
| - spaces: 2, |
244 |
| - }); |
| 233 | +export function replaceAssembly( |
| 234 | + assembly: spec.Assembly, |
| 235 | + directory: string, |
| 236 | + { compress = false }: { compress?: boolean } = {}, |
| 237 | +) { |
| 238 | + writeAssembly(directory, _fingerprint(assembly), { compress }); |
245 | 239 | }
|
246 | 240 |
|
247 | 241 | /**
|
@@ -296,24 +290,23 @@ export function findTypeLookupAssembly(startingDirectory: string): TypeLookupAss
|
296 | 290 | }
|
297 | 291 |
|
298 | 292 | function loadLookupAssembly(directory: string): TypeLookupAssembly | undefined {
|
299 |
| - const assemblyFile = path.join(directory, '.jsii'); |
300 |
| - if (!fs.pathExistsSync(assemblyFile)) { |
| 293 | + try { |
| 294 | + const packageJson = fs.readJSONSync(path.join(directory, 'package.json'), { encoding: 'utf-8' }); |
| 295 | + const assembly: spec.Assembly = loadAssemblyFromPath(directory); |
| 296 | + const symbolIdMap = mkDict([ |
| 297 | + ...Object.values(assembly.types ?? {}).map((type) => [type.symbolId ?? '', type.fqn] as const), |
| 298 | + ...Object.entries(assembly.submodules ?? {}).map(([fqn, mod]) => [mod.symbolId ?? '', fqn] as const), |
| 299 | + ]); |
| 300 | + |
| 301 | + return { |
| 302 | + packageJson, |
| 303 | + assembly, |
| 304 | + directory, |
| 305 | + symbolIdMap, |
| 306 | + }; |
| 307 | + } catch { |
301 | 308 | return undefined;
|
302 | 309 | }
|
303 |
| - |
304 |
| - const packageJson = fs.readJSONSync(path.join(directory, 'package.json'), { encoding: 'utf-8' }); |
305 |
| - const assembly: spec.Assembly = fs.readJSONSync(assemblyFile, { encoding: 'utf-8' }); |
306 |
| - const symbolIdMap = mkDict([ |
307 |
| - ...Object.values(assembly.types ?? {}).map((type) => [type.symbolId ?? '', type.fqn] as const), |
308 |
| - ...Object.entries(assembly.submodules ?? {}).map(([fqn, mod]) => [mod.symbolId ?? '', fqn] as const), |
309 |
| - ]); |
310 |
| - |
311 |
| - return { |
312 |
| - packageJson, |
313 |
| - assembly, |
314 |
| - directory, |
315 |
| - symbolIdMap, |
316 |
| - }; |
317 | 310 | }
|
318 | 311 |
|
319 | 312 | function findPackageJsonLocation(currentPath: string): string | undefined {
|
|
0 commit comments