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 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 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(computedKeywords)",
"searchable(owner.name)",
"deprecated",
],
Expand Down
8 changes: 7 additions & 1 deletion src/__tests__/__snapshots__/formatPkg.test.js.snap

Large diffs are not rendered by default.

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

expect(formatted.readme).toHaveLength(451070);
expect(formatted.readme).toHaveLength(451134);
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.computedKeywords).toEqual(['angular-cli-schematic']);
expect(formatted.computedMetadata).toEqual({
schematics: 'bli-blo',
});
});

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.computedKeywords).toEqual(['babel-plugin']);
expect(formattedUnofficialDogs.computedKeywords).toEqual(['babel-plugin']);
});

describe('adds vue-cli plugins', () => {
Expand All @@ -69,6 +90,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.computedKeywords).toEqual(['vue-cli-plugin']);
expect(formattedUnofficialDogs.computedKeywords).toEqual(['vue-cli-plugin']);
expect(formattedScopedDogs.computedKeywords).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(computedKeywords)',
'searchable(owner.name)',
'deprecated',
],
Expand Down
62 changes: 44 additions & 18 deletions src/formatPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ 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 { computedKeywords, computedMetadata } = getComputedData(cleaned, pkg);
const keywords = [...getKeywords(cleaned), ...computedKeywords]; // concat with the subset for backward compat

const dependencies = cleaned.dependencies || {};
const devDependencies = cleaned.devDependencies || {};
Expand Down Expand Up @@ -86,6 +87,8 @@ export default function formatPkg(pkg) {
homepage: getHomePage(cleaned.homepage, cleaned.repository),
license,
keywords,
computedKeywords,
computedMetadata,
created: Date.parse(cleaned.created),
modified: Date.parse(cleaned.modified),
lastPublisher,
Expand Down Expand Up @@ -209,31 +212,54 @@ 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 }) => ({
name: 'babel-plugin',
include:
name.startsWith('@babel/plugin') || name.startsWith('babel-plugin-'),
}),

({ name }) => ({
name: 'vue-cli-plugin',
include: /^(@vue\/|vue-|@[\w-]+\/vue-)cli-plugin-/.test(name),
}),

(_, { schematics = '' }) => ({
name: 'angular-cli-schematic',
include: schematics.length > 0,
metadata: { schematics },
}),
];

function getComputedData(cleaned, original) {
const registrySubsets = registrySubsetRules.reduce(
(acc, matcher) => {
const { include, metadata, name } = matcher(cleaned, original);
return include
? {
computedKeywords: [...acc.computedKeywords, name],
computedMetadata: {
...acc.computedMetadata,
...metadata,
},
}
: acc;
},
{ computedKeywords: [], computedMetadata: {} }
);
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