Skip to content

Commit

Permalink
chore: improve package json typings
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Sep 1, 2021
1 parent 9447c60 commit 2845d21
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 25 deletions.
7 changes: 4 additions & 3 deletions packages/addon-shim/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AddonMeta,
AddonInstance,
isDeepAddonInstance,
PackageInfo,
} from '@embroider/shared-internals';
import buildFunnel from 'broccoli-funnel';
import type { Node } from 'broccoli-node-api';
Expand All @@ -13,7 +14,7 @@ export interface ShimOptions {
disabled?: (options: any) => boolean;
}

function addonMeta(pkgJSON: any): AddonMeta {
function addonMeta(pkgJSON: PackageInfo): AddonMeta {
let meta = pkgJSON['ember-addon'];
if (meta?.version !== 2 || meta?.type !== 'addon') {
throw new Error(`did not find valid v2 addon metadata in ${pkgJSON.name}`);
Expand All @@ -22,7 +23,7 @@ function addonMeta(pkgJSON: any): AddonMeta {
}

export function addonV1Shim(directory: string, options: ShimOptions = {}) {
let pkg = JSON.parse(
let pkg: PackageInfo = JSON.parse(
readFileSync(resolve(directory, './package.json'), 'utf8')
);

Expand All @@ -42,7 +43,7 @@ export function addonV1Shim(directory: string, options: ShimOptions = {}) {
return {
name: pkg.name,
included(this: AddonInstance, ...args: unknown[]) {
if (((this.parent.pkg as any)['ember-addon']?.version ?? 1) < 2) {
if ((this.parent.pkg['ember-addon']?.version ?? 1) < 2) {
let autoImportVersion = this.parent.addons.find(
(a) => a.name === 'ember-auto-import'
)?.pkg.version;
Expand Down
37 changes: 29 additions & 8 deletions packages/compat/src/v1-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ import resolve from 'resolve';
import { Node } from 'broccoli-node-api';
import { V1Config, WriteV1Config } from './v1-config';
import { WriteV1AppBoot, ReadV1AppBoot } from './v1-appboot';
import { PackageCache, TemplateCompiler, TemplateCompilerPlugins, AddonMeta, Package } from '@embroider/core';
import {
PackageCache,
TemplateCompiler,
TemplateCompilerPlugins,
AddonMeta,
Package,
Project,
PackageInfo,
AddonInstance,
} from '@embroider/core';
import { writeJSONSync, ensureDirSync, copySync, readdirSync, pathExistsSync, existsSync } from 'fs-extra';
import AddToTree from './add-to-tree';
import DummyPackage, { OwningAddon } from './dummy-package';
Expand All @@ -28,6 +37,16 @@ import semver from 'semver';

type FilePath = string;
type OutputFileToInputFileMap = { [filePath: string]: FilePath[] };
type EmberCliHTMLBarsAddon = AddonInstance & {
htmlbarsOptions(): {
isHTMLBars: true;
plugins: {
ast: never[];
};
templateCompiler: unknown;
templateCompilerPath: unknown;
};
};

interface EmberApp {
env: string;
Expand All @@ -41,7 +60,7 @@ interface EmberApp {
options: any;
tests: boolean;
trees: any;
project: any;
project: Project;
registry: any;
testIndex(): Node;
getLintTests(): Node;
Expand All @@ -59,7 +78,7 @@ export default class V1App {
owningAddon: Package | undefined;

static create(app: EmberApp, packageCache: PackageCache): V1App {
if (app.project.pkg.keywords && app.project.pkg.keywords.includes('ember-addon')) {
if (app.project.pkg.keywords?.includes('ember-addon')) {
// we are a dummy app, which is unfortunately weird and special
return new V1DummyApp(app, packageCache);
} else {
Expand Down Expand Up @@ -436,10 +455,10 @@ export default class V1App {
'implicit-test-styles': this.app.vendorTestStaticStyles.map(remapAsset),
'public-assets': mapKeys(this._publicAssets, (_, key) => remapAsset(key)),
};
let meta = {
let meta: PackageInfo = {
name: '@embroider/synthesized-vendor',
version: '0.0.0',
keywords: 'ember-addon',
keywords: ['ember-addon'],
'ember-addon': addonMeta,
};
writeJSONSync(join(outputPath, 'package.json'), meta, { spaces: 2 });
Expand Down Expand Up @@ -499,10 +518,10 @@ export default class V1App {
addonMeta['public-assets']![`./assets/${file}`] = `/assets/${file}`;
}
}
let meta = {
let meta: PackageInfo = {
name: '@embroider/synthesized-styles',
version: '0.0.0',
keywords: 'ember-addon',
keywords: ['ember-addon'],
'ember-addon': addonMeta,
};
writeJSONSync(join(outputPath, 'package.json'), meta, { spaces: 2 });
Expand Down Expand Up @@ -562,7 +581,9 @@ export default class V1App {
}

get htmlbarsPlugins(): TemplateCompilerPlugins {
let addon = this.app.project.addons.find((a: any) => a.name === 'ember-cli-htmlbars');
let addon = this.app.project.addons.find(
(a: AddonInstance) => a.name === 'ember-cli-htmlbars'
) as unknown as EmberCliHTMLBarsAddon;
let options = addon.htmlbarsOptions();
if (options.plugins.ast) {
// even if the app was using @embroider/macros, we drop it from the config
Expand Down
11 changes: 4 additions & 7 deletions packages/shared-internals/src/ember-cli-models.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import type { Node } from 'broccoli-node-api';
import { PackageInfo } from './metadata';
export interface Project {
targets: unknown;
ui: {
write(...args: any[]): void;
};
pkg: {
name: string;
version: string;
dependencies: Record<string, string>;
devDependencies: Record<string, string>;
};
pkg: PackageInfo;
root: string;
addons: AddonInstance[];
name(): string;
configPath(): string;
}

export interface AppInstance {
Expand All @@ -24,7 +21,7 @@ export interface AppInstance {

interface BaseAddonInstance {
project: Project;
pkg: { name: string; version: string };
pkg: PackageInfo;
root: string;
options: any;
addons: AddonInstance[];
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-internals/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { AppMeta, AddonMeta } from './metadata';
export { AppMeta, AddonMeta, PackageInfo } from './metadata';
export { explicitRelative, extensionsPattern } from './paths';
export { getOrCreate } from './get-or-create';
export { default as Package, V2AddonPackage as AddonPackage, V2AppPackage as AppPackage, V2Package } from './package';
Expand Down
24 changes: 24 additions & 0 deletions packages/shared-internals/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type AppRelativeURL = string;
export interface AppMeta {
type: 'app';

main?: string;
'auto-upgraded'?: true;
assets: Filename[];
babel: {
Expand All @@ -27,6 +28,7 @@ export interface AppMeta {
// addon.
export interface AddonMeta {
type: 'addon';
main?: string;
'order-index'?: number;
'lazy-engine'?: boolean;

Expand All @@ -47,3 +49,25 @@ export interface AddonMeta {
'renamed-modules'?: { [fromName: string]: string };
version: 2;
}

export interface PackageInfo {
keywords?: string[];
name: string;
version: string;
main?: string;
peerDependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
dependencies?: Record<string, string>;
'ember-addon':
| AddonMeta
| AppMeta
| {
main?: string;
//
version?: 1;
type?: 'addon' | 'app';
paths?: string[];
before?: string | string[];
after?: string | string[];
};
}
11 changes: 5 additions & 6 deletions packages/shared-internals/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { Memoize } from 'typescript-memoize';
import { readFileSync, existsSync } from 'fs-extra';
import { join, extname } from 'path';
import get from 'lodash/get';
import { AddonMeta, AppMeta } from './metadata';
import { AddonMeta, AppMeta, PackageInfo } from './metadata';
import PackageCache from './package-cache';
import flatMap from 'lodash/flatMap';

export default class Package {
private dependencyKeys: ('dependencies' | 'devDependencies' | 'peerDependencies')[];

Expand Down Expand Up @@ -35,7 +34,7 @@ export default class Package {
}

@Memoize()
get packageJSON() {
get packageJSON(): PackageInfo {
let json = this.internalPackageJSON;
if (this.nonResolvableDeps) {
if (!json.dependencies) {
Expand All @@ -51,7 +50,7 @@ export default class Package {
get meta(): AddonMeta | AppMeta | undefined {
let m = this.packageJSON['ember-addon'];
if (this.isV2App()) {
return m as AppMeta;
return m as unknown as AppMeta;
}
if (this.isV2Addon()) {
return m as AddonMeta;
Expand All @@ -69,7 +68,7 @@ export default class Package {
}

isLazyEngine(): boolean {
return this.isEngine() && this.packageJSON['ember-addon']['lazy-engine'];
return this.isEngine() && get(this.packageJSON, 'ember-addon.lazy-engine');
}

isV2Ember(): this is V2Package {
Expand Down Expand Up @@ -197,7 +196,7 @@ export default class Package {
hasDependency(name: string): boolean {
for (let section of this.dependencyKeys) {
if (this.packageJSON[section]) {
if (this.packageJSON[section][name]) {
if ((this.packageJSON[section] as Record<string, string>)[name]) {
return true;
}
}
Expand Down

0 comments on commit 2845d21

Please sign in to comment.