|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import prompts from "prompts"; |
| 4 | +import z from "zod"; |
| 5 | +import { |
| 6 | + createConfig, |
| 7 | + getConfigFromWorkDir, |
| 8 | + localConfigSchema, |
| 9 | +} from "../../config/localConfig"; |
| 10 | +import yargs from "yargs"; |
| 11 | +import { globalOptions } from "../helpers/options"; |
| 12 | +import { TelemetryEvents, trackEvent } from "../../telemetry"; |
| 13 | + |
| 14 | +async function promptForEntryPointPath(currentPath: string) { |
| 15 | + // Read the current directory's contents |
| 16 | + const items = fs.readdirSync(currentPath).map((item) => { |
| 17 | + const fullPath = path.join(currentPath, item); |
| 18 | + const isDir = fs.statSync(fullPath).isDirectory(); |
| 19 | + return { title: item + (isDir ? "/" : ""), value: fullPath, isDir }; |
| 20 | + }); |
| 21 | + |
| 22 | + // Add an option to go up a directory |
| 23 | + if (currentPath !== path.parse(currentPath).root) { |
| 24 | + items.unshift({ |
| 25 | + title: "../", |
| 26 | + value: path.join(currentPath, ".."), |
| 27 | + isDir: true, |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + // Ask user to select a file or directory |
| 32 | + const response = await prompts({ |
| 33 | + type: "select", |
| 34 | + name: "selectedPath", |
| 35 | + message: `Select the entrypoint file of your application\nNavigate through: ${currentPath}`, |
| 36 | + choices: items, |
| 37 | + }); |
| 38 | + |
| 39 | + // If the user selected a directory, navigate into it |
| 40 | + if ( |
| 41 | + response.selectedPath && |
| 42 | + fs.statSync(response.selectedPath).isDirectory() |
| 43 | + ) { |
| 44 | + return promptForEntryPointPath(response.selectedPath); |
| 45 | + } |
| 46 | + |
| 47 | + // Return the file path if a file is selected |
| 48 | + return response.selectedPath; |
| 49 | +} |
| 50 | + |
| 51 | +async function handler( |
| 52 | + argv: yargs.ArgumentsCamelCase< |
| 53 | + yargs.InferredOptionTypes<typeof globalOptions> |
| 54 | + >, |
| 55 | +) { |
| 56 | + const startTime = Date.now(); |
| 57 | + trackEvent(TelemetryEvents.INIT_COMMAND, { |
| 58 | + message: "Init command started", |
| 59 | + }); |
| 60 | + try { |
| 61 | + if (getConfigFromWorkDir(argv.workdir)) { |
| 62 | + const response = await prompts({ |
| 63 | + type: "confirm", |
| 64 | + name: "confirm", |
| 65 | + message: `A .napirc file already exists in the selected directory. Do you want to overwrite it?`, |
| 66 | + initial: false, |
| 67 | + }); |
| 68 | + if (!response.confirm) { |
| 69 | + return; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + const absoluteFilePath = await promptForEntryPointPath(argv.workdir); |
| 74 | + const relativeFilePath = path.relative(argv.workdir, absoluteFilePath); |
| 75 | + |
| 76 | + const napiConfig: z.infer<typeof localConfigSchema> = { |
| 77 | + entrypoint: relativeFilePath, |
| 78 | + out: "napi_dist", |
| 79 | + }; |
| 80 | + |
| 81 | + createConfig(napiConfig, argv.workdir); |
| 82 | + |
| 83 | + console.info("Successfully created .napirc"); |
| 84 | + trackEvent(TelemetryEvents.INIT_COMMAND, { |
| 85 | + message: "Init command finished", |
| 86 | + duration: Date.now() - startTime, |
| 87 | + }); |
| 88 | + } catch (error) { |
| 89 | + trackEvent(TelemetryEvents.INIT_COMMAND, { |
| 90 | + message: "Init command error", |
| 91 | + duration: Date.now() - startTime, |
| 92 | + error: error, |
| 93 | + }); |
| 94 | + throw error; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export default { |
| 99 | + command: "init", |
| 100 | + describe: "initialize a NanoAPI project", |
| 101 | + builder: {}, |
| 102 | + handler, |
| 103 | +}; |
0 commit comments