-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start establishing the concept of "artifacts"
Start moving towards the cloud-assembly specification where an output of a CDK program is a bunch of artifacts and those are processed by the toolkit. Related #956 Related #233 Related #1119
- Loading branch information
Elad Ben-Israel
committed
Feb 27, 2019
1 parent
297b66b
commit dca97c6
Showing
10 changed files
with
932 additions
and
267 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import cxapi = require('@aws-cdk/cx-api'); | ||
|
||
export function collectRuntimeInformation(): cxapi.AppRuntime { | ||
const libraries: { [name: string]: string } = {}; | ||
|
||
for (const fileName of Object.keys(require.cache)) { | ||
const pkg = findNpmPackage(fileName); | ||
if (pkg && !pkg.private) { | ||
libraries[pkg.name] = pkg.version; | ||
} | ||
} | ||
|
||
// include only libraries that are in the @aws-cdk npm scope | ||
for (const name of Object.keys(libraries)) { | ||
if (!name.startsWith('@aws-cdk/')) { | ||
delete libraries[name]; | ||
} | ||
} | ||
|
||
// add jsii runtime version | ||
libraries['jsii-runtime'] = getJsiiAgentVersion(); | ||
|
||
return { libraries }; | ||
} | ||
|
||
/** | ||
* Determines which NPM module a given loaded javascript file is from. | ||
* | ||
* The only infromation that is available locally is a list of Javascript files, | ||
* and every source file is associated with a search path to resolve the further | ||
* ``require`` calls made from there, which includes its own directory on disk, | ||
* and parent directories - for example: | ||
* | ||
* [ '...repo/packages/aws-cdk-resources/lib/cfn/node_modules', | ||
* '...repo/packages/aws-cdk-resources/lib/node_modules', | ||
* '...repo/packages/aws-cdk-resources/node_modules', | ||
* '...repo/packages/node_modules', | ||
* // etc... | ||
* ] | ||
* | ||
* We are looking for ``package.json`` that is anywhere in the tree, except it's | ||
* in the parent directory, not in the ``node_modules`` directory. For this | ||
* reason, we strip the ``/node_modules`` suffix off each path and use regular | ||
* module resolution to obtain a reference to ``package.json``. | ||
* | ||
* @param fileName a javascript file name. | ||
* @returns the NPM module infos (aka ``package.json`` contents), or | ||
* ``undefined`` if the lookup was unsuccessful. | ||
*/ | ||
function findNpmPackage(fileName: string): { name: string, version: string, private?: boolean } | undefined { | ||
const mod = require.cache[fileName]; | ||
const paths = mod.paths.map(stripNodeModules); | ||
|
||
try { | ||
const packagePath = require.resolve('package.json', { paths }); | ||
return require(packagePath); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
|
||
/** | ||
* @param s a path. | ||
* @returns ``s`` with any terminating ``/node_modules`` | ||
* (or ``\\node_modules``) stripped off.) | ||
*/ | ||
function stripNodeModules(s: string): string { | ||
if (s.endsWith('/node_modules') || s.endsWith('\\node_modules')) { | ||
// /node_modules is 13 characters | ||
return s.substr(0, s.length - 13); | ||
} | ||
return s; | ||
} | ||
} | ||
|
||
function getJsiiAgentVersion() { | ||
let jsiiAgent = process.env.JSII_AGENT; | ||
|
||
// if JSII_AGENT is not specified, we will assume this is a node.js runtime | ||
// and plug in our node.js version | ||
if (!jsiiAgent) { | ||
jsiiAgent = `node.js/${process.version}`; | ||
} | ||
|
||
return jsiiAgent; | ||
} |
Oops, something went wrong.