Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export common functions as a submodule #188

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ project.package.addField('exports', {
'.': `./${project.package.entrypoint}`,
'./bin/jsii': './bin/jsii',
'./package.json': './package.json',
'./common': './lib/common/index.js',
});

// Remove TypeScript devDependency (it's a direct/normal dependency here)
Expand Down
3 changes: 2 additions & 1 deletion package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as log4js from 'log4js';
import * as ts from 'typescript';

import * as Case from './case';
import { symbolIdentifier } from './common/symbol-id';
import { Directives } from './directives';
import { getReferencedDocParams, parseSymbolDocumentation, TypeSystemHints } from './docs';
import { Emitter } from './emitter';
Expand All @@ -17,7 +18,6 @@ import * as literate from './literate';
import * as bindings from './node-bindings';
import { ProjectInfo } from './project-info';
import { isReservedName } from './reserved-words';
import { symbolIdentifier } from './symbol-id';
import { DeprecatedRemover } from './transforms/deprecated-remover';
import { DeprecationWarningsInjector } from './transforms/deprecation-warnings';
import { RuntimeTypeInfoInjector } from './transforms/runtime-info';
Expand Down
8 changes: 8 additions & 0 deletions src/common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# jsii/common

jsii has some features that other packages might need to depend on, without needing the whole of jsii.

This submodule is addressing this need by exporting *small, self-contained* functions.s

Anything in here MUST NOT depend on any other code in jsii.
It SHOULD be kept very lightweight and mostly depend on TypeScript and Node built-ins.
62 changes: 62 additions & 0 deletions src/common/find-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as fs from 'node:fs';
import * as path from 'node:path';

/**
* Find a directory up the tree from a starting directory matching a condition
*
* Will return `undefined` if no directory matches
*
* (This code is duplicated among jsii/jsii-pacmak/jsii-reflect. Changes should be done in all
* 3 locations, and we should unify these at some point: https://github.com/aws/jsii/issues/3236)
*/
export function findUp(directory: string, pred: (dir: string) => boolean): string | undefined {
const result = pred(directory);

if (result) {
return directory;
}

const parent = path.dirname(directory);
if (parent === directory) {
return undefined;
}

return findUp(parent, pred);
}

/**
* Find the package.json for a given package upwards from the given directory
*
* (This code is duplicated among jsii/jsii-pacmak/jsii-reflect. Changes should be done in all
* 3 locations, and we should unify these at some point: https://github.com/aws/jsii/issues/3236)
*/
export function findPackageJsonUp(packageName: string, directory: string) {
return findUp(directory, (dir) => {
const pjFile = path.join(dir, 'package.json');
return fs.existsSync(pjFile) && JSON.parse(fs.readFileSync(pjFile, 'utf-8')).name === packageName;
});
}

/**
* Find the directory that contains a given dependency, identified by its 'package.json', from a starting search directory
*
* (This code is duplicated among jsii/jsii-pacmak/jsii-reflect. Changes should be done in all
* 3 locations, and we should unify these at some point: https://github.com/aws/jsii/issues/3236)
*/
export function findDependencyDirectory(dependencyName: string, searchStart: string) {
// Explicitly do not use 'require("dep/package.json")' because that will fail if the
// package does not export that particular file.
const entryPoint = require.resolve(dependencyName, {
paths: [searchStart],
});

// Search up from the given directory, looking for a package.json that matches
// the dependency name (so we don't accidentally find stray 'package.jsons').
const depPkgJsonPath = findPackageJsonUp(dependencyName, path.dirname(entryPoint));

if (!depPkgJsonPath) {
throw new Error(`Could not find dependency '${dependencyName}' from '${searchStart}'`);
}

return depPkgJsonPath;
}
1 change: 1 addition & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { symbolIdentifier } from './symbol-id';
4 changes: 2 additions & 2 deletions src/symbol-id.ts → src/common/symbol-id.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import { Assembly } from '@jsii/spec';
import type { Assembly } from '@jsii/spec';
import * as ts from 'typescript';

import { findUp } from './utils';
import { findUp } from './find-utils';

/**
* Additional options that may be provided to the symbolIdentifier.
Expand Down
3 changes: 2 additions & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as ts from 'typescript';

import { Assembler } from './assembler';
import * as Case from './case';
import { findDependencyDirectory } from './common/find-utils';
import { emitDownleveledDeclarations, TYPES_COMPAT } from './downlevel-dts';
import { Emitter } from './emitter';
import { JsiiDiagnostic } from './jsii-diagnostic';
Expand Down Expand Up @@ -495,7 +496,7 @@ export class Compiler implements Emitter {
}

try {
const depDir = utils.findDependencyDirectory(depName, this.options.projectInfo.projectRoot);
const depDir = findDependencyDirectory(depName, this.options.projectInfo.projectRoot);

const dep = path.join(depDir, 'tsconfig.json');
if (!fs.existsSync(dep)) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './jsii-diagnostic';
export * from './symbol-id';
export * from './common/symbol-id';
export * from './helpers';
3 changes: 2 additions & 1 deletion src/project-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import * as log4js from 'log4js';
import * as semver from 'semver';
import * as ts from 'typescript';

import { findDependencyDirectory } from './common/find-utils';
import { JsiiDiagnostic } from './jsii-diagnostic';
import { parsePerson, parseRepository, findDependencyDirectory } from './utils';
import { parsePerson, parseRepository } from './utils';

// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const spdx: Set<string> = require('spdx-license-list/simple');
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/deprecation-warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as spec from '@jsii/spec';
import { Assembly } from '@jsii/spec';
import * as ts from 'typescript';

import { symbolIdentifier } from '../common/symbol-id';
import { ProjectInfo } from '../project-info';
import { symbolIdentifier } from '../symbol-id';

export const WARNINGSCODE_FILE_NAME = '.warnings.jsii.js';
const WARNING_FUNCTION_NAME = 'print';
Expand Down
62 changes: 0 additions & 62 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as log4js from 'log4js';
import * as ts from 'typescript';

Expand Down Expand Up @@ -153,66 +151,6 @@ export function parseRepository(value: string): { url: string } {
}
}

/**
* Find the directory that contains a given dependency, identified by its 'package.json', from a starting search directory
*
* (This code is duplicated among jsii/jsii-pacmak/jsii-reflect. Changes should be done in all
* 3 locations, and we should unify these at some point: https://github.com/aws/jsii/issues/3236)
*/
export function findDependencyDirectory(dependencyName: string, searchStart: string) {
// Explicitly do not use 'require("dep/package.json")' because that will fail if the
// package does not export that particular file.
const entryPoint = require.resolve(dependencyName, {
paths: [searchStart],
});

// Search up from the given directory, looking for a package.json that matches
// the dependency name (so we don't accidentally find stray 'package.jsons').
const depPkgJsonPath = findPackageJsonUp(dependencyName, path.dirname(entryPoint));

if (!depPkgJsonPath) {
throw new Error(`Could not find dependency '${dependencyName}' from '${searchStart}'`);
}

return depPkgJsonPath;
}

/**
* Find the package.json for a given package upwards from the given directory
*
* (This code is duplicated among jsii/jsii-pacmak/jsii-reflect. Changes should be done in all
* 3 locations, and we should unify these at some point: https://github.com/aws/jsii/issues/3236)
*/
export function findPackageJsonUp(packageName: string, directory: string) {
return findUp(directory, (dir) => {
const pjFile = path.join(dir, 'package.json');
return fs.existsSync(pjFile) && JSON.parse(fs.readFileSync(pjFile, 'utf-8')).name === packageName;
});
}

/**
* Find a directory up the tree from a starting directory matching a condition
*
* Will return `undefined` if no directory matches
*
* (This code is duplicated among jsii/jsii-pacmak/jsii-reflect. Changes should be done in all
* 3 locations, and we should unify these at some point: https://github.com/aws/jsii/issues/3236)
*/
export function findUp(directory: string, pred: (dir: string) => boolean): string | undefined {
const result = pred(directory);

if (result) {
return directory;
}

const parent = path.dirname(directory);
if (parent === directory) {
return undefined;
}

return findUp(parent, pred);
}

const ANSI_REGEX =
// eslint-disable-next-line no-control-regex
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
Expand Down