Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/environment #1

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,23 @@ class Scriptable {
};
}

environment() {
return {
...(process.env || {}),
...((((this.serverless || {}).service || {}).provider || {}).environment || {}),
};
}

runCommand(script) {
if (this.showCommands) {
console.log(`Running command: ${script}`);
}

try {
return execSync(script, { stdio: [this.stdin, this.stdout, this.stderr] });
return execSync(script, {
env: this.environment(),
stdio: [this.stdin, this.stdout, this.stderr],
});
} catch (err) {
throw new SimpleError(`Failed to run command: ${script}`);
}
Expand Down Expand Up @@ -154,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
Expand Down
33 changes: 29 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 } } };
}
});
Loading