Skip to content

Commit bddfd60

Browse files
committed
make work
1 parent 2897571 commit bddfd60

File tree

3 files changed

+175
-17
lines changed

3 files changed

+175
-17
lines changed

docs-viewer/src/-utils.ts

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { findWorkspaceDir } from '@pnpm/find-workspace-dir';
2+
import path from 'path';
3+
import chalk from 'chalk';
4+
import fs from 'fs';
5+
6+
const workspaceRoot = (await findWorkspaceDir(process.cwd())) as string;
7+
8+
if (!workspaceRoot) {
9+
throw new Error('Could not find workspace root');
10+
}
11+
12+
const docsViewerRoot = path.join(workspaceRoot, 'docs-viewer');
13+
const projectRoot = path.join(docsViewerRoot, './projects');
14+
15+
export { workspaceRoot, docsViewerRoot, projectRoot };
16+
17+
export function log(message: string) {
18+
console.log(chalk.grey(`[docs-viewer]\t${message}`));
19+
}
20+
21+
export async function getCurrentVersion(tool: string) {
22+
const proc = Bun.spawn([tool, '--version'], {
23+
env: process.env,
24+
stdio: ['inherit', 'pipe', 'inherit'],
25+
});
26+
await proc.exited;
27+
const version = await new Response(proc.stdout).text();
28+
return version.trim().replace('v', '');
29+
}
30+
31+
export function determinePackageManager(dir: string) {
32+
if (fs.existsSync(path.join(dir, 'pnpm-lock.yaml'))) {
33+
return 'pnpm';
34+
}
35+
if (fs.existsSync(path.join(dir, 'package-lock.json'))) {
36+
return 'npm';
37+
}
38+
if (fs.existsSync(path.join(dir, 'yarn.lock'))) {
39+
return 'yarn';
40+
}
41+
42+
return 'npm';
43+
}
44+
45+
export async function generateDocs() {
46+
const currentVersion = require(path.join(workspaceRoot, 'package.json')).version;
47+
const absoluteVersion = currentVersion.split('-')[0];
48+
const command = ['bun', 'gen', '--project', 'ember-data', '--version', absoluteVersion];
49+
const proc = Bun.spawn(command, {
50+
cwd: path.join(projectRoot, 'ember-jsonapi-docs'),
51+
env: Object.assign({}, process.env, { COREPACK_INTEGRITY_KEYS: 0 }),
52+
stdio: ['inherit', 'inherit', 'inherit'],
53+
});
54+
await proc.exited;
55+
56+
const command2 = ['bun', 'fix:files'];
57+
const proc2 = Bun.spawn(command2, {
58+
cwd: path.join(projectRoot, 'ember-api-docs-data'),
59+
env: process.env,
60+
stdio: ['inherit', 'inherit', 'inherit'],
61+
});
62+
await proc2.exited;
63+
}
64+
65+
export function repoDetails(gitUrl: string) {
66+
const repoPath = gitUrl.replace('.git', '').replace('git@github.com:', '');
67+
const [org, name] = repoPath.split('/');
68+
const installPathFromRoot = path.join('./projects', name);
69+
const location = path.join(docsViewerRoot, installPathFromRoot);
70+
71+
return {
72+
org,
73+
name,
74+
repoPath,
75+
gitUrl,
76+
installPathFromRoot,
77+
location,
78+
relativePath: path.relative(__dirname, path.join(docsViewerRoot, installPathFromRoot)),
79+
};
80+
}
81+
82+
export async function installDeps(packageManager: 'pnpm' | 'npm' | 'yarn', details: ReturnType<typeof repoDetails>) {
83+
const proc = Bun.spawn(
84+
[packageManager, 'install', packageManager === 'pnpm' ? '--ignore-workspace' : ''].filter(Boolean),
85+
{
86+
cwd: details.location,
87+
env: process.env,
88+
stdio: ['inherit', 'inherit', 'inherit'],
89+
}
90+
);
91+
await proc.exited;
92+
}
93+
94+
export async function maybeMakePNPMInstallable(details: ReturnType<typeof repoDetails>) {
95+
// get the version to use from package.json
96+
const packageJson = require(path.join(details.location, 'package.json'));
97+
98+
const nodeVersion = await getCurrentVersion('node');
99+
const pnpmVersion = await getCurrentVersion('pnpm');
100+
101+
if (
102+
!packageJson.volta ||
103+
packageJson.volta.node !== nodeVersion ||
104+
packageJson.volta.pnpm !== pnpmVersion ||
105+
packageJson.packageManager ||
106+
packageJson.engines?.node !== nodeVersion
107+
) {
108+
delete packageJson.packageManager;
109+
packageJson.volta = {
110+
node: nodeVersion,
111+
pnpm: pnpmVersion,
112+
};
113+
packageJson.engines = packageJson.engines || {};
114+
packageJson.engines.node = nodeVersion;
115+
116+
// if this is ember-api-docs we need to also force it to use dart-sass
117+
if (packageJson.name === 'ember-api-docs') {
118+
packageJson.pnpm = packageJson.pnpm || {};
119+
packageJson.pnpm.overrides = packageJson.pnpm.overrides || {};
120+
packageJson.pnpm.overrides['node-sass'] = 'npm:sass@^1.86.0';
121+
}
122+
123+
fs.writeFileSync(path.join(details.location, 'package.json'), JSON.stringify(packageJson, null, 2));
124+
125+
// run install to pickup the lockfile change
126+
await installDeps('pnpm', details);
127+
128+
const proc = Bun.spawn(['git', 'commit', '-am', '"ensure volta works as expected"'], {
129+
cwd: details.location,
130+
env: process.env,
131+
stdio: ['inherit', 'inherit', 'inherit'],
132+
});
133+
await proc.exited;
134+
}
135+
}

docs-viewer/src/preview-docs.ts

+39-16
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
determinePackageManager,
1010
docsViewerRoot,
1111
generateDocs,
12-
getCurrentVersion,
12+
installDeps,
1313
log,
1414
maybeMakePNPMInstallable,
1515
projectRoot,
1616
repoDetails,
1717
workspaceRoot,
18-
} from './-utils';
18+
} from './-utils.ts';
1919

2020
const EMBER_API_DOCS_REPO = `git@github.com:ember-learn/ember-api-docs.git`;
2121
const EMBER_JSONAPI_DOCS_REPO = `git@github.com:ember-learn/ember-jsonapi-docs.git`;
@@ -24,12 +24,17 @@ const EMBER_API_DOCS_DATA_REPO = `git@github.com:ember-learn/ember-api-docs-data
2424
async function getOrUpdateRepo(gitUrl: string) {
2525
const details = repoDetails(gitUrl);
2626

27+
let updated = true;
2728
if (fs.existsSync(details.location)) {
28-
await getLatest(details);
29+
updated = await getLatest(details);
2930
} else {
3031
await cloneRepo(details);
3132
}
3233

34+
if (!updated) {
35+
return;
36+
}
37+
3338
// install dependencies
3439
const packageManager = determinePackageManager(details.location);
3540
if (packageManager === 'pnpm') {
@@ -40,32 +45,50 @@ async function getOrUpdateRepo(gitUrl: string) {
4045
}
4146

4247
log(`Installing dependencies in ${chalk.green(details.installPathFromRoot)} using ${chalk.yellow(packageManager)}`);
43-
const proc = Bun.spawn(
44-
[packageManager, 'install', packageManager === 'pnpm' ? '--ignore-workspace' : ''].filter(Boolean),
45-
{
46-
cwd: details.location,
47-
env: process.env,
48-
stdio: ['inherit', 'inherit', 'inherit'],
49-
}
50-
);
51-
await proc.exited;
48+
await installDeps(packageManager, details);
49+
}
50+
51+
async function getSHA(details: ReturnType<typeof repoDetails>, reference: string) {
52+
log(`Getting commit sha for ${reference} for ${chalk.green(details.repoPath)}`);
53+
const shaProc1 = Bun.spawn(['git', 'rev-parse', reference], {
54+
cwd: details.location,
55+
});
56+
await shaProc1.exited;
57+
return (await new Response(shaProc1.stdout).text()).trim();
5258
}
5359

5460
/**
5561
* Updates the repo by fetching only the latest commit on the main branch
5662
* and resetting the local repo to that commit
63+
*
64+
* Returns true if the repo was updated, false if it was already up to date
5765
*/
58-
async function getLatest(details: ReturnType<typeof repoDetails>) {
66+
async function getLatest(details: ReturnType<typeof repoDetails>): Promise<boolean> {
67+
const currentSha = await getSHA(details, 'HEAD^1');
68+
5969
log(`Updating ${chalk.green(details.repoPath)} in ${chalk.green(details.installPathFromRoot)} to latest`);
6070
const mainBranch = details.name === 'ember-jsonapi-docs' ? 'master' : 'main';
6171
const proc = Bun.spawn(['git', 'fetch', 'origin', mainBranch, '--depth=1'], {
6272
cwd: details.location,
6373
});
6474
await proc.exited;
75+
76+
// if the commit sha has not changed, we do not need to reset
77+
const newSha = await getSHA(details, `origin/${mainBranch}`);
78+
79+
if (currentSha === newSha) {
80+
log(`${chalk.green(details.repoPath)} is already up to date`);
81+
return false;
82+
} else {
83+
log(`Resetting ${chalk.green(details.repoPath)} from ${chalk.red(currentSha)} to ${chalk.green(newSha)}`);
84+
}
85+
6586
const proc2 = Bun.spawn(['git', 'reset', '--hard', `origin/${mainBranch}`], {
6687
cwd: details.location,
6788
});
6889
await proc2.exited;
90+
91+
return true;
6992
}
7093

7194
/**
@@ -91,12 +114,12 @@ async function main() {
91114
await getOrUpdateRepo(EMBER_JSONAPI_DOCS_REPO);
92115
await getOrUpdateRepo(EMBER_API_DOCS_REPO);
93116

94-
// symlink our own project root into projects as 'ember-data'
117+
// symlink our own project root into projects as 'data'
95118
/////////////////////////////////////////////////////////////
96119
//
97-
const emberDataLocation = path.join(projectRoot, 'ember-data');
120+
const emberDataLocation = path.join(projectRoot, 'data');
98121
if (!fs.existsSync(emberDataLocation)) {
99-
log(`Symlinking ${chalk.green('ember-data')} to ${chalk.green('./projects/ember-data')}`);
122+
log(`Symlinking ${chalk.green('ember-data')} to ${chalk.green('./projects/data')}`);
100123
fs.symlinkSync(workspaceRoot, emberDataLocation);
101124
}
102125

docs-viewer/src/rebuild-docs.ts

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

7-
import { generateDocs } from './-utils';
7+
import { generateDocs } from './-utils.ts';
88

99
async function main() {
1010
await generateDocs();

0 commit comments

Comments
 (0)