Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ const STARTER = {
endpoints: [],
};

function printNextSteps() {
console.log("");
console.log(" Next steps:");
console.log(` 1. Create a ${chalk.cyan(".env")} file in this directory:`);
console.log(` ${chalk.gray("STAGING_TOKEN=your_token_here")}`);
console.log(` ${chalk.gray("PROD_TOKEN=your_token_here")}`);
console.log(
` 2. Add ${chalk.cyan(".env")} to your ${chalk.cyan(".gitignore")}`,
);
console.log(
` 3. Run: ${chalk.cyan("apidrift snapshot --tag v1.0 --env staging")}`,
);
console.log("");
}

export async function runInit() {
const dest = path.resolve(process.cwd(), "apidrift.config.json");

Expand All @@ -28,6 +43,8 @@ export async function runInit() {
initial: false,
});
if (!overwrite) {
console.log(chalk.yellow("Keeping existing apidrift.config.json"));
printNextSteps();
return;
}
}
Expand Down Expand Up @@ -79,16 +96,5 @@ export async function runInit() {

fs.writeFileSync(dest, JSON.stringify(config, null, 2));
console.log(chalk.green("✓ Created apidrift.config.json"));
console.log("");
console.log(" Next steps:");
console.log(` 1. Create a ${chalk.cyan(".env")} file in this directory:`);
console.log(` ${chalk.gray("STAGING_TOKEN=your_token_here")}`);
console.log(` ${chalk.gray("PROD_TOKEN=your_token_here")}`);
console.log(
` 2. Add ${chalk.cyan(".env")} to your ${chalk.cyan(".gitignore")}`,
);
console.log(
` 3. Run: ${chalk.cyan("apidrift snapshot --tag v1.0 --env staging")}`,
);
console.log("");
printNextSteps();
}
48 changes: 48 additions & 0 deletions tests/commands/init.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import fs from "fs";
import os from "os";
import path from "path";

const promptsMock = jest.fn();

jest.unstable_mockModule("prompts", () => ({
default: promptsMock,
}));

describe("runInit", () => {
let originalCwd;
let tempDir;
let logs;

beforeEach(() => {
jest.resetModules();
promptsMock.mockReset();
originalCwd = process.cwd();
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "apidrift-init-"));
process.chdir(tempDir);
logs = [];
jest.spyOn(console, "log").mockImplementation((message = "") => {
logs.push(String(message));
});
});

afterEach(() => {
console.log.mockRestore();
process.chdir(originalCwd);
fs.rmSync(tempDir, { recursive: true, force: true });
});

test("prints next steps without overwriting existing config when declined", async () => {
const configPath = path.join(tempDir, "apidrift.config.json");
const originalConfig = '{ "keep": true }\n';
fs.writeFileSync(configPath, originalConfig);
promptsMock.mockResolvedValueOnce({ overwrite: false });

const { runInit } = await import("../../src/commands/init.js");
await runInit();

expect(fs.readFileSync(configPath, "utf-8")).toBe(originalConfig);
expect(logs.join("\n")).toContain("Keeping existing apidrift.config.json");
expect(logs.join("\n")).toContain("STAGING_TOKEN=your_token_here");
expect(logs.join("\n")).toContain("apidrift snapshot --tag v1.0 --env staging");
});
});