Skip to content
Closed
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 .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CHANGELOG.md
README.md
*.d.ts
recipes

Expand Down
19 changes: 19 additions & 0 deletions jest/docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const path = require('path');
const utils = require('../@tunnckocore/utils/src');

const ROOT = path.dirname(__dirname);
const { exts, workspaces } = utils.createAliases(ROOT);

module.exports = {
rootDir: ROOT,
displayName: 'docs',
testMatch: workspaces.map((ws) => `<rootDir>/${ws}/*/src/index.js`),
testPathIgnorePatterns: [
/node_modules/.toString(),
/(?:__)?(?:fixtures?|supports?|shared)(?:__)?/.toString(),
/.+(?:-config|babel-preset).+/.toString(),
],
// moduleNameMapper: alias,
moduleFileExtensions: exts,
runner: path.join(ROOT, 'packages/jest-runner-docs/src/index.js'),
};
48 changes: 48 additions & 0 deletions packages/jest-runner-docs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "jest-runner-docs",
"version": "0.0.1",
"description": "WIP",
"repository": "https://github.com/tunnckoCore/opensource/tree/master/packages/jest-runner-docs",
"homepage": "https://tunnckocore.com/opensource",
"author": "Charlike Mike Reagent <opensource@tunnckocore.com> (https://tunnckocore.com)",
"license": "MPL-2.0",
"engines": {
"node": ">=8.11"
},
"main": "src/index.js",
"module": "src/index.js",
"types": "dist/types/index.d.ts",
"files": [
"src"
],
"keywords": [
"tunnckocorehq",
"tunnckocore-oss",
"hela",
"development",
"developer-experience",
"dx",
"docs",
"documentation",
"typescript",
"tsc",
"parse-comments",
"comments",
"jsdoc",
"tsdoc"
],
"scripts": {},
"peerDependencies": {},
"dependencies": {
"@tunnckocore/create-jest-runner": "^0.7.0",
"@tunnckocore/utils": "^0.9.0",
"cosmiconfig": "^5.2.1",
"parse-comments": "^1.0.0"
},
"devDependencies": {},
"publishConfig": {
"access": "public",
"tag": "latest"
},
"licenseStart": 2019
}
56 changes: 56 additions & 0 deletions packages/jest-runner-docs/src/docks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const fs = require('fs');
const Comments = require('parse-comments');

const parser = new Comments();

module.exports = function docks(filepath) {
const comments = parser.parse(fs.readFileSync(filepath, 'utf8'));
const contents = comments
.filter((cmt) =>
cmt.tags.find(
(x) =>
(x.title === 'api' && x.name === 'public') || x.title === 'public',
),
)
.reduce((acc, comment) => {
const tagName = comment.tags.find((tag) => tag.title === 'name');
const tags = tagName
? comment.tags.filter((x) => x.title !== 'name')
: comment.tags;

let name = (tagName && tagName.name) || comment.code.context.name;
name = !name.startsWith('.') ? `.${name}` : name;

const index = comment.code.value.indexOf('(');
const signature = comment.code.value.slice(index, -1);
const signatureBlock = `\n\`\`\`ts\nfunction${signature}\n\`\`\`\n`;

const tagsString = tags
.filter((tag) => !/api|public|private/.test(tag.title))
.map((tag) =>
/returns?/.test(tag.title) ? { ...tag, name: 'returns' } : tag,
)
.map((tag) => `- **${tag.name}** - ${tag.description}`)
.join('\n');

const str = `### ${name}\n\n${
comment.description
}\n\n**Signature**\n${signatureBlock}\n**Params**\n\n${tagsString}\n${comment.examples
.map(
(example) =>
`\n${
example.description
}\n\n**Example**\n\n\`\`\`${example.language || 'js'}${
example.value
}\`\`\``,
)
.join('\n')}`;

return `${acc}\n\n${str}`;
}, '');

return {
filepath,
contents,
};
};
4 changes: 4 additions & 0 deletions packages/jest-runner-docs/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { join } = require('path');
const { createJestRunner } = require('@tunnckocore/create-jest-runner');

module.exports = createJestRunner(join(__dirname, 'runner.js'));
148 changes: 148 additions & 0 deletions packages/jest-runner-docs/src/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const fs = require('fs');
const path = require('path');

const { pass, fail, skip } = require('@tunnckocore/create-jest-runner');
const { isMonorepo } = require('@tunnckocore/utils');

const cosmiconfig = require('cosmiconfig');
const docks = require('./docks');

// const jestRunnerConfig = cosmiconfig('jest-runner');
// const jestRunnerDocks = cosmiconfig('docks');

// ! todo: support multiple entry files, not only "index"
module.exports = async function jetRunnerDocs({ testPath, config }) {
const start = new Date();

const outputFile = await tryCatch(testPath, start, () => {
const { contents } = docks(testPath);

if (contents.length === 0) {
return {
skip: skip({
start,
end: Date.now(),
test: {
path: testPath,
title: 'Docks',
},
}),
};
}

/** Find correct root path */
const pkgRoot = isMonorepo(config.cwd)
? path.dirname(path.dirname(testPath))
: config.rootDir;

// ! todo: better structure
const outFile = path.join(pkgRoot, 'docs', 'README.md');
const outDir = path.dirname(outFile);

fs.mkdirSync(outDir, { recursive: true });
fs.writeFileSync(outFile, contents);

return outFile;
});
if (outputFile.hasError) return outputFile.error;

if (outputFile.skip) return outputFile.skip;

return pass({
start,
end: Date.now(),
test: {
path: outputFile,
title: 'Docks',
},
});
};

async function tryCatch(testPath, start, fn) {
try {
return await fn();
} catch (err) {
return {
hasError: true,
error: fail({
start,
end: new Date(),
test: {
path: testPath,
title: 'Docks',
errorMessage: `jest-runner-docs: ${err.message}`,
},
}),
};
}
}

// async function tryLoadConfig({ testPath, config: jestConfig, start }) {
// const cfg = await tryCatch(testPath, start, () => {
// let result = null;
// result = jestRunnerConfig.searchSync();

// if (
// // if `jest-runner.config.js` not found
// !result ||
// // or found
// (result && // but // the `rollup` property is not an object
// ((result.config.docs && typeof result.config.docs !== 'object') ||
// // or, the `rolldown` property is not an object
// (result.config.docks && typeof result.config.docks !== 'object') ||
// // or, there is not such fields
// (!result.config.docs || !result.config.docks)))
// ) {
// // then we trying `jest-runner-rollup.config.js`
// result = jestRunnerDocks.searchSync();
// } else {
// // if `jest-runner.config.js` found, we try one of both properties
// result = {
// ...result,
// config: result.config.docs || result.config.docks,
// };
// }

// return result;
// });

// if (cfg.hasError) return cfg;

// if (!cfg || (cfg && !cfg.config)) {
// const filepath = cfg && path.relative(cfg.filepath, jestConfig.cwd);
// const message = cfg
// ? `Empty configuration, found at: ${filepath}`
// : 'Cannot find configuration for Docks.';

// return {
// hasError: true,
// error: fail({
// start,
// end: new Date(),
// test: {
// path: testPath,
// title: 'Docks',
// errorMessage: `jest-runner-docs: ${message}`,
// },
// }),
// };
// }

// return cfg.config;
// }

// function tryExtensions(filepath, config) {
// const { extensions } = getWorkspacesAndExtensions(config.cwd);
// const hasExtension = path.extname(filepath).length > 0;

// if (hasExtension) {
// return filepath;
// }

// const extension = extensions.find((ext) => fs.existsSync(filepath + ext));
// if (!extension) {
// throw new Error(`Cannot find input file: ${filepath}`);
// }

// return filepath + extension;
// }
6 changes: 6 additions & 0 deletions packages/jest-runner-docs/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mod from '../src';

test('make tests for jest-runner-docs package', async () => {
expect(typeof mod).toStrictEqual('function');
mod();
});
2 changes: 1 addition & 1 deletion packages/koa-better-body/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ You might also be interested in these packages:

## Contributing

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/tunnckoCore/koa-better-body/issues/new).
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/tunnckoCore/koa-better-body/issues/new).
But before doing anything, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) guidelines.

### Contributing Recipes
Expand Down
2 changes: 1 addition & 1 deletion packages/koa-better-body/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"is-buffer": "2.0.3",
"koa": "1.6.2",
"koa-route": "3.2.0",
"qs": "6.8.0",
"qs": "6.9.0",
"supertest": "4.0.2"
},
"publishConfig": {
Expand Down
Loading