Skip to content

Commit

Permalink
feat(project): Agrega parseado de project.tags
Browse files Browse the repository at this point in the history
  • Loading branch information
lupomontero committed May 22, 2024
1 parent e031017 commit e4daa01
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/__tests__/__fixtures__/01-a-project-tags-not-array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# A project

## Índice

Blah blah blah

***

## 1. Preámbulo

Blah blah blah

## 2. Resumen del proyecto

Blah blah blah
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
track: web-dev
tracks:
- web-dev
tags: omg
learningObjectives:
- html
- css
- dom
15 changes: 15 additions & 0 deletions lib/__tests__/__fixtures__/01-a-project-tags-not-strings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# A project

## Índice

Blah blah blah

***

## 1. Preámbulo

Blah blah blah

## 2. Resumen del proyecto

Blah blah blah
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
track: web-dev
tracks:
- web-dev
tags:
- featured
- foo: true
bar: 1
learningObjectives:
- html
- css
- dom
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# A project

## Índice

Blah blah blah

***

## 1. Preámbulo

Blah blah blah

## 2. Resumen del proyecto

Blah blah blah
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
track: web-dev
tracks:
- web-dev
tags:
- featured
- foo
learningObjectives:
- html
- css
- dom
15 changes: 15 additions & 0 deletions lib/__tests__/__fixtures__/01-a-project-with-tags/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# A project

## Índice

Blah blah blah

***

## 1. Preámbulo

Blah blah blah

## 2. Resumen del proyecto

Blah blah blah
12 changes: 12 additions & 0 deletions lib/__tests__/__fixtures__/01-a-project-with-tags/project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
track: web-dev
tracks:
- web-dev
tags:
- featured
- beta
- deprecated
- hidden
learningObjectives:
- html
- css
- dom
47 changes: 47 additions & 0 deletions lib/__tests__/project.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 27 additions & 1 deletion lib/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -197,14 +219,17 @@ export const parseProject = async (dir, opts, pkg) => {
}),
);

const { cover, thumb } = meta;

const { cover, thumb, tags } = meta;
const { track, tracks } = parseTracks(meta);

const {
learningObjectives,
variants,
} = await transformLearningObjectives(dir, opts, meta) || {};

const parsedTags = parseTags(tags);

return {
slug,
repo: opts.repo,
Expand All @@ -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(
Expand Down

0 comments on commit e4daa01

Please sign in to comment.