From e4daa01f70080c3b99a39633f9d2790ff592c4ae Mon Sep 17 00:00:00 2001 From: Lupo Montero Date: Wed, 22 May 2024 18:13:22 +0200 Subject: [PATCH] feat(project): Agrega parseado de project.tags --- .../01-a-project-tags-not-array/README.md | 15 ++++++ .../01-a-project-tags-not-array/project.yml | 8 ++++ .../01-a-project-tags-not-strings/README.md | 15 ++++++ .../01-a-project-tags-not-strings/project.yml | 11 +++++ .../01-a-project-with-invalid-tags/README.md | 15 ++++++ .../project.yml | 10 ++++ .../01-a-project-with-tags/README.md | 15 ++++++ .../01-a-project-with-tags/project.yml | 12 +++++ lib/__tests__/project.spec.js | 47 +++++++++++++++++++ lib/project.js | 28 ++++++++++- 10 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 lib/__tests__/__fixtures__/01-a-project-tags-not-array/README.md create mode 100644 lib/__tests__/__fixtures__/01-a-project-tags-not-array/project.yml create mode 100644 lib/__tests__/__fixtures__/01-a-project-tags-not-strings/README.md create mode 100644 lib/__tests__/__fixtures__/01-a-project-tags-not-strings/project.yml create mode 100644 lib/__tests__/__fixtures__/01-a-project-with-invalid-tags/README.md create mode 100644 lib/__tests__/__fixtures__/01-a-project-with-invalid-tags/project.yml create mode 100644 lib/__tests__/__fixtures__/01-a-project-with-tags/README.md create mode 100644 lib/__tests__/__fixtures__/01-a-project-with-tags/project.yml diff --git a/lib/__tests__/__fixtures__/01-a-project-tags-not-array/README.md b/lib/__tests__/__fixtures__/01-a-project-tags-not-array/README.md new file mode 100644 index 0000000..841e5d5 --- /dev/null +++ b/lib/__tests__/__fixtures__/01-a-project-tags-not-array/README.md @@ -0,0 +1,15 @@ +# A project + +## Índice + +Blah blah blah + +*** + +## 1. Preámbulo + +Blah blah blah + +## 2. Resumen del proyecto + +Blah blah blah diff --git a/lib/__tests__/__fixtures__/01-a-project-tags-not-array/project.yml b/lib/__tests__/__fixtures__/01-a-project-tags-not-array/project.yml new file mode 100644 index 0000000..daec0ea --- /dev/null +++ b/lib/__tests__/__fixtures__/01-a-project-tags-not-array/project.yml @@ -0,0 +1,8 @@ +track: web-dev +tracks: + - web-dev +tags: omg +learningObjectives: + - html + - css + - dom diff --git a/lib/__tests__/__fixtures__/01-a-project-tags-not-strings/README.md b/lib/__tests__/__fixtures__/01-a-project-tags-not-strings/README.md new file mode 100644 index 0000000..841e5d5 --- /dev/null +++ b/lib/__tests__/__fixtures__/01-a-project-tags-not-strings/README.md @@ -0,0 +1,15 @@ +# A project + +## Índice + +Blah blah blah + +*** + +## 1. Preámbulo + +Blah blah blah + +## 2. Resumen del proyecto + +Blah blah blah diff --git a/lib/__tests__/__fixtures__/01-a-project-tags-not-strings/project.yml b/lib/__tests__/__fixtures__/01-a-project-tags-not-strings/project.yml new file mode 100644 index 0000000..17bbcb8 --- /dev/null +++ b/lib/__tests__/__fixtures__/01-a-project-tags-not-strings/project.yml @@ -0,0 +1,11 @@ +track: web-dev +tracks: + - web-dev +tags: + - featured + - foo: true + bar: 1 +learningObjectives: + - html + - css + - dom diff --git a/lib/__tests__/__fixtures__/01-a-project-with-invalid-tags/README.md b/lib/__tests__/__fixtures__/01-a-project-with-invalid-tags/README.md new file mode 100644 index 0000000..841e5d5 --- /dev/null +++ b/lib/__tests__/__fixtures__/01-a-project-with-invalid-tags/README.md @@ -0,0 +1,15 @@ +# A project + +## Índice + +Blah blah blah + +*** + +## 1. Preámbulo + +Blah blah blah + +## 2. Resumen del proyecto + +Blah blah blah diff --git a/lib/__tests__/__fixtures__/01-a-project-with-invalid-tags/project.yml b/lib/__tests__/__fixtures__/01-a-project-with-invalid-tags/project.yml new file mode 100644 index 0000000..c961a5f --- /dev/null +++ b/lib/__tests__/__fixtures__/01-a-project-with-invalid-tags/project.yml @@ -0,0 +1,10 @@ +track: web-dev +tracks: + - web-dev +tags: + - featured + - foo +learningObjectives: + - html + - css + - dom diff --git a/lib/__tests__/__fixtures__/01-a-project-with-tags/README.md b/lib/__tests__/__fixtures__/01-a-project-with-tags/README.md new file mode 100644 index 0000000..841e5d5 --- /dev/null +++ b/lib/__tests__/__fixtures__/01-a-project-with-tags/README.md @@ -0,0 +1,15 @@ +# A project + +## Índice + +Blah blah blah + +*** + +## 1. Preámbulo + +Blah blah blah + +## 2. Resumen del proyecto + +Blah blah blah diff --git a/lib/__tests__/__fixtures__/01-a-project-with-tags/project.yml b/lib/__tests__/__fixtures__/01-a-project-with-tags/project.yml new file mode 100644 index 0000000..0b85acf --- /dev/null +++ b/lib/__tests__/__fixtures__/01-a-project-with-tags/project.yml @@ -0,0 +1,12 @@ +track: web-dev +tracks: + - web-dev +tags: + - featured + - beta + - deprecated + - hidden +learningObjectives: + - html + - css + - dom diff --git a/lib/__tests__/project.spec.js b/lib/__tests__/project.spec.js index 0221666..34d8780 100644 --- a/lib/__tests__/project.spec.js +++ b/lib/__tests__/project.spec.js @@ -250,6 +250,53 @@ describe('parseProject', () => { }); }); + it('includes allowed tags when present in yml', () => { + const p = resolveFixturePath('01-a-project-with-tags'); + return parseProject(p, { + repo: 'Laboratoria/bootcamp', + version: '1.0.0', + }, pkg) + .then((result) => { + expect(result.tags).toEqual(['featured', 'beta', 'deprecated', 'hidden']); + }); + }); + + it('throws when unknown tags when present in yml', () => { + const p = resolveFixturePath('01-a-project-with-invalid-tags'); + expect.assertions(1); + return parseProject(p, { + repo: 'Laboratoria/bootcamp', + version: '1.0.0', + }, pkg) + .catch((err) => { + expect(err.message).toBe('Invalid tag: foo'); + }); + }); + + it('throws when tags not array', () => { + const p = resolveFixturePath('01-a-project-tags-not-array'); + expect.assertions(1); + return parseProject(p, { + repo: 'Laboratoria/bootcamp', + version: '1.0.0', + }, pkg) + .catch((err) => { + expect(err.message).toBe('Invalid tags'); + }); + }); + + it('throws when tags not array of strings', () => { + const p = resolveFixturePath('01-a-project-tags-not-strings'); + expect.assertions(1); + return parseProject(p, { + repo: 'Laboratoria/bootcamp', + version: '1.0.0', + }, pkg) + .catch((err) => { + expect(err.message).toBe('Invalid tag'); + }); + }); + it('extracts first paragraph of _resumen del proyecto_ as summary', () => { const p = resolveFixturePath('01-a-project-with-summary'); expect.assertions(2); diff --git a/lib/project.js b/lib/project.js index e9c4f74..af053ec 100644 --- a/lib/project.js +++ b/lib/project.js @@ -183,6 +183,28 @@ export const transformLearningObjectives = async (dir, opts, meta = {}) => { }; }; +const allowedTags = ['featured', 'beta', 'deprecated', 'hidden']; +const parseTags = (tags) => { + if (!tags) { + return null; + } + + if (!Array.isArray(tags)) { + throw new Error('Invalid tags'); + } + + tags.forEach((tag) => { + if (typeof tag !== 'string') { + throw new Error('Invalid tag'); + } + if (!allowedTags.includes(tag)) { + throw new Error(`Invalid tag: ${tag}`); + } + }); + + return tags; +}; + export const parseProject = async (dir, opts, pkg) => { const { prefix, slug } = parseDirname(dir); const langs = await detectLangs(dir); @@ -197,7 +219,8 @@ export const parseProject = async (dir, opts, pkg) => { }), ); - const { cover, thumb } = meta; + + const { cover, thumb, tags } = meta; const { track, tracks } = parseTracks(meta); const { @@ -205,6 +228,8 @@ export const parseProject = async (dir, opts, pkg) => { variants, } = await transformLearningObjectives(dir, opts, meta) || {}; + const parsedTags = parseTags(tags); + return { slug, repo: opts.repo, @@ -215,6 +240,7 @@ export const parseProject = async (dir, opts, pkg) => { prefix: parseInt(prefix, 10), track, tracks, + ...(!!parsedTags?.length && { tags: parsedTags }), ...(!!learningObjectives && { learningObjectives }), ...(!!variants && { variants }), intl: langs.reduce(