Skip to content

Commit

Permalink
fix(auto): 🐛 Fixes in the exports and readme generation
Browse files Browse the repository at this point in the history
  • Loading branch information
NiccoloOlivieriAchille committed Nov 22, 2023
1 parent 4f82a02 commit a850f2c
Show file tree
Hide file tree
Showing 11 changed files with 533 additions and 413 deletions.
5 changes: 1 addition & 4 deletions .autorc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const base = require('@bepower/auto-config').default;
const { default: base, coverage, packages } = require('@bepower/auto-config');

const packages = require.resolve('@bepower/auto-config/scripts/add-packages-to-readme');

const coverage = require.resolve('@bepower/auto-config/scripts/add-coverage-to-readme');
/**
* @type {import('@bepower/auto-config/scripts/add-coverage-to-readme').AddCoverageToReadmePluginOptions}
*/
Expand Down
51 changes: 47 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,51 @@
# Title
# BePower Code Style

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

<!-- COVERAGE-BADGE:START - Do not remove or modify this section -->
[badge-all-contributors]: https://img.shields.io/badge/all_contributors-1-orange.svg

![coverage: 99%](https://img.shields.io/badge/coverage-99%25-green.svg)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

<!-- COVERAGE-BADGE:END -->
[![All Contributors][badge-all-contributors]](#contributors-) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md)

This repository contains common configurations for building apps at BePower.

## Usage

This repo is managed as a monorepo that is composed of many npm packages, where each package has its own `README` and documentation describing usage.

### Packages Index

<!-- PACKAGES-TABLE:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
| Package | Install command |
| --- | --- |
| [![@bepower/auto-config: 10.0.2](https://img.shields.io/badge/@bepower/auto--config-10.0.2-brightgreen.svg)](packages/auto-config) | `$ npm install --save-dev @bepower/[email protected]` |
| [![@bepower/eslint-plugin: 4.0.0](https://img.shields.io/badge/@bepower/eslint--plugin-4.0.0-brightgreen.svg)](packages/eslint-plugin) | `$ npm install --save-dev @bepower/[email protected]` |
| [![@bepower/prettier-config: 5.0.0](https://img.shields.io/badge/@bepower/prettier--config-5.0.0-brightgreen.svg)](packages/prettier-config) | `$ npm install --save-dev @bepower/[email protected]` |
<!-- prettier-ignore-end -->
<!-- PACKAGES-TABLE:END -->

### Releasing

Everything is automated with `auto` and Github Actions

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://github.com/NiccoloOlivieriAchille"><img src="https://avatars.githubusercontent.com/u/55181558?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Niccolò Olivieri Achille</b></sub></a><br /><a href="#infra-NiccoloOlivieriAchille" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/BePowerDeploy <[email protected]>/@bepower/dev-configs/commits?author=NiccoloOlivieriAchille" title="Code">💻</a> <a href="https://github.com/BePowerDeploy <[email protected]>/@bepower/dev-configs/commits?author=NiccoloOlivieriAchille" title="Documentation">📖</a> <a href="https://github.com/BePowerDeploy <[email protected]>/@bepower/dev-configs/commits?author=NiccoloOlivieriAchille" title="Tests">⚠️</a></td>
</tr>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
"format": "prettier --write \"**/*.{ts,json,md,yml}\"",
"lint": "eslint --fix . --ext .ts",
"check": "npm run format && npm run lint",
"build": "tsc",
"build": "tsc --build",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"
},
"devDependencies": {
"@auto-it/all-contributors": "^11.0.4",
"@auto-it/conventional-commits": "^11.0.4",
"@auto-it/core": "^11.0.4",
"@auto-it/first-time-contributor": "^11.0.4",
"@bepower/auto-config": "^2.0.0",
"@bepower/eslint-plugin": "^3.0.0",
"@bepower/prettier-config": "^3.0.0",
"@bepower/auto-config": "^10.0.2",
"@bepower/eslint-plugin": "^4.0.0",
"@bepower/prettier-config": "^5.0.0",
"@tsconfig/recommended": "^1.0.3",
"@types/eslint": "^8.44.7",
"@types/jest": "^29.5.9",
Expand Down
8 changes: 7 additions & 1 deletion packages/auto-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ A collection of `auto` config and scripts to ease package deployment

Regenerates the `README.md` file in every package to add the coverage badge, if present.

To mark the badge you have to add `<!-- COVERAGE-BADGE:START - Do not remove or modify this section -->` and `
To mark the badge you have to add `<!-- COVERAGE-BADGE:START - Do not remove or modify this section -->` and `<!-- COVERAGE-BADGE:END -->`.

### `scripts/add-packages-to-readme.js`

Regenerates the `README.md` file in the root of the project to reflect npm packages under the `packages` folder (or whatever folder you choose).

To mark the badge you have to add `<!-- PACKAGES-TABLE:START - Do not remove or modify this section -->` and `<!-- PACKAGES-TABLE:END -->`.
3 changes: 3 additions & 0 deletions packages/auto-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { AutoRc } from 'auto';
export * from './scripts/add-coverage-to-readme';
export * from './scripts/add-packages-to-readme';

export const coverage = require.resolve('./scripts/add-coverage-to-readme');
export const packages = require.resolve('./scripts/add-packages-to-readme');

const conventionalCommitsOptions: ConventionalCommitsOptions = {
preset: 'angular',
};
Expand Down
29 changes: 16 additions & 13 deletions packages/auto-config/src/scripts/add-coverage-to-readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,29 @@ export default class AddCoverageToReadmePlugin implements IPlugin {
}

const [firstPart] = readme.split(AddCoverageToReadmePlugin.START_TAG);
const [, lastPart] = readme.split(AddCoverageToReadmePlugin.END_TAG);
const [, lastPart, ...otherParts] = readme.split(AddCoverageToReadmePlugin.END_TAG);

const badgeString = this.badgeTemplate
.replace(/{PERC}/g, coveragePerc.toString())
.replace(/{COLOR}/g, coverageColor);

auto.logger.verbose.info('Badge string:', badgeString);

writeFileSync(
readmePath,
[
firstPart,
AddCoverageToReadmePlugin.START_TAG,
'\n\n',
badgeString,
'\n\n',
AddCoverageToReadmePlugin.END_TAG,
lastPart,
].join(''),
);
const contentArr = [
firstPart,
AddCoverageToReadmePlugin.START_TAG,
'\n\n',
badgeString,
'\n\n',
AddCoverageToReadmePlugin.END_TAG,
lastPart,
otherParts.join(AddCoverageToReadmePlugin.END_TAG),
];
if (otherParts.length > 0 && readme.endsWith(AddCoverageToReadmePlugin.END_TAG)) {
contentArr.push(AddCoverageToReadmePlugin.END_TAG);
}

writeFileSync(readmePath, contentArr.join(''));

return true;
});
Expand Down
36 changes: 19 additions & 17 deletions packages/auto-config/src/scripts/add-packages-to-readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class AddPackagesToReadmePlugin implements IPlugin {
}

const [firstPart] = readme.split(AddPackagesToReadmePlugin.START_TAG);
const [, lastPart] = readme.split(AddPackagesToReadmePlugin.END_TAG);
const [, lastPart, ...otherParts] = readme.split(AddPackagesToReadmePlugin.END_TAG);

const rows = ['| Package | Install command |', '| --- | --- |'];
rows.push(
Expand All @@ -70,22 +70,24 @@ export default class AddPackagesToReadmePlugin implements IPlugin {
const rowsString = rows.join('\n');
auto.logger.verbose.info('Packages table:', rowsString);

writeFileSync(
readmePath,
[
firstPart,
AddPackagesToReadmePlugin.START_TAG,
'\n',
'<!-- prettier-ignore-start -->',
'\n',
rowsString,
'\n',
'<!-- prettier-ignore-end -->',
'\n',
AddPackagesToReadmePlugin.END_TAG,
lastPart,
].join(''),
);
const contentArr = [
firstPart,
AddPackagesToReadmePlugin.START_TAG,
'\n',
'<!-- prettier-ignore-start -->',
'\n',
rowsString,
'\n',
'<!-- prettier-ignore-end -->',
'\n',
AddPackagesToReadmePlugin.END_TAG,
lastPart,
];
if (otherParts.length > 0 && readme.endsWith(AddPackagesToReadmePlugin.END_TAG)) {
contentArr.push(AddPackagesToReadmePlugin.END_TAG);
}

writeFileSync(readmePath, contentArr.join(''));

const changedFiles = await execPromise('git', ['status', '--porcelain']);

Expand Down
60 changes: 60 additions & 0 deletions packages/auto-config/test/scripts/add-coverage-to-readme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,64 @@ describe('Add coverage to Readme Plugin', () => {
expect(gitShow).not.toHaveBeenCalledWith('git', ['add', '**/README.md']);
});
});

describe('duplicated tags', () => {
test('should substitute only the first occurence', async () => {
const addCoverageToReadme = new AddCoverageToReadme();
const autoHooks = makeHooks();

mockRead.mockReturnValueOnce(
[
'# Title',
'',
'',
'<!-- COVERAGE-BADGE:START - Do not remove or modify this section -->',
'<!-- COVERAGE-BADGE:END -->',
'<!-- COVERAGE-BADGE:START - Do not remove or modify this section --><!-- COVERAGE-BADGE:END -->',
].join('\n'),
);
gitShow.mockReturnValueOnce('README.md');

mockRootCoverage.mockReturnValueOnce(coverageSummary);

addCoverageToReadme.apply({
hooks: autoHooks,
logger: dummyLog(),
} as Auto);

await autoHooks.afterChangelog.promise({
bump: SEMVER.patch,
currentVersion: '0.0.0',
lastRelease: '0.0.0',
releaseNotes: '',
commits: [],
});

expect(mockRootCoverage).toHaveBeenCalled();

expect(mockWrite).toHaveBeenCalledWith(
join(process.cwd(), 'README.md'),
[
'# Title',
'',
'',
'<!-- COVERAGE-BADGE:START - Do not remove or modify this section -->',
'',
'![coverage: 99%](https://img.shields.io/badge/coverage-99%25-green.svg)',
'',
'<!-- COVERAGE-BADGE:END -->',
'<!-- COVERAGE-BADGE:START - Do not remove or modify this section --><!-- COVERAGE-BADGE:END -->',
].join('\n'),
);

expect(gitShow).toHaveBeenCalledWith('git', ['status', '--porcelain']);
expect(gitShow).toHaveBeenCalledWith('git', ['add', '**/README.md']);
expect(gitShow).toHaveBeenCalledWith('git', [
'commit',
'--no-verify',
'-m',
'"ci: :memo: Update README.md to add coverage [skip ci]"',
]);
});
});
});
Loading

0 comments on commit a850f2c

Please sign in to comment.