-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge pre-existing package.json with file generated by Cloverfield #40
Open
halhenke
wants to merge
13
commits into
cloverfield-tools:master
Choose a base branch
from
halhenke:feaure/package-check
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d4771ad
require package-merge
halhenke 5c736c9
functions to merge a pre-existing package.son file with generated tem…
halhenke d753600
Add merge package step between template compilation and content saving
halhenke 2a626cc
export functions rather than object in mergePack
halhenke 0746fa5
Swap out package-merge for loads.merge for package.json merging
halhenke 4bb6fb4
test that package.json output has decent formatting
halhenke 81f1663
Get rid of unused `package.merge` package
halhenke 4aa5791
Swap order of arguments in loads.merge in order to choose users origi…
halhenke 763e1af
Add object rest/spread syntax
halhenke 3a4f07a
Very Rough version of changes
halhenke 5bb8e17
use `parse-author` & `parse-github-url` to get author & Github info f…
halhenke 959e47f
Option to merge pre-existing dependencies, devDependencies and script…
halhenke 52e69f8
Refactor
halhenke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"presets": ["es2015"] | ||
"presets": ["es2015"], | ||
"plugins": ["transform-object-rest-spread"] | ||
} |
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,51 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import merge from 'lodash.merge'; | ||
import parseAuthor from 'parse-author'; | ||
import parseGithub from 'parse-github-url'; | ||
|
||
export const getPackage = () => { | ||
let origPackage; | ||
try { | ||
origPackage = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8')); | ||
} catch (e) { | ||
origPackage = false; | ||
} | ||
return origPackage; | ||
}; | ||
|
||
export const getPackageProps = (existingPackage, packageProperties, schema) => { | ||
let gitHub, authorInfo; | ||
if (existingPackage) { | ||
console.log(`Existing package.json file has been used to get default variables.`); | ||
schema.properties = {...schema.properties, ...packageProperties}; | ||
schema.properties['package.name'].default = existingPackage.name; | ||
schema.properties['package.description'].default = existingPackage.description; | ||
// Try to get Name, Email & GitHub Username | ||
if (existingPackage.repository && existingPackage.repository.url) { | ||
gitHub = parseGithub(existingPackage.repository.url).owner; | ||
schema.properties['user.github'].default = gitHub; | ||
} | ||
if (existingPackage.author) { | ||
authorInfo = parseAuthor(existingPackage.author); | ||
if (authorInfo.name) { | ||
schema.properties['user.name'].default = authorInfo.name; | ||
} | ||
if (authorInfo.email) { | ||
schema.properties['user.email'].default = authorInfo.email; | ||
} | ||
} | ||
} | ||
return schema; | ||
}; | ||
|
||
export const mergePackages = (origPackage, destinations) => compiledFiles => { | ||
if (origPackage) { | ||
const newPackagePath = path.join(process.cwd(), 'package.json'); | ||
const packageIndex = destinations.indexOf(newPackagePath); | ||
const packageContents = JSON.parse(compiledFiles[packageIndex]); | ||
const mergedPack = JSON.stringify(merge(packageContents, origPackage), null, 2); | ||
compiledFiles[packageIndex] = mergedPack; | ||
} | ||
return compiledFiles; | ||
}; |
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,16 @@ | ||
import templatePackage from '../template/package.json'; | ||
|
||
const packageFields = ['dependencies', 'devDependencies', 'scripts']; | ||
|
||
const setPackageProps = userPackage => props => { | ||
packageFields.forEach((field) => { | ||
if (props[`package.${field}`]) { | ||
props[`package.${field}`] = {...templatePackage[field], ...userPackage[field]}; | ||
} else { | ||
props[`package.${field}`] = {...templatePackage[field]}; | ||
} | ||
}); | ||
return props; | ||
}; | ||
|
||
export default setPackageProps; |
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,51 @@ | ||
import {test} from 'blue-tape'; | ||
import {mergePackages} from '../../lib/mergePack'; | ||
import path from 'path'; | ||
|
||
test('Merge Packages', t => { | ||
|
||
const packPath = path.join(process.cwd(), 'package.json'); | ||
const templatedFile = JSON.stringify({ | ||
name: 'templatePackage', | ||
description: 'This should not overwrite stuff', | ||
main: 'src/index.js', | ||
scripts: { | ||
start: 'do a lotta new stuff', | ||
test: 'mocha this package' | ||
} | ||
}); | ||
const originalFile = { | ||
name: 'originalPackage', | ||
description: 'This should not be overwritten by a template', | ||
scripts: { | ||
start: 'do a lotta old stuff', | ||
lint: 'lint this package' | ||
} | ||
}; | ||
const mergedFile = { | ||
name: 'originalPackage', | ||
description: 'This should not be overwritten by a template', | ||
main: 'src/index.js', | ||
scripts: { | ||
start: 'do a lotta old stuff', | ||
lint: 'lint this package', | ||
test: 'mocha this package' | ||
} | ||
}; | ||
|
||
t.ok(mergePackages instanceof Function, 'should be function'); | ||
|
||
t.ok(mergePackages() instanceof Function, 'should return function'); | ||
|
||
t.deepEqual(mergePackages(false, [packPath])([templatedFile]), [templatedFile], | ||
'should return the generated package when there is no pre-existing package.json'); | ||
|
||
const merger = mergePackages(originalFile, [packPath])([templatedFile])[0]; | ||
|
||
t.deepEqual(JSON.parse(merger), mergedFile, | ||
'should merge an existing package.json with template and return results'); | ||
|
||
t.ok(merger.match(/\s\s/), 'merged package should be formatted with spaces'); | ||
|
||
t.end(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried it several times and tried to fix it too... No luck. With current implmentation no matter what I chose deps/devdeps/scrips are preserved and never updated. If I swap
packageContents
andorigPackage
- they always updated instead. Somehow those props fromsetPackageProps
are just totally ignored =(