Skip to content

Commit

Permalink
Refactor privacy handling and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-chernicki committed Jul 14, 2024
1 parent 27bc629 commit b2b1e2a
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 66 deletions.
35 changes: 2 additions & 33 deletions apps/commonality/src/cli/commands/publish.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
/* eslint-disable unicorn/no-process-exit */
import { getRootDirectory } from '@commonalityco/data-project';
import { getCodeownersData } from './../../../../../packages/data-codeowners/src/get-codeowners-data';
import { getCodeownersData } from '@commonalityco/data-codeowners';
import { Command } from 'commander';
import { z } from 'zod';
import { createSnapshotSchema } from '@commonalityco/utils-core';
import { getDependencies, getPackages } from '@commonalityco/data-packages';
import ky, { HTTPError } from 'ky';
import * as prompts from '@clack/prompts';

const command = new Command();

export const createSnapshotSchema = z.object({
publishKey: z.string(),
projectId: z.string(),
codeowners: z.array(
z.object({
packageName: z.string(),
codeowners: z.array(z.string()),
}),
),
blocks: z.array(
z.object({
name: z.string(),
description: z.string().optional(),
path: z.string(),
version: z.string(),
type: z.enum(['REACT', 'NODE', 'NEXT']),
churn: z.number(),
complexity: z.number(),
license: z.string().optional(),
private: z.boolean(),
}),
),
dependencies: z.array(
z.object({
source: z.string(),
target: z.string(),
type: z.enum(['PRODUCTION', 'DEVELOPMENT', 'PEER']),
}),
),
});

const publishSpinner = prompts.spinner();

export const publish = command
Expand Down
17 changes: 11 additions & 6 deletions packages/data-packages/src/get-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Package, PackageJson } from '@commonalityco/types';
import path from 'node:path';
import fs from 'fs-extra';
import { BlockType } from '@commonalityco/utils-core/constants';
import { privacyEnum } from '@commonalityco/utils-core';

const typeOrder = new Set([BlockType.NEXT, BlockType.REACT, BlockType.NODE]);

Expand Down Expand Up @@ -53,19 +54,23 @@ export const getPackage = async ({
return;
}

const getIsPrivate = () => {
const getPrivacy = () => {
if (packageJson.private === true) {
return true;
return privacyEnum.enum.PRIVATE;
}
if (packageJson.publishConfig?.access === 'restricted') {
return true;
return privacyEnum.enum.PRIVATE;
}
if (packageJson?.name?.startsWith('@')) {
// Scoped packages default to private
return packageJson.private !== false;
return packageJson.private === false
? privacyEnum.enum.PUBLIC
: privacyEnum.enum.PRIVATE;
}
// Unscoped packages default to public
return packageJson.private ?? false;
return packageJson.private
? privacyEnum.enum.PRIVATE
: privacyEnum.enum.PUBLIC;
};

return {
Expand All @@ -80,6 +85,6 @@ export const getPackage = async ({
churn: 0.3,
complexity: 0.3,
license: packageJson.license,
private: getIsPrivate(),
privacy: getPrivacy(),
} satisfies Package;
};
6 changes: 3 additions & 3 deletions packages/data-packages/src/get-packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('getPackages', () => {
churn: 0.3,
complexity: 0.3,
license: undefined,
private: false,
privacy: 'PUBLIC',
},
]);
});
Expand All @@ -48,7 +48,7 @@ describe('getPackages', () => {
churn: 0.3,
complexity: 0.3,
license: 'MIT',
private: true,
privacy: 'PRIVATE',
},
{
name: '@scope/pkg-two',
Expand All @@ -59,7 +59,7 @@ describe('getPackages', () => {
churn: 0.3,
complexity: 0.3,
license: undefined,
private: true,
privacy: 'PRIVATE',
},
]);
});
Expand Down
3 changes: 3 additions & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@
"type": "git",
"url": "https://github.com/commonalityco/commonality",
"directory": "packages/types"
},
"dependencies": {
"zod": "^3.23.8"
}
}
5 changes: 4 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
AllPackagesWildcard,
BlockType,
} from '@commonalityco/utils-core';
import { z } from 'zod';

export const privacyEnum = z.enum(['PUBLIC', 'PRIVATE']);

export type Constraint =
| {
Expand Down Expand Up @@ -65,7 +68,7 @@ export type Package = {
churn: number;
complexity: number;
license?: string;
private: boolean;
privacy: z.infer<typeof privacyEnum>;
};

export type Workspace = {
Expand Down
12 changes: 6 additions & 6 deletions packages/utils-conformance/src/get-conformance-results.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('getConformanceResults', () => {
type: BlockType.NODE,
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PUBLIC',
},
];
const tagsData: TagsData[] = [{ packageName: 'pkg-a', tags: ['*'] }];
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('getConformanceResults', () => {
type: BlockType.NODE,
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PUBLIC',
},
];
const tagsData: TagsData[] = [{ packageName: 'pkg-a', tags: ['*'] }];
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('getConformanceResults', () => {
type: BlockType.NODE,
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PUBLIC',
},
];
const tagsData: TagsData[] = [{ packageName: 'pkg-a', tags: ['*'] }];
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('getConformanceResults', () => {
type: BlockType.NODE,
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PUBLIC',
},
];
const tagsData: TagsData[] = [];
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('getConformanceResults', () => {
type: BlockType.NODE,
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PUBLIC',
},
];
const tagsData: TagsData[] = [{ packageName: 'pkg-a', tags: ['*'] }];
Expand Down Expand Up @@ -232,7 +232,7 @@ describe('getConformanceResults', () => {
type: BlockType.NODE,
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PUBLIC',
},
];
const tagsData: TagsData[] = [{ packageName: 'pkg-a', tags: ['tag1'] }];
Expand Down
30 changes: 15 additions & 15 deletions packages/utils-conformance/src/get-conformance-score.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message1' },
status: Status.Pass,
Expand All @@ -31,7 +31,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message2' },
status: Status.Pass,
Expand All @@ -46,7 +46,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message3' },
status: Status.Pass,
Expand All @@ -67,7 +67,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message1' },
status: Status.Fail,
Expand All @@ -82,7 +82,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message2' },
status: Status.Fail,
Expand All @@ -97,7 +97,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message3' },
status: Status.Fail,
Expand All @@ -118,7 +118,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message1' },
status: Status.Warn,
Expand All @@ -133,7 +133,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message2' },
status: Status.Warn,
Expand All @@ -148,7 +148,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message3' },
status: Status.Warn,
Expand All @@ -169,7 +169,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message1' },
status: Status.Pass,
Expand All @@ -184,7 +184,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message2' },
status: Status.Warn,
Expand All @@ -199,7 +199,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message3' },
status: Status.Fail,
Expand All @@ -225,7 +225,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message1' },
status: Status.Pass,
Expand All @@ -240,7 +240,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message2' },
status: Status.Pass,
Expand All @@ -255,7 +255,7 @@ describe('getConformanceScore', () => {
path: '/path',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
message: { message: 'message3' },
status: Status.Fail,
Expand Down
4 changes: 2 additions & 2 deletions packages/utils-conformance/src/run-fixes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('runFixes', () => {
version: '1.0.0',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
},
];
Expand All @@ -35,7 +35,7 @@ describe('runFixes', () => {
version: '1.0.0',
churn: 0.5,
complexity: 0.5,
private: false,
privacy: 'PRIVATE',
},
];
const rootDirectory = 'root/directory';
Expand Down
2 changes: 2 additions & 0 deletions packages/utils-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from './format-tag-name.js';
export * from './format-package-name.js';
export * from './logger.js';
export * from './number-safe-parse.js';

export * from './schemas/create-snapshot-schema.js';
Loading

0 comments on commit b2b1e2a

Please sign in to comment.