Skip to content

Commit 9922174

Browse files
committed
Improve bulk repository handling
1 parent 4d9a16e commit 9922174

File tree

9 files changed

+650
-151
lines changed

9 files changed

+650
-151
lines changed

commands/lint.ts

Lines changed: 83 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -94,59 +94,33 @@ export async function run(options: CommandFixOptions, positionals: string[]) {
9494
}
9595

9696
// Collect rule files that match the ecosystem.
97-
const ecosystems: string[] = []
98-
{
99-
if (await fileExists('package.json')) {
100-
await collect(`400-ecosystem/nodejs/*`)
101-
ecosystems.push('nodejs')
102-
103-
const content: PackageJson = JSON.parse(
104-
await fs.readFile('package.json', 'utf-8'),
105-
)
106-
if (content.displayName) {
97+
const ecosystems = await getEcosystems(Deno.cwd()) // TODO
98+
for (const ecosystem of ecosystems) {
99+
switch (ecosystem) {
100+
case 'nodejs':
101+
await collect(`400-ecosystem/nodejs/*`)
102+
break
103+
case 'vscode-extension':
107104
await collect(`400-ecosystem/vscode-extension/*`)
108-
ecosystems.push('vscode-extension')
109-
}
110-
}
111-
112-
if ((await fileExists('deno.jsonc')) || (await fileExists('deno.json'))) {
113-
await collect(`400-ecosystem/deno/*`)
114-
ecosystems.push('deno')
115-
}
116-
117-
if ((await globby('*.c')).length > 0) {
118-
await collect(`400-ecosystem/c/*`)
119-
ecosystems.push('c')
120-
}
121-
122-
// https://cmake.org/cmake/help/latest/command/project.html
123-
if (await fileExists('CMakeLists.txt')) {
124-
const content = await fs.readFile('CMakeLists.txt', 'utf-8')
125-
const language: { groups?: { lang?: string } } = content.match(
126-
/project\((?:.*? (?<lang>[a-zA-Z]+)\)|.*?LANGUAGES[ \t]+(?<lang>[a-zA-Z]+))/,
127-
) as any
128-
if (language.groups?.lang === 'C') {
105+
break
106+
case 'deno':
107+
await collect(`400-ecosystem/deno/*`)
108+
break
109+
case 'c':
129110
await collect(`400-ecosystem/c/*`)
130-
ecosystems.push('c')
131-
} else if (language.groups?.lang === 'CXX') {
111+
break
112+
case 'cpp':
132113
await collect(`400-ecosystem/cpp/*`)
133-
ecosystems.push('cpp')
134-
}
135-
}
136-
137-
if (await fileExists('basalt.toml')) {
138-
await collect(`400-ecosystem/bash/*`)
139-
ecosystems.push('bash')
140-
}
141-
142-
// https://zed.dev/docs/extensions/developing-extensions
143-
if (await fileExists('extension.toml')) {
144-
await collect(`400-ecosystem/zed-extension/*`)
145-
ecosystems.push('zed-extension')
114+
break
115+
case 'bash':
116+
await collect(`400-ecosystem/bash/*`)
117+
break
118+
case 'zed-extension':
119+
await collect(`400-ecosystem/zed-extension/*`)
120+
break
146121
}
147-
148-
await collect(`400-ecosystem/_/*`)
149122
}
123+
await collect(`400-ecosystem/_/*`)
150124

151125
// Collect rule files that match the name.
152126
{
@@ -177,7 +151,9 @@ export async function run(options: CommandFixOptions, positionals: string[]) {
177151
let str = ''
178152
str += `${styleText(['blue', 'bold'], 'Directory:')} ${project.rootDir}\n`
179153
str += `${styleText(['blue', 'bold'], 'Ecosystems:')} ${
180-
new Intl.ListFormat().format(ecosystems)
154+
new Intl.ListFormat().format(
155+
ecosystems,
156+
)
181157
}\n`
182158
if (project.type === 'with-remote-url') {
183159
str += `${styleText(['blue', 'bold'], 'Project:')} ${
@@ -375,6 +351,64 @@ async function getProject(): Promise<Project> {
375351
}
376352
}
377353

354+
export async function getEcosystems(rootDir: string): Promise<string[]> {
355+
using _ = ((origDir: string) => ({
356+
[Symbol.dispose]: () => Deno.chdir(origDir),
357+
}))(Deno.cwd())
358+
Deno.chdir(rootDir)
359+
360+
const ecosystems: string[] = []
361+
362+
if (await fileExists('package.json')) {
363+
ecosystems.push('nodejs')
364+
365+
const content: PackageJson = JSON.parse(
366+
await fs.readFile('package.json', 'utf-8'),
367+
)
368+
if (content.displayName) {
369+
ecosystems.push('vscode-extension')
370+
}
371+
}
372+
373+
if ((await fileExists('deno.jsonc')) || (await fileExists('deno.json'))) {
374+
ecosystems.push('deno')
375+
}
376+
377+
if ((await globby('*.c')).length > 0) {
378+
ecosystems.push('c')
379+
}
380+
381+
// https://cmake.org/cmake/help/latest/command/project.html
382+
if (await fileExists('CMakeLists.txt')) {
383+
const content = await fs.readFile('CMakeLists.txt', 'utf-8')
384+
const language = content.match(
385+
/project\((?:.*? (?<lang>[a-zA-Z]+)\)|.*?LANGUAGES[ \t]+(?<lang>[a-zA-Z]+))/,
386+
)
387+
if (language?.groups?.lang === 'C') {
388+
ecosystems.push('c')
389+
} else if (language?.groups?.lang === 'CXX') {
390+
ecosystems.push('cpp')
391+
} else {
392+
// TODO
393+
console.error(
394+
`CMAkeLists.txt should have language defined in project()`,
395+
)
396+
process.exit(1)
397+
}
398+
}
399+
400+
if (await fileExists('basalt.toml')) {
401+
ecosystems.push('bash')
402+
}
403+
404+
// https://zed.dev/docs/extensions/developing-extensions
405+
if (await fileExists('extension.toml')) {
406+
ecosystems.push('zed-extension')
407+
}
408+
409+
return ecosystems
410+
}
411+
378412
function printWithTips(str: string, tips: string[]) {
379413
console.info(str)
380414
for (const tip of tips) {

commands/new.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,29 @@ import * as fs from 'node:fs/promises'
22
import * as path from 'node:path'
33
import * as util from 'node:util'
44
import { existsSync } from 'node:fs'
5-
import enquirer from 'enquirer'
5+
66
import * as ejs from 'ejs'
77
import { globby } from 'globby'
88
import { execa } from 'execa'
99
import Handlebars from 'handlebars'
1010
import { fileExists } from '#common'
11+
import * as inquirer from '@inquirer/prompts'
1112

1213
import type { CommandNewOptions } from '#types'
1314

14-
const { prompt } = enquirer
15-
1615
export async function run(values: CommandNewOptions, positionals: string[]) {
1716
if (!values.ecosystem) {
18-
const input = (await prompt({
19-
type: 'select',
20-
name: 'value',
17+
const input = await inquirer.select({
2118
message: 'Choose an ecosystem',
2219
choices: [
23-
{ message: 'Deno', name: 'deno' },
24-
{ message: 'NodeJS', name: 'nodejs' },
25-
{ message: 'Rust', name: 'rust' },
26-
{ message: 'Go', name: 'go' },
27-
{ message: 'C++', name: 'cpp' },
20+
{ name: 'Deno', value: 'deno' },
21+
{ name: 'NodeJS', value: 'nodejs' },
22+
{ name: 'Rust', value: 'rust' },
23+
{ name: 'Go', value: 'go' },
24+
{ name: 'C++', value: 'cpp' },
2825
],
29-
})) as { value: string }
30-
values.ecosystem = input.value
26+
})
27+
values.ecosystem = input
3128
}
3229

3330
if (!values.templateName) {
@@ -37,25 +34,21 @@ export async function run(values: CommandNewOptions, positionals: string[]) {
3734
throw new Error(`Ecosystem "${values.ecosystem}" not supported`)
3835
}
3936

40-
const { value } = (await prompt({
41-
type: 'select',
42-
name: 'value',
37+
const value = await inquirer.select({
4338
message: `Choose a "${values.ecosystem}" template`,
4439
choices: Object.entries(parameters).map(([id, { name }]) => ({
45-
name: id,
46-
message: name,
40+
name,
41+
value: id,
4742
})),
48-
})) as { value: string }
43+
})
4944

5045
values.templateName = value
5146
}
5247

5348
if (!values.projectName) {
54-
const { value } = (await prompt({
55-
type: 'input',
56-
name: 'value',
49+
const value = await inquirer.input({
5750
message: 'What is the project name?',
58-
})) as { value: string }
51+
})
5952

6053
values.projectName = value
6154
}

0 commit comments

Comments
 (0)