-
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.
feat: add feature flags script and func for on\off
- Loading branch information
Showing
7 changed files
with
131 additions
and
6 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
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('Enter a feature name'); | ||
} | ||
|
||
if (!featureState) { | ||
throw new Error('Enter a feature state (on or off)'); | ||
} | ||
|
||
if (featureState !== 'on' && featureState !== 'off') { | ||
throw new Error('Incorrect feature state (on or 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
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 { setFeatureFlags, getFeatureFlag } 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,20 @@ | ||
import { FeatureFlags } from '@/shared/types/featureFlags'; | ||
import { getFeatureFlag } from './setGetFeatures'; | ||
|
||
interface ToggleFeaturesOptions<T> { | ||
name: keyof FeatureFlags; | ||
on: () => T; | ||
off: () => T; | ||
} | ||
|
||
export function toggleFeatures<T>({ | ||
off, | ||
on, | ||
name, | ||
}: ToggleFeaturesOptions<T>): T { | ||
if (getFeatureFlag(name)) { | ||
return on(); | ||
} | ||
|
||
return off(); | ||
} |