Skip to content

Commit

Permalink
style: 💄 prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
janbiasi committed Jul 13, 2019
1 parent 32fd12a commit 5664241
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 47 deletions.
13 changes: 12 additions & 1 deletion src/Packer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ import {
ArtificalPackage,
IAnalyticsWithIntegrity
} from './types';
import { fs, asyncForEach, pkg, rimraf, copyDir, matcher, execa, displayPath, createHash, createIntegrityHash } from './utils';
import {
fs,
asyncForEach,
pkg,
rimraf,
copyDir,
matcher,
execa,
displayPath,
createHash,
createIntegrityHash
} from './utils';
import { Taper } from './Taper';
import { AdapterLerna } from './adapter';
import { useDebugHooks } from './helper/debug-hooks';
Expand Down
2 changes: 1 addition & 1 deletion src/Taper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Taper<K extends string, T extends Record<K, TaperFunction<unknown>[
private forwarder: TaperReceiver[] = [];
private connected: Taper<K, T>[] = [];

constructor(private packer: Packer, private factory: Partial<T>) { }
constructor(private packer: Packer, private factory: Partial<T>) {}

public on<TFeedback extends any = undefined>(point: K, taperFn: TaperFunction<TFeedback>) {
if (!this.factory[point]) {
Expand Down
35 changes: 25 additions & 10 deletions src/adapter/Lerna.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export class AdapterLerna extends Adapter {
}

// aggregation of internal dependencies
private async resolveDependantInternals(graph: IAnalytics['graph'] = {}, sourcePackage: Package, lernaPackageInfo: ILernaPackageInfo): Promise<ILernaResolvedTree> {
private async resolveDependantInternals(
graph: IAnalytics['graph'] = {},
sourcePackage: Package,
lernaPackageInfo: ILernaPackageInfo
): Promise<ILernaResolvedTree> {
// aggregation of installable external dependencies
const productionDependencies = extractDependencies(sourcePackage.dependencies, dependency => {
return lernaPackageInfo.names.indexOf(dependency) === -1;
Expand All @@ -84,26 +88,37 @@ export class AdapterLerna extends Adapter {
});

// check if the related module(s) also need to be resolved and do so
const recuriveModules = await Promise.all<ILernaResolvedTree>(internalDependencies.map(async entry => {
const childModulePkg = await this.fetchPackage(resolve(entry.location, 'package.json'));
return await this.resolveDependantInternals(graph, childModulePkg, lernaPackageInfo);
}));
const recuriveModules = await Promise.all<ILernaResolvedTree>(
internalDependencies.map(async entry => {
const childModulePkg = await this.fetchPackage(resolve(entry.location, 'package.json'));
return await this.resolveDependantInternals(graph, childModulePkg, lernaPackageInfo);
})
);

// format recursive output according to type definition
const recursiveInternals = recuriveModules.reduce((prev, curr) => [...prev, ...curr.internal], [] as LernaPackageList)
const recursiveExternals = recuriveModules.reduce((prev, curr) => ({ ...prev, ...curr.external }), {} as DependenciesLike)
const recursiveInternals = recuriveModules.reduce(
(prev, curr) => [...prev, ...curr.internal],
[] as LernaPackageList
);
const recursiveExternals = recuriveModules.reduce(
(prev, curr) => ({ ...prev, ...curr.external }),
{} as DependenciesLike
);

// create resolved map
const resolved: Pick<ILernaResolvedTree, 'internal' | 'external'> = {
internal: internalDependencies.concat(recursiveInternals),
external: { ...productionDependencies, ...recursiveExternals }
}
};

// build related graph from recursive modules
graph[sourcePackage.name] = {
internal: recursiveInternals.reduce((prev, { name, version }) => ({ ...prev, [name]: version }), {} as DependenciesLike),
internal: recursiveInternals.reduce(
(prev, { name, version }) => ({ ...prev, [name]: version }),
{} as DependenciesLike
),
external: recursiveExternals
}
};

return {
...resolved,
Expand Down
17 changes: 11 additions & 6 deletions src/bin/commands/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export async function pack(opts: IPackerOptions) {
init: [
async () => {
spinner.succeed(
`Initialized packer v${Packer.version} for ${displayPath(
opts.cwd || CWD,
opts.source
)}`
`Initialized packer v${Packer.version} for ${displayPath(opts.cwd || CWD, opts.source)}`
);

if (opts.cwd) {
Expand Down Expand Up @@ -57,7 +54,10 @@ export async function pack(opts: IPackerOptions) {
);
spinner.succeed(`Found ${countMsg(analytics.dependencies.internal, 'internal package')} to copy`);
spinner.succeed(
`Found ${countMsg(analytics.dependencies.peer, 'aggregated peer package')} to include in production`
`Found ${countMsg(
analytics.dependencies.peer,
'aggregated peer package'
)} to include in production`
);
}
],
Expand All @@ -70,7 +70,12 @@ export async function pack(opts: IPackerOptions) {
postinstall: [
async (_packer, { dependencies }) => {
const [installationTimeInSeconds] = process.hrtime(installTimer);
spinner.succeed(`Production packages have been installed (${countMsg(dependencies, 'package')} in ~${installationTimeInSeconds}s)`);
spinner.succeed(
`Production packages have been installed (${countMsg(
dependencies,
'package'
)} in ~${installationTimeInSeconds}s)`
);
}
],
prelink: [
Expand Down
19 changes: 6 additions & 13 deletions src/helper/debug-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as debug from 'debug';
import { IPackerOptions, HookPhase } from "../types";
import { Packer, DEFAULT_PACKED_PATH } from "../Packer";
import { countMsg, displayPath } from "../utils";
import { IPackerOptions, HookPhase } from '../types';
import { Packer, DEFAULT_PACKED_PATH } from '../Packer';
import { countMsg, displayPath } from '../utils';

const CWD = process.cwd();
const log = debug('packer');
Expand All @@ -14,12 +14,7 @@ export function useDebugHooks(opts: Pick<IPackerOptions, 'target' | 'cwd' | 'sou
return {
[HookPhase.INIT]: [
async () => {
log(
`Initialized packer v${Packer.version} for ${displayPath(
opts.cwd || CWD,
opts.source
)}`
)
log(`Initialized packer v${Packer.version} for ${displayPath(opts.cwd || CWD, opts.source)}`);

log(`Setting packing output to ${opts.target || `${DEFAULT_PACKED_PATH} (default)`}`);
}
Expand All @@ -41,9 +36,7 @@ export function useDebugHooks(opts: Pick<IPackerOptions, 'target' | 'cwd' | 'sou
log('Skip writing analytics file to output');
}

log(
`Found ${countMsg(analytics.dependencies.external, 'external package')} to install via NPM`
);
log(`Found ${countMsg(analytics.dependencies.external, 'external package')} to install via NPM`);
log(`Found ${countMsg(analytics.dependencies.internal, 'internal package')} to copy`);
log(
`Found ${countMsg(analytics.dependencies.peer, 'aggregated peer package')} to include in production`
Expand Down Expand Up @@ -76,5 +69,5 @@ export function useDebugHooks(opts: Pick<IPackerOptions, 'target' | 'cwd' | 'sou
log(`Application ${artificalPackage.name} packed successfully!`);
}
]
}
};
}
2 changes: 1 addition & 1 deletion src/types/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ export interface IAnalytics {
}

export interface IAnalyticsWithIntegrity extends IAnalytics {
integrity: string
integrity: string;
}
27 changes: 17 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ export function getLernaPackages(root: string) {
(() => {
try {
return loadJson(`${root}/lerna.json`).packages;
} catch (err) { }
} catch (err) {}
try {
return loadJson(`${root}/package.json`).workspaces;
} catch (err) { }
} catch (err) {}
return [];
})() || [];

Expand Down Expand Up @@ -160,17 +160,24 @@ export function displayPath(base: string, toDisplay: string) {
}

export function createHash(value: string) {
return createNativeHash('sha256').update(value).digest('hex')
return createNativeHash('sha256')
.update(value)
.digest('hex');
}

export function createIntegrityHash(version: string, analytics: IAnalytics) {
return createHash(version + JSON.stringify({
...analytics,
dependencies: {
...analytics.dependencies,
internal: analytics.dependencies.internal.map(entry => `${entry.name}@${entry.version}|${!!entry.private}`)
}
}))
return createHash(
version +
JSON.stringify({
...analytics,
dependencies: {
...analytics.dependencies,
internal: analytics.dependencies.internal.map(
entry => `${entry.name}@${entry.version}|${!!entry.private}`
)
}
})
);
}

export const pkg = require('../package.json');
Expand Down
10 changes: 5 additions & 5 deletions test/Packer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const analyticsToSnapshot = (analytics: IAnalytics) => {
private: entry.private
}))
}
}
}
};
};

describe('Packer', () => {
describe('Lerna', () => {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('Packer', () => {
expect(deserialized.dependencies.peer).toBeDefined();
expect(deserialized.dependencies.internal.length).toEqual(1);
expect(deserialized.dependencies.peer.smallest).toBeDefined();
} catch (err) { }
} catch (err) {}
});

it('should generate an artificial package correctly', async () => {
Expand All @@ -71,10 +71,10 @@ describe('Packer', () => {
expect(generatedPkg).toMatchSnapshot();
expect(generatedPkg.name).toEqual('@fixture/main-packed');
expect(generatedPkg.dependencies).toStrictEqual({
"ansi-styles": "4.0.0", // from subsub
'ansi-styles': '4.0.0', // from subsub
debug: '*', // from main
smallest: '1.0.1', // from sub
"supports-color": "7.0.0" // from subsubsub
'supports-color': '7.0.0' // from subsubsub
});
});

Expand Down

0 comments on commit 5664241

Please sign in to comment.