Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SmartDataLab
public
applications
renovate
Commits
449bc3ec
Unverified
Commit
449bc3ec
authored
3 years ago
by
Jamie Magee
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
refactor(npm): extract replacement to a private function (#11195)
parent
b0cc6080
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/manager/npm/update/dependency/index.ts
+57
-56
57 additions, 56 deletions
lib/manager/npm/update/dependency/index.ts
with
57 additions
and
56 deletions
lib/manager/npm/update/dependency/index.ts
+
57
−
56
View file @
449bc3ec
import
{
dequal
}
from
'
dequal
'
;
import
{
dequal
}
from
'
dequal
'
;
import
type
{
PackageJson
}
from
'
type-fest
'
;
import
{
logger
}
from
'
../../../../logger
'
;
import
{
logger
}
from
'
../../../../logger
'
;
import
{
matchAt
,
replaceAt
}
from
'
../../../../util/string
'
;
import
{
matchAt
,
replaceAt
}
from
'
../../../../util/string
'
;
import
type
{
UpdateDependencyConfig
}
from
'
../../../types
'
;
import
type
{
UpdateDependencyConfig
}
from
'
../../../types
'
;
function
replaceAsString
(
parsedContents
:
PackageJson
,
fileContent
:
string
,
depType
:
string
,
depName
:
string
,
oldVersion
:
string
,
newValue
:
string
):
string
|
null
{
// Update the file = this is what we want
// eslint-disable-next-line no-param-reassign
parsedContents
[
depType
][
depName
]
=
newValue
;
// Look for the old version number
const
searchString
=
`"
${
oldVersion
}
"`
;
const
newString
=
`"
${
newValue
}
"`
;
// Skip ahead to depType section
let
searchIndex
=
fileContent
.
indexOf
(
`"
${
depType
}
"`
)
+
depType
.
length
;
logger
.
trace
(
`Starting search at index
${
searchIndex
}
`
);
// Iterate through the rest of the file
for
(;
searchIndex
<
fileContent
.
length
;
searchIndex
+=
1
)
{
// First check if we have a hit for the old version
if
(
matchAt
(
fileContent
,
searchIndex
,
searchString
))
{
logger
.
trace
(
`Found match at index
${
searchIndex
}
`
);
// Now test if the result matches
const
testContent
=
replaceAt
(
fileContent
,
searchIndex
,
searchString
,
newString
);
// Compare the parsed JSON structure of old and new
if
(
dequal
(
parsedContents
,
JSON
.
parse
(
testContent
)))
{
return
testContent
;
}
}
}
// istanbul ignore next
throw
new
Error
();
}
export
function
updateDependency
({
export
function
updateDependency
({
fileContent
,
fileContent
,
upgrade
,
upgrade
,
...
@@ -30,41 +70,21 @@ export function updateDependency({
...
@@ -30,41 +70,21 @@ export function updateDependency({
}
}
logger
.
debug
(
`npm.updateDependency():
${
depType
}
.
${
depName
}
=
${
newValue
}
`
);
logger
.
debug
(
`npm.updateDependency():
${
depType
}
.
${
depName
}
=
${
newValue
}
`
);
try
{
try
{
const
parsedContents
=
JSON
.
parse
(
fileContent
);
const
parsedContents
:
PackageJson
=
JSON
.
parse
(
fileContent
);
// Save the old version
// Save the old version
const
oldVersion
:
string
=
parsedContents
[
depType
][
depName
];
const
oldVersion
:
string
=
parsedContents
[
depType
][
depName
];
if
(
oldVersion
===
newValue
)
{
if
(
oldVersion
===
newValue
)
{
logger
.
trace
(
'
Version is already updated
'
);
logger
.
trace
(
'
Version is already updated
'
);
return
fileContent
;
return
fileContent
;
}
}
// Update the file = this is what we want
let
newFileContent
=
replaceAsString
(
parsedContents
[
depType
][
depName
]
=
newValue
;
parsedContents
,
// Look for the old version number
const
searchString
=
`"
${
oldVersion
}
"`
;
const
newString
=
`"
${
newValue
}
"`
;
let
newFileContent
=
null
;
// Skip ahead to depType section
let
searchIndex
=
fileContent
.
indexOf
(
`"
${
depType
}
"`
)
+
depType
.
length
;
logger
.
trace
(
`Starting search at index
${
searchIndex
}
`
);
// Iterate through the rest of the file
for
(;
searchIndex
<
fileContent
.
length
;
searchIndex
+=
1
)
{
// First check if we have a hit for the old version
if
(
matchAt
(
fileContent
,
searchIndex
,
searchString
))
{
logger
.
trace
(
`Found match at index
${
searchIndex
}
`
);
// Now test if the result matches
const
testContent
=
replaceAt
(
fileContent
,
fileContent
,
searchIndex
,
depType
,
searchString
,
depName
,
newString
oldVersion
,
newValue
);
);
// Compare the parsed JSON structure of old and new
if
(
dequal
(
parsedContents
,
JSON
.
parse
(
testContent
)))
{
newFileContent
=
testContent
;
break
;
}
}
}
// istanbul ignore if
// istanbul ignore if
if
(
!
newFileContent
)
{
if
(
!
newFileContent
)
{
logger
.
debug
(
logger
.
debug
(
...
@@ -93,33 +113,14 @@ export function updateDependency({
...
@@ -93,33 +113,14 @@ export function updateDependency({
'
Upgraded dependency exists in yarn resolutions but is different version
'
'
Upgraded dependency exists in yarn resolutions but is different version
'
);
);
}
}
// Look for the old version number
newFileContent
=
replaceAsString
(
const
oldResolution
=
`"
${
String
(
parsedContents
.
resolutions
[
depKey
])}
"`
;
parsedContents
,
const
newResolution
=
`"
${
newValue
}
"`
;
// Update the file = this is what we want
parsedContents
.
resolutions
[
depKey
]
=
newValue
;
// Skip ahead to depType section
searchIndex
=
newFileContent
.
indexOf
(
`"resolutions"`
);
logger
.
trace
(
`Starting search at index
${
searchIndex
}
`
);
// Iterate through the rest of the file
for
(;
searchIndex
<
newFileContent
.
length
;
searchIndex
+=
1
)
{
// First check if we have a hit for the old version
if
(
matchAt
(
newFileContent
,
searchIndex
,
oldResolution
))
{
logger
.
trace
(
`Found match at index
${
searchIndex
}
`
);
// Now test if the result matches
const
testContent
=
replaceAt
(
newFileContent
,
newFileContent
,
searchIndex
,
'
resolutions
'
,
oldResolution
,
depKey
,
newResolution
parsedContents
.
resolutions
[
depKey
],
newValue
);
);
// Compare the parsed JSON structure of old and new
if
(
dequal
(
parsedContents
,
JSON
.
parse
(
testContent
)))
{
newFileContent
=
testContent
;
break
;
}
}
}
}
}
}
}
return
newFileContent
;
return
newFileContent
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment