Skip to content

Commit

Permalink
Merge pull request #50 from Commencis/feature/package-json-remove-lef…
Browse files Browse the repository at this point in the history
…tover

feat: remove leftover props from package.json
  • Loading branch information
ymehmetcan authored Dec 31, 2024
2 parents 1563ebf + 9fdf741 commit 33d12be
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-lemons-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commencis/cli': patch
---

feat: update package.json initial values on generation
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './commencisLogo';
export * from './package';
export * from './template';
5 changes: 5 additions & 0 deletions src/constants/package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const PACKAGE_DEFAULTS = {
version: '1.0.0',
license: 'UNLICENSED',
private: true,
};
27 changes: 22 additions & 5 deletions src/helpers/updatePackageData.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import fs from 'node:fs/promises';
import path from 'node:path';

import { PACKAGE_DEFAULTS } from '@/constants';

export async function updatePackageData(
directoryPath: string,
projectName: string
): Promise<{ templateVersion: string }> {
try {
const packageJsonPath = path.join(directoryPath, 'package.json');
const packageJsonData = await fs.readFile(packageJsonPath, 'utf-8');
const packageJson = JSON.parse(packageJsonData);
const templateVersion = packageJson.version;

packageJson.name = projectName;
packageJson.version = '1.0.0';
const {
name: _name,
license: _license,
description: _description,
author: _author,
version: templateVersion,
build,
...originalPackageJsonData
} = JSON.parse(packageJsonData);

const updatedPackageJson = {
name: projectName,
version: PACKAGE_DEFAULTS.version,
build,
license: PACKAGE_DEFAULTS.license,
private: PACKAGE_DEFAULTS.private,
...originalPackageJsonData,
};

const formattedPackageJson = JSON.stringify(packageJson, null, 2) + '\n';
const formattedPackageJson =
JSON.stringify(updatedPackageJson, null, 2) + '\n';

await fs.writeFile(packageJsonPath, formattedPackageJson, 'utf-8');
return { templateVersion };
Expand Down

0 comments on commit 33d12be

Please sign in to comment.