Skip to content

Commit

Permalink
fix: update store value
Browse files Browse the repository at this point in the history
  • Loading branch information
winchesHe committed Dec 23, 2024
1 parent 42475d4 commit 13a0f2e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {writeFileSync} from 'node:fs';

import {HEROUI_CSS_VARIABLES_PREFIX, NEXTUI_CSS_VARIABLES_PREFIX} from '../../../constants/prefix';
import {getStore} from '../../store';
import {getStore, writeFileAndUpdateStore} from '../../store';

export function migrateCssVariables(files: string[]) {
for (const file of files) {
Expand All @@ -14,7 +12,7 @@ export function migrateCssVariables(files: string[]) {
HEROUI_CSS_VARIABLES_PREFIX
);

writeFileSync(file, content, 'utf-8');
writeFileAndUpdateStore(file, 'rawContent', content);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {writeFileSync} from 'node:fs';

import jscodeshift from 'jscodeshift';

import {HEROUI_PREFIX, NEXTUI_PREFIX} from '../../../constants/prefix';
import {type StoreObject, getStore} from '../../store';
import {type StoreObject, getStore, writeFileAndUpdateStore} from '../../store';

/**
* Migrate the import package will directly write the file
Expand All @@ -25,7 +23,7 @@ export function migrateImportPackageWithPaths(paths: string[]) {

if (dirtyFlag) {
// Write the modified content back to the file
writeFileSync(path, parsedContent.toSource(), 'utf-8');
writeFileAndUpdateStore(path, 'parsedContent', parsedContent);
}
// eslint-disable-next-line no-empty
} catch {}
Expand Down
6 changes: 2 additions & 4 deletions packages/codemod/src/helpers/actions/migrate/migrate-json.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type {SAFE_ANY} from '@helpers/type';

import {writeFileSync} from 'node:fs';

import {Logger} from '@helpers/logger';

import {HEROUI_PREFIX, NEXTUI_PREFIX} from '../../../constants/prefix';
import {getStore} from '../../store';
import {getStore, writeFileAndUpdateStore} from '../../store';

const DEFAULT_INDENT = 2;

Expand All @@ -25,7 +23,7 @@ export async function migrateJson(files: string[]) {
if (dirtyFlag) {
const replacedContent = content.replaceAll(NEXTUI_PREFIX, HEROUI_PREFIX);

writeFileSync(file, replacedContent, 'utf-8');
writeFileAndUpdateStore(file, 'rawContent', replacedContent);
}
})
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {writeFileSync} from 'node:fs';

import {HEROUI_PROVIDER, NEXTUI_PROVIDER} from '../../../constants/prefix';
import {getStore} from '../../store';
import {getStore, writeFileAndUpdateStore} from '../../store';

import {migrateImportName, migrateJSXElementName} from './migrate-common';

Expand Down Expand Up @@ -29,7 +27,7 @@ export function migrateNextuiProvider(paths: string[]) {
migrateImportName(parsedContent, NEXTUI_PROVIDER, HEROUI_PROVIDER);

// Write the modified content back to the file
writeFileSync(path, parsedContent.toSource(), 'utf-8');
writeFileAndUpdateStore(path, 'parsedContent', parsedContent);
}
// eslint-disable-next-line no-empty
} catch {}
Expand Down
6 changes: 2 additions & 4 deletions packages/codemod/src/helpers/actions/migrate/migrate-npmrc.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {writeFileSync} from 'node:fs';

import {HEROUI_PREFIX, NEXTUI_PREFIX} from '../../../constants/prefix';
import {getStore} from '../../store';
import {getStore, writeFileAndUpdateStore} from '../../store';

export function migrateNpmrc(files: string[]) {
for (const file of files) {
const rawContent = getStore(file, 'rawContent');
const content = rawContent.replaceAll(NEXTUI_PREFIX, HEROUI_PREFIX);

writeFileSync(file, content, 'utf-8');
writeFileAndUpdateStore(file, 'rawContent', content);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type {SAFE_ANY} from '@helpers/type';

import {writeFileSync} from 'node:fs';

import jscodeshift from 'jscodeshift';

import {
Expand All @@ -10,7 +8,7 @@ import {
NEXTUI_PLUGIN,
NEXTUI_PREFIX
} from '../../../constants/prefix';
import {getStore} from '../../store';
import {getStore, writeFileAndUpdateStore} from '../../store';

import {migrateCallExpressionName, migrateImportName} from './migrate-common';
import {migrateImportPackage} from './migrate-import';
Expand Down Expand Up @@ -57,7 +55,7 @@ export function migrateTailwindcss(paths: string[]) {
});

if (dirtyFlag) {
writeFileSync(path, parsedContent.toSource(), 'utf-8');
writeFileAndUpdateStore(path, 'parsedContent', parsedContent);
}
}
}
36 changes: 35 additions & 1 deletion packages/codemod/src/helpers/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {SAFE_ANY} from '@helpers/type';
import type {Collection} from 'jscodeshift';

import {readFileSync} from 'node:fs';
import {readFileSync, writeFileSync} from 'node:fs';

import {Logger} from '@helpers/logger';
import {basename} from 'pathe';
Expand All @@ -17,6 +17,8 @@ export type StoreObject = {

export type StoreKey = keyof StoreObject;

type ExcludeStoreKey = Exclude<StoreKey, 'filePath'>;

/**
* Used to temporarily store the data that parsed from the file
*/
Expand Down Expand Up @@ -65,3 +67,35 @@ export function getStore<T extends StoreKey>(path: string, key: T): StoreObject[

return store[path]?.[key] as StoreObject[T];
}

export function updateStore<K extends ExcludeStoreKey>(
path: string,
key: K,
value: StoreObject[K]
) {
if (!store[path]) {
store[path] = {
filePath: path,
[key]: value
} as SAFE_ANY;

return;
}

store[path]![key] = value as SAFE_ANY;
}

export function writeFileAndUpdateStore<K extends ExcludeStoreKey>(
path: string,
key: K,
parsedContent: StoreObject[K]
) {
const data: Record<ExcludeStoreKey, SAFE_ANY> = {
parsedContent,
rawContent: typeof parsedContent === 'string' ? parsedContent : parsedContent?.toSource()
};
const value = data[key];

writeFileSync(path, data.rawContent, 'utf-8');
updateStore(path, key, value);
}

0 comments on commit 13a0f2e

Please sign in to comment.