-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codemod(v6): Adds codemod to convert all process.env usage to use dot…
… notation
- Loading branch information
Showing
7 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/codemods/src/codemods/v6.x.x/processEnvDotNotation/README.md
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,10 @@ | ||
# Process Env Dot Notation | ||
|
||
Finds all cases where `process.env` is accessed via array notation (specifically string literals), and converts it to dot notation. | ||
|
||
```diff | ||
- process.env['BAZINGA'] | ||
+ process.env.BAZINGA | ||
``` | ||
|
||
**NOTE** - this does not deal with dynamic access case. This is something users will need to do themselves |
10 changes: 10 additions & 0 deletions
10
...ages/codemods/src/codemods/v6.x.x/processEnvDotNotation/__testfixtures__/default.input.js
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,10 @@ | ||
process.env['NODE_ENV'] | ||
process.env['ACCESS_TWO'] | ||
process.env.ACCESS_THREE | ||
|
||
if (process.env['NODE_ENV'] === 'production') { | ||
console.log('Im in production') | ||
} | ||
|
||
|
||
process.env['KITTENS'].replaceAll('claws', 'cuddles') |
9 changes: 9 additions & 0 deletions
9
...ges/codemods/src/codemods/v6.x.x/processEnvDotNotation/__testfixtures__/default.output.js
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,9 @@ | ||
process.env.NODE_ENV | ||
process.env.ACCESS_TWO | ||
process.env.ACCESS_THREE | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
console.log('Im in production') | ||
} | ||
|
||
process.env.KITTENS.replaceAll('claws', 'cuddles') |
5 changes: 5 additions & 0 deletions
5
...odemods/src/codemods/v6.x.x/processEnvDotNotation/__tests__/processEnvDotNotation.test.ts
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,5 @@ | ||
describe('processEnvDotNotation', () => { | ||
it('Replaces array access syntax with dot notation', async () => { | ||
await matchTransformSnapshot('processEnvDotNotation', 'default') | ||
}) | ||
}) |
30 changes: 30 additions & 0 deletions
30
packages/codemods/src/codemods/v6.x.x/processEnvDotNotation/processEnvDotNotation.ts
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,30 @@ | ||
import { FileInfo, API } from 'jscodeshift' | ||
|
||
export default function transform(file: FileInfo, api: API) { | ||
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
root | ||
.find(j.MemberExpression, { | ||
object: { | ||
type: 'MemberExpression', | ||
object: { name: 'process' }, | ||
property: { name: 'env' }, | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const envVarName = path.value.property | ||
|
||
// Only apply the codemod if process.env['bbb'] | ||
// where bbb is the string literal. Otherwise it catches process.env.bbb too | ||
if (j.StringLiteral.check(envVarName)) { | ||
const dotNotation = j.memberExpression( | ||
j.memberExpression(j.identifier('process'), j.identifier('env')), | ||
j.identifier(envVarName.value) | ||
) | ||
j(path).replaceWith(dotNotation) | ||
} | ||
}) | ||
|
||
return root.toSource() | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/codemods/src/codemods/v6.x.x/processEnvDotNotation/processEnvDotNotation.yargs.ts
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,29 @@ | ||
import path from 'path' | ||
|
||
import task, { TaskInnerAPI } from 'tasuku' | ||
|
||
import { getPaths } from '@redwoodjs/project-config' | ||
import runTransform from '../../../lib/runTransform' | ||
|
||
export const command = 'process-env-dot-notation' | ||
export const description = '(v6.x.x->v6.x.x) Converts world to bazinga' | ||
|
||
export const handler = () => { | ||
task( | ||
'Process Env Dot Notation', | ||
async ({ setOutput }: TaskInnerAPI) => { | ||
await runTransform({ | ||
transformPath: path.join(__dirname, 'processEnvDotNotation.js'), | ||
// Here we know exactly which file we need to transform, but often times you won't. | ||
// If you need to transform files based on their name, location, etc, use `fast-glob`. | ||
// If you need to transform files based on their contents, use `getFilesWithPattern`. | ||
targetPaths: fg.sync('**/*.{js,jsx,tsx}', { | ||
cwd: getPaths().web.src, | ||
absolute: true, | ||
}), | ||
}) | ||
|
||
setOutput('All done! Run `yarn rw lint --fix` to prettify your code') | ||
} | ||
) | ||
} |
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