diff --git a/src/change/write-main-changelog.ts b/src/change/write-main-changelog.ts index bffec38..13a8f79 100644 --- a/src/change/write-main-changelog.ts +++ b/src/change/write-main-changelog.ts @@ -1,13 +1,14 @@ -import { writeFileSync, existsSync, mkdirSync } from 'fs'; +import { existsSync, mkdirSync } from 'fs'; import type { Module } from '../types'; import { logger } from '../helpers/logger'; +import { writeFile } from '../helpers/write-file'; const { error, log } = logger('[change] write main changelog'); export const writeMainChangelog: Module = (env) => { const mainChangeLogDir = env.config!.changelogPath; - const mainChangeLogPath = env.mainChangelogPath; + const mainChangeLogPath = env.mainChangelogPath!; if (!mainChangeLogDir) { const errorMessage = @@ -23,10 +24,7 @@ export const writeMainChangelog: Module = (env) => { } log('writing main changelog'); + writeFile(mainChangeLogPath, env.changelog!); - writeFileSync( - mainChangeLogPath!, - `${JSON.stringify(env.changelog, null, 2)}\n` - ); return env; }; diff --git a/src/change/write-package-changelogs.ts b/src/change/write-package-changelogs.ts index 9228b82..b62e227 100644 --- a/src/change/write-package-changelogs.ts +++ b/src/change/write-package-changelogs.ts @@ -1,7 +1,5 @@ import { join } from 'path'; -import fs from 'fs'; - import { clone } from 'ramda'; import { white } from 'chalk'; @@ -9,10 +7,11 @@ import type { Module } from '../types'; import { logger } from '../helpers/logger'; import { getReleasablePackages } from '../helpers/get-releasable-packages'; +import { writeFile } from '../helpers/write-file'; import { PackageAfterPrepareChangelogs } from '../types'; -const { error, log } = logger('[change] write package changelogs'); +const { log, error } = logger('[change] write package changelogs'); export const writePackageChangelogs: Module = (env) => { if (!env.packages) { @@ -20,7 +19,7 @@ export const writePackageChangelogs: Module = (env) => { error(errorMessage); throw new Error(errorMessage); } - const releasablePackages = getReleasablePackages(clone(env.packages)); + const releasablePackages = getReleasablePackages(clone(env.packages!)); releasablePackages.forEach((packageName) => { const currentPackage = env.packages![ @@ -31,10 +30,7 @@ export const writePackageChangelogs: Module = (env) => { const changelogJson = currentPackage.changelogs; const changelogFile = join(currentPackage.path, 'changelog.json'); - fs.writeFileSync( - changelogFile, - `${JSON.stringify(changelogJson, null, 2)}\n` - ); + writeFile(changelogFile, changelogJson); }); return env; }; diff --git a/src/change/write-package-jsons-to-npm.ts b/src/change/write-package-jsons-to-npm.ts index d12ab42..5867c9d 100644 --- a/src/change/write-package-jsons-to-npm.ts +++ b/src/change/write-package-jsons-to-npm.ts @@ -1,10 +1,7 @@ -import { writeFileSync } from 'fs'; - -import { white } from 'chalk'; - import type { Module } from '../types'; import { logger } from '../helpers/logger'; +import { writeFile } from '../helpers/write-file'; const { log, error } = logger('[change] write packageJsons (NPM)'); @@ -23,11 +20,10 @@ export const writePackageJsonsToNpm: Module = (env) => { error(errorMessage); throw new Error(errorMessage); } - log(`Writting "${white(packageName)}"`); - - writeFileSync( + log(`Writting "${packageName}"`); + writeFile( `${currentPackage.path}/package.json`, - `${JSON.stringify(currentPackage.packageJsonNpm, null, 2)}\n` + currentPackage.packageJsonNpm ); }); diff --git a/src/change/write-rlsr-json.ts b/src/change/write-rlsr-json.ts index 7517145..dbeb257 100644 --- a/src/change/write-rlsr-json.ts +++ b/src/change/write-rlsr-json.ts @@ -1,9 +1,7 @@ import { join } from 'path'; - -import { writeFileSync } from 'fs'; - import type { Module } from '../types'; import { logger } from '../helpers/logger'; +import { writeFile } from '../helpers/write-file'; const { error, log } = logger('[change] write main changelog'); @@ -17,7 +15,7 @@ export const writeRlsrJson: Module = (env) => { log('writing status information to rlsr.json'); const statusFile = join(env.appRoot, 'rlsr.json'); + writeFile(statusFile, env.newStatus); - writeFileSync(statusFile, `${JSON.stringify(env.newStatus, null, 2)}\n`); return env; }; diff --git a/src/helpers/write-file.ts b/src/helpers/write-file.ts new file mode 100644 index 0000000..ec9b24b --- /dev/null +++ b/src/helpers/write-file.ts @@ -0,0 +1,4 @@ +import { writeFileSync } from 'fs'; + +export const writeFile = (filePath: string, content: object) => + writeFileSync(filePath, `${JSON.stringify(content, null, 2)}\n`); diff --git a/src/persist/write-package-jsons-for-git.ts b/src/persist/write-package-jsons-for-git.ts index 46eeb2e..f4983d7 100644 --- a/src/persist/write-package-jsons-for-git.ts +++ b/src/persist/write-package-jsons-for-git.ts @@ -1,15 +1,10 @@ -import { writeFileSync } from 'fs'; - import type { Module } from '../types'; import { logger } from '../helpers/logger'; +import { writeFile } from '../helpers/write-file'; const { error, log } = logger('[persist] package.json files (git)'); -// revert package.jsons -// after the package is publishet, we bring back the package.jsons with the `*` -// dependencies. The data structure should be in the env. -// This is a very similar step to change > writeToPackageJsons export const writePackageJsonsForGit: Module = (env) => { if (!env.packages) { const errorMessage = 'missing "packages" on env object.'; @@ -29,10 +24,7 @@ export const writePackageJsonsForGit: Module = (env) => { const packageJsonPath = `${currentPackage.path}/package.json`; log(`Reverting "${packageName}" (${packageJsonPath})`); - writeFileSync( - packageJsonPath, - `${JSON.stringify(currentPackage.packageJsonGit, null, 2)}\n` - ); + writeFile(packageJsonPath, currentPackage.packageJsonGit); }); return env;