Skip to content

Jakob/build snapshot #4

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

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
node_modules
node_modules
deps.json*
10 changes: 10 additions & 0 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ CACHE_DIR=${CACHE_DIR} \
ENV_DIR=${ENV_DIR} \
SOURCE_VERSION=${SOURCE_VERSION} \
node ${BP_DIR}/lib/upload.js | output "$LOG_FILE"


### Send deps to Frontier Dashboard
if [ -f $1/package.json ]; then
printf "\nFrontier Dashboard steps:\n"
printf "Scanning deps... " && npm ls --json --prefix ${BUILD_DIR} > deps.json && printf "Done.\n"
printf "Flattening deps... " && BUILD_DIR=${BUILD_DIR} BP_DIR=${BP_DIR} node ${BP_DIR}/bin/flatten-deps.js && printf "Done.\n"
printf "Sending deps to Frontier Dashboard... " curl -X POST -H "x-api-key: hmPlMe6As2aNih6Dg3sRj8mHhiM9mDWo1bldZoaz" -d "$(cat ./deps.json.flat)" https://tiagtww9kj.execute-api.us-east-1.amazonaws.com/dev/app/deploy/snapshot && printf "Done.\n"
printf "Done with Frontier Dashboard steps\n"
fi
17 changes: 17 additions & 0 deletions bin/deps-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# bin/compile <build-dir> <cache-dir> <env-dir>

### Configure environment

set -o errexit # always exit on error
set -o pipefail # don't ignore exit codes when piping output
set -o nounset # fail on unset variables
unset GIT_DIR # Avoid GIT_DIR leak from previous build steps

### Send deps to Frontier Dashboard
printf "\nFrontier Dashboard steps:\n"
printf "$(ls -la)"
printf "Scanning deps... " && npm ls --json > deps.json && printf "Done.\n"
printf "Flattening deps... " && node bin/flatten-deps.js && printf "Done.\n"
printf "Sending deps to Frontier Dashboard... " curl -X POST -H "x-api-key: hmPlMe6As2aNih6Dg3sRj8mHhiM9mDWo1bldZoaz" -d "$(cat ./deps.json.flat)" https://tiagtww9kj.execute-api.us-east-1.amazonaws.com/dev/app/deploy/snapshot && printf "Done.\n"
printf "Done with Frontier Dashboard steps\n"
76 changes: 76 additions & 0 deletions bin/flatten-deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const path = require('path');
const fs = require('fs');
const semverGt = require('semver/functions/gt')
const semverValid = require('semver/functions/valid')
// heroku buildpack dir or local test
const buildDir = process.env.BUILD_DIR || '../'
const bpDir = process.env.BP_DIR || '../'
const deps = require(path.join(bpDir, 'deps.json'));
// get host app's package.json
const package = require(path.join(buildDir, 'package.json'));
console.log('Flattening dependencies for', package.name, '...');

const output = {
name: deps.name,
version: deps.version
}

const recursiveSearch = (obj, searchKey, results = []) => {
const r = results;
Object.keys(obj).forEach(key => {
const value = obj[key];
// push package version
if (key !== searchKey && typeof value === 'object' && value.hasOwnProperty('version')) {
r.push({ name: key, version: value.version });
}
// recurse into deps
if (key === searchKey && typeof value === 'object') {
recursiveSearch(value, searchKey, r);
}
// recurse into sub-deps
if (typeof value === 'object' && value.hasOwnProperty(searchKey)) {
recursiveSearch(value[searchKey], searchKey, r);
}
});
return r;
};

function findDepVersion(deps, searchKey) {
let dep = {name: searchKey};
for (const d of deps) {
if (d.name === searchKey) {
dep = d;
break;
}
};
return dep
}

const deepDeps = recursiveSearch(deps, 'dependencies');
console.log("Deep dependency count:", deepDeps.length);

// dedupe deps, keeping the highest semver version for each
const dedupedDeps = deepDeps.reduce((acc, dep) => {
const existing = acc.find(d => d.name === dep.name);
if (existing) {
console.log('exists, versions:', existing.version, dep.version);
if (semverValid(existing.version) && semverValid(dep.version) && semverGt(dep.version, existing.version)) {
acc.splice(acc.indexOf(existing), 1, dep);
}
} else {
acc.push(dep);
}
return acc;
}, []);
console.log("Deduped dependency count:", dedupedDeps.length);


// normal deps
output.deps = package.dependencies ? Object.keys(package.dependencies).map(d => findDepVersion(dedupedDeps, d)): [];
output.devDeps = package.devDependencies ? Object.keys(package.devDependencies).map(d => findDepVersion(dedupedDeps, d)) : [];
output.peerDeps = package.peerDependencies ? Object.keys(package.peerDependencies).map(d => findDepVersion(dedupedDeps, d)) : [];
// deps that aren't in deps, devDeps, or peerDeps
output.secondaryDeps = dedupedDeps.filter(d => !output.deps.includes(d) && !output.devDeps.includes(d) && !output.peerDeps.includes(d));

// Write to file
fs.writeFileSync(path.join(bpDir, 'deps.json.flat'), JSON.stringify(output, null, 2));
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
"aws-sdk": "^2.5.0",
"del": "^6.0.0",
"glob": "^7.0.5",
"lodash": "^4.15.0",
"mime-types": "^2.1.11",
"semver": "^7.3.7",
"shelljs": "^0.8.4"
},
"devDependencies": {
"async": "^3.2.0",
"aws-sdk": "^2.5.0"
},
"peerDependencies": {
"lodash": "^4.15.0",
"mime-types": "^2.1.11",
"shelljs": "^0.8.4"
Expand Down