Skip to content

Commit c498e8b

Browse files
committed
added init and analyze-repo commands
1 parent 904b860 commit c498e8b

File tree

7 files changed

+341
-29
lines changed

7 files changed

+341
-29
lines changed

package-lock.json

Lines changed: 182 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"homepage": "https://github.com/stackbit/stackbit-cli#readme",
3636
"dependencies": {
37-
"@stackbit/sdk": "^0.1.2",
37+
"@stackbit/sdk": "^0.1.4",
3838
"chalk": "^4.1.0",
3939
"lodash": "^4.17.21",
4040
"yargs": "^16.2.0"

src/analyze-repo.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { FileBrowser, GitHubFileBrowserAdapter, matchCMS, matchSSG } from '@stackbit/sdk';
2+
import { printCMSMatchResult, printSSGMatchResult } from './utils';
3+
4+
export interface AnalyzeRepoOptions {
5+
repoUrl: string;
6+
branch: string;
7+
auth?: string;
8+
}
9+
10+
export async function analyzeRepo({ repoUrl, branch, auth }: AnalyzeRepoOptions) {
11+
const parsedUrl = parseGitHubUrl(repoUrl);
12+
if (!parsedUrl) {
13+
console.log(`could not parse repository url: ${repoUrl}`);
14+
return;
15+
}
16+
17+
console.log('Analyzing repository files in ...');
18+
const fileBrowserAdapter = new GitHubFileBrowserAdapter({
19+
owner: parsedUrl.owner,
20+
repo: parsedUrl.repo,
21+
branch: branch,
22+
auth: auth
23+
});
24+
const fileBrowser = new FileBrowser({ fileBrowserAdapter });
25+
const ssgMatchResult = await matchSSG({ fileBrowser });
26+
printSSGMatchResult(ssgMatchResult);
27+
28+
if (!ssgMatchResult) {
29+
return;
30+
}
31+
const cmsMatchResult = await matchCMS({ fileBrowser });
32+
printCMSMatchResult(cmsMatchResult);
33+
}
34+
35+
function parseGitHubUrl(repoUrl: string) {
36+
const match = repoUrl.match(/github\.com[/:](.+?)\/(.+?)(\.git)?$/);
37+
if (!match) {
38+
return null;
39+
}
40+
const owner = match[1]!;
41+
const repo = match[2]!;
42+
return { owner, repo };
43+
}

src/init.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { matchSSG, FileSystemFileBrowserAdapter, FileBrowser, matchCMS, writeConfig } from '@stackbit/sdk';
2+
import { printCMSMatchResult, printSSGMatchResult } from './utils';
3+
import path from 'path';
4+
import chalk from 'chalk';
5+
import _ from 'lodash';
6+
7+
export async function init({ inputDir, dryRun }: { inputDir: string; dryRun: boolean }) {
8+
console.log(`Analyzing files in ${chalk.blueBright(path.resolve(inputDir))} ...`);
9+
const fileBrowserAdapter = new FileSystemFileBrowserAdapter({ dirPath: inputDir });
10+
const fileBrowser = new FileBrowser({ fileBrowserAdapter });
11+
const ssgMatchResult = await matchSSG({ fileBrowser });
12+
printSSGMatchResult(ssgMatchResult);
13+
14+
if (!ssgMatchResult) {
15+
return;
16+
}
17+
18+
const cmsMatchResult = await matchCMS({ fileBrowser });
19+
printCMSMatchResult(cmsMatchResult);
20+
21+
const stackbitYaml = _.omitBy(
22+
{
23+
stackbitVersion: '~0.3.0',
24+
ssgName: ssgMatchResult.ssgName,
25+
cmsName: cmsMatchResult?.cmsName
26+
},
27+
_.isNil
28+
);
29+
console.log(`\nstackbit.yaml: ${JSON.stringify(stackbitYaml, null, 2)}`);
30+
}

0 commit comments

Comments
 (0)