Skip to content

Commit 588e956

Browse files
committed
feat: add init command
1 parent 67d5a10 commit 588e956

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/cli.ts

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { URL } from 'url';
66

77
import newCmd from '#commands/new';
88
import generateCmd from '#commands/generate';
9+
import initCmd from '#commands/init';
910

1011
const sapphire = new Command();
1112

@@ -30,4 +31,6 @@ sapphire
3031
.argument('<name>', 'file name')
3132
.action(generateCmd);
3233

34+
sapphire.command('init').description('creates a config file on an existing Sapphire project').alias('i').action(initCmd);
35+
3336
sapphire.parse(process.argv);

src/commands/init.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { PromptInit } from '#prompts';
2+
import prompts from 'prompts';
3+
import YAML from 'yaml';
4+
import { writeFile } from 'fs/promises';
5+
import FindUp from 'find-up';
6+
import chalk from 'chalk';
7+
8+
const { red } = chalk;
9+
10+
export default async () => {
11+
const packageJson = await FindUp('package.json');
12+
if (!packageJson) {
13+
console.log(red("Can't find package.json"));
14+
process.exit(1);
15+
}
16+
17+
const response = await prompts(PromptInit);
18+
if (!response.preconditions) return process.exit(1);
19+
20+
const config = {
21+
projectLanguage: response.projectLanguage,
22+
locations: {
23+
base: response.base,
24+
arguments: response.arguments,
25+
commands: response.commands,
26+
listeners: response.listeners,
27+
preconditions: response.preconditions
28+
},
29+
customFileTemplates: {
30+
enabled: response.cftEnabled,
31+
location: response.cftLocation ?? ''
32+
}
33+
};
34+
35+
const file = response.configFormat === 'json' ? JSON.stringify(config, null, 2) : YAML.stringify(config);
36+
return writeFile(`.sapphirerc.${response.configFormat}`, file);
37+
};

0 commit comments

Comments
 (0)