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(third-party): add handling of Angular CLI schematics, and rework registry subset #169

Merged
merged 5 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 src/__tests__/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Object {
"attributesForFaceting": Array [
"filterOnly(concatenatedName)",
"searchable(keywords)",
"searchable(registrySubsets)",
"searchable(owner.name)",
"deprecated",
],
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/__snapshots__/formatPkg.test.js.snap

Large diffs are not rendered by default.

28 changes: 25 additions & 3 deletions src/__tests__/formatPkg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,46 @@ it('truncates long readmes', () => {
formatted.readme.length - truncatedEnding.length
);

expect(formatted.readme).toHaveLength(451070);
expect(formatted.readme).toHaveLength(451100);
expect(ending).toBe(truncatedEnding);

formatted.lastCrawl = '<!-- date replaced -->';
expect(formatted).toMatchSnapshot();
});

describe('adds angular cli schematics', () => {
const angularSchema = {
name: 'angular-cli-schema-1',
schematics: 'bli-blo',
keywords: ['hi'],
lastPublisher: { name: 'angular-god' },
};

const formatted = formatPkg(angularSchema);
expect(formatted.keywords).toEqual(['hi', 'angular-cli-schematic']);
expect(formatted.registrySubsets).toEqual(['angular-cli-schematic']);
});

describe('adds babel plugins', () => {
const dogs = {
name: '@babel/plugin-dogs',
keywords: 'babel',
lastPublisher: { name: 'xtuc' },
};
const unofficialDogs = {
name: 'babel-plugin-dogs',
keywords: ['dogs'],
lastPublisher: { name: 'unknown' },
};

const formattedDogs = formatPkg(dogs);
const formattedUnofficialDogs = formatPkg(unofficialDogs);

expect(formattedDogs.keywords).toEqual(['babel-plugin']);
expect(formattedUnofficialDogs.keywords).toEqual(['babel-plugin']);
expect(formattedDogs.keywords).toEqual(['babel', 'babel-plugin']);
expect(formattedUnofficialDogs.keywords).toEqual(['dogs', 'babel-plugin']);

expect(formattedDogs.registrySubsets).toEqual(['babel-plugin']);
expect(formattedUnofficialDogs.registrySubsets).toEqual(['babel-plugin']);
});

describe('adds vue-cli plugins', () => {
Expand All @@ -69,6 +87,10 @@ describe('adds vue-cli plugins', () => {
expect(formattedDogs.keywords).toEqual(['vue-cli-plugin']);
expect(formattedUnofficialDogs.keywords).toEqual(['vue-cli-plugin']);
expect(formattedScopedDogs.keywords).toEqual(['vue-cli-plugin']);

expect(formattedDogs.registrySubsets).toEqual(['vue-cli-plugin']);
expect(formattedUnofficialDogs.registrySubsets).toEqual(['vue-cli-plugin']);
expect(formattedScopedDogs.registrySubsets).toEqual(['vue-cli-plugin']);
});

describe('test getRepositoryInfo', () => {
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const defaultConfig = {
attributesForFaceting: [
'filterOnly(concatenatedName)' /* optionalFacetFilters to boost the name */,
'searchable(keywords)',
'searchable(registrySubsets)',
'searchable(owner.name)',
'deprecated',
],
Expand Down
49 changes: 31 additions & 18 deletions src/formatPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export default function formatPkg(pkg) {

const owner = getOwner(repository, lastPublisher, author); // always favor the repository owner
const badPackage = isBadPackage(owner);
const keywords = getKeywords(cleaned);
const registrySubsets = getRegistrySubsets(cleaned, pkg);
const __keywords = getKeywords(cleaned);
const keywords = [...__keywords, ...registrySubsets]; // concat with the subset for backward compat
Copy link
Contributor

@vvo vvo Mar 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just ...getKeywords(cleaned) to avoid the __ and temp variable


const dependencies = cleaned.dependencies || {};
const devDependencies = cleaned.devDependencies || {};
Expand Down Expand Up @@ -86,6 +88,7 @@ export default function formatPkg(pkg) {
homepage: getHomePage(cleaned.homepage, cleaned.repository),
license,
keywords,
registrySubsets,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only have an array of keywords for now and not actual metadata then let's just call it "computedKeywords".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the name keywords

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically they are more like identifiers than keywords

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subset is how we see one usecase for this feature today. If someone reads the registry it must be clear what those are, so I guess computedKeywords is what this data is: a list of keywords we compute

created: Date.parse(cleaned.created),
modified: Date.parse(cleaned.modified),
lastPublisher,
Expand Down Expand Up @@ -209,31 +212,41 @@ function getVersions(cleaned) {
return {};
}

const forcedKeywords = {
'babel-plugin': ({ name }) =>
name.startsWith('@babel/plugin') || name.startsWith('babel-plugin-'),
'vue-cli-plugin': ({ name }) =>
/^(@vue\/|vue-|@[\w-]+\/vue-)cli-plugin-/.test(name),
};
const registrySubsetRules = [
{
name: 'babel-plugin',
matcher: ({ name }) =>
name.startsWith('@babel/plugin') || name.startsWith('babel-plugin-'),
},
{
name: 'vue-cli-plugin',
matcher: ({ name }) => /^(@vue\/|vue-|@[\w-]+\/vue-)cli-plugin-/.test(name),
},
{
name: 'angular-cli-schematic',
matcher: (_, { schematics = '' }) => schematics.length > 0,
},
];

function getRegistrySubsets(cleaned, original) {
const registrySubsets = registrySubsetRules.reduce(
(acc, { name, matcher }) =>
matcher(cleaned, original) ? [...acc, name] : acc,
[]
);
return registrySubsets;
}

function getKeywords(cleaned) {
// Forced keywords
const keywords = [];
for (const keyword in forcedKeywords) {
if (forcedKeywords[keyword](cleaned)) {
keywords.push(keyword);
}
}

if (cleaned.keywords) {
if (Array.isArray(cleaned.keywords)) {
return [...cleaned.keywords, ...keywords];
return [...cleaned.keywords];
}
if (typeof cleaned.keywords === 'string') {
return [cleaned.keywords, ...keywords];
return [cleaned.keywords];
}
}
return [...keywords];
return [];
}

function getGitHubRepoInfo({ repository, gitHead = 'master' }) {
Expand Down