Skip to content

Commit 6287304

Browse files
emmatownNoviny
authored andcommitted
Make init write the default config file if it doesn't exist with a special message if an old config file exists (#153)
* Make init write the default config file if it doesn't exist with a special message if an old config file exists * Don't log "you already initialized stuff" message if the config doesn't exist * Test things
1 parent d2529c5 commit 6287304

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

.changeset/real-carrots-run.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/cli": patch
3+
---
4+
5+
Make init write the default config file if it doesn't exist with a special message if an old config file exists

packages/cli/src/commands/init/__tests__/command.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { copyFixtureIntoTempDir } from "jest-fixtures";
22
import fs from "fs-extra";
33
import path from "path";
4+
import { defaultWrittenConfig } from "@changesets/config";
45

56
import initializeCommand from "..";
67

@@ -25,16 +26,32 @@ describe("init", () => {
2526
expect(fs.pathExistsSync(readmePath)).toBe(true);
2627
expect(fs.pathExistsSync(configPath)).toBe(true);
2728
});
28-
it("should fail in a project with a .changeset folder", async () => {
29+
it("should write the default config if it doesn't exist", async () => {
2930
const cwd = await copyFixtureIntoTempDir(__dirname, "simple-project");
3031
await fs.remove(path.join(cwd, ".changeset/config.json"));
3132

3233
expect(fs.pathExistsSync(path.join(cwd, ".changeset/README.md"))).toBe(
3334
true
3435
);
3536
await initializeCommand(cwd);
36-
expect(fs.pathExistsSync(path.join(cwd, ".changeset/config.json"))).toBe(
37-
false
37+
expect(await fs.readJson(path.join(cwd, ".changeset/config.json"))).toEqual(
38+
defaultWrittenConfig
39+
);
40+
});
41+
it("shouldn't overwrite a config if it does exist", async () => {
42+
const cwd = await copyFixtureIntoTempDir(__dirname, "simple-project");
43+
await fs.writeJson(path.join(cwd, ".changeset/config.json"), {
44+
changelog: false
45+
});
46+
47+
expect(fs.pathExistsSync(path.join(cwd, ".changeset/README.md"))).toBe(
48+
true
49+
);
50+
await initializeCommand(cwd);
51+
expect(await fs.readJson(path.join(cwd, ".changeset/config.json"))).toEqual(
52+
{
53+
changelog: false
54+
}
3855
);
3956
});
4057
});

packages/cli/src/commands/init/index.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,35 @@ export default async function init(cwd: string) {
1212
const changesetBase = await getChangesetBase(cwd);
1313

1414
if (fs.existsSync(changesetBase)) {
15-
logger.warn(
16-
"It looks like you already have changesets initialized. You should be able to run changeset commands no problems."
17-
);
15+
if (!fs.existsSync(path.join(changesetBase, "config.json"))) {
16+
if (fs.existsSync(path.join(changesetBase, "config.js"))) {
17+
logger.error(
18+
"It looks like you're using the version 1 `.changeset/config.js` file"
19+
);
20+
logger.error(
21+
"The format of the config object has significantly changed in v2 as well"
22+
);
23+
logger.error(
24+
" - we thoroughly recommend looking at the changelog for this package for what has changed"
25+
);
26+
logger.error(
27+
"Changesets will write the defaults for the new config, remember to transfer your options into the new config at `.changeset/config.json`"
28+
);
29+
} else {
30+
logger.error("It looks like you don't have a config file");
31+
logger.error(
32+
"The default config file will be written at `.changeset/config.json`"
33+
);
34+
}
35+
await fs.writeFile(
36+
path.resolve(changesetBase, "config.json"),
37+
JSON.stringify(defaultWrittenConfig, null, 2)
38+
);
39+
} else {
40+
logger.warn(
41+
"It looks like you already have changesets initialized. You should be able to run changeset commands no problems."
42+
);
43+
}
1844
} else {
1945
await fs.copy(path.resolve(pkgPath, "./default-files"), changesetBase);
2046
await fs.writeFile(

0 commit comments

Comments
 (0)