Skip to content

Commit

Permalink
Merge pull request #24 from universal-ember/sort-tree-and-json-c
Browse files Browse the repository at this point in the history
Sort trees and json C
  • Loading branch information
NullVoxPopuli authored Feb 21, 2024
2 parents bfd1ff2 + f2f2f77 commit 18c58ba
Show file tree
Hide file tree
Showing 29 changed files with 424 additions and 35 deletions.
10 changes: 10 additions & 0 deletions docs-app/app/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Route from '@ember/routing/route';
import { service } from '@ember/service';

export default class NotHere extends Route {
@service router;

beforeModel() {
this.router.replaceWith('/usage/setup.md');
}
}
2 changes: 1 addition & 1 deletion docs-app/public/docs/components/api-docs.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# `<APIDocs />`

```hbs live
```hbs live no-shadow
<APIDocs @module="declarations/browser/re-exports" @name="APIDocs" @apiDocs="/docs/kolay.json" />
```
3 changes: 3 additions & 0 deletions docs-app/public/docs/components/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"order": ["component-signature", "api-docs", "logs"]
}
5 changes: 5 additions & 0 deletions docs-app/public/docs/meta.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
// We want usage to be first, which breaks the
// default sort order provided by the filesystem: Alphabetical
"order": ["usage", "components", "plugins"],
}
11 changes: 9 additions & 2 deletions docs-app/public/docs/plugins/create-manifest.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# `createManifest(...)`

```hbs live
## Config

either json or jsonc

- set order
- change name

```hbs live no-shadow
<APIDocs @module="declarations/plugins/vite" @name="createManifest" @apiDocs="/docs/kolay.json" />
```

```hbs live
```hbs live no-shadow
<APIDocs @module="src/plugins/types" @name="CreateManifestOptions" @apiDocs="/docs/kolay.json" />
```
1 change: 1 addition & 0 deletions fixtures/discover/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
currently, top level pages are not allowed - everything must be in a folder
Empty file added fixtures/discover/a.md
Empty file.
Empty file added fixtures/discover/b.md
Empty file.
Empty file added fixtures/discover/c/c-a.md
Empty file.
Empty file added fixtures/discover/c/c-b.md
Empty file.
Empty file added fixtures/discover/c/d/d-a.md
Empty file.
Empty file added fixtures/discover/c/d/d-b.md
Empty file.
Empty file added fixtures/discover/c/e/e-a.md
Empty file.
Empty file added fixtures/discover/c/e/e-b.md
Empty file.
3 changes: 3 additions & 0 deletions fixtures/discover/c/e/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"order": ["e-b", "e-a"]
}
3 changes: 3 additions & 0 deletions fixtures/discover/c/meta.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"order": ["c-b", "c-a"],
}
3 changes: 3 additions & 0 deletions fixtures/discover/meta.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"order": ["b", "a"],
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@tsconfig/ember": "^3.0.3",
"@zamiell/typedoc-plugin-not-exported": "^0.2.0",
"globby": "^14.0.0",
"json5": "^2.2.3",
"package-up": "^5.0.0",
"typedoc": "^0.25.8",
"unplugin": "^1.6.0"
Expand Down
5 changes: 5 additions & 0 deletions pnpm-lock.yaml

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

33 changes: 33 additions & 0 deletions src/plugins/create-manifest/discover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { reshape } from './hydrate.js';

/**
* @typedef {object} Options
* @property {string | undefined} [ include ]
* @property {string[] | undefined} [ exclude ]
* @property {string} cwd
* @property {boolean | undefined} [ onlyDirectories ]
*
* @param {Options} options
*/
export async function discover({ include, onlyDirectories, exclude, cwd }) {
include ??= '**/*';
exclude ??= [];
onlyDirectories ??= false;

const { globbySync } = await import('globby');

let paths = globbySync(include, {
cwd,
expandDirectories: true,
onlyDirectories,
});

// Needs to be const, because TS thinks exclude can change while `filter` is running.
const excludePattern = exclude;

paths = paths.filter((path) => !excludePattern.some((pattern) => path.match(pattern)));

const reshaped = await reshape(paths, cwd);

return reshaped;
}
79 changes: 79 additions & 0 deletions src/plugins/create-manifest/discover.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import path from 'node:path';
import url from 'node:url';

import { describe, expect, test } from 'vitest';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

import { discover } from './discover.js';

const fixtures = path.join(__dirname, '../../../fixtures/discover');

describe('discover', () => {
test('it works', async () => {
let result = await discover({ cwd: fixtures });

expect(result.tree).toMatchInlineSnapshot(`
{
"first": "/c/c-b.md",
"name": "root",
"pages": [
{
"first": "/c/c-b.md",
"name": "c",
"pages": [
{
"groupName": "c",
"name": "c-b",
"path": "/c/c-b.md",
"tutorialName": "cb",
},
{
"groupName": "c",
"name": "c-a",
"path": "/c/c-a.md",
"tutorialName": "ca",
},
{
"first": "/c/d/d-a.md",
"name": "d",
"pages": [
{
"groupName": "d",
"name": "d-a",
"path": "/c/d/d-a.md",
"tutorialName": "da",
},
{
"groupName": "d",
"name": "d-b",
"path": "/c/d/d-b.md",
"tutorialName": "db",
},
],
},
{
"first": "/c/e/e-b.md",
"name": "e",
"pages": [
{
"groupName": "e",
"name": "e-b",
"path": "/c/e/e-b.md",
"tutorialName": "eb",
},
{
"groupName": "e",
"name": "e-a",
"path": "/c/e/e-a.md",
"tutorialName": "ea",
},
],
},
],
},
],
}
`);
});
});
6 changes: 5 additions & 1 deletion src/plugins/create-manifest/hydrate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { parse } from './parse.js';
import { configsFrom, parse } from './parse.js';
import { sortTree } from './sort.js';

/**
* @param {string[]} paths
* @param {string} cwd path on disk that the paths are relative to - needed for looking up configs
*/
export async function reshape(paths, cwd) {
let tree = await parse(paths, cwd);
let configs = await configsFrom(paths, cwd);

tree = sortTree(tree, configs);

addInTheFirstPage(tree);

Expand Down
19 changes: 2 additions & 17 deletions src/plugins/create-manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'node:assert';

import { createUnplugin } from 'unplugin';

import { reshape } from './hydrate.js';
import { discover } from './discover.js';

/**
*
Expand All @@ -25,26 +25,11 @@ export const createManifest = createUnplugin(
name: 'create-manifest',
async buildStart() {
name ??= 'manifest.json';
include ??= '**/*';
exclude ??= [];
onlyDirectories ??= false;

const path = await import('node:path');
const { globbySync } = await import('globby');

const cwd = src ? path.join(process.cwd(), src) : process.cwd();
let paths = globbySync(include, {
cwd,
expandDirectories: true,
onlyDirectories,
});

// Needs to be const, because TS thinks exclude can change while `filter` is running.
const excludePattern = exclude;

paths = paths.filter((path) => !excludePattern.some((pattern) => path.match(pattern)));

const reshaped = await reshape(paths, cwd);
const reshaped = await discover({ cwd, onlyDirectories, exclude, include });

this.emitFile({
type: 'asset',
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/create-manifest/parse.build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('build', () => {
"pages": [
{
"groupName": "top",
"name": "nested.md",
"name": "nested",
"path": "/top/nested.md",
"tutorialName": "nested",
},
Expand All @@ -42,19 +42,19 @@ describe('build', () => {
"pages": [
{
"groupName": "top",
"name": "nested.md",
"name": "nested",
"path": "/top/nested.md",
"tutorialName": "nested",
},
{
"groupName": "top",
"name": "nested-sibling.md",
"name": "nested-sibling",
"path": "/top/nested-sibling.md",
"tutorialName": "nestedsibling",
},
{
"groupName": "top",
"name": "other.md",
"name": "other",
"path": "/top-2/other.md",
"tutorialName": "other",
},
Expand All @@ -80,7 +80,7 @@ describe('build', () => {
"pages": [
{
"groupName": "deep",
"name": "path.md",
"name": "path",
"path": "/top/deep/path.md",
"tutorialName": "path",
},
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('build', () => {
"pages": [
{
"groupName": "another",
"name": "index.md",
"name": "index",
"path": "/top/deep/another/index.md",
"tutorialName": "index",
},
Expand Down
Loading

0 comments on commit 18c58ba

Please sign in to comment.