Skip to content

Commit 875eb61

Browse files
committed
Add a manifest auto command which tries to zero config generate a manifest
1 parent cabccdf commit 875eb61

File tree

4 files changed

+116
-13
lines changed

4 files changed

+116
-13
lines changed

src/commands/manifest/auto.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import fs from 'node:fs'
2+
3+
import meow from 'meow'
4+
5+
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
6+
import { scala } from './scala.ts'
7+
8+
const description =
9+
'Auto-detect build and attempt to generate manifest file'
10+
11+
const help = (name: string) => `
12+
Usage
13+
$ ${name}
14+
15+
Tries to figure out what language your current repo uses. If it finds a
16+
supported case then it will try to generate the manifest file for that
17+
language with the default or detected settings.
18+
19+
This command takes no arguments except --verbose.
20+
`
21+
22+
export const auto: CliSubcommand = {
23+
description,
24+
async run(argv, importMeta, { parentName }) {
25+
// Allow `--verbose` to pass through
26+
let verbose = false;
27+
const args = argv.filter(arg => {
28+
if (arg === '--verbose') {
29+
verbose = true;
30+
return false;
31+
}
32+
return true;
33+
});
34+
35+
const name = `${parentName} auto`
36+
if (args.length) {
37+
// note: meow will exit if it prints the --help screen
38+
meow(help(name), {
39+
argv: ['--help'],
40+
description,
41+
importMeta
42+
})
43+
}
44+
45+
const subArgs = [];
46+
if (verbose) subArgs.push('--verbose', '1');
47+
const scalaDir = '.';
48+
if (fs.existsSync(scalaDir)) {
49+
console.log('Detected a Scala sbt build, running default Scala generator...')
50+
subArgs.push(scalaDir)
51+
await scala.run(subArgs, importMeta, {parentName})
52+
return;
53+
}
54+
55+
// Show new help screen and exit
56+
meow(`
57+
$ ${name}
58+
59+
Unfortunately this script did not discover a supported language in the
60+
current folder.
61+
62+
- Make sure this script would work with your target build
63+
- Make sure to run it from the correct folder
64+
- Make sure the necessary build tools are available (\`PATH\`)
65+
66+
If that doesn't work, see \`${name} <lang> --help\` for config details
67+
`, {
68+
argv: ['--help'],
69+
description,
70+
importMeta
71+
})
72+
}
73+
}
74+

src/commands/manifest/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { scala } from './scala'
44
import { meowWithSubcommands } from '../../utils/meow-with-subcommands'
55

66
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
7+
import { auto } from './auto.ts'
78

89
const description =
9-
'Generate a "Software Bill of Materials" for given file or dir'
10+
'Generate a dependency manifest for given file or dir'
1011
const help = (name: string) => `
1112
Usage
1213
@@ -25,10 +26,15 @@ const help = (name: string) => `
2526
Examples
2627
2728
$ ${name} scala .
29+
30+
To have it auto-detect and attempt to run:
31+
32+
$ ${name} yolo
2833
`
2934

3035
export const manifest: CliSubcommand = {
3136
description,
37+
hidden: true,
3238
async run(argv, importMeta, { parentName }) {
3339
const name = `${parentName} manifest`
3440

@@ -44,10 +50,18 @@ export const manifest: CliSubcommand = {
4450

4551
await meowWithSubcommands(
4652
{
47-
scala
53+
scala,
54+
auto
4855
},
4956
{
5057
argv,
58+
aliases: {
59+
yolo: {
60+
description: auto.description,
61+
hidden: true,
62+
argv: ['auto']
63+
}
64+
},
5165
description,
5266
importMeta,
5367
name

src/commands/manifest/scala.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type ListDescription =
1919
const renamep = util.promisify(fs.rename)
2020

2121
const description =
22-
'Generate a "Software Bill of Materials" (`pom.xml`) from Scala\'s `build.sbt` file'
22+
'Generate a manifest file (`pom.xml`) from Scala\'s `build.sbt` file'
2323

2424
const scalaCmdFlags: Record<string, ListDescription> = {
2525
bin: {
@@ -84,6 +84,7 @@ const help = (name: string, flags: Record<string, ListDescription>) => `
8484
export const scala: CliSubcommand = {
8585
description,
8686
async run(argv, importMeta, { parentName }) {
87+
// console.log('scala', argv, parentName)
8788
const name = `${parentName} scala`
8889
// note: meow will exit if it prints the --help screen
8990
const cli = meow(help(name, scalaCmdFlags), {
@@ -92,6 +93,10 @@ export const scala: CliSubcommand = {
9293
importMeta
9394
})
9495

96+
if (cli.flags['verbose']) {
97+
console.log('[VERBOSE] cli.flags:', cli.flags, ', cli.input:', cli.input)
98+
}
99+
95100
const target = cli.input[0]
96101

97102
if (!target) {
@@ -158,19 +163,22 @@ async function startConversion(
158163
verbose: boolean,
159164
sbtOpts: Array<string>
160165
) {
161-
const spinner = new Spinner()
162-
163166
const rbin = path.resolve(bin)
164167
const rtarget = path.resolve(target)
165168
const rout = out === '-' ? '-' : path.resolve(out)
166169

167170
if (verbose) {
168-
spinner.clear()
169-
console.log(`- Absolute bin path: \`${rbin}\``)
170-
console.log(`- Absolute target path: \`${rtarget}\``)
171-
console.log(`- Absolute out path: \`${rout}\``)
171+
console.log(`[VERBOSE] - Absolute bin path: \`${rbin}\``)
172+
console.log(`[VERBOSE] - Absolute target path: \`${rtarget}\``)
173+
console.log(`[VERBOSE] - Absolute out path: \`${rout}\``)
174+
} else {
175+
console.log(`- executing: \`${bin}\``);
176+
console.log(`- src dir: \`${target}\``)
177+
console.log(`- dst dir: \`${out}\``)
172178
}
173179

180+
const spinner = new Spinner()
181+
174182
spinner.start(`Running sbt from \`${bin}\` on \`${target}\`...`)
175183

176184
try {
@@ -182,15 +190,19 @@ async function startConversion(
182190
})
183191
spinner.success()
184192
if (verbose) {
185-
console.group('sbt stdout:')
193+
console.group('[VERBOSE] sbt stdout:')
186194
console.log(output)
187195
console.groupEnd()
188196
}
189197

190198
if (output.stderr) {
191199
spinner.error('There were errors while running sbt')
192200
// (In verbose mode, stderr was printed above, no need to repeat it)
193-
if (!verbose) console.error(output.stderr)
201+
if (!verbose) {
202+
console.group('[VERBOSE] stderr:');
203+
console.error(output.stderr)
204+
console.groupEnd();
205+
}
194206
process.exit(1)
195207
}
196208

@@ -222,9 +234,11 @@ async function startConversion(
222234
spinner.start().success(`OK. File should be available in \`${out}\``)
223235
}
224236
} catch (e) {
225-
spinner.error('There was an unexpected error while running this')
237+
spinner.error('There was an unexpected error while running this' + (verbose ? '' : ' (use --verbose for details)'))
226238
if (verbose) {
239+
console.group('[VERBOSE] error:');
227240
console.log(e)
241+
console.groupEnd();
228242
}
229243
process.exit(1)
230244
}

src/utils/meow-with-subcommands.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { Options } from 'meow'
99

1010
interface CliAlias {
1111
description: string
12+
hidden?: boolean
1213
argv: readonly string[]
1314
}
1415

@@ -81,7 +82,7 @@ export async function meowWithSubcommands(
8182
...toSortedObject(
8283
Object.fromEntries(
8384
Object.entries(aliases).filter(
84-
entry => !subcommands[entry[1]?.argv[0]!]?.hidden
85+
entry => !entry[1]?.hidden && !subcommands[entry[1]?.argv[0]!]?.hidden
8586
)
8687
)
8788
)

0 commit comments

Comments
 (0)