Skip to content

Commit

Permalink
feat(init): keep pre and post scripts as they work in npm (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Miklet authored and Kent C. Dodds committed Apr 21, 2017
1 parent 56dc0ad commit f95d207
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dist
.opt-out
npm-debug.log
.idea/
.vscode/
19 changes: 14 additions & 5 deletions src/bin-utils/initialize/fixtures/_package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ module.exports = {
stuff: 'echo start:stuff'
},
test: 'echo test',
prefoo: {
default: 'echo prefoo',
bar: 'echo prefoo:bar'
},
foo: {
default: 'echo foo',
default: 'nps prefoo && echo foo',
bar: {
default: 'echo foo:bar',
default: 'nps prefoo.bar && echo foo:bar && nps postfoo.bar',
baz: 'echo foo:bar:baz'
}
},
bar: 'echo bar',
fooBar: 'echo foo-bar',
bar: 'echo bar && nps postbar',
postbar: 'echo postbar',
prefooBar: 'echo prefoo-bar',
fooBar: 'nps prefooBar && echo foo-bar',
foobar: 'echo "foo bar"',
baz: 'echo \'baz buzz\''
baz: 'echo \'baz buzz\'',
postfoo: {
bar: 'echo postfoo:bar'
}
}
};
15 changes: 11 additions & 4 deletions src/bin-utils/initialize/fixtures/_package-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ scripts:
default: echo start
stuff: 'echo start:stuff'
test: echo test
prefoo:
default: echo prefoo
bar: 'echo prefoo:bar'
foo:
default: echo foo
default: nps prefoo && echo foo
bar:
default: 'echo foo:bar'
default: 'nps prefoo.bar && echo foo:bar && nps postfoo.bar'
baz: 'echo foo:bar:baz'
bar: echo bar
foo-bar: echo foo-bar
bar: echo bar && nps postbar
postbar: echo postbar
prefooBar: echo prefoo-bar
fooBar: nps prefooBar && echo foo-bar
foobar: echo "foo bar"
baz: echo 'baz buzz'
postfoo:
bar: 'echo postfoo:bar'
5 changes: 5 additions & 0 deletions src/bin-utils/initialize/fixtures/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
"start": "echo start",
"start:stuff": "echo start:stuff",
"test": "echo test",
"prefoo": "echo prefoo",
"foo": "echo foo",
"bar": "echo bar",
"postbar": "echo postbar",
"prefoo-bar": "echo prefoo-bar",
"foo-bar": "echo foo-bar",
"foobar": "echo \"foo bar\"",
"baz": "echo 'baz buzz'",
"prefoo:bar": "echo prefoo:bar",
"foo:bar": "echo foo:bar",
"postfoo:bar": "echo postfoo:bar",
"foo:bar:baz": "echo foo:bar:baz"
}
}
22 changes: 16 additions & 6 deletions src/bin-utils/initialize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,23 @@ function structureScripts(scripts) {
// start out by giving every script a `default`
const defaultedScripts = Object.keys(scripts).reduce((obj, key) => {
const keyParts = key.split(':')
let deepKey = [...keyParts, 'default'].join('.')
const isKeyScriptHook = isScriptHook(keyParts[0]);
let deepKey = keyParts.map(key => camelCase(key)).join('.')
let defaultDeepKey = `${deepKey}.default`
if (key.indexOf('start') === 0) {
deepKey = [
defaultDeepKey = [
'default',
...keyParts.slice(1, keyParts.length),
'default',
].join('.')
}
const script = scripts[key]
set(obj, deepKey, script)
let script = scripts[key]
if (!isKeyScriptHook) {
const preHook = scripts[`pre${key}`] ? `nps pre${deepKey} && ` : ''
const postHook = scripts[`post${key}`] ? ` && nps post${deepKey}` : ''
script = `${preHook}${script}${postHook}`
}
set(obj, defaultDeepKey, script)
return obj
}, {})
// traverse the object and replace all objects that
Expand Down Expand Up @@ -102,9 +109,8 @@ function jsObjectStringify(object, indent) {
} else {
value = `'${escapeSingleQuote(script)}'`
}
const camelKey = camelCase(key)
const comma = isLast(object, index) ? '' : ','
return `${string}\n${indent}${camelKey}: ${value}${comma}`
return `${string}\n${indent}${key}: ${value}${comma}`
},
'',
)
Expand All @@ -123,3 +129,7 @@ function escapeSingleQuote(string) {
function isLast(object, index) {
return Object.keys(object).length - 1 === index
}

function isScriptHook(script) {
return script.indexOf('pre') === 0 || script.indexOf('post') === 0
}
4 changes: 2 additions & 2 deletions src/bin-utils/initialize/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('initialize JS normally', () => {
const expectedPackageScripts = readFileSync(
resolve('./src/bin-utils/initialize/fixtures/_package-scripts.js'),
'utf-8',
)
).replace(/\r?\n/g, '\n');
const mockWriteFileSync = spy()
const mockFindUpSync = spy(file => {
if (file === 'package.json') {
Expand Down Expand Up @@ -59,7 +59,7 @@ test('initialize YML normally', () => {
const expectedPackageScripts = readFileSync(
resolve('./src/bin-utils/initialize/fixtures/_package-scripts.yml'),
'utf-8',
)
).replace(/\r?\n/g, '\n')
const mockWriteFileSync = spy()
const mockFindUpSync = spy(file => {
if (file === 'package.json') {
Expand Down

0 comments on commit f95d207

Please sign in to comment.