Skip to content

Commit

Permalink
feat: update package content on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ymehmetcan committed Dec 26, 2024
1 parent e3e55e1 commit faa2ac9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .changeset/great-lemons-carry.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@commencis/cli': patch
---

alter package.json file content
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,
};
37 changes: 20 additions & 17 deletions src/helpers/updatePackageData.ts
Original file line number Diff line number Diff line change
@@ -1,34 +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');
let packageJson = JSON.parse(packageJsonData);
const templateVersion = packageJson.version;

packageJson.name = projectName;
packageJson.version = '1.0.0';

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

// To add 'private' after common locations such as after 'license'
const { name, version, ...rest } = packageJson;
packageJson = {
name,
version,
license: 'UNLICENSED',
private: true,
...rest,
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 faa2ac9

Please sign in to comment.