From f95d20738c722263e48a374c96d8f6dd500bbaeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Miko=C5=82ajczyk?= Date: Fri, 21 Apr 2017 19:52:56 +0200 Subject: [PATCH] feat(init): keep pre and post scripts as they work in npm (#134) --- .gitignore | 1 + .../initialize/fixtures/_package-scripts.js | 19 +++++++++++----- .../initialize/fixtures/_package-scripts.yml | 15 +++++++++---- .../initialize/fixtures/_package.json | 5 +++++ src/bin-utils/initialize/index.js | 22 ++++++++++++++----- src/bin-utils/initialize/index.test.js | 4 ++-- 6 files changed, 49 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 3b7bf8e..cbfcf32 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ dist .opt-out npm-debug.log .idea/ +.vscode/ diff --git a/src/bin-utils/initialize/fixtures/_package-scripts.js b/src/bin-utils/initialize/fixtures/_package-scripts.js index e0d310c..95dfbb6 100644 --- a/src/bin-utils/initialize/fixtures/_package-scripts.js +++ b/src/bin-utils/initialize/fixtures/_package-scripts.js @@ -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' + } } }; diff --git a/src/bin-utils/initialize/fixtures/_package-scripts.yml b/src/bin-utils/initialize/fixtures/_package-scripts.yml index bc8a4ef..88cf416 100644 --- a/src/bin-utils/initialize/fixtures/_package-scripts.yml +++ b/src/bin-utils/initialize/fixtures/_package-scripts.yml @@ -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' diff --git a/src/bin-utils/initialize/fixtures/_package.json b/src/bin-utils/initialize/fixtures/_package.json index 66cdc0e..5b0f727 100644 --- a/src/bin-utils/initialize/fixtures/_package.json +++ b/src/bin-utils/initialize/fixtures/_package.json @@ -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" } } diff --git a/src/bin-utils/initialize/index.js b/src/bin-utils/initialize/index.js index 59e2b5d..e7032b4 100644 --- a/src/bin-utils/initialize/index.js +++ b/src/bin-utils/initialize/index.js @@ -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 @@ -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}` }, '', ) @@ -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 +} diff --git a/src/bin-utils/initialize/index.test.js b/src/bin-utils/initialize/index.test.js index 20e094b..9b262ee 100644 --- a/src/bin-utils/initialize/index.test.js +++ b/src/bin-utils/initialize/index.test.js @@ -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') { @@ -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') {