Skip to content

Commit

Permalink
fix: Avoid re-registration of ts-node (#9254)
Browse files Browse the repository at this point in the history
  • Loading branch information
apancutt committed Apr 7, 2021
1 parent c88a2ea commit 88baf06
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/configuration/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,29 @@ const parseConfigurationFile = async (configurationPath) => {
}
}
case '.ts': {
const tsNodePath = await (async () => {
if (!process[Symbol.for('ts-node.register.instance')]) {
const tsNodePath = await (async () => {
try {
return await resolveTsNode(path.dirname(configurationPath));
} catch (error) {
throw new ServerlessError(
`Cannot parse "${path.basename(
configurationPath
)}": Resolution of "ts-node" failed with: ${error.message}`,
'CONFIGURATION_RESOLUTION_ERROR'
);
}
})();
try {
return await resolveTsNode(path.dirname(configurationPath));
require(tsNodePath).register();
} catch (error) {
throw new ServerlessError(
`Cannot parse "${path.basename(
configurationPath
)}": Resolution of "ts-node" failed with: ${error.message}`,
)}": Register of "ts-node" failed with: ${error.message}`,
'CONFIGURATION_RESOLUTION_ERROR'
);
}
})();
try {
require(tsNodePath).register();
} catch (error) {
throw new ServerlessError(
`Cannot parse "${path.basename(configurationPath)}": Register of "ts-node" failed with: ${
error.message
}`,
'CONFIGURATION_RESOLUTION_ERROR'
);
}
}
// fallthrough
Expand Down
16 changes: 16 additions & 0 deletions test/unit/lib/configuration/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ describe('test/unit/lib/configuration/read.test.js', () => {
}
});

it('should register ts-node only if it is not already registered', async () => {
try {
expect(process[Symbol.for('ts-node.register.instance')]).to.be.undefined;
process[Symbol.for('ts-node.register.instance')] = 'foo';
configurationPath = 'serverless.ts';
const configuration = {
service: 'test-ts',
provider: { name: 'aws' },
};
await fs.writeFile(configurationPath, `module.exports = ${JSON.stringify(configuration)}`);
expect(await readConfiguration(configurationPath)).to.deep.equal(configuration);
} finally {
delete process[Symbol.for('ts-node.register.instance')];
}
});

it('should support deferred configuration result', async () => {
// JS configurations are required (so immune to modules caching).
// In this tests we cannot use same JS configuration path twice for testing
Expand Down

0 comments on commit 88baf06

Please sign in to comment.