Skip to content

Commit b515ee3

Browse files
authored
Merge pull request #24 from hanabi-rest/fix/fix-build-error
fix:
2 parents 66296c5 + b33a5f3 commit b515ee3

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

src/index.ts

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,69 +34,56 @@ const onPromptState = (state: {
3434
let projectPath = "";
3535

3636
async function main() {
37-
3837
const program = new Command()
3938
.name(packageJson.name)
4039
.version(packageJson.version, "-v, --version", "display the version number")
4140
.usage(`${chalk.green("create <id>")} [options]`);
4241

43-
4442
program
4543
.command("create <id>")
4644
.description("Create a new project based on the specified version id")
4745
.option("--dir <project-directory>", "directory name")
48-
.option(
49-
"--skip-code-package",
50-
"Skip installation of npm packages imported in the code"
51-
)
46+
.option("--skip-code-package", "Skip installation of npm packages imported in the code")
5247
.option("--main-only", "Dumps API code only.")
5348
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
5449
.action(async (versionId, options) => {
5550
const online = await getOnline();
5651

5752
if (!online) {
58-
console.error(
59-
"You appear to be offline. Please check your internet connection and try again."
60-
);
53+
console.error("You appear to be offline. Please check your internet connection and try again.");
6154
process.exit(1);
6255
}
6356

6457
const config = readConfig();
6558

6659
if (!config?.api_token) {
67-
console.info(chalk.yellow(`Warning: To access a private application, set the token with ${chalk.bold("@hanabi.rest/cli config set")}`));
60+
console.info(
61+
chalk.yellow(`Warning: To access a private application, set the token with ${chalk.bold("@hanabi.rest/cli config set")}`),
62+
);
6863
console.info();
6964
}
7065

7166
if (options.mainOnly) {
7267
const { source } = await getFiles(versionId);
68+
if (!source) {
69+
console.error("Failed to fetch files. Your API may be in the process of being generated or may have failed to generate.");
70+
process.exit(1);
71+
}
7372

7473
const fileExists = fs.existsSync(path.join(process.cwd(), "index.ts"));
7574

7675
if (fileExists) {
77-
console.info(
78-
"The index.ts file already exists. Please retry with a different name."
79-
);
76+
console.info("The index.ts file already exists. Please retry with a different name.");
8077
process.exit(1);
8178
}
8279

8380
fs.writeFileSync(path.join(process.cwd(), "index.ts"), source);
84-
console.info(
85-
`${chalk.green("Success!")} Created index.ts file in ${chalk.green(
86-
process.cwd()
87-
)}`
88-
);
81+
console.info(`${chalk.green("Success!")} Created index.ts file in ${chalk.green(process.cwd())}`);
8982

9083
return;
9184
}
9285

93-
const packageManager = options.useNpm
94-
? "npm"
95-
: options.usePnpm
96-
? "pnpm"
97-
: options.useYarn
98-
? "yarn"
99-
: getPkgManager();
86+
const packageManager = options.useNpm ? "npm" : options.usePnpm ? "pnpm" : options.useYarn ? "yarn" : getPkgManager();
10087

10188
projectPath = options.dir;
10289

@@ -108,9 +95,7 @@ async function main() {
10895
message: "What is your project named?",
10996
initial: "my-app",
11097
validate: (name) => {
111-
const validation = validateNpmName(
112-
path.basename(path.resolve(name))
113-
);
98+
const validation = validateNpmName(path.basename(path.resolve(name)));
11499
if (validation.valid) {
115100
return true;
116101
}
@@ -128,11 +113,7 @@ async function main() {
128113
const validation = validateNpmName(projectName);
129114

130115
if (!validation.valid) {
131-
console.error(
132-
`Could not create a project called ${chalk.red(
133-
`"${projectName}"`
134-
)} because of npm naming restrictions:`
135-
);
116+
console.error(`Could not create a project called ${chalk.red(`"${projectName}"`)} because of npm naming restrictions:`);
136117

137118
for (const p of validation.problems) {
138119
console.error(`${chalk.red(chalk.bold("*"))} ${p}`);
@@ -144,9 +125,7 @@ async function main() {
144125
const folderExists = fs.existsSync(root);
145126

146127
if (folderExists) {
147-
console.info(
148-
"The directory already exists. Please try again with a different name."
149-
);
128+
console.info("The directory already exists. Please try again with a different name.");
150129
process.exit(1);
151130
}
152131

@@ -159,35 +138,34 @@ async function main() {
159138
})
160139
.allowUnknownOption();
161140

162-
const configCommand = program.command('config')
163-
.description('Configure the CLI tool');
141+
const configCommand = program.command("config").description("Configure the CLI tool");
164142

165143
configCommand
166-
.command('set')
167-
.description('Set a configuration option')
168-
.option('--api-key [key]', 'Set the access token for authentication')
144+
.command("set")
145+
.description("Set a configuration option")
146+
.option("--api-key [key]", "Set the access token for authentication")
169147
.action(async (options) => {
170148
let apiKey: string = options.apiKey;
171149

172150
if (!apiKey) {
173-
console.info(chalk.blue("To get your access token, visit: https://hanabi.rest/settings/tokens"))
174-
console.info()
151+
console.info(chalk.blue("To get your access token, visit: https://hanabi.rest/settings/tokens"));
152+
console.info();
175153

176154
const response = await prompts({
177-
type: 'password',
178-
name: 'key',
179-
message: 'Enter your Access token:',
155+
type: "password",
156+
name: "key",
157+
message: "Enter your Access token:",
180158
});
181159
apiKey = response.key;
182160
}
183161

184162
if (apiKey) {
185163
writeAuthConfigFile({ api_token: apiKey });
186164

187-
console.info()
188-
console.info(chalk.green('access token saved successfully.'));
165+
console.info();
166+
console.info(chalk.green("access token saved successfully."));
189167
} else {
190-
console.error(chalk.red('access token is required.'));
168+
console.error(chalk.red("access token is required."));
191169
}
192170
});
193171

0 commit comments

Comments
 (0)