Repository: atlassian/changesets
Pull request #153 (2019-09-13 13:20:18)

Make init write the default config file if it doesn't exist with a special message if an old config file exists

changeset-bot[bot]: ###  🦋  Changeset is good to go

Latest commit: f806efa86e0277bca6cdac994cde2250eccebd68

**We got this.**

Not sure what this means? [Click here  to learn what changesets are](https://github.com/Noviny/changesets/blob/master/docs/adding-a-changeset.md).

Patch:
diff --git a/.changeset/real-carrots-run.md b/.changeset/real-carrots-run.md
new file mode 100644
index 0000000..1082f3c
--- /dev/null
+++ b/.changeset/real-carrots-run.md
@@ -0,0 +1,5 @@
+---
+"@changesets/cli": patch
+---
+
+Make init write the default config file if it doesn't exist with a special message if an old config file exists
diff --git a/packages/cli/src/commands/init/index.ts b/packages/cli/src/commands/init/index.ts
index 137e5db..ebf30d4 100644
--- a/packages/cli/src/commands/init/index.ts
+++ b/packages/cli/src/commands/init/index.ts
@@ -12,9 +12,35 @@ export default async function init(cwd: string) {
   const changesetBase = await getChangesetBase(cwd);
 
   if (fs.existsSync(changesetBase)) {
-    logger.warn(
-      "It looks like you already have changesets initialized. You should be able to run changeset commands no problems."
-    );
+    if (!fs.existsSync(path.join(changesetBase, "config.json"))) {
+      if (fs.existsSync(path.join(changesetBase, "config.js"))) {
+        logger.error(
+          "It looks like you're using the version 1 `.changeset/config.js` file"
+        );
+        logger.error(
+          "The format of the config object has significantly changed in v2 as well"
+        );
+        logger.error(
+          " - we thoroughly recommend looking at the changelog for this package for what has changed"
+        );
+        logger.error(
+          "Changesets will write the defaults for the new config, remember to transfer your options into the new config at `.changeset/config.json`"
+        );
+      } else {
+        logger.error("It looks like you don't have a config file");
+        logger.error(
+          "The default config file will be written at `.changeset/config.json`"
+        );
+      }
+      await fs.writeFile(
+        path.resolve(changesetBase, "config.json"),
+        JSON.stringify(defaultWrittenConfig, null, 2)
+      );
+    } else {
+      logger.warn(
+        "It looks like you already have changesets initialized. You should be able to run changeset commands no problems."
+      );
+    }
   } else {
     await fs.copy(path.resolve(pkgPath, "./default-files"), changesetBase);
     await fs.writeFile(


Test patch:
diff --git a/packages/cli/src/commands/init/__tests__/command.ts b/packages/cli/src/commands/init/__tests__/command.ts
index 7a3465e..cb69faf 100644
--- a/packages/cli/src/commands/init/__tests__/command.ts
+++ b/packages/cli/src/commands/init/__tests__/command.ts
@@ -1,6 +1,7 @@
 import { copyFixtureIntoTempDir } from "jest-fixtures";
 import fs from "fs-extra";
 import path from "path";
+import { defaultWrittenConfig } from "@changesets/config";
 
 import initializeCommand from "..";
 
@@ -25,7 +26,7 @@ describe("init", () => {
     expect(fs.pathExistsSync(readmePath)).toBe(true);
     expect(fs.pathExistsSync(configPath)).toBe(true);
   });
-  it("should fail in a project with a .changeset folder", async () => {
+  it("should write the default config if it doesn't exist", async () => {
     const cwd = await copyFixtureIntoTempDir(__dirname, "simple-project");
     await fs.remove(path.join(cwd, ".changeset/config.json"));
 
@@ -33,8 +34,24 @@ describe("init", () => {
       true
     );
     await initializeCommand(cwd);
-    expect(fs.pathExistsSync(path.join(cwd, ".changeset/config.json"))).toBe(
-      false
+    expect(await fs.readJson(path.join(cwd, ".changeset/config.json"))).toEqual(
+      defaultWrittenConfig
+    );
+  });
+  it("shouldn't overwrite a config if it does exist", async () => {
+    const cwd = await copyFixtureIntoTempDir(__dirname, "simple-project");
+    await fs.writeJson(path.join(cwd, ".changeset/config.json"), {
+      changelog: false
+    });
+
+    expect(fs.pathExistsSync(path.join(cwd, ".changeset/README.md"))).toBe(
+      true
+    );
+    await initializeCommand(cwd);
+    expect(await fs.readJson(path.join(cwd, ".changeset/config.json"))).toEqual(
+      {
+        changelog: false
+      }
     );
   });
 });