-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add toggle features + auto remove script
- Loading branch information
Showing
4 changed files
with
151 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Node, Project, SyntaxKind } from 'ts-morph'; | ||
|
||
const removedFeatureName = process.argv[2]; // example isArticleEnabled | ||
const featureState = process.argv[3]; // example off\on | ||
|
||
if (!removedFeatureName) { | ||
throw new Error('Укажите название фича-флага'); | ||
} | ||
|
||
if (!featureState) { | ||
throw new Error('Укажите состояние фичи (on или off)'); | ||
} | ||
|
||
if (featureState !== 'on' && featureState !== 'off') { | ||
throw new Error('Некорректное значение состояния фичи (on или off)'); | ||
} | ||
|
||
const project = new Project({}); | ||
|
||
project.addSourceFilesAtPaths('src/**/*.ts'); | ||
project.addSourceFilesAtPaths('src/**/*.tsx'); | ||
|
||
const files = project.getSourceFiles(); | ||
|
||
function isToggleFunction(node: Node) { | ||
let isToggleFeatures = false; | ||
|
||
node.forEachChild((child) => { | ||
if ( | ||
child.isKind(SyntaxKind.Identifier) && | ||
child.getText() === 'toggleFeatures' | ||
) { | ||
isToggleFeatures = true; | ||
} | ||
}); | ||
|
||
return isToggleFeatures; | ||
} | ||
|
||
files.forEach((sourceFile) => { | ||
sourceFile.forEachDescendant((node) => { | ||
if (node.isKind(SyntaxKind.CallExpression) && isToggleFunction(node)) { | ||
const objectOptions = node.getFirstDescendantByKind( | ||
SyntaxKind.ObjectLiteralExpression, | ||
); | ||
|
||
if (!objectOptions) return; | ||
|
||
const offFunctionProperty = objectOptions.getProperty('off'); | ||
const onFunctionProperty = objectOptions.getProperty('on'); | ||
|
||
const featureNameProperty = objectOptions.getProperty('name'); | ||
|
||
const onFunction = onFunctionProperty?.getFirstDescendantByKind( | ||
SyntaxKind.ArrowFunction, | ||
); | ||
const offFunction = offFunctionProperty?.getFirstDescendantByKind( | ||
SyntaxKind.ArrowFunction, | ||
); | ||
const featureName = featureNameProperty | ||
?.getFirstDescendantByKind(SyntaxKind.StringLiteral) | ||
?.getText() | ||
.slice(1, -1); | ||
|
||
if (featureName !== removedFeatureName) return; | ||
|
||
if (featureState === 'on') { | ||
node.replaceWithText(onFunction?.getBody().getText() ?? ''); | ||
} | ||
|
||
if (featureState === 'off') { | ||
node.replaceWithText(offFunction?.getBody().getText() ?? ''); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
project.save(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { getFeatureFlag, setFeatureFlags } from "./setGetFeatures"; | ||
export { toggleFeatures } from "./toggleFeatures"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { FeatureFlags } from "@/shared/types/featureFlags"; | ||
import { getFeatureFlag } from './setGetFeatures'; | ||
|
||
export interface ToggleFeaturesOptions<T> { | ||
name: keyof FeatureFlags; | ||
on: () => T; | ||
off: () => T; | ||
} | ||
|
||
export function toggleFeatures<T>({ name, on, off }: ToggleFeaturesOptions<T>) { | ||
if (getFeatureFlag(name)) { | ||
return on(); | ||
} | ||
|
||
return off(); | ||
} |