Skip to content

Commit

Permalink
refactor: extract writing file into helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
Iryna Makar committed Aug 2, 2021
1 parent 4ca2619 commit 72fe138
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 32 deletions.
8 changes: 3 additions & 5 deletions src/change/write-main-changelog.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { join } from 'path';

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');

Expand All @@ -25,10 +26,7 @@ export const writeMainChangelog: Module = (env) => {
}

log('writing main changelog');
writeFile(mainChangeLogFile, env.changelog!);

writeFileSync(
mainChangeLogFile,
`${JSON.stringify(env.changelog, null, 2)}\n`
);
return env;
};
12 changes: 4 additions & 8 deletions src/change/write-package-changelogs.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { join } from 'path';

import fs from 'fs';

import { clone } from 'ramda';

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) {
const errorMessage = '"packages" attribute not found on env config object';
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![
Expand All @@ -30,10 +29,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;
};
8 changes: 3 additions & 5 deletions src/change/write-package-jsons-to-npm.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { writeFileSync } from 'fs';

import type { Module } from '../types';

import { logger } from '../helpers/logger';
import { getReleasablePackages } from '../helpers/get-releasable-packages';
import { writeFile } from '../helpers/write-file';

const { log, error } = logger('[change] write packageJsons (NPM)');

Expand All @@ -27,10 +26,9 @@ export const writePackageJsonsToNpm: Module = (env) => {
throw new Error(errorMessage);
}
log(`Writting "${packageName}"`);

writeFileSync(
writeFile(
`${currentPackage.path}/package.json`,
`${JSON.stringify(currentPackage.packageJsonNpm, null, 2)}\n`
currentPackage.packageJsonNpm
);
});

Expand Down
6 changes: 2 additions & 4 deletions src/change/write-rlsr-json.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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;
};
4 changes: 4 additions & 0 deletions src/helpers/write-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { writeFileSync } from 'fs';

export const writeFile = (filePath: string, content: object) =>
writeFileSync(filePath, `${JSON.stringify(content, null, 2)}\n`);
12 changes: 2 additions & 10 deletions src/persist/write-package-jsons-for-git.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { writeFileSync } from 'fs';

import type { Module } from '../types';

import { logger } from '../helpers/logger';
import { getReleasablePackages } from '../helpers/get-releasable-packages';
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.';
Expand All @@ -34,10 +29,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;
Expand Down

0 comments on commit 72fe138

Please sign in to comment.