Skip to content

Commit 2897571

Browse files
committed
updates
1 parent e324bd5 commit 2897571

File tree

2 files changed

+42
-104
lines changed

2 files changed

+42
-104
lines changed

docs-viewer/src/preview-docs.ts

+37-103
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,22 @@
55
import chalk from 'chalk';
66
import path from 'path';
77
import fs from 'fs';
8-
import { findWorkspaceDir } from '@pnpm/find-workspace-dir';
8+
import {
9+
determinePackageManager,
10+
docsViewerRoot,
11+
generateDocs,
12+
getCurrentVersion,
13+
log,
14+
maybeMakePNPMInstallable,
15+
projectRoot,
16+
repoDetails,
17+
workspaceRoot,
18+
} from './-utils';
919

1020
const EMBER_API_DOCS_REPO = `git@github.com:ember-learn/ember-api-docs.git`;
1121
const EMBER_JSONAPI_DOCS_REPO = `git@github.com:ember-learn/ember-jsonapi-docs.git`;
1222
const EMBER_API_DOCS_DATA_REPO = `git@github.com:ember-learn/ember-api-docs-data.git`;
1323

14-
const workspaceRoot = (await findWorkspaceDir(process.cwd())) as string;
15-
16-
if (!workspaceRoot) {
17-
throw new Error('Could not find workspace root');
18-
}
19-
20-
const docsViewerRoot = path.join(workspaceRoot, 'docs-viewer');
21-
22-
function log(message: string) {
23-
console.log(chalk.grey(`[docs-viewer]\t${message}`));
24-
}
25-
26-
function repoDetails(gitUrl: string) {
27-
const repoPath = gitUrl.replace('.git', '').replace('git@github.com:', '');
28-
const [org, name] = repoPath.split('/');
29-
const installPathFromRoot = path.join('./projects', name);
30-
const location = path.join(docsViewerRoot, installPathFromRoot);
31-
32-
return {
33-
org,
34-
name,
35-
repoPath,
36-
gitUrl,
37-
installPathFromRoot,
38-
location,
39-
relativePath: path.relative(__dirname, path.join(docsViewerRoot, installPathFromRoot)),
40-
};
41-
}
42-
43-
async function getCurrentVersion(tool: string) {
44-
const proc = Bun.spawn([tool, '--version'], {
45-
env: process.env,
46-
stdio: ['inherit', 'pipe', 'inherit'],
47-
});
48-
await proc.exited;
49-
const version = await new Response(proc.stdout).text();
50-
return version.trim().replace('v', '');
51-
}
52-
53-
function determinePackageManager(dir: string) {
54-
if (fs.existsSync(path.join(dir, 'pnpm-lock.yaml'))) {
55-
return 'pnpm';
56-
}
57-
if (fs.existsSync(path.join(dir, 'package-lock.json'))) {
58-
return 'npm';
59-
}
60-
if (fs.existsSync(path.join(dir, 'yarn.lock'))) {
61-
return 'yarn';
62-
}
63-
64-
return 'npm';
65-
}
66-
6724
async function getOrUpdateRepo(gitUrl: string) {
6825
const details = repoDetails(gitUrl);
6926

@@ -75,42 +32,11 @@ async function getOrUpdateRepo(gitUrl: string) {
7532

7633
// install dependencies
7734
const packageManager = determinePackageManager(details.location);
78-
7935
if (packageManager === 'pnpm') {
80-
// get the version to use from package.json
81-
const packageJson = require(path.join(details.location, 'package.json'));
82-
83-
let nodeVersion = await getCurrentVersion('node');
84-
const pnpmVersion = await getCurrentVersion('pnpm');
85-
86-
// ember-api-docs requires an older node due to node-sass
87-
if (packageJson.name === 'ember-api-docs') {
88-
nodeVersion = '20.19.0';
89-
}
90-
91-
if (
92-
!packageJson.volta ||
93-
packageJson.volta.node !== nodeVersion ||
94-
packageJson.volta.pnpm !== pnpmVersion ||
95-
packageJson.packageManager ||
96-
packageJson.engines?.node !== nodeVersion
97-
) {
98-
delete packageJson.packageManager;
99-
packageJson.volta = {
100-
node: nodeVersion,
101-
pnpm: pnpmVersion,
102-
};
103-
packageJson.engines = packageJson.engines || {};
104-
packageJson.engines.node = nodeVersion;
105-
106-
fs.writeFileSync(path.join(details.location, 'package.json'), JSON.stringify(packageJson, null, 2));
107-
const proc = Bun.spawn(['git', 'commit', '-am', '"ensure volta works as expected"'], {
108-
cwd: details.location,
109-
env: process.env,
110-
stdio: ['inherit', 'inherit', 'inherit'],
111-
});
112-
await proc.exited;
113-
}
36+
// some of the repositories use pnpm but do not have volta configured
37+
// and have not had their engines/packageManager field updated in a while.
38+
// this lets us still install them with pnpm if that is the case
39+
await maybeMakePNPMInstallable(details);
11440
}
11541

11642
log(`Installing dependencies in ${chalk.green(details.installPathFromRoot)} using ${chalk.yellow(packageManager)}`);
@@ -125,6 +51,10 @@ async function getOrUpdateRepo(gitUrl: string) {
12551
await proc.exited;
12652
}
12753

54+
/**
55+
* Updates the repo by fetching only the latest commit on the main branch
56+
* and resetting the local repo to that commit
57+
*/
12858
async function getLatest(details: ReturnType<typeof repoDetails>) {
12959
log(`Updating ${chalk.green(details.repoPath)} in ${chalk.green(details.installPathFromRoot)} to latest`);
13060
const mainBranch = details.name === 'ember-jsonapi-docs' ? 'master' : 'main';
@@ -138,6 +68,9 @@ async function getLatest(details: ReturnType<typeof repoDetails>) {
13868
await proc2.exited;
13969
}
14070

71+
/**
72+
* Clones the repo, fetching only the latest commit
73+
*/
14174
async function cloneRepo(details: ReturnType<typeof repoDetails>) {
14275
const relativePath = path.join('./projects', details.name);
14376
log(`Cloning ${chalk.green(details.repoPath)} to ${chalk.green(relativePath)}`);
@@ -152,44 +85,45 @@ async function main() {
15285
log(`\tCurrent working directory: ${chalk.green(process.cwd())}`);
15386

15487
// clone repos
88+
//////////////
89+
//
15590
await getOrUpdateRepo(EMBER_API_DOCS_DATA_REPO);
15691
await getOrUpdateRepo(EMBER_JSONAPI_DOCS_REPO);
15792
await getOrUpdateRepo(EMBER_API_DOCS_REPO);
15893

15994
// symlink our own project root into projects as 'ember-data'
160-
const emberDataLocation = path.join(docsViewerRoot, './projects/ember-data');
95+
/////////////////////////////////////////////////////////////
96+
//
97+
const emberDataLocation = path.join(projectRoot, 'ember-data');
16198
if (!fs.existsSync(emberDataLocation)) {
16299
log(`Symlinking ${chalk.green('ember-data')} to ${chalk.green('./projects/ember-data')}`);
163100
fs.symlinkSync(workspaceRoot, emberDataLocation);
164101
}
165102

166-
// symlink `ember-api-docs-data` to `ember-api-docs`
167-
const projectRoot = path.join(docsViewerRoot, './projects');
103+
// symlink `ember-api-docs-data` into `ember-api-docs`
104+
////////////////////////////////////////////////////
105+
//
168106
const emberApiDocsData = path.join(projectRoot, 'ember-api-docs-data');
169107
const symLinkLocation = path.join(projectRoot, 'ember-api-docs/ember-api-docs-data');
170-
171108
if (!fs.existsSync(symLinkLocation)) {
172109
log(`Symlinking ${chalk.green('ember-api-docs-data')} to ${chalk.green('ember-api-docs')}`);
173110
fs.symlinkSync(emberApiDocsData, symLinkLocation);
174111
}
175112

176113
log('Docs viewer setup complete');
177-
178114
log('Generating the current docs in ember-jsonapi-docs (these will be inserted into ember-api-docs-data)');
179-
const currentVersion = require(path.join(workspaceRoot, 'package.json')).version;
180-
const absoluteVersion = currentVersion.split('-')[0];
181-
const command = ['bun', 'gen', '--project', 'ember-data', '--version', absoluteVersion];
182115

183-
const proc = Bun.spawn(command, {
184-
cwd: path.join(projectRoot, 'ember-jsonapi-docs'),
185-
env: process.env,
186-
stdio: ['inherit', 'inherit', 'inherit'],
187-
});
188-
await proc.exited;
116+
// generate the docs
117+
////////////////////
118+
//
119+
await generateDocs();
189120

190121
log('Docs generated. Run `bun regenerate-docs` to update the docs with any changes');
191122
log('Starting the docs viewer');
192123

124+
// start the docs viewer
125+
////////////////////////
126+
//
193127
const proc2 = Bun.spawn(['pnpm', 'start'], {
194128
cwd: path.join(projectRoot, 'ember-api-docs'),
195129
env: process.env,

docs-viewer/src/rebuild-docs.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
* Rebuilds the data used by the docs viewer app
55
*/
66

7-
async function main();
7+
import { generateDocs } from './-utils';
8+
9+
async function main() {
10+
await generateDocs();
11+
}
812

913
main();

0 commit comments

Comments
 (0)