diff --git a/.changeset/great-lemons-carry.md b/.changeset/great-lemons-carry.md index 0f056f6..974cb6d 100644 --- a/.changeset/great-lemons-carry.md +++ b/.changeset/great-lemons-carry.md @@ -2,4 +2,4 @@ '@commencis/cli': patch --- -alter package.json file content +feat: update package.json initial values on generation diff --git a/src/constants/index.ts b/src/constants/index.ts index 7f33bb8..dc8766b 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,2 +1,3 @@ export * from './commencisLogo'; +export * from './package'; export * from './template'; diff --git a/src/constants/package.ts b/src/constants/package.ts new file mode 100644 index 0000000..5712ebb --- /dev/null +++ b/src/constants/package.ts @@ -0,0 +1,5 @@ +export const PACKAGE_DEFAULTS = { + version: '1.0.0', + license: 'UNLICENSED', + private: true, +}; diff --git a/src/helpers/updatePackageData.ts b/src/helpers/updatePackageData.ts index ddd8497..bf88210 100644 --- a/src/helpers/updatePackageData.ts +++ b/src/helpers/updatePackageData.ts @@ -1,6 +1,8 @@ import fs from 'node:fs/promises'; import path from 'node:path'; +import { PACKAGE_DEFAULTS } from '@/constants'; + export async function updatePackageData( directoryPath: string, projectName: string @@ -8,27 +10,28 @@ export async function updatePackageData( 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 };