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
a921ff26
Commit
a921ff26
authored
8 years ago
by
Rhys Arkins
Browse files
Options
Downloads
Patches
Plain Diff
Refactor packageJson logging
parent
9e4aac1d
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/helpers/packageJson.js
+41
-31
41 additions, 31 deletions
src/helpers/packageJson.js
src/index.js
+3
-3
3 additions, 3 deletions
src/index.js
with
44 additions
and
34 deletions
src/helpers/packageJson.js
+
41
−
31
View file @
a921ff26
const
_
=
require
(
'
lodash
'
);
const
_
=
require
(
'
lodash
'
);
module
.
exports
=
{
let
logger
=
null
;
setNewValue
(
currentFileContent
,
depType
,
depName
,
newVersion
)
{
module
.
exports
=
function
packageJson
(
config
)
{
logger
=
config
.
logger
;
this
.
setNewValue
=
setNewValue
;
return
this
;
};
function
setNewValue
(
currentFileContent
,
depType
,
depName
,
newVersion
)
{
logger
.
debug
(
`setNewValue:
${
depType
}
.
${
depName
}
=
${
newVersion
}
`
);
const
parsedContents
=
JSON
.
parse
(
currentFileContent
);
const
parsedContents
=
JSON
.
parse
(
currentFileContent
);
// Save the old version
// Save the old version
const
oldVersion
=
parsedContents
[
depType
][
depName
];
const
oldVersion
=
parsedContents
[
depType
][
depName
];
...
@@ -12,14 +20,16 @@ module.exports = {
...
@@ -12,14 +20,16 @@ module.exports = {
const
newString
=
`"
${
newVersion
}
"`
;
const
newString
=
`"
${
newVersion
}
"`
;
let
newFileContent
=
null
;
let
newFileContent
=
null
;
// Skip ahead to depType section
// Skip ahead to depType section
let
searchIndex
=
currentFileContent
.
indexOf
(
`"
${
depType
}
"`
)
+
let
searchIndex
=
currentFileContent
.
indexOf
(
`"
${
depType
}
"`
)
+
depType
.
length
;
depType
.
length
;
logger
.
debug
(
`Starting search at index
${
searchIndex
}
`
)
;
// Iterate through the rest of the file
// Iterate through the rest of the file
for
(;
searchIndex
<
currentFileContent
.
length
;
searchIndex
+=
1
)
{
for
(;
searchIndex
<
currentFileContent
.
length
;
searchIndex
+=
1
)
{
// First check if we have a hit for the old version
// First check if we have a hit for the old version
if
(
matchAt
(
currentFileContent
,
searchIndex
,
searchString
))
{
if
(
matchAt
(
currentFileContent
,
searchIndex
,
searchString
))
{
logger
.
debug
(
`Found match at index
${
searchIndex
}
`
);
// Now test if the result matches
// Now test if the result matches
const
testContent
=
replaceAt
(
currentFileContent
,
searchIndex
,
searchString
,
newString
);
const
testContent
=
replaceAt
(
currentFileContent
,
searchIndex
,
searchString
,
newString
);
logger
.
debug
(
`testContent =
${
testContent
}
`
);
// Compare the parsed JSON structure of old and new
// Compare the parsed JSON structure of old and new
if
(
_
.
isEqual
(
parsedContents
,
JSON
.
parse
(
testContent
)))
{
if
(
_
.
isEqual
(
parsedContents
,
JSON
.
parse
(
testContent
)))
{
newFileContent
=
testContent
;
newFileContent
=
testContent
;
...
@@ -31,8 +41,7 @@ module.exports = {
...
@@ -31,8 +41,7 @@ module.exports = {
throw
new
Error
(
'
Could not find old version
'
);
throw
new
Error
(
'
Could not find old version
'
);
}
}
return
newFileContent
;
return
newFileContent
;
},
}
};
// Return true if the match string is found at index in content
// Return true if the match string is found at index in content
function
matchAt
(
content
,
index
,
match
)
{
function
matchAt
(
content
,
index
,
match
)
{
...
@@ -41,5 +50,6 @@ function matchAt(content, index, match) {
...
@@ -41,5 +50,6 @@ function matchAt(content, index, match) {
// Replace oldString with newString at location index of content
// Replace oldString with newString at location index of content
function
replaceAt
(
content
,
index
,
oldString
,
newString
)
{
function
replaceAt
(
content
,
index
,
oldString
,
newString
)
{
logger
.
debug
(
`Replacing
${
oldString
}
with
${
newString
}
at index
${
index
}
`
);
return
content
.
substr
(
0
,
index
)
+
newString
+
content
.
substr
(
index
+
oldString
.
length
);
return
content
.
substr
(
0
,
index
)
+
newString
+
content
.
substr
(
index
+
oldString
.
length
);
}
}
This diff is collapsed.
Click to expand it.
src/index.js
+
3
−
3
View file @
a921ff26
const
semver
=
require
(
'
semver
'
);
const
semver
=
require
(
'
semver
'
);
const
configurator
=
require
(
'
./helpers/configurator
'
);
const
config
=
configurator
.
init
(
process
.
argv
);
const
github
=
require
(
'
./helpers/github
'
);
const
github
=
require
(
'
./helpers/github
'
);
const
configurator
=
require
(
'
./helpers/configurator
'
);
const
npm
=
require
(
'
./helpers/npm
'
);
const
npm
=
require
(
'
./helpers/npm
'
);
const
packageJson
=
require
(
'
./helpers/packageJson
'
);
const
packageJson
=
require
(
'
./helpers/packageJson
'
)
(
config
)
;
const
config
=
configurator
.
init
(
process
.
argv
);
const
logger
=
config
.
logger
;
const
logger
=
config
.
logger
;
// Initialize npm
// Initialize npm
...
...
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