| 16 | } |
| 17 | |
| 18 | async function writeChangeset( |
| 19 | changeset: Changeset, |
| 20 | rootDir: string, |
| 21 | options?: { prettier?: boolean } |
| 22 | ): Promise<string> { |
| 23 | const { summary, releases } = changeset; |
| 24 | |
| 25 | const changesetBase = path.resolve(rootDir, ".changeset"); |
| 26 | |
| 27 | // Worth understanding that the ID merely needs to be a unique hash to avoid git conflicts |
| 28 | // experimenting with human readable ids to make finding changesets easier |
| 29 | const changesetID = humanId({ |
| 30 | separator: "-", |
| 31 | capitalize: false, |
| 32 | }); |
| 33 | |
| 34 | const prettierInstance = |
| 35 | options?.prettier !== false ? getPrettierInstance(rootDir) : undefined; |
| 36 | const newChangesetPath = path.resolve(changesetBase, `${changesetID}.md`); |
| 37 | |
| 38 | // NOTE: The quotation marks in here are really important even though they are |
| 39 | // not spec for yaml. This is because package names can contain special |
| 40 | // characters that will otherwise break the parsing step |
| 41 | const changesetContents = `--- |
| 42 | ${releases.map((release) => `"${release.name}": ${release.type}`).join("\n")} |
| 43 | --- |
| 44 | |
| 45 | ${summary} |
| 46 | `; |
| 47 | |
| 48 | await fs.outputFile( |
| 49 | newChangesetPath, |
| 50 | prettierInstance |
| 51 | ? // Prettier v3 returns a promise |
| 52 | await prettierInstance.format(changesetContents, { |
| 53 | ...(await prettierInstance.resolveConfig(newChangesetPath)), |
| 54 | parser: "markdown", |
| 55 | }) |
| 56 | : changesetContents |
| 57 | ); |
| 58 | |
| 59 | return changesetID; |
| 60 | } |
| 61 | |
| 62 | export default writeChangeset; |