From cb2c1f542787bf20909d10e5995cfcad3210ac7b Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Wed, 15 May 2024 11:12:14 -0400 Subject: [PATCH 1/5] add provider env --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ecc3142..61edb52 100644 --- a/index.js +++ b/index.js @@ -116,8 +116,13 @@ class Scriptable { console.log(`Running command: ${script}`); } + console.log(`Provider environment: ${this.serverless.provider.environment}`); + try { - return execSync(script, { stdio: [this.stdin, this.stdout, this.stderr] }); + return execSync(script, { + env: { ...process.env, ...this.serverless.provider.environment }, + stdio: [this.stdin, this.stdout, this.stderr], + }); } catch (err) { throw new SimpleError(`Failed to run command: ${script}`); } From e8d8293f8e5c1d8e81aa81f35e35f1cfbff6cbb6 Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Wed, 15 May 2024 11:14:38 -0400 Subject: [PATCH 2/5] update path to env --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 61edb52..f845185 100644 --- a/index.js +++ b/index.js @@ -116,11 +116,11 @@ class Scriptable { console.log(`Running command: ${script}`); } - console.log(`Provider environment: ${this.serverless.provider.environment}`); + console.log(`Provider environment: ${this.serverless.service.provider.environment}`); try { return execSync(script, { - env: { ...process.env, ...this.serverless.provider.environment }, + env: { ...process.env, ...this.serverless.service.provider.environment }, stdio: [this.stdin, this.stdout, this.stderr], }); } catch (err) { From 4db7d78d38392ca9b11575a37fc17b17ccca33de Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Wed, 15 May 2024 11:15:51 -0400 Subject: [PATCH 3/5] stringify --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f845185..29d13fb 100644 --- a/index.js +++ b/index.js @@ -116,7 +116,7 @@ class Scriptable { console.log(`Running command: ${script}`); } - console.log(`Provider environment: ${this.serverless.service.provider.environment}`); + console.log(`Provider environment: ${JSON.stringify(this.serverless.service.provider.environment)}`); try { return execSync(script, { From 5fbbf949348f144716e2d524d41f012b33001d83 Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Wed, 15 May 2024 11:19:31 -0400 Subject: [PATCH 4/5] update environment --- index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 29d13fb..315cd95 100644 --- a/index.js +++ b/index.js @@ -111,16 +111,21 @@ class Scriptable { }; } + environment() { + return { + ...(process.env || {}), + ...((((this.serverless || {}).service || {}).provider || {}).environment || {}), + }; + } + runCommand(script) { if (this.showCommands) { console.log(`Running command: ${script}`); } - console.log(`Provider environment: ${JSON.stringify(this.serverless.service.provider.environment)}`); - try { return execSync(script, { - env: { ...process.env, ...this.serverless.service.provider.environment }, + env: this.environment(), stdio: [this.stdin, this.stdout, this.stderr], }); } catch (err) { From 0688ab19552c73c588cb0ddda47e6e0c12669f1d Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Wed, 15 May 2024 18:07:34 -0400 Subject: [PATCH 5/5] add tests --- index.js | 3 +++ test/index.test.js | 33 +++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 315cd95..e201cac 100644 --- a/index.js +++ b/index.js @@ -164,6 +164,9 @@ class Scriptable { __filename: scriptFile, __dirname: path.dirname(fs.realpathSync(scriptFile)), exports: Object(), + process: { + env: this.environment(), + }, }; // See: https://github.com/nodejs/node/blob/7c452845b8d44287f5db96a7f19e7d395e1899ab/lib/internal/modules/cjs/helpers.js#L14 diff --git a/test/index.test.js b/test/index.test.js index d7094db..f7d9f14 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -400,6 +400,31 @@ describe('ScriptablePluginTest', () => { expect(scriptable.first(1, false)).equal(1); }); + it('should run command with serverless environment', () => { + const scriptable = new Scriptable(serviceWithScripts({ test: 'echo ${HELLO}' }, { HELLO: 'WORLD' })); + + scriptable.stdout = tmp.fileSync({ prefix: 'stdout-' }); + scriptable.stderr = tmp.fileSync({ prefix: 'stderr-' }); + + return runScript(scriptable, 'test') + .then(() => { + console.log('checking file', scriptable.stdout.name); + expect(fs.readFileSync(scriptable.stdout.name, { encoding: 'utf-8' })).string('WORLD'); + expect(fs.readFileSync(scriptable.stderr.name, { encoding: 'utf-8' })).equal(''); + }); + }); + + it('should run javascript with serverless environment', () => { + const scriptFile = tmp.fileSync({ postfix: '.js' }); + fs.writeFileSync(scriptFile.name, 'serverless.service.artifact = `${process.env.HELLO}.zip`;'); + + const serverless = serviceWithScripts({ test: scriptFile.name }, { HELLO: 'WORLD' }); + const scriptable = new Scriptable(serverless); + + return runScript(scriptable, 'test') + .then(() => expect(serverless.service.artifact).equal('WORLD.zip')); + }); + it('manual check: should run command with color', () => { const scriptable = new Scriptable(serviceWithScripts({ test: 'node test/scripts/test-with-color.js' })); return runScript(scriptable, 'test'); @@ -415,11 +440,11 @@ describe('ScriptablePluginTest', () => { return Bluebird.resolve(scriptable.hooks[event]()); } - function serviceWithScripts(scriptHooks) { - return serviceWithCustom({ scriptHooks }); + function serviceWithScripts(scriptHooks, environment) { + return serviceWithCustom({ scriptHooks }, environment); } - function serviceWithCustom(custom) { - return { service: { custom } }; + function serviceWithCustom(custom, environment) { + return { service: { custom, provider: { environment } } }; } });