Skip to content

Commit b7bfda8

Browse files
authored
Added Support to with local environment variable (#1256)
1 parent 0f0a3dc commit b7bfda8

File tree

5 files changed

+99
-6
lines changed

5 files changed

+99
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [4.3.0]
9+
10+
### Added
11+
12+
- Support to `.npmrc` with local environment variable
13+
814
## [4.2.2]
915

1016
### Fixed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vtex",
3-
"version": "4.2.2",
3+
"version": "4.3.0",
44
"description": "The platform for e-commerce apps",
55
"bin": "bin/run",
66
"main": "lib/api/index.js",

src/api/files/ProjectFilesManager.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,64 @@ import { createReadStream, lstat, readFileSync, statSync } from 'fs-extra'
22
import glob from 'globby'
33
import { join } from 'path'
44
import { reject } from 'ramda'
5+
import { PassThrough } from 'stream'
56
import { BatchStream } from '../typings/types'
67

8+
/**
9+
* Expands environment variables in .npmrc file content
10+
* @param content The original .npmrc file content
11+
* @returns The content with environment variables expanded
12+
*/
13+
const expandEnvironmentVariables = (content: string): string => {
14+
return content.replace(/\$\{([^}]+)\}/g, (_, envVar) => {
15+
const value = process.env[envVar]
16+
if (value === undefined) {
17+
throw new Error(`Environment variable ${envVar} is not defined`)
18+
}
19+
return value
20+
})
21+
}
22+
23+
/**
24+
* Creates a stream from a string
25+
* @param str The string to convert to a stream
26+
* @returns A readable stream
27+
*/
28+
const stringToStream = (str: string) => {
29+
const stream = new PassThrough()
30+
stream.end(str)
31+
return stream
32+
}
33+
734
export const createPathToFileObject = (root: string, prefix = '') => {
835
return (path: string): BatchStream => {
936
const realAbsolutePath = join(root, path)
1037
const stats = statSync(realAbsolutePath)
38+
39+
// Check if this is a .npmrc file that needs environment variable expansion
40+
if (path.endsWith('.npmrc')) {
41+
try {
42+
const originalContent = readFileSync(realAbsolutePath, 'utf8')
43+
const expandedContent = expandEnvironmentVariables(originalContent)
44+
const expandedStream = stringToStream(expandedContent)
45+
46+
return {
47+
path: join(prefix, path),
48+
content: expandedStream,
49+
byteSize: Buffer.byteLength(expandedContent, 'utf8'),
50+
}
51+
} catch (error) {
52+
// If environment variable expansion fails, fall back to original file
53+
console.warn(`Warning: Failed to expand environment variables in ${path}: ${error.message}`)
54+
return {
55+
path: join(prefix, path),
56+
content: createReadStream(realAbsolutePath),
57+
byteSize: stats.size,
58+
}
59+
}
60+
}
61+
62+
// For all other files, use the original behavior
1163
return {
1264
path: join(prefix, path),
1365
content: createReadStream(realAbsolutePath),
@@ -52,7 +104,7 @@ export class ProjectFilesManager {
52104
}
53105

54106
public async getLocalFiles(test = false): Promise<string[]> {
55-
const files: string[] = await glob(['manifest.json', 'policies.json', 'node/.*', 'react/.*'], {
107+
const files: string[] = await glob(['manifest.json', 'policies.json', '.npmrc', 'node/.*', 'react/.*'], {
56108
cwd: this.root,
57109
follow: true,
58110
ignore: this.getIgnoredPaths(test),

src/api/modules/apps/file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const getIgnoredPaths = (root: string, test = false): string[] => {
4444

4545
export const listLocalFiles = (root: string, test = false, folder?: string): Promise<string[]> =>
4646
Promise.resolve(
47-
glob(['manifest.json', 'policies.json', 'node/.*', 'react/.*', `${safeFolder(folder)}`], {
47+
glob(['manifest.json', 'policies.json', '.npmrc', 'node/.*', 'react/.*', `${safeFolder(folder)}`], {
4848
cwd: root,
4949
follow: true,
5050
ignore: getIgnoredPaths(root, test),

src/modules/apps/link.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ const watchAndSendChanges = async (
164164
throw err
165165
}
166166

167-
const defaultPatterns = ['*/**', 'manifest.json', 'policies.json', 'cypress.json']
167+
const defaultPatterns = ['*/**', 'manifest.json', 'policies.json', '.npmrc', 'cypress.json']
168168
const linkedDepsPatterns = map(path => join(path, '**'), yarnFilesManager.symlinkedDepsDirs)
169169

170170
const pathModifier = pipe(
@@ -173,8 +173,43 @@ const watchAndSendChanges = async (
173173
)
174174

175175
const pathToChange = (path: string, remove?: boolean): ChangeToSend => {
176-
const content = remove ? null : readFileSync(resolvePath(root, path)).toString('base64')
177-
const byteSize = remove ? 0 : Buffer.byteLength(content)
176+
if (remove) {
177+
return {
178+
content: null,
179+
byteSize: 0,
180+
path: pathModifier(path),
181+
}
182+
}
183+
184+
const filePath = resolvePath(root, path)
185+
let fileContent: string
186+
187+
// Handle .npmrc files with environment variable expansion
188+
if (path.endsWith('.npmrc')) {
189+
try {
190+
const originalContent = readFileSync(filePath, 'utf8')
191+
// Expand environment variables in .npmrc content
192+
const expandedContent = originalContent.replace(/\$\{([^}]+)\}/g, (_, envVar) => {
193+
const value = process.env[envVar]
194+
if (value === undefined) {
195+
throw new Error(`Environment variable ${envVar} is not defined`)
196+
}
197+
return value
198+
})
199+
fileContent = expandedContent
200+
} catch (error) {
201+
// If environment variable expansion fails, fall back to original file
202+
log.warn(`Warning: Failed to expand environment variables in ${path}: ${error.message}`)
203+
fileContent = readFileSync(filePath, 'utf8')
204+
}
205+
} else {
206+
// For all other files, read as binary and convert to base64
207+
fileContent = readFileSync(filePath).toString('base64')
208+
}
209+
210+
const content = Buffer.from(fileContent, 'utf8').toString('base64')
211+
const byteSize = Buffer.byteLength(content)
212+
178213
return {
179214
content,
180215
byteSize,

0 commit comments

Comments
 (0)