Skip to content

Commit

Permalink
Fix value duplication in package.json for theemo build (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi authored Aug 4, 2024
1 parent 54d2717 commit 45971ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
31 changes: 17 additions & 14 deletions packages/@theemo/build/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ import fs from 'node:fs';
import path from 'node:path';

import { readPackageSync } from 'read-pkg';
import { updatePackageSync } from 'write-package';
import { writePackageSync } from 'write-package';

import type { GenerateConfig, SchemeConfig } from './config';
import type { PackageJson } from 'read-pkg';

interface Package {
name: string;
keywords: string[];
type Package = PackageJson & {
theemo?: {
name: string;
colorSchemes?: string[];
file?: string;
};
}
};

function getBlockFromFile(file: string) {
const contents = fs.existsSync(file) ? fs.readFileSync(file, 'utf-8') : '';
Expand Down Expand Up @@ -88,7 +87,7 @@ function prepareColorSchemes(config: GenerateConfig, name: string) {
function getThemeName() {
const data = readPackageSync() as Package;

return data.theemo?.name ?? data.name;
return (data.theemo?.name ?? data.name) as string;
}

/**
Expand All @@ -111,23 +110,27 @@ export function build(config: GenerateConfig) {
fs.writeFileSync(outFile, contents.join('\n'));

// update package.json
const originalPackageJson = readPackageSync({ normalize: false }) as Package;
const packageJson = {
keywords: Array.isArray(originalPackageJson.keywords) ? originalPackageJson.keywords : [],
theemo: originalPackageJson.theemo ?? {
name: originalPackageJson.name
}
};
const packageJson = readPackageSync({ normalize: false }) as Package;

if (!packageJson.theemo) {
packageJson.theemo = {
name: packageJson.name as string
};
}

if (config.colorSchemes) {
packageJson.theemo.colorSchemes = Object.keys(config.colorSchemes);
}

packageJson.theemo.file = outFile;

if (!Array.isArray(packageJson.keywords)) {
packageJson.keywords = [];
}

if (!packageJson.keywords.includes('theemo-theme')) {
packageJson.keywords.push('theemo-theme');
}

updatePackageSync(packageJson);
writePackageSync(packageJson);
}
7 changes: 0 additions & 7 deletions testing/playground-theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@
"description": "Test theme for API",
"license": "MIT",
"keywords": [
"theemo-theme",
"theemo-theme",
"theemo-theme",
"theemo-theme"
],
"theemo": {
"name": "theemo",
"colorSchemes": [
"light",
"dark",
"light",
"dark",
"light",
"dark"
],
Expand Down

0 comments on commit 45971ca

Please sign in to comment.