-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bin-utils): do not move core scripts, convert npm run scripts in…
…to (#154) nps * Core scripts ex. npm lifecycle scripts, husky hooks, will be not moved to * package-scripts.js * Scripts in package.json which contains npm run command will be converted * to nps command
- Loading branch information
Showing
9 changed files
with
228 additions
and
58 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/bin-utils/initialize/__tests__/__snapshots__/stringify-object.js.snap
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,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`stringify object correctly 1`] = ` | ||
" | ||
foo: 'a', | ||
bar: { | ||
baz: 'b' | ||
}" | ||
`; |
9 changes: 9 additions & 0 deletions
9
src/bin-utils/initialize/__tests__/fixtures/_package-core-scripts.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,9 @@ | ||
{ | ||
"scripts": { | ||
"foo": "echo \"foo\"", | ||
"precommit": "precommit hook", | ||
"postcommit": "postcommit hook", | ||
"prepublish": "prepublish hook", | ||
"preuninstall": "preuninstall hook" | ||
} | ||
} |
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,13 @@ | ||
import stringifyObject from './../stringify-object' | ||
|
||
const objectToStringify = { | ||
foo: 'a', | ||
bar: { | ||
baz: 'b', | ||
}, | ||
} | ||
|
||
test('stringify object correctly', () => { | ||
const stringObject = stringifyObject(objectToStringify, ' ') | ||
expect(stringObject).toMatchSnapshot() | ||
}) |
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,37 @@ | ||
import {isPlainObject} from 'lodash' | ||
|
||
/** | ||
* Converts given object to its string representation. | ||
* Every line is preceded by given indent. | ||
* | ||
* @param {object} object "Object to convert" | ||
* @param {string} indent "Indent to use" | ||
* @returns {string} "Stringified and indented object" | ||
*/ | ||
function stringifyObject(object, indent) { | ||
return Object.keys(object).reduce((string, key, index) => { | ||
const script = object[key] | ||
let value | ||
if (isPlainObject(script)) { | ||
value = `{${stringifyObject(script, `${indent} `)}\n${indent}}` | ||
} else { | ||
value = `'${escapeSingleQuote(script)}'` | ||
} | ||
const comma = getComma(isLast(object, index)) | ||
return `${string}\n${indent}${key}: ${value}${comma}` | ||
}, '') | ||
} | ||
|
||
function getComma(condition) { | ||
return condition ? '' : ',' | ||
} | ||
|
||
function isLast(object, index) { | ||
return Object.keys(object).length - 1 === index | ||
} | ||
|
||
function escapeSingleQuote(string) { | ||
return string.replace(/'/g, "\\'") | ||
} | ||
|
||
export default stringifyObject |