-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publish): add new option
--remove-package-fields
before publish (
#359) * feat(publish): add new option `--remove-package-fields` before publish
- Loading branch information
1 parent
efaf011
commit 45a2107
Showing
21 changed files
with
497 additions
and
52 deletions.
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
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
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,79 @@ | ||
import cloneDeep from 'clone-deep'; | ||
import npmlog from 'npmlog'; | ||
|
||
import { deleteComplexObjectProp, getComplexObjectValue } from '../object-utils'; | ||
|
||
describe('deleteComplexObjectProp method', () => { | ||
let obj = {}; | ||
beforeEach(() => { | ||
obj = { id: 1, user: { firstName: 'John', lastName: 'Doe', address: { number: 123, street: 'Broadway' } } }; | ||
}); | ||
|
||
it('should expect the same object as the original object when no path is provided', () => { | ||
const originalObj = cloneDeep(obj); | ||
deleteComplexObjectProp(obj, undefined as any); | ||
expect(originalObj).toEqual(obj); | ||
}); | ||
|
||
it('should expect the same object as the original object when search argument is not part of the input object', () => { | ||
const originalObj = cloneDeep(obj); | ||
deleteComplexObjectProp(obj, 'users'); | ||
expect(originalObj).toEqual(obj as any); | ||
}); | ||
|
||
it('should expect the object to remove an entire property when path is a single string without dot notation', () => { | ||
const logSpy = jest.spyOn(npmlog, 'verbose'); | ||
deleteComplexObjectProp(obj, 'user', 'some object name'); | ||
expect(obj).toEqual({ id: 1 }); | ||
expect(logSpy).toHaveBeenCalledWith('mutation', 'Removed "user" field from some object name.'); | ||
}); | ||
|
||
it('should expect the object descendant to be removed when path is using dot notation', () => { | ||
const logSpy = jest.spyOn(npmlog, 'verbose'); | ||
deleteComplexObjectProp(obj, 'user.firstName'); | ||
expect(obj).toEqual({ id: 1, user: { lastName: 'Doe', address: { number: 123, street: 'Broadway' } } }); | ||
expect(logSpy).toHaveBeenCalledWith('mutation', 'Removed "user.firstName" field from n/a.'); | ||
}); | ||
|
||
it('should expect the object last descendant to be removed when using multiple levels of dot notation', () => { | ||
const logSpy = jest.spyOn(npmlog, 'verbose'); | ||
deleteComplexObjectProp(obj, 'user.address.street', '"@workspace/pkg-1" package'); | ||
expect(obj).toEqual({ id: 1, user: { firstName: 'John', lastName: 'Doe', address: { number: 123 } } }); | ||
expect(logSpy).toHaveBeenCalledWith( | ||
'mutation', | ||
'Removed "user.address.street" field from "@workspace/pkg-1" package.' | ||
); | ||
}); | ||
}); | ||
|
||
describe('getComplexObjectValue method', () => { | ||
let obj = {}; | ||
beforeEach(() => { | ||
obj = { id: 1, user: { firstName: 'John', lastName: 'Doe', address: { number: 123, street: 'Broadway' } } }; | ||
}); | ||
|
||
it('should return original object when no path is provided', () => { | ||
const output = getComplexObjectValue(obj, undefined as any); | ||
expect(output).toBe(obj); | ||
}); | ||
|
||
it('should return undefined when search argument is not part of the input object', () => { | ||
const output = getComplexObjectValue(obj, 'users'); | ||
expect(output).toBe(undefined as any); | ||
}); | ||
|
||
it('should return the object descendant even when path given is not a dot notation', () => { | ||
const output = getComplexObjectValue(obj, 'user'); | ||
expect(output).toEqual(obj['user']); | ||
}); | ||
|
||
it('should return the object descendant when using dot notation', () => { | ||
const output = getComplexObjectValue(obj, 'user.firstName'); | ||
expect(output).toEqual('John'); | ||
}); | ||
|
||
it('should return the object descendant when using multiple levels of dot notation', () => { | ||
const output = getComplexObjectValue(obj, 'user.address.street'); | ||
expect(output).toEqual('Broadway'); | ||
}); | ||
}); |
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,39 @@ | ||
import log from 'npmlog'; | ||
|
||
/** | ||
* From a dot (.) notation path, find and delete a property within an object if found given a complex object path | ||
* @param {Object} object - object to search from | ||
* @param {String} path - complex object path to find descendant property from, must be a string with dot (.) notation | ||
* @param {String} [sourceName] - source name of which object name to delete the field from. | ||
*/ | ||
export function deleteComplexObjectProp(object: any, path: string, sourceName?: string) { | ||
if (!object || !path) { | ||
return object; | ||
} | ||
const props = path.split('.'); | ||
const lastProp = props.slice(-1).pop(); | ||
|
||
return props.reduce((obj, prop) => { | ||
if (lastProp !== undefined && obj?.[prop] !== undefined && prop === lastProp) { | ||
delete obj[prop]; | ||
log.verbose('mutation', `Removed "${path}" field from ${sourceName || 'n/a'}.`); | ||
} else { | ||
return obj?.[prop]; | ||
} | ||
}, object); | ||
} | ||
|
||
/** | ||
* From a dot (.) notation path, find and return a property within an object given a complex object path | ||
* Note that the object path does should not include the parent itself | ||
* for example if we want to get `address.zip` from `user` object, we would call `getComplexObjectValue(user, 'address.zip')` | ||
* @param object - object to search from | ||
* @param path - complex object path to find descendant property from, must be a string with dot (.) notation | ||
* @returns outputValue - the object property value found if any | ||
*/ | ||
export function getComplexObjectValue<T>(object: any, path: string): T { | ||
if (!object || !path) { | ||
return object; | ||
} | ||
return path.split('.').reduce((obj, prop) => obj?.[prop], object); | ||
} |
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
3 changes: 3 additions & 0 deletions
3
packages/publish/src/__tests__/__fixtures__/remove-fields/lerna.json
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,3 @@ | ||
{ | ||
"version": "1.0.0" | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/publish/src/__tests__/__fixtures__/remove-fields/package.json
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,4 @@ | ||
{ | ||
"name": "snake-graph", | ||
"description": "when a change in the head (package-1) occurs, the tail (package-5) should be bumped as well" | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/publish/src/__tests__/__fixtures__/remove-fields/packages/package-1/package.json
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 @@ | ||
{ | ||
"name": "package-1", | ||
"description": "no local dependencies, four local dependents (three transitive)", | ||
"version": "1.0.0", | ||
"browser": "src/index.ts", | ||
"main": "dist/cjs/index.js", | ||
"scripts": { | ||
"build": "tsc --project tsconfig.json" | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/publish/src/__tests__/__fixtures__/remove-fields/packages/package-2/package.json
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,14 @@ | ||
{ | ||
"name": "package-2", | ||
"description": "one local dependency, one direct dependent, no transitive dependencies", | ||
"version": "1.0.0", | ||
"main": "dist/cjs/index.js", | ||
"dependencies": { | ||
"package-1": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"build": "tsc --project tsconfig.json", | ||
"build:dev": "tsc --incremental --watch", | ||
"pack-tarball": "npm pack" | ||
} | ||
} |
Oops, something went wrong.