Skip to content

Commit

Permalink
feat(third-party): add handling of Angular CLI schematics, and rework…
Browse files Browse the repository at this point in the history
… registry subset (#169)

* feat(third-party): add handling of Angular CLI schematics, and rework registry subset

This way we don't change the keywords anymore, and can do actual tests on the package contents.

cc @manekinekko 🙌

* avoid temp variable

* chore: rename to computedKeywords and computedMetadata

* chore: simplify code
  • Loading branch information
Haroenv committed Mar 30, 2018
1 parent b906ebf commit bfab179
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/config.test.js.snap
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
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
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
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

0 comments on commit bfab179

Please sign in to comment.